Book contents

REGRESSION MODELLING FOR HEALTH PRACTITIONERS · CHAPTER 7

Survival Analysis

When time matters

In many clinical studies, interest lies not only in whether an event occurs, but also in the time until the event occurs. Examples include time from diagnosis to death, time from surgery to relapse, or duration of hospital stay. Standard regression techniques ignore the timing of events and cannot properly accommodate censored observations—subjects who do not experience the event before the end of the study period. Survival analysis encompasses statistical methods that account for time to event and censoring. Key tools include the Kaplan–Meier curve, the log–rank test for comparing groups, and the Cox proportional hazards model for multivariable analysis. If survival times are analysed without these special techniques, faulty interpretation can result.

Kaplan–Meier curves and the log–rank test

The Kaplan–Meier (KM) estimator provides a non–parametric estimate of the survival function S(t)=Pr(T>t)S(t) = \Pr(T>t) and accommodates censored observations. A plot of S(t)S(t) versus time displays the probability of surviving past each time point and typically appears as a step function. Survival curves for different treatment groups can be compared visually and statistically using the log–rank test, which assesses whether the survival experience differs across groups. The log–rank statistic is based on observed and expected numbers of events at each time.

Cox proportional hazards model

To examine the effect of several covariates on survival simultaneously, the Cox proportional hazards model specifies the hazard (instantaneous event rate) as h(t∣𝐱)=h0(t)exp(β1x1+⋯+βpxp),h(t \mid \mathbf{x}) = h_0(t)\,\exp(\beta_1 x_1 + \cdots + \beta_p x_p), where h0(t)h_0(t) is an unspecified baseline hazard and βj\beta_j are log–hazard ratios. The hazard ratio exp(βj)\exp(\beta_j) compares the hazard of the event for a one–unit increase in xjx_j, holding other variables constant. Cox models assume proportional hazards (hazard ratios constant over time). When this assumption is violated, time–dependent covariates or alternative models may be needed. Survival analysis also distinguishes between overall survival and cause–specific survival; competing risks models are used when different event types compete.

Worked example

Suppose we analyse survival of patients after myocardial infarction (MI), comparing two treatments: standard care (0) vs a new drug (1). Patients are followed for up to five years. We record Time (follow–up time), Status (1 if death, 0 if censored), Treatment, and Age.

Worked example

R

library(survival)

# Assume df has columns: Time, Status, Treatment, Age
fit_km <- survfit(Surv(Time, Status) ~ Treatment, data = df)
plot(fit_km, col = c("black", "blue"), xlab = "Time (years)",
     ylab = "Survival probability")
legend("bottomleft", legend = c("Standard", "New drug"), col = c("black", "blue"), lty = 1)

survdiff(Surv(Time, Status) ~ Treatment, data = df)  # log-rank test

# Cox model with covariates Treatment and Age
fit_cox <- coxph(Surv(Time, Status) ~ Treatment + Age, data = df)
summary(fit_cox)
exp(coef(fit_cox))         # hazard ratios
exp(confint(fit_cox))      # confidence intervals

Python (lifelines)

import pandas as pd
from lifelines import KaplanMeierFitter, CoxPHFitter

# df: DataFrame with Time, Status, Treatment, Age
kmf = KaplanMeierFitter()
for label, group in df.groupby('Treatment'):
    kmf.fit(group['Time'], group['Status'], label=str(label))
    kmf.plot_survival_function()

from lifelines.statistics import logrank_test
res = logrank_test(df.loc[df['Treatment']==0,'Time'],
                   df.loc[df['Treatment']==1,'Time'],
                   event_observed_A=df.loc[df['Treatment']==0,'Status'],
                   event_observed_B=df.loc[df['Treatment']==1,'Status'])
print(res.p_value)

cox = CoxPHFitter()
cox.fit(df[['Time','Status','Treatment','Age']], 'Time', event_col='Status')
cox.print_summary()

Julia

using DataFrames, Survival, StatsPlots

# df: DataFrame with Time, Status, Treatment, Age
fit_km = fit(KaplanMeier, df.Time, df.Status, strata = df.Treatment)
plot(fit_km, xlabel = "Time (years)", ylabel = "Survival probability")

# Log-rank test
logrank(df.Time, df.Status, df.Treatment)

# Cox model
fit_cox = fit(CoxPHModel, @formula(Surv(Time, Status) ~ Treatment + Age), df)
coeftable(fit_cox)
exp.(coef(fit_cox))

Interpretation

Exercises

Exercises

  1. Using the MI dataset described above, estimate and plot the Kaplan–Meier survival curves for the two treatment groups. Perform the log–rank test and interpret the result.

  2. Fit a Cox proportional hazards model including treatment and age. Report hazard ratios and 95% confidence intervals. Test the proportional hazards assumption using a relevant method in your chosen software.

  3. Explain the difference between a hazard ratio and a risk ratio. Provide an example where a hazard ratio of 0.7 might correspond to a modest absolute difference in survival probability.

  4. Simulate survival times from an exponential distribution with rate λ\lambda depending on a binary exposure. Fit Cox and exponential models to estimate the exposure effect and compare the results.

Code examples use illustrative datasets. Supply data with the named columns to run them.