generated from kapsner/rpkgTemplate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
learner_surv_xgboost_aft.R
252 lines (229 loc) · 6.61 KB
/
learner_surv_xgboost_aft.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
#' @title R6 Class to construct a Xgboost survival learner for accelerated
#' failure time models
#'
#' @description
#' The `LearnerSurvXgboostAft` class is the interface to accelerated failure
#' time models with the `xgboost` R package for use with the `mlexperiments`
#' package.
#'
#' @details
#' Optimization metric: needs to be specified with the learner parameter
#' `eval_metric`.
#' Can be used with
#' * [mlexperiments::MLTuneParameters]
#' * [mlexperiments::MLCrossValidation]
#' * [mlexperiments::MLNestedCVs]
#' Also see the official xgboost documentation on
#' [aft models](https://xgboost.readthedocs.io/en/stable/tutorials/aft_
#' survival_analysis.html)
#'
#' @seealso [xgboost::xgb.train()], [xgboost::xgb.cv()]
#'
#' @examples
#' \donttest{# execution time >2.5 sec
#' # survival analysis
#'
#' dataset <- survival::colon |>
#' data.table::as.data.table() |>
#' na.omit()
#' dataset <- dataset[get("etype") == 2, ]
#'
#' seed <- 123
#' surv_cols <- c("status", "time", "rx")
#'
#' feature_cols <- colnames(dataset)[3:(ncol(dataset) - 1)]
#'
#' param_list_xgboost <- expand.grid(
#' objective = "survival:aft",
#' eval_metric = "aft-nloglik",
#' subsample = seq(0.6, 1, .2),
#' colsample_bytree = seq(0.6, 1, .2),
#' min_child_weight = seq(1, 5, 4),
#' learning_rate = c(0.1, 0.2),
#' max_depth = seq(1, 5, 4)
#' )
#' ncores <- 2L
#'
#' split_vector <- splitTools::multi_strata(
#' df = dataset[, .SD, .SDcols = surv_cols],
#' strategy = "kmeans",
#' k = 4
#' )
#'
#' train_x <- model.matrix(
#' ~ -1 + .,
#' dataset[, .SD, .SDcols = setdiff(feature_cols, surv_cols[1:2])]
#' )
#' train_y <- survival::Surv(
#' event = (dataset[, get("status")] |>
#' as.character() |>
#' as.integer()),
#' time = dataset[, get("time")],
#' type = "right"
#' )
#'
#' fold_list <- splitTools::create_folds(
#' y = split_vector,
#' k = 3,
#' type = "stratified",
#' seed = seed
#' )
#'
#' surv_xgboost_aft_optimizer <- mlexperiments::MLCrossValidation$new(
#' learner = LearnerSurvXgboostAft$new(
#' metric_optimization_higher_better = FALSE
#' ),
#' fold_list = fold_list,
#' ncores = ncores,
#' seed = seed
#' )
#' surv_xgboost_aft_optimizer$learner_args <- c(as.list(
#' data.table::data.table(param_list_xgboost[1, ], stringsAsFactors = FALSE)
#' ),
#' nrounds = 45L
#' )
#' surv_xgboost_aft_optimizer$performance_metric <- c_index
#'
#' # set data
#' surv_xgboost_aft_optimizer$set_data(
#' x = train_x,
#' y = train_y
#' )
#'
#' surv_xgboost_aft_optimizer$execute()
#' }
#'
#' @export
#'
LearnerSurvXgboostAft <- R6::R6Class( # nolint
classname = "LearnerSurvXgboostAft",
inherit = mllrnrs::LearnerXgboost,
public = list(
#' @description
#' Create a new `LearnerSurvXgboostAft` object.
#'
#' @param metric_optimization_higher_better A logical. Defines the direction
#' of the optimization metric used throughout the hyperparameter
#' optimization.
#'
#' @return A new `LearnerSurvXgboostAft` R6 object.
#'
#' @examples
#' LearnerSurvXgboostAft$new(metric_optimization_higher_better = FALSE)
#'
initialize = function(metric_optimization_higher_better) { # nolint
super$initialize(metric_optimization_higher_better =
metric_optimization_higher_better)
self$environment <- "mlsurvlrnrs"
self$cluster_export <- surv_xgboost_aft_ce()
private$fun_optim_cv <- surv_xgboost_aft_optimization
private$fun_bayesian_scoring_function <- surv_xgboost_aft_bsF
}
)
)
surv_xgboost_aft_ce <- function() {
c("surv_xgboost_aft_optimization")
}
surv_xgboost_aft_bsF <- function(...) { # nolint
params <- list(...)
set.seed(seed)#, kind = "L'Ecuyer-CMRG")
bayes_opt_xgboost <- surv_xgboost_aft_optimization(
x = x,
y = y,
params = params,
fold_list = method_helper$fold_list,
ncores = 1L, # important, as bayesian search is already parallelized
seed = seed
)
ret <- kdry::list.append(
list("Score" = bayes_opt_xgboost$metric_optim_mean),
bayes_opt_xgboost
)
return(ret)
}
surv_xgboost_aft_optimization <- function(
x,
y,
params,
fold_list,
ncores,
seed
) {
stopifnot(
inherits(x = y, what = "Surv"),
is.list(params),
params$objective == "survival:aft"
)
# initialize a dataframe to store the results
results_df <- data.table::data.table(
"fold" = character(0),
"metric" = numeric(0)
)
# loop over the folds
for (fold in names(fold_list)) {
# get row-ids of the current fold
train_idx <- fold_list[[fold]]
dtrain <- mllrnrs:::setup_xgb_dataset(
x = kdry::mlh_subset(x, train_idx),
y = kdry::mlh_subset(y, train_idx),
objective = params$objective
)
# use the rest for testing
dtest <- mllrnrs:::setup_xgb_dataset(
x = kdry::mlh_subset(x, -train_idx),
y = kdry::mlh_subset(y, -train_idx),
objective = params$objective
)
# setup the watchlist for monitoring the validation-metric using dtest
# this is important for early-stopping
watchlist <- list(train = dtrain, val = dtest)
fit_args <- list(
data = dtrain,
params = params,
print_every_n = as.integer(options("mlexperiments.xgb.print_every_n")),
nthread = ncores,
nrounds = as.integer(options("mlexperiments.optim.xgb.nrounds")),
watchlist = watchlist,
early_stopping_rounds = as.integer(
options("mlexperiments.optim.xgb.early_stopping_rounds")
),
verbose = as.logical(options("mlexperiments.xgb.verbose"))
)
set.seed(seed)
# fit the model
cvfit <- do.call(xgboost::xgb.train, fit_args)
# create predictions for calculating the c-index
preds <- mllrnrs:::xgboost_predict(
model = cvfit,
newdata = dtest,
ncores = ncores
)
# calculate Harrell's c-index using the `glmnet::Cindex`-implementation
perf <- c_index(
predictions = preds,
ground_truth = kdry::mlh_subset(y, -train_idx)
)
# save the results of this fold into a dataframe
# from help("ranger::ranger"):
# prediction.error - Overall out of bag prediction error. [...] for
# survival one minus Harrell's C-index.
results_df <- data.table::rbindlist(
l = list(
results_df,
list(
"fold" = fold,
"metric" = cvfit$best_score,
"validation_metric" = perf,
"best_iteration" = cvfit$best_iteration
)
),
fill = TRUE
)
}
res <- list(
"metric_optim_mean" = mean(results_df$metric),
"nrounds" = mean(results_df$best_iteration)
#% nrounds + ceiling(nrounds * (1 / length(fold_list)))
)
return(res)
}