Skip to content

Predicting time to event

Use penalized Cox, a random survival forest or boosted survival models with censored targets. Score them with concordance, integrated Brier and time-dependent AUC.

Advanced ~12 min9 steps

What runs in CodeSplash

You will be able to

  • Set up a survival experiment with a time target and an event
  • Compare penalized Cox, random survival forests and boosted survival
  • Read Harrell's and Uno's C, the integrated Brier score and time-dependent AUC

Read first

01

A target with holes

Explore

For some rows you only know that the event hadn't happened yet. That's censoring, and it breaks ordinary regression.

Raise Censoring rate and compare Dropping censored and Counting as events with True median. Then switch on High-risk patients leave early.

FExplorable · Why you cannot just drop censored patients

What goes wrong if you ignore or discard censored observations?

If you drop the censored patients, how does the median compare with the truth?

How sure?
0.10
0.100
300
median6.9Truth7.5Kaplan–Meier3.4Drop censored3.3As events

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

Make your prediction to see the result.

So you need learners that understand a time plus an event flag, and metrics that do too.

02

Three learners

Read
LearnerIdeaAssumption
Penalized Cox regressionCox with an elastic-net penalty; can select featuresProportional hazards
Random survival forestLog-rank trees, each leaf holding a cumulative hazardNone about the hazard's shape
XGBoost, with Survival modelBoosted Cox, or accelerated failure timeDepends on the model you pick

Penalized Cox gets survival curves from a Breslow baselineBreslow baselineThe cumulative hazard for a person with risk score zero, estimated from the training rows; it turns a risk score into survival curves. on the training rows.

03

Will a forest beat Cox?

Predict

Predict first

The veteran lung-cancer trial: 110 training rows and 9 design columns. Which gets the best cross-validated concordance?

Penalized CoxRandom survival forestBoosted Cox
Reveal

Penalized Cox: 0.710, against 0.658 for the forest and 0.648 for boosting. On 27 test rows, boosting has 0.755 and Cox 0.713: too few rows to say anything. Few rows favor fewer assumptions to estimate, even when those assumptions are strong.

04

Concordance

Read

The C-indexConcordance indexThe chance a survival model ranks two people correctly by who has the event first; 0.5 is guessing, 1 is perfect. is AUC's cousin. Take pairs where you can tell who had the event first. It's the share in which the model gave the earlier one a higher risk.

  • 0.5 is chance, and 1 is a perfect ordering.
  • Censored rows can't form a pair with someone who outlives their censoring time.

Heavy censoring can bias Harrell's version. Uno's CUno's CConcordance weighted by the inverse chance of staying uncensored, so it does not drift with the censoring pattern. reweights pairs by the censoring distribution. For example, with penalized Cox on veteran with fewer features and a stronger penalty: 0.718.

05

Beyond ranking

Explore

Concordance sees only order, so it needs partners:

  • Integrated Brier scoreIntegrated Brier scoreHow far predicted survival probabilities are from what happened, averaged over time; lower is better.: squared error of predicted survival over time, weighted for censoring. Lower is better.
  • Time-dependent AUCTime-dependent AUCROC AUC for having the event by a given time, so you can see at which horizons the model ranks well.: at time tt, can the model separate who has had the event from who hasn't?
Time-dependent AUCHow well does a survival model rank who has the event by each time?
View data
Days since diagnosisTime-dependent AUC
1200.7100
1800.6780
2400.6850
3000.6830
3600.6550
4300.6620
5200.6440
6100.6090
7000.5900

Find where the curve is highest. A model can separate early events well, and later ones badly.

06

Low concordance can be real

Read

Lung cancer data, with few features: penalized Cox has 0.639 and a forest 0.616. On 46 test rows they fall to 0.561 and 0.499.

That's close to chance on these rows. It's a fair finding about what these few features can predict, and no learner will change it.

07

Check your understanding

Checkpoint

Check yourself

A survival model has a C-index of 0.72. Which reading is right?

Show the answer

Answer: a

Concordance is a ranking measure. It says nothing about whether the predicted survival curves are well calibrated, and for that you read the integrated Brier score.

Check yourself

Why not train an ordinary regression on the observed time, ignoring censoring?

Show the answer

Answer: b

Dropping those rows biases things too, because long survivors are the ones most often censored. Survival learners use the partial information correctly.

Check yourself

Why might you pick a random survival forest over penalized Cox?

Show the answer

Answer: a

