STATHARBOR ANALYTICS · INTRODUCTION TO STATISTICS · CHAPTER 19
Introduction to Survival Analysis
Survival analysis (or time–to–event analysis) deals with time until an event occurs, such as time to death, relapse, or machine failure. Unlike ordinary regression, survival data may be censored: the event is not observed for all individuals. This chapter introduces key concepts, the Kaplan–Meier estimator, hazard functions and the Cox proportional hazards model.
Censoring and Survival Function
In survival data, each individual contributes a time and an event indicator (1 if the event is observed, 0 if censored). Right censoring occurs when the observation period ends before the event. The survival function is giving the probability of surviving beyond time . The hazard function describes the instantaneous risk of event occurrence, defined as
Kaplan–Meier Estimator
The Kaplan–Meier (KM) estimator provides a nonparametric estimate of using observed event times and counts (number of events at ) and (number at risk just before ): Confidence bands can be obtained via Greenwood’s formula. When comparing two survival curves, the log–rank test assesses whether hazard functions differ.
Worked example
Kaplan–Meier curves in R and Python.
library(survival)
times <- c(5, 8, 10, 12, 20, 25, 30)
event <- c(1, 1, 0, 1, 1, 0, 1)
km_fit <- survfit(Surv(times, event) ~ 1)
print(km_fit)
plot(km_fit)
from lifelines import KaplanMeierFitter
times = [5, 8, 10, 12, 20, 25, 30]
event = [1, 1, 0, 1, 1, 0, 1]
kmf = KaplanMeierFitter()
kmf.fit(times, event)
kmf.plot_survival_function()
The output shows the estimated survival function and plot. The steps drop at event times and remain flat during censored intervals.
Cox Proportional Hazards Model
To assess covariate effects on survival time, the Cox model specifies the hazard for individual as where is an unspecified baseline hazard and are log–hazard ratios. Estimation uses partial likelihood; no distributional assumptions are made for survival times. The hazard ratio represents the multiplicative effect on the hazard per unit increase in .
Example: Time to Relapse
Suppose we follow patients after treatment to record time to relapse and baseline covariates. We fit a Cox model using R and Python.
Worked example
Cox model in R and Python.
library(survival)
times <- c(5,8,10,12,20,25,30)
event <- c(1,1,0,1,1,0,1)
age <- c(60,55,70,65,50,75,68)
cox_fit <- coxph(Surv(times, event) ~ age)
summary(cox_fit)
from lifelines import CoxPHFitter
import pandas as pd
df = pd.DataFrame({'time': [5,8,10,12,20,25,30],
'event': [1,1,0,1,1,0,1],
'age': [60,55,70,65,50,75,68]})
cox = CoxPHFitter()
cox.fit(df, duration_col='time', event_col='event')
cox.summary
The coefficient for age reflects the increase in log–hazard per year. A positive coefficient implies higher age increases hazard, shortening survival.
Exercises
Practice
Simulate survival times for 50 individuals from an exponential distribution with mean 10 and censor 30% at 12. Estimate the Kaplan–Meier curve and plot it using R/Python/Julia.
Use the
survival(R) orlifelines(Python) package to compare survival between two treatment groups. Perform a log–rank test.Derive the partial likelihood for the Cox model and outline how the coefficients are estimated. Show that the baseline hazard is left unspecified.
Fit a Cox model with two covariates (e.g., age and biomarker) and interpret the hazard ratios. Check the proportional hazards assumption using Schoenfeld residuals.
References
Klein JP, Moeschberger ML. Survival Analysis: Techniques for Censored and Truncated Data. 2nd ed. Springer; 2003.
Therneau TM, Grambsch PM. Modeling Survival Data: Extending the Cox Model. Springer; 2000.
Collett D. Modelling Survival Data in Medical Research. 3rd ed. CRC Press; 2014.