-
Notifications
You must be signed in to change notification settings - Fork 0
/
coxboost_bench.R
166 lines (141 loc) · 5.43 KB
/
coxboost_bench.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
#'#############################################
#' Benchmark
#' - CoxBoost using mRNA only vs mRNA + clinical
#' Dataset: mRNA, with simple train/test split
#' + bootstrap results on test set
#' Tuning: Bayesian Optimization
#'#############################################
library(mlr3verse)
library(mlr3proba)
library(mlr3extralearners)
library(mlr3mbo)
library(progressr)
library(tictoc)
source('scripts/boot_mlr3.R')
source('scripts/helpers.R')
res_path = 'results/coxboost/'
if (!dir.exists(res_path)) {
dir.create(res_path)
}
# Reproducibility
set.seed(42)
# Progress bars
options(progressr.enable = TRUE)
handlers(global = TRUE)
handlers('progress')
# Logging (less)
lgr::get_logger('bbotk')$set_threshold('warn')
lgr::get_logger('mlr3')$set_threshold('warn')
# Global variables ----
n_folds = 5 # CV folds
n_evals = 250 # number of hyperparameter configurations to search
harrell_cindex = msr('surv.cindex')
# Tasks ----
## mRNA (~10000 features) ----
tasks = readRDS(file = 'data/tasks.rds')
task_mRNA = tasks$mRNA
## ~70%/30% train/test split
train_indx = sample(seq_len(task_mRNA$nrow), 100)
test_indx = setdiff(seq_len(task_mRNA$nrow), train_indx)
## Clinical (8 features) ----
task_clinical = readRDS(file = 'data/task_clinical.rds')
## Clinical + mRNA ----
# make combined task with clinical first and then mRNA features
cmb_task = task_clinical$clone(deep = TRUE)
cmb_task$cbind(task_mRNA$data(cols = task_mRNA$feature_names))
# Benchmarks ----
## CoxBoost (only mRNA) ----
coxboost_lrn = lrn('surv.coxboost',
standardize = FALSE, # data already standardized
return.score = FALSE, # don't need this in the output
fallback = lrn('surv.kaplan'),
stepno = to_tune(p_int(50, 500)),
penalty = to_tune(p_int(10, 1000, logscale = TRUE)), # leave at default => 9 * sum(status == 1)?
stepsize.factor = to_tune(p_dbl(1e-01, 10, logscale = TRUE)), # leave at default => 1?
criterion = to_tune(p_fct(c('pscore', 'hpscore'))) # penalized scores, `hpscore` is faster to calculate
)
coxboost_at = AutoTuner$new(
learner = coxboost_lrn,
resampling = rsmp('cv', folds = n_folds),
measure = harrell_cindex,
terminator = trm('evals', n_evals = n_evals),
tuner = tnr('mbo')
)
print('Train Start: CoxBoost (mRNA only)')
tic()
coxboost_at$train(task_mRNA, train_indx)
toc()
print('Train Finished')
print('Performance on test set of hpc that BO chose')
coxboost_at$predict(task_mRNA, test_indx)$score(harrell_cindex)
print('Performance on test set of best hpc')
best_hpc = coxboost_at$archive$best()$x_domain[[1L]]
coxboost_lrn$param_set$values = mlr3misc::insert_named(coxboost_lrn$param_set$values, best_hpc)
coxboost_lrn$train(task_mRNA, train_indx)
coxboost_lrn$predict(task_mRNA, test_indx)$score(harrell_cindex)
print('Calculate bootstrap CIs for C-index on test set (best hpc)')
tic()
boot_cox = boot_ci(data = task_mRNA$data(rows = test_indx), nrsmps = 1000,
learner = coxboost_lrn, num_threads = 1, parallel = 'no')
toc()
print('Calculate C-indexes for all hpcs (test + train set)')
tic()
hpc_res = get_cindex_all_hps(at = coxboost_at, task = task_mRNA, train_indx = train_indx)
toc()
print('Saving results...')
saveRDS(object = list(
train_indx = train_indx,
test_indx = test_indx,
coxboost_lrn = coxboost_lrn, # trained learner with best hpc from BO
boot_cox = boot_cox, # bootstrap test set results of learner with best hpc
hpc_res = hpc_res),
file = paste0(res_path, 'coxboost_mRNA_test.rds')
)
## CoxBoost (clinical + mRNA) ----
coxboost_lrn = lrn('surv.coxboost',
standardize = FALSE, # data already standardized
return.score = FALSE, # don't need this in the output
fallback = lrn('surv.kaplan'),
stepno = to_tune(p_int(50, 500)),
penalty = to_tune(p_int(10, 1000, logscale = TRUE)), # leave at default => 9 * sum(status == 1)?
stepsize.factor = to_tune(p_dbl(1e-01, 10, logscale = TRUE)), # leave at default => 1?
criterion = to_tune(p_fct(c('pscore', 'hpscore'))), # penalized scores, `hpscore` is faster to calculate
unpen.index = 1:length(task_clinical$feature_names) # add clinical features as mandatory
)
coxboost_at = AutoTuner$new(
learner = coxboost_lrn,
resampling = rsmp('cv', folds = n_folds),
measure = harrell_cindex,
terminator = trm('evals', n_evals = n_evals),
tuner = tnr('mbo')
)
print('Train Start: CoxBoost (mRNA + clinical)')
tic()
coxboost_at$train(cmb_task, train_indx)
toc()
print('Train Finished')
print('Performance on test set of hpc that BO chose')
coxboost_at$predict(cmb_task, test_indx)$score(harrell_cindex)
print('Performance on test set of best hpc')
best_hpc = coxboost_at$archive$best()$x_domain[[1L]]
coxboost_lrn$param_set$values = mlr3misc::insert_named(coxboost_lrn$param_set$values, best_hpc)
coxboost_lrn$train(cmb_task, train_indx)
coxboost_lrn$predict(cmb_task, test_indx)$score(harrell_cindex)
print('Calculate bootstrap CIs for C-index on test set (best hpc)')
tic()
boot_cox = boot_ci(data = cmb_task$data(rows = test_indx), nrsmps = 1000,
learner = coxboost_lrn, num_threads = 1, parallel = 'no')
toc()
print('Calculate C-indexes for all hpcs (test + train set)')
tic()
hpc_res = get_cindex_all_hps(at = coxboost_at, task = cmb_task, train_indx = train_indx)
toc()
print('Saving results...')
saveRDS(object = list(
train_indx = train_indx,
test_indx = test_indx,
coxboost_lrn = coxboost_lrn, # trained learner with best hpc from BO
boot_cox = boot_cox, # bootstrap test set results of learner with best hpc
hpc_res = hpc_res),
file = paste0(res_path, 'coxboost_clinical_mRNA_test.rds')
)