The forest makes no proportional-hazards assumption and finds interactions by itself. Its engines agree statistically, not digit for digit, and it offers no native coefficients.

08

Set it up

In the app

In Setup, choose the time as Target and set Event (survival), with Event level if the column is categorical. Task becomes Survival (time to event). Survival runs have Overview, Evaluation, Code and Log tabs, and Evaluation shows these metrics with survival curves by risk group.

Survival by risk groupDo people the model calls riskier actually have the event sooner?
View data
DaysSurvivalRisk group
620.9730Low risk (n = 76)
1170.9300Low risk (n = 76)
1590.8900Low risk (n = 76)
2230.8640Low risk (n = 76)
2510.8320Low risk (n = 76)
3180.7950Low risk (n = 76)
3530.7690Low risk (n = 76)
4240.7400Low risk (n = 76)
4740.7130Low risk (n = 76)
5310.6840Low risk (n = 76)
5980.6590Low risk (n = 76)
6600.6380Low risk (n = 76)
7080.6170Low risk (n = 76)
7460.5950Low risk (n = 76)
7900.5750Low risk (n = 76)
8300.5580Low risk (n = 76)
680.9230Middle risk (n = 76)
1100.8570Middle risk (n = 76)
1520.8110Middle risk (n = 76)
2330.7660Middle risk (n = 76)
2530.7110Middle risk (n = 76)
3010.6590Middle risk (n = 76)
3520.6170Middle risk (n = 76)
4120.5770Middle risk (n = 76)
4590.5330Middle risk (n = 76)
5290.4920Middle risk (n = 76)
5480.4590Middle risk (n = 76)
6110.4280Middle risk (n = 76)
6620.3860Middle risk (n = 76)
7210.3540Middle risk (n = 76)
7660.3310Middle risk (n = 76)
8290.3110Middle risk (n = 76)
530.9370High risk (n = 76)
870.8600High risk (n = 76)
1430.7750High risk (n = 76)
1870.6870High risk (n = 76)
2330.6290High risk (n = 76)
3040.5600High risk (n = 76)
3600.5110High risk (n = 76)
4040.4530High risk (n = 76)
4580.3960High risk (n = 76)
5040.3530High risk (n = 76)
5760.3240High risk (n = 76)
6170.2890High risk (n = 76)
6790.2570High risk (n = 76)
7100.2300High risk (n = 76)
7460.2000High risk (n = 76)
7900.1760High risk (n = 76)
Python
import numpy as np
import pandas as pd
from sksurv.datasets import load_veterans_lung_cancer
from sksurv.ensemble import RandomSurvivalForest
from sksurv.linear_model import CoxnetSurvivalAnalysis
from sksurv.preprocessing import OneHotEncoder
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler

X, y = load_veterans_lung_cancer()
X = OneHotEncoder().fit_transform(X).astype(float)
models = {"coxnet": CoxnetSurvivalAnalysis(l1_ratio=0.5, alphas=[0.02]),
          "forest": RandomSurvivalForest(200, min_samples_leaf=5, random_state=1)}
for name, m in models.items():
    c = []
    for tr, va in KFold(4, shuffle=True, random_state=1).split(X):
        s = StandardScaler().fit(X.iloc[tr])
        m.fit(s.transform(X.iloc[tr]), y[tr])
        c.append(m.score(s.transform(X.iloc[va]), y[va]))
    print(name, round(float(np.mean(c)), 3))
R
library(survival)
library(ranger)
d <- veteran
set.seed(1)
fold <- sample(rep(1:4, length.out = nrow(d)))
conc <- function(pred, te) concordance(Surv(te$time, te$status) ~ pred, reverse = TRUE)$concordance
res <- sapply(1:4, function(k) {
  tr <- d[fold != k, ]; te <- d[fold == k, ]
  cox <- coxph(Surv(time, status) ~ ., tr)
  rsf <- ranger(Surv(time, status) ~ ., tr, num.trees = 200, min.node.size = 5, seed = 1)
  risk <- rowSums(predict(rsf, te)$chf)
  c(cox = conc(predict(cox, te), te), forest = conc(risk, te))
})
round(rowMeans(res), 3)
09

Recap

Recap
  • Time-to-event targets need learners and metrics that understand censoring.
  • With few rows a penalized Cox model is hard to beat. Forests and boosting need more.
  • Read concordance with the integrated Brier score and time-dependent AUC.

Concepts in this lesson