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.
What runs in CodeSplash
- Penalized Cox regression Verified in Python and R
- Random survival forest Verified in Python and R
- XGBoost Verified in Python and R
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
A target with holes
ExploreFor 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?
Commit your prediction above to see the result
Make your prediction to see the result.
So you need learners that understand a time plus an event flag, and metrics that do too.
Three learners
Read| Learner | Idea | Assumption |
|---|---|---|
| Penalized Cox regression | Cox with an elastic-net penalty; can select features | Proportional hazards |
| Random survival forest | Log-rank trees, each leaf holding a cumulative hazard | None about the hazard's shape |
| XGBoost, with Survival model | Boosted Cox, or accelerated failure time | Depends 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.
Will a forest beat Cox?
PredictPredict first
The veteran lung-cancer trial: 110 training rows and 9 design columns. Which gets the best cross-validated concordance?
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.
Concordance
ReadThe 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.
Beyond ranking
ExploreConcordance 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 , can the model separate who has had the event from who hasn't?
View data
| Days since diagnosis | Time-dependent AUC |
|---|---|
| 120 | 0.7100 |
| 180 | 0.6780 |
| 240 | 0.6850 |
| 300 | 0.6830 |
| 360 | 0.6550 |
| 430 | 0.6620 |
| 520 | 0.6440 |
| 610 | 0.6090 |
| 700 | 0.5900 |
Find where the curve is highest. A model can separate early events well, and later ones badly.
Low concordance can be real
ReadLung 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.
Check your understanding
CheckpointCheck 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.
Set it up
In the appIn 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.
View data
| Days | Survival | Risk group |
|---|---|---|
| 62 | 0.9730 | Low risk (n = 76) |
| 117 | 0.9300 | Low risk (n = 76) |
| 159 | 0.8900 | Low risk (n = 76) |
| 223 | 0.8640 | Low risk (n = 76) |
| 251 | 0.8320 | Low risk (n = 76) |
| 318 | 0.7950 | Low risk (n = 76) |
| 353 | 0.7690 | Low risk (n = 76) |
| 424 | 0.7400 | Low risk (n = 76) |
| 474 | 0.7130 | Low risk (n = 76) |
| 531 | 0.6840 | Low risk (n = 76) |
| 598 | 0.6590 | Low risk (n = 76) |
| 660 | 0.6380 | Low risk (n = 76) |
| 708 | 0.6170 | Low risk (n = 76) |
| 746 | 0.5950 | Low risk (n = 76) |
| 790 | 0.5750 | Low risk (n = 76) |
| 830 | 0.5580 | Low risk (n = 76) |
| 68 | 0.9230 | Middle risk (n = 76) |
| 110 | 0.8570 | Middle risk (n = 76) |
| 152 | 0.8110 | Middle risk (n = 76) |
| 233 | 0.7660 | Middle risk (n = 76) |
| 253 | 0.7110 | Middle risk (n = 76) |
| 301 | 0.6590 | Middle risk (n = 76) |
| 352 | 0.6170 | Middle risk (n = 76) |
| 412 | 0.5770 | Middle risk (n = 76) |
| 459 | 0.5330 | Middle risk (n = 76) |
| 529 | 0.4920 | Middle risk (n = 76) |
| 548 | 0.4590 | Middle risk (n = 76) |
| 611 | 0.4280 | Middle risk (n = 76) |
| 662 | 0.3860 | Middle risk (n = 76) |
| 721 | 0.3540 | Middle risk (n = 76) |
| 766 | 0.3310 | Middle risk (n = 76) |
| 829 | 0.3110 | Middle risk (n = 76) |
| 53 | 0.9370 | High risk (n = 76) |
| 87 | 0.8600 | High risk (n = 76) |
| 143 | 0.7750 | High risk (n = 76) |
| 187 | 0.6870 | High risk (n = 76) |
| 233 | 0.6290 | High risk (n = 76) |
| 304 | 0.5600 | High risk (n = 76) |
| 360 | 0.5110 | High risk (n = 76) |
| 404 | 0.4530 | High risk (n = 76) |
| 458 | 0.3960 | High risk (n = 76) |
| 504 | 0.3530 | High risk (n = 76) |
| 576 | 0.3240 | High risk (n = 76) |
| 617 | 0.2890 | High risk (n = 76) |
| 679 | 0.2570 | High risk (n = 76) |
| 710 | 0.2300 | High risk (n = 76) |
| 746 | 0.2000 | High risk (n = 76) |
| 790 | 0.1760 | High risk (n = 76) |
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))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)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