Book contents

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 TiT_i and an event indicator δi\delta_i (1 if the event is observed, 0 if censored). Right censoring occurs when the observation period ends before the event. The survival function is S(t)=P(T>t),S(t) = P(T > t), giving the probability of surviving beyond time tt. The hazard function describes the instantaneous risk of event occurrence, defined as h(t)=limΔt→0P(t≤T<t+Δt|T≥t)Δt.h(t) = \lim_{\Delta t \to 0} \frac{P(t \leq T < t+\Delta t | T \geq t)}{\Delta t}.

Kaplan–Meier Estimator

The Kaplan–Meier (KM) estimator provides a nonparametric estimate of S(t)S(t) using observed event times t1<t2<⋯<tkt_1 < t_2 < \cdots < t_k and counts djd_j (number of events at tjt_j) and njn_j (number at risk just before tjt_j): Ŝ(t)=∏tj≤t(1−djnj).\hat{S}(t) = \prod_{t_j \leq t} \left(1 - \frac{d_j}{n_j}\right). 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 ii as hi(t)=h0(t)exp(𝛃⊤𝐱i),h_i(t) = h_0(t) \exp(\boldsymbol{\beta}^\top \mathbf{x}_i), where h0(t)h_0(t) is an unspecified baseline hazard and 𝛃\boldsymbol{\beta} are log–hazard ratios. Estimation uses partial likelihood; no distributional assumptions are made for survival times. The hazard ratio exp(βj)\exp(\beta_j) represents the multiplicative effect on the hazard per unit increase in XjX_j.

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

  1. 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.

  2. Use the survival (R) or lifelines (Python) package to compare survival between two treatment groups. Perform a log–rank test.

  3. Derive the partial likelihood for the Cox model and outline how the coefficients are estimated. Show that the baseline hazard is left unspecified.

  4. 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