10.1 Example

This example uses the rats data from survival package which comes as predefined TaskSurv.

library(mlr3survival)
task = mlr_tasks$get("rats")

# the target column is a survival object:
head(task$truth())
## [1] 101+  49  104+  91+ 104+ 102+

# kaplan meier estimator of survival times
task$survfit()
## Call: survfit(formula = f, data = self$data(cols = vars))
## 
##       n  events  median 0.95LCL 0.95UCL 
##     300      42      NA      NA      NA

# kaplan-meier estimator of survival times
# stratify on binarized age variable
sf = task$survfit("sex")
print(sf)
## Call: survfit(formula = f, data = self$data(cols = vars))
## 
##         n events median 0.95LCL 0.95UCL
## sex=f 150     40     NA      NA      NA
## sex=m 150      2     NA      NA      NA

# plot using ggfortify
library("ggfortify")
## Loading required package: ggplot2
autoplot(sf)

keys = mlr_learners$keys("^surv")
print(keys)
## [1] "surv.coxph"       "surv.featureless" "surv.ranger"      "surv.rpart"
learners = mlr_learners$mget(keys)

keys = mlr_measures$keys("^surv")
print(keys)
## [1] "surv.harrells_c" "surv.unos_c"
measures = mlr_measures$mget(keys)

set.seed(1)
bmr = benchmark(expand_grid(task, learners, "cv"), measures = measures)
print(bmr)
## <BenchmarkResult> of 40 experiments in 4 resamplings:
##  task          learner resampling surv.harrells_c surv.unos_c
##  rats      surv.ranger         cv          0.7862      0.8637
##  rats       surv.rpart         cv          0.7747      0.7666
##  rats       surv.coxph         cv          0.7614      0.8868
##  rats surv.featureless         cv          0.4271      0.5106