forked from achamma723/Variable_Importance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compute_simulations.R
341 lines (316 loc) · 9.39 KB
/
compute_simulations.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
DEBUG <- FALSE
N_SIMULATIONS <- `if`(!DEBUG, 1L:100L, 1L)
N_CPU <- ifelse(!DEBUG, 100L, 1L)
suppressMessages({
require(data.table)
if (!DEBUG) {
require(snowfall)
sfInit(parallel = TRUE, cpus = N_CPU, type = "SOCK")
sfLibrary(cpi)
sfLibrary(gtools)
sfLibrary(mlr3learners)
sfLibrary(party)
sfLibrary(permimp)
sfLibrary(reticulate)
sfLibrary(snowfall)
sfSource("data/data_gen.R")
sfSource("utils/compute_methods.R")
} else {
library(cpi)
library(mlr3learners)
library(gtools)
library("party", quietly = TRUE)
library(permimp)
library(reticulate)
source("data/data_gen.R")
source("utils/compute_methods.R")
}
})
my_apply <- lapply
if (!DEBUG) {
my_apply <- sfLapply
}
##### Running Methods #####
methods <- c(
"marginal",
"d0crt",
"permfit",
"cpi",
"cpi_rf",
"lazy",
"cpi_knockoff",
"loco",
"strobl",
# "loco_dnn"
"knockoff",
"shap",
"sage",
"mdi",
"bart"
)
list_models <- paste0("Best_model_1_", N_SIMULATIONS)
##### Configuration #####
param_grid <- expand.grid(
# File, if given, for the real data
file = "",
# The file to regenerate samples with same covariance, if given
sigma = "",
# The number of samples
n_samples = ifelse(!DEBUG, 1000L, 100L),
# n_samples = `if`(!DEBUG, seq(100, 100, by = 100), 10),
# The number of covariates
n_features = ifelse(!DEBUG, 50L, 5L),
# The number of relevant covariates
n_signal = ifelse(!DEBUG, 20L, 2L),
# The mean for the simulation
mean = c(0),
# The correlation coefficient
rho = c(
# 0,
# 0.2,
# 0.5,
0.8
),
# Number of blocks
n_blocks = ifelse(!DEBUG, 10L, 1L),
# Type of simulation
# It can be ["blocks_toeplitz", "blocks_fixed",
# "simple_toeplitz", "simple_fixed"]
type_sim = c("blocks_fixed"),
# Signal-to-Noise ratio
snr = c(4),
# The task (computation of the response vector)
prob_sim_data = c(
"classification",
"regression",
"regression_combine"
"regression_product",
"regression_relu"
# "regression_perm"
),
# The running methods implemented
method = methods,
# The d0crt method'statistic tests scaled or not
scaled_statistics = c(
# TRUE,
FALSE
),
# Refit parameter for the d0crt method
refit = FALSE,
# The statistic to use with the knockoff method
stat_knockoff = c(
# "l1_regu_path",
"lasso_cv",
"bart",
"deep"
),
# Number of permutations/samples for the DNN algos
n_perm = c(100L),
# Number of cpus for the multiprocessing unit
n_jobs = c(1L)
)
param_grid <- param_grid[
((!param_grid$scaled_statistics) & # if scaled stats
(param_grid$stat_knockoff %in% c(param_grid$stat_knockoff[[1]])) & # and defaults
(!param_grid$refit) & # and refit
(!param_grid$method %in% c(
"d0crt",
"knockoff"
))) |
((!param_grid$scaled_statistics) & # or scaled
(!param_grid$refit) &
(param_grid$method == "knockoff")) |
((param_grid$stat_knockoff %in% c(param_grid$stat_knockoff[[1]])) &
(param_grid$method == "d0crt")) |
((!param_grid$scaled_statistics) &
(!param_grid$refit) &
(param_grid$stat_knockoff %in% c(param_grid$stat_knockoff[[1]]))),
]
param_grid$index_i <- 1:nrow(param_grid)
cat(sprintf("Number of rows: %i \n", nrow(param_grid)))
if (!DEBUG) {
# Models names for saving DNNs
sfExport("list_models")
sfExport("param_grid")
}
compute_method <- function(method,
index_i,
n_simulations, ...) {
print("Begin")
cat(sprintf("%s: %i \n", method, index_i))
compute_fun <- function(seed, ...) {
sim_data <- generate_data(
seed,
...
)
print("Done loading data!")
timing <- system.time(
out <- switch(as.character(method),
marginal = compute_marginal(
sim_data,
...
),
ale = compute_ale(sim_data,
ntree = 100L,
...
),
knockoff = compute_knockoff(sim_data,
seed,
list_models[[seed]],
verbose = TRUE,
...
),
bart = compute_bart(sim_data,
ntree = 100L,
...
),
mdi = compute_mdi(sim_data,
ntree = 500L,
...
),
shap = compute_shap(sim_data,
ntree = 100L,
...
),
sage = compute_sage(sim_data,
seed,
ntree = 100L,
...
),
strobl = compute_strobl(sim_data,
ntree = 100L,
conditional = TRUE,
...
),
d0crt = compute_d0crt(sim_data,
seed,
loss = "least_square",
statistic = "randomforest",
ntree = 100L,
verbose = TRUE,
...
),
permfit = compute_permfit(
sim_data,
seed,
nominal = if (list(...)$sigma != "") read.csv(paste0(list(...)$sigma, "_nominal_columns.csv"))$x else "",
...
),
cpi = compute_cpi(
sim_data,
seed,
nominal = if (list(...)$sigma != "") read.csv(paste0(list(...)$sigma, "_nominal_columns.csv"))$x else "",
...
),
cpi_rf = compute_cpi_rf(
sim_data,
seed,
nominal = if (list(...)$sigma != "") read.csv(paste0(list(...)$sigma, "_nominal_columns.csv"))$x else "",
...
),
lazy = compute_lazy(
sim_data,
...
),
cpi_knockoff = compute_cpi_knockoff(
sim_data,
...
),
loco = compute_loco(
sim_data,
dnn = FALSE,
ntree = 100L,
...
),
loco_dnn = compute_loco(
sim_data,
dnn=TRUE,
ntree = 100L,
...
)
)
)
out <- data.frame(out)
out$elapsed <- timing[[3]]
out$correlation <- list(...)$rho
out$correlation_group <- list(...)$rho_group
out$n_samples <- list(...)$n
out$prob_data <- list(...)$prob_sim_data
return(out)
}
sim_range <- n_simulations
# compute results
result <- my_apply(sim_range, compute_fun, ...)
# postprocess and package outputs
result <- do.call(rbind, lapply(sim_range, function(ii) {
out <- result[[ii - min(sim_range) + 1]]
out$iteration <- ii
out
}))
res <- data.table(result)[,
mean(elapsed),
by = .(
n_samples,
correlation,
method,
iteration,
prob_data
)
]
res <- res[,
sum(V1) / (N_CPU * 60),
by = .(
n_samples,
method,
correlation,
prob_data
)
]
print(res)
print("Finish")
return(result)
}
# if (DEBUG) {
# set.seed(42)
# param_grid <- param_grid[sample(1:nrow(param_grid), 5), ]
# }
results <-
by(
param_grid, 1:nrow(param_grid),
function(x) {
with(
x,
compute_method(
file = file,
n = n_samples,
p = n_features,
n_signal = n_signal,
mean = mean,
rho = rho,
sigma = sigma,
n_blocks = n_blocks,
type_sim = type_sim,
snr = snr,
method = method,
index_i = index_i,
n_simulations = N_SIMULATIONS,
stat_knockoff = stat_knockoff,
refit = refit,
scaled_statistics = scaled_statistics,
prob_sim_data = prob_sim_data,
prob_type = strsplit(as.character(prob_sim_data), "_")[[1]][1],
n_perm = n_perm,
n_jobs = n_jobs
)
)
}
)
results <- rbindlist(results, fill=TRUE)
out_fname <- paste0(getwd(), "/results/results_csv/", "simulation_results_blocks_100_allMethods.csv")
if (DEBUG) {
out_fname <- gsub("\\.csv", "-debug.csv", out_fname)
}
fwrite(results, out_fname)
if (!DEBUG) {
sfStop()
}