-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcode.R
More file actions
358 lines (277 loc) · 11.5 KB
/
Copy pathcode.R
File metadata and controls
358 lines (277 loc) · 11.5 KB
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
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/usr/bin/Rscript
#-------------------------------------------------------------------------------
# This script contains the code examples in sections 2 and 3 of the paper.
# The benchmarking study of section 4 can be found in `benchmark.R`.
#-------------------------------------------------------------------------------
.libPaths("vendor")
options(digits = 3)
set.seed(2832362)
quick_mode <- "--quick" %in% commandArgs(trailing = TRUE)
library("emil")
sum(sapply(c(fit, tune, evaluate), function(f) length(body(f))))
library("caret")
sum(sapply(c(caret:::train.default, caret:::nominalTrainWorkflow,
caret:::looTrainWorkflow, caret:::adaptiveWorkflow),
function(f) length(body(f))))
#------------------------------------------------[ Section 2: The main example ]
set.seed(1234)
data("prostate", package = "ElemStatLearn")
cv <- resample(method = "crossvalidation", y = prostate$lpsa,
nrepeat = 2, nfold = 3)
result <- evaluate(procedure = "lasso",
x = prostate[1:8],
y = prostate$lpsa,
resample = cv,
pre_process = function(x, y, fold) {
data <- pre_split(x, y, fold)
data <- pre_scale(data, center = TRUE)
data <- pre_convert(data, x_fun = as.matrix)
return(data)
})
get_performance(result)
#----------------------------------------------------[ Section 2.2: Resampling ]
set.seed(1234)
cv <- resample(method = "crossvalidation", y = prostate$lpsa,
nrepeat = 2, nfold = 3)
head(cv)
#----------------------------------[ Section 2.3: Splitting and pre-processing ]
set.seed(1234)
prostate_split <- pre_split(x = prostate[1:8],
y = prostate$lpsa,
fold = cv[[1]])
prostate_split <- pre_scale(prostate_split, center = TRUE)
prostate_split <- pre_convert(prostate_split, x_fun = as.matrix)
print(prostate_split)
# Piped pre-processing chain
prostate_split <- pre_split(x = prostate[1:8], y = prostate$lpsa,
fold = cv[[1]]) %>%
pre_scale %>%
pre_convert(x_fun = as.matrix)
# Example of a pre-processing function
print(pre_pca)
# Incorporation of pre-processing chain using list representation
result <- evaluate(procedure = "lasso",
x = prostate[1:8],
y = prostate$lpsa,
resample = cv,
pre_process = list(pre_split, pre_scale,
function(data) pre_convert(data, x_fun = as.matrix)
))
#-------------------------------------[ Section 2.4: Model fitting and testing ]
set.seed(1234)
model <- fit(procedure = "lasso", x = prostate_split$fit$x,
y = prostate_split$fit$y)
prediction <- predict(object = model, x = prostate_split$test$x)
rmse(prostate_split$test$y, prediction)
head(get_prediction(result, resample = cv, format = "wide"))
rf <- modeling_procedure(method = "randomForest",
parameter = list(mtry = c(1, 3, 6)))
print(rf)
tuned_rf <- tune(rf, x = prostate[1:8], y = prostate$lpsa, resample = cv)
get_tuning(tuned_rf)
print(rf$fit_fun)
if (interactive()) {
debugonce(rf$fit_fun)
rf_result <- evaluate(procedure = rf, x = prostate[1:8], y = prostate$lpsa,
resample = cv, pre_process = list(pre_split, pre_scale),
.verbose = TRUE)
}
#------------------------------------------[ Section 2.5: Model interpretation ]
set.seed(1234)
lasso <- modeling_procedure("lasso")
model <- fit(procedure = lasso, x = prostate_split$fit$x,
y = prostate_split$fit$y)
get_importance(model)
print(lasso$importance_fun)
#-------------------------------------------[ Section 2.6: Downstream analysis ]
subtree(x = result, TRUE, "error")
select(result, Fold = TRUE, RMSE = "error")
internal_tuning <- select(result, Fold = TRUE, "model", "model",
function(m) {
data.frame(Lambda = m$lambda,
TuningRMSE = m$cvm)
}
)
head(internal_tuning)
library("ggplot2")
p <- ggplot(internal_tuning, aes(x = Lambda, y = TuningRMSE, group = Fold)) +
geom_line()
cairo_pdf("tuning-rmse.pdf", 7, 3.3)
print(p)
dev.off()
#----------------------------------------------[ Section 2.7: Model comparison ]
set.seed(1234)
# Compare regression methods
comparison <- evaluate(procedure = c(LASSO = "lasso",
RR = "ridge_regression"),
x = prostate[1:8],
y = prostate$lpsa,
resample = cv,
pre_process = function(x, y, fold) {
pre_split(x, y, fold) %>%
pre_scale %>%
pre_convert(x_fun = as.matrix)
})
get_performance(comparison, format = "wide")
#---------------------------------------------------[ Section 2.8: Scalability ]
library("parallel")
cluster <- makeCluster(spec = 4)
clusterEvalQ(cluster, library("emil"))
clusterExport(cluster, "prostate")
result <- parLapply(cluster, cv, function(fold) {
evaluate(procedure = "lasso",
x = prostate[1:8],
y = prostate$lpsa,
resample = fold,
pre_process = function(x, y, fold) {
pre_split(x, y, fold) %>%
pre_scale %>%
pre_convert(x_fun = as.matrix)
},
.cores = 8,
.checkpoint_dir = tempdir())
})
#-------------------------------------[ Section 3.1: A parallelized simulation ]
if (!quick_mode) {
# Set up the problem
x <- matrix(rnorm(100 * 10000), 100, 10000)
y <- gl(2, 50)
cv <- resample("crossvalidation", y, nrepeat = 4, nfold = 8)
# Evaluate the sequential solution
proc <- modeling_procedure("randomForest", param = list(ntree = 8000))
system.time(result_seq <- evaluate(procedure = proc, x = x, y = y,
resample = cv))
# Evaluate the standard parallel solution
system.time(result_par1 <- evaluate(procedure = proc, x = x, y = y,
resample = cv, .cores = 16))
# Set up and evaluate the alternative parallel solution
library("parallel")
options(mc.cores = 16)
par_proc <- proc
par_proc$fit_fun <- function(x, y, ntree, ...) {
require("randomForest")
# Calculate how many trees each core needs compute
nc <- getOption("mc.cores")
ntree <- table(findInterval(1:ntree - 1, ntree / nc * 1:nc))
# Fit the cores' forests
forests <- mclapply(ntree, function(nt) randomForest(x, y, ntree = nt, ...))
# Combine the cores' forests into a single forest
do.call(combine, forests)
}
system.time(result_par2 <- evaluate(procedure = par_proc, x = x, y = y,
resample = cv))
}
#---------------------------------------------[ Section 3.2: Survival modeling ]
if (!quick_mode) {
set.seed(267404)
# Load data
library("Biobase")
library("survival")
data("upp", package = "breastCancerUPP")
x <- data.frame(treatment = pData(upp)$treatment, t(exprs(upp)))
y <- with(pData(upp), Surv(t.rfs, e.rfs))
# Set up method
pre_cox_pca <- function(data) {
pca <- prcomp(data$fit$x[-1]) # Don't include the treatment feature
data$fit$x <- data.frame(treatment = data$fit$x$treatment, pca$x)
data$test$x <- data.frame(treatment = data$test$x$treatment,
predict(pca, data$test$x[-1]))
data
}
pca_cox <- modeling_procedure(
method = "pca-cox",
fit_fun = function(x, y, nfeat) {
terms <- c("treatment", sprintf("PC%i", seq_len(nfeat)))
formula <- as.formula(sprintf("y ~ %s",
paste(terms, collapse = " + ")))
coxph(formula, x)
},
predict_fun = predict_coxph,
param = list(nfeat = c(0, 1, 2, 3, 5, 9, 15))
)
# Run
options(emil_max_indent = 4)
ho <- resample("holdout", y, nfold = 10, test_fraction = 1/4,
subset = complete.cases(x))
result <- evaluate(procedure = pca_cox, x = x, y = y, resample = ho,
pre_process = list(pre_split, pre_cox_pca))
}
#----------------------------------------------[ Section 3.3: Custom ensembles ]
set.seed(35092)
fit_ensemble <- function(x, y, procedure_list) {
samples <- resample("bootstrap", y, nfold = length(procedure_list))
Map(function(procedure, fold) {
try(fit(procedure, x[index_fit(fold), ], y[index_fit(fold)]),
silent = TRUE)
}, procedure_list, samples)
}
library("dplyr")
library("tidyr")
predict_ensemble <- function(object, x) {
# Use each individual classifiers to make predictions
prediction <- lapply(object, function(model) {
if (inherits(model, "model")) {
data.frame(id = seq_len(nrow(x)),
prediction = predict(model, x)$prediction)
} else {
NULL
}
})
# Caluculate the number of votes for each class
vote <- do.call(rbind, prediction)
vote <- count(vote, id, prediction)
vote <- spread(vote, prediction, n)
# Return final predictions and voting statistics
vote[is.na(vote)] <- 0
n_model <- sum(sapply(object, inherits, "model"))
return(list(
prediction = factor(apply(vote[-1], 1, which.max),
levels = 2:ncol(vote)-1,
labels = colnames(vote)[-1]),
vote = as.data.frame(vote[-1]/n_model)
))
}
ensemble <- modeling_procedure(
method = "ensemble",
parameter = list(procedure = list(rep(c("lda", "qda", "rpart"),
each = 100)))
)
data("Sonar", package = "mlbench")
cv <- resample("crossvalidation", Sonar$Class, nrepeat = 3, nfold = 5)
comparison <- evaluate(procedure = list("lda", "qda", "rpart", ensemble),
x = Sonar, y = "Class", resample = cv)
perf <- get_performance(comparison, format = "long")
p <- ggplot(perf, aes(x = method, y = error)) + geom_boxplot() + coord_flip()
cairo_pdf("ensemble.pdf", 7, 13/6)
print(p)
dev.off()
#-------------------------------------------------------[ Section 4: Benchmark ]
# Note that this file section does not contain the
# main benchmarking code but only the examples dicussed
# later in the benchmarking section of the paper.
# Customized pre-processing to adapt data set format for a method `pamr` that
# does not use the same standard as emil
pre_pamr <- function(data) {
data$fit$x <- list(x = t(data$fit$x),
y = data$fit$y)
data$fit$y <- NULL
data$test$x <- t(data$test$x)
data
}
y <- factor(findInterval(prostate$lpsa, quantile(prostate$lpsa, 1:2/3)),
labels = c("low", "intermediate", "high"))
cv <- resample("crossvalidation", y, nfold = 5, nrep = 1)
result <- evaluate(procedure = "pamr",
x = prostate[1:8], y = y,
resample = cv, pre_process = list(pre_split, pre_pamr))
# Using emil to evalute caret models
modeling_procedure(method = "caret", parameter = list(
method = "rf",
ntree = 10000,
trControl = list(trainControl(
method = "repeatedcv", number = 5, repeats = 5,
returnData = FALSE, allowParallel = TRUE,
verboseIter = TRUE
)),
tuneGrid = list(data.frame(mtry = c(10, 50, 200)))
))