-
Notifications
You must be signed in to change notification settings - Fork 0
/
4-disability.Rmd
511 lines (387 loc) · 12.1 KB
/
4-disability.Rmd
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
---
title: "4-disability"
author: "Bernard"
date: "2021-06-25"
output: workflowr::wflow_html
editor_options:
chunk_output_type: console
---
# Introduction
```{r, include = FALSE}
knitr::opts_chunk$set(eval = FALSE)
```
```{r}
# Helper
library (tidyverse)
# ML
library (mlr3)
library (mlr3learners)
library (mlr3tuning)
library (mlr3viz)
library (mlr3fselect)
library (mlr3pipelines)
library (mlr3hyperband)
set.seed(7832)
lgr::get_logger("mlr3")$set_threshold("warn")
lgr::get_logger("bbotk")$set_threshold("warn")
```
# Load data
```{r}
dat <- readRDS("output/df.RDS")
train <- dat$df_list$dis$train_imp
test <- dat$df_list$dis$test_imp
comb <- bind_rows(train, test)
train_id <- 1: nrow (train)
test_id <- (nrow (train) + 1): nrow (comb)
```
# Set task
```{r}
# Set training task
task<- TaskClassif$new (id = "disability", backend = comb, target = "outcome")
task$nrow
task$feature_names
task$set_col_roles("ID", roles = "name")
# # Set test task
# task_tr <- TaskClassif$new (id = "neckpain", backend = test, target = "imp_np")
# task_tr$set_col_roles("ID", roles = "name")
# Set pre proc sets
poe <- po("encode", method = "one-hot")
poe$train(list(task))[[1]]$data()
poscale <- po("scale", param_vals = list (center = TRUE, scale = TRUE))
poscale$train(list(task))[[1]]$data()
```
# Set tuning
```{r}
evals <- trm("none")
measure <- msr("classif.auc")
measures <- list (msr("classif.auc"),
msr("classif.acc"),
msr("classif.tpr"),
msr("classif.fpr"),
msr("classif.fnr"),
msr("classif.tnr"))
# Set resample
cv_inner <- rsmp("cv", folds = 5)
cv_outer <- rsmp("cv", folds = 3)
```
# Set logistic regression model
```{r}
# Set learner with type proability
lrn_logreg <- lrn("classif.log_reg", id = "log", predict_type = "prob")
# Graph with factor encoding and scaling
grln_logreg <- poscale %>>%
#poe %>>%
lrn_logreg
plot (grln_logreg)
grln_logreg_lnr <- GraphLearner$new(grln_logreg)
# Set autotuner
at_grln_logreg <- AutoFSelector$new(
learner = grln_logreg_lnr,
resampling = cv_inner,
measure = measure,
terminator = trm("combo"),
fselect = fs("sequential", strategy = "sbs"),
store_models = TRUE)
# Runs the outer loop sequentially and the inner loop in parallel
future::plan(list("sequential", "multisession"))
# Nested resampling for internal validation
rr_logreg <- resample(task,
at_grln_logreg,
cv_outer,
store_models = TRUE)
future:::ClusterRegistry("stop")
future::plan("multisession")
# Train learner
at_grln_logreg$train (task, row_ids = train_id)
as.data.table (at_grln_logreg$archive)
at_grln_logreg$fselect_result
future:::ClusterRegistry("stop")
# Predict learner
# prediction = at_grln_logreg$predict(task, row_ids = test_id)
# autoplot(prediction, type = "roc")
# prediction$score (measures)
```
# Set kknn model
```{r}
# Set learner with type proability
lrn_kknn <- lrn("classif.kknn", id = "kknn", predict_type = "prob")
# Graph with factor encoding and scaling
grln_kknn <- poscale %>>%
poe %>>%
lrn_kknn
plot (grln_kknn)
grln_kknn_lnr <- GraphLearner$new(grln_kknn)
# Tuning
grln_kknn_lnr$param_set$values$kknn.k <- to_tune(1, 10)
#grln_kknn_lnr$param_set$values$threshold.thresholds <- to_tune(p_dbl (0,1))
# Set autotuner
at_grln_kknn <- AutoTuner$new(
learner = grln_kknn_lnr,
resampling = cv_inner,
measure = measure,
terminator = evals,
tuner = tnr("grid_search", resolution = 10),
store_models = TRUE
)
# Runs the outer loop sequentially and the inner loop in parallel
future::plan(list("sequential", "multisession"))
# Nested resampling for internal validation
rr_kknn <- resample(task,
at_grln_kknn,
cv_outer,
store_models = TRUE)
future:::ClusterRegistry("stop")
future::plan("multisession")
# Train learner
at_grln_kknn$train (task, row_ids = train_id)
at_grln_kknn$archive
at_grln_kknn$tuning_result
future:::ClusterRegistry("stop")
#grln_kknn_lnr$param_set$values <- at_grln_kknn$tuning_instance$result_learner_param_vals
# Predict learner
# prediction = at_grln_kknn$predict(task, row_ids = test_id)
# prediction$score (measures)
# autoplot(prediction, type = "roc")
```
# Set xgboost
```{r}
lrn_xgb <- lrn("classif.xgboost", id = "xgb", predict_type = "prob", eta = 0.01)
grln_xgb <- poscale %>>%
poe %>>%
lrn_xgb
plot (grln_xgb)
grln_xgb_lnr <- GraphLearner$new(grln_xgb)
grln_xgb_lnr$param_set
ps_xgb = ParamSet$new(
params = list(
ParamDbl$new("xgb.eta", lower = 0.001, upper = 0.2),
ParamDbl$new("xgb.max_depth", lower = 1, upper = 20),
ParamDbl$new("xgb.nrounds", lower = 100, upper = 5000, tags = "budget"),
ParamDbl$new("xgb.colsample_bytree", lower = 0.5, upper = 1),
ParamDbl$new("xgb.colsample_bylevel", lower = 0.5, upper = 1),
ParamDbl$new("xgb.subsample", lower = 0.5, upper = 1),
ParamDbl$new("xgb.gamma", lower = -7, upper = 6),
ParamDbl$new("xgb.lambda", lower = -10, upper = 10),
ParamDbl$new("xgb.alpha", lower = -10, upper = 10)
))
ps_xgb$trafo = function(x, param_set) {
idx_gamma = grep("gamma", names(x))
x[[idx_gamma]] = 2^(x[[idx_gamma]])
idx_lambda = grep("lambda", names(x))
x[[idx_lambda]] = as.integer (2^(x[[idx_lambda]]))
idx_alpha = grep("alpha", names(x))
x[[idx_alpha]] = as.integer (2^(x[[idx_alpha]]))
idx_nrounds = grep("nrounds", names(x))
x[[idx_nrounds]] = as.integer (x[[idx_nrounds]])
idx_depth = grep("depth", names(x))
x[[idx_depth]] = as.integer (x[[idx_depth]])
x
}
bind_rows(generate_design_grid(ps_xgb, 3)$transpose())
at_grln_xgb <- AutoTuner$new (
learner = grln_xgb_lnr,
resampling = cv_inner,
measure = measure,
search_space = ps_xgb,
terminator = evals,
tuner = tnr("hyperband", eta = 5),
store_models = TRUE
)
# Runs the outer loop sequentially and the inner loop in parallel
future::plan(list("sequential", "multisession"))
# Nested resampling for internal validation
rr_xgb <- resample(task,
at_grln_xgb,
cv_outer,
store_models = TRUE)
future:::ClusterRegistry("stop")
future::plan("multisession")
# test learner
at_grln_xgb$train (task, row_ids = train_id)
at_grln_xgb$archive
at_grln_xgb$tuning_result
future:::ClusterRegistry("stop")
# prediction = at_grln_xgb$predict(task, row_ids = test_id)
# prediction$score (measures)
# autoplot(prediction, type = "roc")
```
# Set lasso
```{r}
lrn_lasso <- lrn("classif.glmnet", id = "lasso", predict_type = "prob")
grln_lasso <- poscale %>>%
poe %>>%
lrn_lasso
plot (grln_lasso)
grln_lasso_lnr <- GraphLearner$new(grln_lasso)
grln_lasso_lnr$param_set
grln_lasso_lnr$param_set$values$lasso.s <- to_tune(0, 1)
at_grln_lasso <- AutoTuner$new (
learner = grln_lasso_lnr,
resampling = cv_inner,
measure = measure,
terminator = evals,
tuner = tnr("grid_search", resolution = 100),
store_models = TRUE
)
# Runs the outer loop sequentially and the inner loop in parallel
future::plan(list("sequential", "multisession"))
# Nested resampling for internal validation
rr_lasso <- resample(task,
at_grln_lasso,
cv_outer,
store_models = TRUE)
future:::ClusterRegistry("stop")
future::plan("multisession")
# test learner
at_grln_lasso$train (task, row_ids = train_id)
at_grln_lasso$archive
at_grln_lasso$tuning_result
future:::ClusterRegistry("stop")
# prediction = at_grln_lasso$predict(task, row_ids = test_id)
# prediction$score (measures)
# autoplot(prediction, type = "roc")
```
# Set random forest
```{r}
lrn_rf <- lrn("classif.ranger", id = "rf", predict_type = "prob")
grln_rf <- poscale %>>%
poe %>>%
lrn_rf
plot (grln_rf)
grln_rf_lnr <- GraphLearner$new(grln_rf)
grln_rf_lnr$param_set
ps_rf <- ParamSet$new(list (
ParamInt$new ("rf.mtry", lower = 5, upper = 15, tags = "budget"),
ParamDbl$new ("rf.sample.fraction", lower = 0.5, upper = 1),
ParamInt$new ("rf.min.node.size", lower = 1, upper = 20)
))
bind_rows(generate_design_grid(ps_rf, 5)$transpose())
at_grln_rf <- AutoTuner$new (
learner = grln_rf_lnr,
resampling = cv_inner,
measure = measure,
terminator = evals,
search_space = ps_rf,
tuner = tnr("hyperband", eta = 5),
store_models = TRUE
)
# Runs the outer loop sequentially and the inner loop in parallel
future::plan(list("sequential", "multisession"))
# Nested resampling for internal validation
rr_rf <- resample(task,
at_grln_rf,
cv_outer,
store_models = TRUE)
future:::ClusterRegistry("stop")
future::plan("multisession")
# test learner
at_grln_rf$train (task, row_ids = train_id)
at_grln_rf$archive
at_grln_rf$tuning_result
future:::ClusterRegistry("stop")
# prediction = at_grln_rf$predict(task, row_ids = test_id)
# prediction$score (measures)
# autoplot(prediction, type = "roc")
```
# Set neural net
```{r}
lrn_net <- lrn("classif.nnet", id = "nnet", predict_type = "prob")
grln_net <- poscale %>>%
poe %>>%
lrn_net
plot (grln_net)
grln_net_lnr <- GraphLearner$new(grln_net)
grln_net_lnr$param_set
ps_net <- ParamSet$new(list (
ParamInt$new ("nnet.size", lower = 1, upper = 10),
ParamDbl$new ("nnet.decay", lower = 0.1, upper = 0.5)
))
bind_rows(generate_design_grid(ps_net, 5)$transpose())
at_grln_net <- AutoTuner$new (
learner = grln_net_lnr,
resampling = cv_inner,
measure = measure,
terminator = evals,
search_space = ps_net,
tuner = tnr("grid_search", resolution = 10),
store_models = TRUE
)
# Runs the outer loop sequentially and the inner loop in parallel
future::plan(list("sequential", "multisession"))
# Nested resampling for internal validation
rr_net <- resample(task,
at_grln_net,
cv_outer,
store_models = TRUE)
future:::ClusterRegistry("stop")
future::plan("multisession")
# test learner
at_grln_net$train (task, row_ids = train_id)
at_grln_net$archive
at_grln_net$tuning_result
future:::ClusterRegistry("stop")
# prediction = at_grln_net$predict(task, row_ids = test_id)
# prediction$score (measures)
# autoplot(prediction, type = "roc")
```
# Set support vector machine
```{r}
lrn_svm <- lrn("classif.svm", id = "svm", type = "C-classification", kernel = "radial", predict_type = "prob")
grln_svm <- poscale %>>%
poe %>>%
lrn_svm
plot (grln_svm)
grln_svm_lnr <- GraphLearner$new(grln_svm)
grln_svm_lnr$param_set
ps_svm <- ParamSet$new(list (
ParamDbl$new ("svm.cost", lower = 0.1, upper = 10),
ParamDbl$new ("svm.gamma", lower = 0, upper = 5)
))
bind_rows(generate_design_grid(ps_svm, 5)$transpose())
at_grln_svm <- AutoTuner$new (
learner = grln_svm_lnr,
resampling = cv_inner,
measure = measure,
terminator = evals,
search_space = ps_svm,
tuner = tnr("grid_search", resolution = 10),
store_models = TRUE
)
# Runs the outer loop sequentially and the inner loop in parallel
future::plan(list("sequential", "multisession"))
# Nested resampling for internal validation
rr_svm <- resample(task,
at_grln_svm,
cv_outer,
store_models = TRUE)
future:::ClusterRegistry("stop")
future::plan("multisession")
# test learner
at_grln_svm$train (task, row_ids = train_id)
at_grln_svm$archive
at_grln_svm$tuning_result
future:::ClusterRegistry("stop")
# prediction = at_grln_svm$predict(task, row_ids = test_id)
# prediction$score (measures)
# autoplot(prediction, type = "roc")
```
# Save files
```{r}
rsmp_list <- list (rr_logreg = rr_logreg,
rr_kknn = rr_kknn,
rr_xgb = rr_xgb,
rr_lasso = rr_lasso,
rr_rf = rr_rf,
rr_net = rr_net,
rr_svm = rr_svm)
model_list <- list (at_grln_logreg = at_grln_logreg,
at_grln_kknn = at_grln_kknn,
at_grln_xgb = at_grln_xgb,
at_grln_lasso = at_grln_lasso,
at_grln_rf = at_grln_rf,
at_grln_net = at_grln_net,
at_grln_svm = at_grln_svm)
saveRDS (list (rsmp_list = rsmp_list,
model_list = model_list),
"output/dis_result.RDS")
```