-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesslbn_sim.R
345 lines (267 loc) · 10.2 KB
/
esslbn_sim.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
local_run <- FALSE
# to run locally we need to set the seed and network size
# this is done externally on the cluster runs
write_data <- FALSE
if (local_run) {
kk <- 1
sim_setting <- 1
seed_number <- 101 # the seed
}
n <- c(20, 80, 140, 200)[kk] # number of nodes
default_alpha <- min(0.4, 20/n) # default value of alpha
N <- 10*n # number of observations
exp_parents <- 2 # expected number of parents
graph_type <- "ERs"
include_truth <- FALSE # don't make artificial search space
MCMC_sample_its <- max(25000, 5*round(n*n*log(n)))
if (sim_setting == 2){ # smaller sample size
N <- 2*n # number of observations
}
if (sim_setting == 3){ # ER graphs
graph_type <- "ER"
}
if (sim_setting == 4){ # BA graphs
graph_type <- "BA"
}
if (sim_setting == 5){ # BA graphs, fewer parents
graph_type <- "BA"
exp_parents <- 1 # expected number of parents
}
if (sim_setting == 6){ # iER graphs, fewer parents
graph_type <- "iER"
exp_parents <- 3 # expected number of parents
}
if (graph_type == "BA") {
default_alpha <- min(0.4, 20/n)/2 # default value of alpha
}
# load libraries
library(pcalg)
library(graph)
library(BiDAG)
source("DAGfns.R")
# store values for later dataframe
setup_vec <- c(n, N, exp_parents, graph_type, seed_number)
names(setup_vec) <- c("n", "N", "parents", "type", "seed")
# create a name for the directory to store stuff
f_name <- paste(paste(names(setup_vec), setup_vec, sep = "_"), collapse = "_")
subdir_name <- paste(paste(names(setup_vec[-5]), setup_vec[-5], sep = "_"), collapse = "_")
dir_name <- paste("./sims", subdir_name, sep = "/")
### Generate data
set.seed(seed_number) # set seed
# generate random DAG
if (graph_type == "ERs") {
trueDAGedges <- as(pcalg::randDAG(n = n, d = 2*exp_parents,
wFUN = list(runif, min=0.4, max=2)), "matrix")
}
if (graph_type == "ER") {
trueDAGedges <- as(pcalg::randDAG(n = n, d = 2*exp_parents), "matrix")
}
if (graph_type == "BA") {
trueDAGedges <- as(pcalg::randDAG(n = n, d = 2*exp_parents,
method = "barabasi", par1 = 0.4), "matrix")
}
if (graph_type == "iER") {
trueDAGedges <- as(pcalg::randDAG(n = n, d = 2*exp_parents,
method = "interEr", par1 = 2, par2 = 0.1), "matrix")
}
trueDAG <- 1*(trueDAGedges != 0)
trueCPDAG <- BiDAG:::dagadj2cpadj(trueDAG)
set.seed(seed_number) # set seed
# generate simulated data
data <- rmvDAG(trueDAGedges, N)
# create directory if none exists
if (!dir.exists("./sims")) {
dir.create("./sims")
}
if (!dir.exists(dir_name)) {
dir.create(dir_name)
}
if (write_data) {
if (!file.exists(paste0(dir_name, "/", f_name, "_data.csv"))) {
write.csv(data, paste0(dir_name, "/", f_name, "_data.csv"),
row.names = FALSE)
}
}
# to store method name and parameter values
method_vec <- rep(NA, 3)
names(method_vec) <- c("method", "parameter", "value")
### MAP and MCMC search
# fill out missing simulations
if (!file.exists(paste0(dir_name, "/", f_name, "_BiDAG.Rdata"))) {
result_df <- NULL
### MAP search
am_value <- 0.25
method_vec[1] <- "MAP"
method_vec[2] <- "am"
method_vec[3] <- am_value
# Start the clock!
ptm <- proc.time()
scoreObject <- scoreparameters(scoretype = "bge", data = data, bgepar = list(am = am_value))
set.seed(seed_number) # set seed
bestDAGs <- iterativeMCMC(scoreObject, scoreout = TRUE, hardlimit = 16, softlimit = 10,
alpha = default_alpha)
# Stop the clock
MAPtime <- (proc.time() - ptm)[1]
# Extract CPDAG
bestCPDAG <- bestDAGs$CPDAG
# Compare to generating model
MAPresult <- compareDAGs(bestCPDAG, trueCPDAG, rnd = 6)
result_df <- rbind(result_df, data.frame(t(c(setup_vec, method_vec, MAPresult))))
method_vec[2] <- "iteration"
for (ii in 1:length(bestDAGs$maxtrace)) {
method_vec[3] <- ii
itCPDAG <- BiDAG:::dagadj2cpadj(bestDAGs$maxtrace[[ii]]$DAG)
itMAPresult <- compareDAGs(itCPDAG, trueCPDAG, rnd = 6)
result_df <- rbind(result_df, data.frame(t(c(setup_vec, method_vec, itMAPresult))))
}
## Final order sample
# Start the clock!
ptm <- proc.time()
set.seed(seed_number) # set seed
orderchain <- orderMCMC(scoreObject, startspace = bestDAGs$endspace,
MAP = FALSE, plus1 = TRUE, chainout = TRUE,
startorder = bestDAGs$maxorder,
iterations = MCMC_sample_its,
hardlimit = max(colSums(bestDAGs$endspace)))
# Stop the clock
MCMCtime <- (proc.time() - ptm)[1]
method_vec[1] <- "MCMC"
method_vec[2] <- "p"
# Compare consensus graphs
MCMCresult <- samplecomp(orderchain, trueCPDAG, rnd = 6)
for (ii in 1:nrow(MCMCresult)) {
method_vec[3] <- as.numeric(MCMCresult[ii, "p"])
result_df <- rbind(result_df, data.frame(t(c(setup_vec, method_vec, MCMCresult[ii, -9]))))
}
## Initial order sample
# Start the clock!
ptm <- proc.time()
set.seed(seed_number) # set seed
iorderchain <- orderMCMC(scoreObject, startspace = bestDAGs$startspace,
MAP = FALSE, plus1 = TRUE, chainout = TRUE,
startorder = bestDAGs$maxorder,
iterations = MCMC_sample_its,
hardlimit = max(colSums(bestDAGs$startspace)))
# Stop the clock
iMCMCtime <- (proc.time() - ptm)[1]
method_vec[1] <- "iMCMC"
# Compare consensus graphs
iMCMCresult <- samplecomp(iorderchain, trueCPDAG, rnd = 6)
for (ii in 1:nrow(iMCMCresult)) {
method_vec[3] <- as.numeric(iMCMCresult[ii, "p"])
result_df <- rbind(result_df, data.frame(t(c(setup_vec, method_vec, iMCMCresult[ii, -9]))))
}
if (include_truth) {
## Order sample with true DAG included in the search space
joined_space <- bestDAGs$startspace | trueCPDAG
# Start the clock!
ptm <- proc.time()
set.seed(seed_number) # set seed
morderchain <- orderMCMC(scoreObject, startspace = joined_space,
MAP = FALSE, plus1 = TRUE, chainout = TRUE,
startorder = bestDAGs$maxorder,
iterations = MCMC_sample_its,
hardlimit = max(colSums(joined_space)))
# Stop the clock
mMCMCtime <- (proc.time() - ptm)[1]
method_vec[1] <- "mMCMC"
# Compare consensus graphs
mMCMCresult <- samplecomp(morderchain, trueCPDAG, rnd = 6)
for (ii in 1:nrow(mMCMCresult)) {
method_vec[3] <- as.numeric(mMCMCresult[ii, "p"])
result_df <- rbind(result_df, data.frame(t(c(setup_vec, method_vec, mMCMCresult[ii, -9]))))
}
}
time_df <- data.frame(n = n, N = N, parents = exp_parents,
seed = seed_number,
method = c("MAP", "MCMC", "iMCMC"),
time = c(MAPtime, MCMCtime, iMCMCtime))
save(result_df, time_df, file = paste0(dir_name, "/", f_name, "_BiDAG.Rdata"))
}
## GES
# fill out missing simulations
if (!file.exists(paste0(dir_name, "/", f_name, "_GES.Rdata"))) {
result_df <- NULL
# Start the clock!
ptm <- proc.time()
method_vec[1:2] <- c("GES", "lambda")
ges_lambdas <- c(1,2,5,7,9,11,15,25)
for (lambda_scale in ges_lambdas) {
method_vec[3] <- lambda_scale
ges_score <- new("GaussL0penObsScore", data,
lambda = lambda_scale*log(nrow(data)))
set.seed(seed_number) # set seed
ges_fit <- ges(ges_score)
# Extract CPDAG
gesCPDAG <- 1*as(ges_fit$essgraph, "matrix")
# Compare to generating model
GESresult <- compareDAGs(gesCPDAG, trueCPDAG, rnd = 6)
result_df <- rbind(result_df, data.frame(t(c(setup_vec, method_vec, GESresult))))
}
# Stop the clock - this is over all penalisations
GEStime <- (proc.time() - ptm)[1]
time_df <- data.frame(n = n, N = N, parents = exp_parents,
seed = seed_number,
method = c("GES"),
time = c(GEStime))
save(result_df, time_df, file = paste0(dir_name, "/", f_name, "_GES.Rdata"))
}
## PC
# fill out missing simulations
if (!file.exists(paste0(dir_name, "/", f_name, "_PC.Rdata"))) {
result_df <- NULL
# correlation matrix
cor_mat <- cor(data)
# Start the clock!
ptm <- proc.time()
method_vec[1:2] <- c("PC", "alpha")
pc_alphas <- c(0.01, 0.05, 0.1, 0.2, 0.35, 0.45)
if (sim_setting == 5 && n > 100){ # truncate as it takes too long otherwise
pc_alphas <- pc_alphas[1:3]
}
for (alpha in pc_alphas) {
method_vec[3] <- alpha
set.seed(seed_number) # set seed
## estimate CPDAG
pc_fit <- pc(suffStat = list(C = cor_mat, n = nrow(data)),
indepTest = gaussCItest, ## indep.test: partial correlations
alpha = alpha, labels = paste0("V", 1:ncol(data)))
# Extract CPDAG
pcCPDAG <- 1*as(pc_fit@graph, "matrix")
# Compare to generating model
PCresult <- compareDAGs(pcCPDAG, trueCPDAG, rnd = 6)
result_df <- rbind(result_df, data.frame(t(c(setup_vec, method_vec, PCresult))))
}
# Stop the clock - over all alphas
PCtime <- (proc.time() - ptm)[1]
time_df <- data.frame(n = n, N = N, parents = exp_parents,
seed = seed_number,
method = c("PC"),
time = c(PCtime))
save(result_df, time_df, file = paste0(dir_name, "/", f_name, "_PC.Rdata"))
}
## PC time for initialisation
# fill out missing simulations
if (!file.exists(paste0(dir_name, "/", f_name, "_PCt.Rdata"))) {
# correlation matrix
cor_mat <- cor(data)
# Start the clock!
ptm <- proc.time()
method_vec[1:2] <- c("PC", "alpha")
alpha <- default_alpha
method_vec[3] <- alpha
set.seed(seed_number) # set seed
## estimate CPDAG
pc_fit <- pc(suffStat = list(C = cor_mat, n = nrow(data)),
indepTest = gaussCItest, ## indep.test: partial correlations
alpha = alpha, labels = paste0("V", 1:ncol(data)))
# Extract CPDAG
pcCPDAG <- 1*as(pc_fit@graph, "matrix")
# Stop the clock - over all alphas
PCttime <- (proc.time() - ptm)[1]
time_df <- data.frame(n = n, N = N, parents = exp_parents,
seed = seed_number,
method = c("PCt"),
time = c(PCttime))
save(time_df, file = paste0(dir_name, "/", f_name, "_PCt.Rdata"))
}