-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.R
63 lines (52 loc) · 1.9 KB
/
example.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
####################################
# example.R
#
# Example/structure for running
# an analysis of a dataset
####################################
library(CVN)
library(dplyr)
library(readr)
# load the data ---------------------------
data <- CVN::grid
#' In case that you want to use your own dataset:
#' data <- readr::read_rds("filename.rds")
# define the weight matrices --------------
W_grid <- CVN::create_weight_matrix("grid") # weight matrix for a 3x3 grid
W_empty <- CVN::create_weight_matrix("glasso") # each graph is estimated separately, W = 0
# function that returns a grid weight matrix parameterized with alpha
W_radition <- function(alpha) {
a <- alpha
b <- 1 - alpha
matrix(c(
0, a, 0, 1, 0, 0, 0, 0, 0,
a, 0, b, 0, 1, 0, 0, 0, 0,
0, b, 0, 0, 0, 1, 0, 0, 0,
1, 0, 0, 0, a, 0, 1, 0, 0,
0, 1, 0, a, 0, b, 0, 1, 0,
0, 0, 1, 0, b, 0, 0, 0, 1,
0, 0, 0, 1, 0, 0, 0, a, 0,
0, 0, 0, 0, 1, 0, a, 0, b,
0, 0, 0, 0, 0, 1, 0, b, 0
),
ncol = 9)
}
# parameter settings ---------------------
lambda1 <- 1:3 # sparsity
lambda2 <- 1:3 # smoothness level
# estimate the CVN -----------------------
# estimate with no smoothing between the graphs
cvn_no_smoothing <- CVN::CVN(data, W = W_empty,
lambda1 = lambda1, lambda2 = lambda2,
eps = 1e-4, maxiter = 1e4, verbose = TRUE)
# estimate with a grid
cvn_grid <- CVN::CVN(data, W = W_grid,
lambda1 = lambda1, lambda2 = lambda2,
eps = 1e-4, maxiter = 1e4, verbose = TRUE)
# estimate with a parameterized weight matrix
cvn_parameterized <- CVN::CVN(data, W = W_radition(0.5),
lambda1 = lambda1, lambda2 = lambda2,
eps = 1e-4, maxiter = 1e4, verbose = TRUE)
# in case you want to plot the results, simply type
p <- plot(cvn_grid)
p$plots[[1]][[1]]