Survival Analysis in Practice
A practical guide to time-to-event modelling: censoring, Kaplan-Meier curves, Cox regression and the mistakes that are easy to make.
Survival analysis answers a question ordinary regression cannot: how long until something happens, when for many people it has not happened yet. Those unfinished observations are censored, and handling them correctly is the whole reason the methods exist.
Censoring is information, not missing data
A patient followed for three years without the event tells you their survival time is at least three years. Dropping them biases the estimate downward; treating three years as their event time biases it too. Survival methods use exactly what is known and no more.
Describe first: Kaplan-Meier
The Kaplan-Meier estimator gives the probability of surviving beyond each time point, stepping down at every event. Plot it with confidence bands and the number at risk, because the right-hand tail of a curve often rests on very few people. The log-rank test compares curves between groups.
Model next: Cox regression
The Cox model relates covariates to the hazard, the instantaneous rate of the event among those still at risk. Its output is the hazard ratio: a value of 1.5 means a 50% higher rate at any given moment, not a 50% shorter survival.
from lifelines import KaplanMeierFitter, CoxPHFitter
kmf = KaplanMeierFitter()
kmf.fit(df["months"], event_observed=df["event"])
kmf.plot_survival_function()
cph = CoxPHFitter()
cph.fit(df, duration_col="months", event_col="event")
cph.print_summary() # hazard ratios with intervals
cph.check_assumptions(df) # proportional hazards
Check the proportional hazards assumption
The model assumes each hazard ratio is constant over time. When it is not, for instance a treatment that helps early and not later, a single ratio is misleading. Inspect Schoenfeld residuals, and consider stratifying or a time-varying effect.
When there are more features than patients
With genomic data an unpenalised Cox model will not fit. Penalised versions, such as the elastic net, shrink and select at the same time. Because the penalty strength is tuned, performance must be estimated with nested cross-validation, and reported as a concordance index with its interval.
Mistakes worth avoiding
- Immortal time bias: defining a group by something that happens after follow-up starts guarantees that group some event-free time.
- Optimal cut-points: choosing the threshold that best splits a marker inflates the effect. Use a prespecified cut or keep the variable continuous.
- Too few events: precision depends on the number of events, not the number of patients.
- Reading risk groups as proven: a split found and tested in the same data is exploratory until it holds elsewhere.