This vignette is an introduction to performing survival analysis in mlr3proba.

A very quick introduction to survival analysis

Survival analysis is a sub-field of supervised machine learning in which the aim is to predict the survival distribution of a given individual. Arguably the main feature of survival analysis is that unlike classification and regression, learners are trained on two features: 1. the time until the event takes place, 2. the event type: either censoring or death. At a particular time-point, an individual is either: alive, dead, or censored. Censoring occurs if it is unknown if an individual is alive or dead. For example, say we are interested in patients in hospital and every day it is recorded if they are alive or dead, then after a patient leaves it is unknown if they are alive or dead, hence they are censored.

In the case that there is no censoring, but a predicted probability distribution is still the goal, then probabilistic regression learners are advised instead.

Train and Predict


# create task and learner

veteran = mlr3misc::load_dataset("veteran", package = "survival")
task_veteran = TaskSurv$new(id = "veteran", backend = veteran, time = "time", event = "status")
learner = lrn("surv.coxph")

# train/test split 

train_set = sample(task_veteran$nrow, 0.8 * task_veteran$nrow)
test_set = setdiff(seq_len(task_veteran$nrow), train_set)

# fit Cox PH and inspect model

learner$train(task_veteran, row_ids = train_set)
learner$model
#> Call:
#> survival::coxph(formula = task$formula(), data = task$data(), 
#>     x = TRUE)
#> 
#>                        coef exp(coef)  se(coef)      z        p
#> age               -0.007685  0.992345  0.011751 -0.654 0.513130
#> celltypesmallcell  0.976473  2.655076  0.317421  3.076 0.002096
#> celltypeadeno      1.173923  3.234657  0.354138  3.315 0.000917
#> celltypelarge      0.460501  1.584868  0.334837  1.375 0.169039
#> diagtime           0.001186  1.001187  0.009848  0.120 0.904155
#> karno             -0.029826  0.970614  0.006497 -4.590 4.42e-06
#> prior              0.010400  1.010454  0.026531  0.392 0.695071
#> trt                0.225305  1.252705  0.247522  0.910 0.362695
#> 
#> Likelihood ratio test=47.36  on 8 df, p=1.307e-07
#> n= 109, number of events= 100

# make predictions for new data

prediction = learner$predict(task_veteran, row_ids = test_set)
prediction
#> <PredictionSurv> for 28 observations:
#>     row_id time status      crank                distr         lp
#>          3  228   TRUE -0.6936862 <VectorDistribution> -0.6936862
#>          4  126   TRUE -0.7746938 <VectorDistribution> -0.7746938
#>          8  110   TRUE -1.4899216 <VectorDistribution> -1.4899216
#> ---                                                              
#>        119    7   TRUE  1.1495552 <VectorDistribution>  1.1495552
#>        128   19   TRUE  0.9844033 <VectorDistribution>  0.9844033
#>        137   49   TRUE  0.8945899 <VectorDistribution>  0.8945899

Evaluate - crank, lp, and distr

Every PredictionSurv object can predict one or more of:

  • lp - Linear predictor calculated as the fitted coefficients multiplied by the test data.
  • distr - Predicted survival distribution, either discrete or continuous. Implemented in distr6.
  • crank - Continuous risk ranking.

lp and crank can be used with measures of discrimination such as the concordance index. Whilst lp is a specific mathematical prediction, crank is any continuous ranking that identifies who is more or less likely to experience the event. So far the only implemented learner that only returns a continuous ranking is surv.svm. If a PredictionSurv returns an lp then the crank is identical to this. Otherwise crank is calculated as the expectation of the predicted survival distribution. Note that for linear proportional hazards models, the ranking (but not necessarily the crank score itself) given by lp and the expectation of distr, is identical.

Probability distributions with distr6

Predicted distributions are implemented in distr6, which contains functionality for plotting and further analysis of probability distributions. See here for full tutorials. Briefly we will go over the most important parts for mlr3proba.

Composition

Finally we take a look at the PipeOps implemented in mlr3proba, which are used for composition of predict types. For example, if a learner only returns a linear predictor, then PipeOpDistrCompositor can be used to estimate a survival distribution. Or, if a learner returns a distr then PipeOpCrankCompositor can be used to estimate crank from distr. See mlr3pipelines for full tutorials and details on PipeOps.

PipeOpCrankCompositor

Note that a PredictionSurv will always return crank, but this may either be the same as the lp or the expectation of distr. This compositor allows you to change the estimation method.

# PipeOpCrankCompositor - Only one model required.

leaner = lrn("surv.coxph")
prediction = leaner$train(task)$predict(task)

# Doesn't need training - Note: no `overwrite` option as `crank` is always
# present so the compositor if used will always overwrite.

poc = po("crankcompose", param_vals = list(method = "mean"))
composed_prediction = poc$predict(list(prediction))$output

# Note that whilst the actual values of `lp` and `crank` are different,
# the rankings are the same, so discrimination measures are unchanged.

prediction$crank[1:10]
#>  [1]  1.6603932  0.8550971  0.8550971 -1.4162662 -2.2215623 -2.2215623
#>  [7]  1.6773239  0.8720277  0.8720277 -1.3993355
composed_prediction$crank[1:10]
#>  [1] 27.7298076 14.0493649 14.0493649  1.5914788  0.7156143  0.7156143
#>  [7] 28.0961954 14.2641535 14.2641535  1.6183524
all(order(prediction$crank) == order(composed_prediction$crank))
#> [1] TRUE
cbind(Original = prediction$score(), Composed = composed_prediction$score())
#>                Original  Composed
#> surv.harrellC 0.7780967 0.7780967

# Again a wrapper can be used to simplify this
crankcompositor(lrn("surv.coxph"), method = "mean")$train(task)$predict(task)
#> <PredictionSurv> for 300 observations:
#>     row_id time status     crank                distr         lp
#>          1  101  FALSE 27.729808 <VectorDistribution>  1.6603932
#>          2   49   TRUE 14.049365 <VectorDistribution>  0.8550971
#>          3  104  FALSE 14.049365 <VectorDistribution>  0.8550971
#> ---                                                             
#>        298   92  FALSE  3.597482 <VectorDistribution> -0.5866651
#>        299  104  FALSE  1.630197 <VectorDistribution> -1.3919612
#>        300  102  FALSE  1.630197 <VectorDistribution> -1.3919612

All Together Now

Putting all of this together we can perform a benchmark experiment to find the best learner for making predictions on a simulated dataset.