Kaplan-Meier curves and the log-rank test
Today let's understand how to build a survival curve from real patient data, by hand, and how to tell whether two such curves are genuinely different or just noise.
A hospital runs a 24-month trial on 30 patients with a chronic condition. Fifteen are randomised to standard care, and fifteen to a new drug. Every patient is followed for up to 24 months, and for each one the hospital records a time in months, together with whether that time ended in the condition returning or the patient simply still being followed at that point with no return yet.
Here are all 30 patients at the end of the trial.
Fifteen of these rows belong to the standard-care arm, and fifteen to the new drug. Some end in the condition returning. Others end in censored, meaning that patient lasted at least that long, with no return seen yet. From just this table, can you say which arm did better, and whether that gap is real or could easily be chance?
The Kaplan-Meier estimator, built step by step down the table
Start with just the standard-care arm: 15 patients, each with a time and whether that time ended in a return or in censoring.
The idea behind the Kaplan-Meier estimator is this. At time 0, nobody has returned yet, so survival is 1. Then walk down the sorted times one by one. Every time you reach a time where the condition returned, multiply a running product by one minus the fraction of at-risk patients who returned right then. Every time you reach a censored time, leave that running product untouched, since nothing happened to that patient yet, and just drop them out of who counts as at risk for every row after.
Written as a formula, with \(d_i\) returns and \(n_i\) patients at risk at each return time \(t_i\):
\[ S(t) = \prod_{t_i \le t} \left(1 - \frac{d_i}{n_i}\right) \]
In this trial \(d_i\) is always 1, since no two patients return in exactly the same month.
Here is that running product, computed by hand down all 15 rows of the standard arm.
Read the first two rows. At month 1.5, all 15 patients are still at risk and 1 of them returns, so the product becomes \(1 \times (1 - 1/15) = 0.933\). At month 2.2, 14 remain at risk and 1 returns, so the product drops again to \(0.933 \times (1 - 1/14) = 0.867\).
Now look at month 4.6. That row is censored, so the survival column stays at 0.733, exactly what it was after month 4.0. But the at-risk count still falls, from 12 at month 4.0 to 11 at month 4.6, and that lower count of 11 is what the next row, month 5.2, actually divides by. A censored patient never pulls survival down. They only shrink the at-risk count for every row that comes after them.
By month 18.0 only 1 patient is left at risk, and that patient returns, so survival finishes at exactly 0.
The survival package's survfit() function builds this exact same running product with one function call. Confirm your hand table against it.
That is 12 numbers, one for each of the 12 return times. Match them against the survival column from your hand table, skipping the 3 censored rows, and they line up exactly. survfit() only ever reports survival at a return time, since a censored time never changes it.