Guides  /  survival analysis

Survival analysis and Kaplan-Meier

Survival analysis is for questions where the answer is not whether something happened but how long it took, and where some people have not had it happen yet. That last part, censoring, is what makes ordinary methods fail. This guide covers censoring, the Kaplan-Meier estimator, how to read a curve properly, the log-rank test, and the errors that invalidate the whole analysis.

Sidra Khan Written and reviewed by Sidra Khan, Lead Biostatistician
Updated 26 October 202611 min read
What is survival analysis?

A family of methods for time-to-event data, where the outcome is how long until something happens and some people reach the end of the study without it happening. Those incomplete observations are censored: you know the event had not occurred by a certain point, and nothing more. Ordinary methods cannot use that partial information, which is why survival analysis exists. Despite the name it applies to any event, including discharge, relapse, equipment failure or a customer leaving.

The core idea

Censoring, and why it breaks other methods

Right censoring is the usual kind: the study ends, the person withdraws, or they die of something unrelated, and all you know is that the event had not happened by then.

Two obvious alternatives both fail. Averaging time to event means either dropping censored people, which discards the longest survivors and biases the answer downwards, or pretending their last known time was the event time, which is simply false. Treating it as a yes or no outcome throws away the timing entirely, so a group whose events all occur in month one looks identical to one whose events occur in year five.

The assumption censoring rests on

Survival methods assume censoring is non-informative: that people censored at a given time are no more or less likely to have the event than those still being followed. If patients leave the study because they are deteriorating, that assumption fails and the analysis overstates survival. It is the same logic as data missing not at random, and it is just as hard to fix after the fact.

The estimator

The Kaplan-Meier estimator

Kaplan-Meier builds the survival curve as a running product. At each time an event occurs, it computes the proportion of those still at risk who got through, and multiplies it into the running total. Censored people stay in the denominator until the moment they leave, then quietly drop out.

That is why it is also called the product-limit estimator, and why the curve is a series of steps rather than a smooth line: it only changes when an event happens. It assumes nothing about the shape of the underlying distribution, which is its main strength.

✓The curve starts at 1 and only ever falls.
✓Each drop is an event. Its size depends on how many were still at risk, so late drops are large.
✓Tick marks are censored observations, not events.
✓The right-hand tail rests on very few people and is correspondingly unreliable.

Worked example one

A Kaplan-Meier curve calculated by hand

Ten patients are followed. Six have the event; four are censored. Times are in months.

2† 3† 4+ 6† 8† 9+ 11† 14+ 16† 20+
† = event  ยท  + = censored

Step 1. Work down the list, one event at a time

At each event, divide the number of events by the number still at risk, subtract from one, and multiply into the running total. Censored observations change nothing except the number at risk for the next row.

TimeAt riskEvents1 − d/nS(t)
21011 − 1/10 = 0.90000.900
3911 − 1/9 = 0.88890.800
48—censored0.800
6711 − 1/7 = 0.85710.686
8611 − 1/6 = 0.83330.571
95—censored0.571
11411 − 1/4 = 0.75000.429
143—censored0.429
16211 − 1/2 = 0.50000.214
201—censored0.214

Step 2. Notice what the censored rows did

They did not reduce survival. They reduced the denominator for every row below. That is the whole mechanism by which Kaplan-Meier uses partial information: a person censored at month 4 contributed four months of evidence that the event had not yet happened, and then stopped contributing.

Step 3. Notice what happens to the step sizes

The first event drops the curve by 10%. The last drops it by half, from 0.429 to 0.214, because only two people remained at risk. Nothing deteriorated at month 16. The denominator simply ran out, which is why the right-hand end of any Kaplan-Meier curve must be read with the numbers at risk beside it.

Step 4. Median survival

The median is the first time the curve reaches or falls below 0.5. Survival is 0.571 at month 8 and 0.429 at month 11, so median survival is 11 months. It is not interpolated, and it is not the mean of the observed times.

Worked example two

The log-rank test, calculated by hand

Two groups of six. The test compares how many events each group actually had with how many it would have had if survival were identical.

Group A: 2† 6† 8† 14+ 16† 20+
Group B: 3† 4+ 9+ 11† 18+ 22+

Step 1. At each event time, work out the expected count

If the groups were really the same, events should be shared out in proportion to how many of each group are still at risk. So the expected number in group A is the total events at that time multiplied by A's share of those at risk.

TimeAt risk AAt risk BEvents AEvents BExpected AVariance
266101 × 6/12 = 0.5000.250
356011 × 5/11 = 0.4550.248
654101 × 5/9 = 0.5560.247
844101 × 4/8 = 0.5000.250
1133011 × 3/6 = 0.5000.250
1622101 × 2/4 = 0.5000.250
Total423.0101.495

Only event times appear. Censoring times affect the calculation solely by removing people from the at-risk columns further down.

Step 2. Compare observed with expected

Group A had 4 events where 3.010 were expected. Square the difference and divide by the summed variance.

χ² = (4 − 3.010)² / 1.495 = 0.980 / 1.495 = 0.656 on 1 degree of freedom

p = 0.42

Step 3. Say what that does and does not mean

There is no evidence of a difference in survival between these groups. With six patients per arm and six events in total, there was never any realistic prospect of detecting one. A non-significant log-rank test in a small study is a statement about the study, not about the treatment, and reporting it as "no difference in survival" overstates what has been shown.

The test gives no effect size

Nothing above produces a number describing how much better or worse one group did. Report median survival in each group, survival at clinically meaningful time points, and a hazard ratio with its interval alongside the p-value.

Interpretation

Reading a Kaplan-Meier curve

Most of the information people take from these plots is in the part they do not look at.

FeatureWhat it tells you
Numbers at riskPrinted beneath the axis. The single most important element. A separation that opens where only four people remain is noise.
Step sizeLarge late steps mean few people at risk, not a sudden worsening.
Confidence bandsWiden as the sample thins. Without them the tail looks far more certain than it is.
Curves crossingA warning that one group does worse early and better later. A single hazard ratio will not describe it.
A flat tailOften just the last person being censored, not a plateau of cured patients.

If a published curve has no numbers at risk underneath it, treat the right-hand end as decorative.

Comparing groups

The log-rank test

The log-rank test asks whether two or more survival curves differ. At each event time it compares observed events in each group with what would be expected if survival were identical, then accumulates those differences across the whole of follow-up.

Because it weights every event time equally, it is most powerful when one group is consistently worse throughout, which is the proportional hazards situation. It loses power badly when curves cross, and it can return a comfortable p-value for two curves that are dramatically different in opposite directions at different times.

A p-value is not an effect size

The log-rank test tells you the curves differ. It does not tell you by how much. Report it alongside median survival in each group, survival at clinically meaningful time points, and a hazard ratio with its confidence interval.

Summarising

Median survival, not mean

Median survival is the time at which the curve crosses 0.5, and it is preferred for a practical reason: you can calculate it as soon as half the group has had the event, whereas the mean needs everyone to have had it. In studies where many people are still event-free at the end, the mean is not estimable at all without assumptions.

If the curve never drops below 0.5, median survival is "not reached", which is a legitimate and informative result rather than a missing one. Report survival at fixed time points instead, such as one and five years, with intervals.

A common error

Competing risks, and when Kaplan-Meier is wrong

A competing risk is an event that stops the event of interest from ever happening. If you are studying relapse, a patient who dies of an unrelated cause cannot relapse afterwards. Treating that death as ordinary censoring assumes they could have, and that assumption inflates your answer.

A worked comparison

One hundred patients, followed three years, studying relapse. Deaths from other causes are the competing event.

YearAt risk at startRelapsesDeaths, other causes
11001030
2601020
330612

The naive calculation

Treat the other-cause deaths as censored and run Kaplan-Meier on relapse.

yr 1: S = 1 − 10/100 = 0.900
yr 2: S = 0.900 × (1 − 10/60) = 0.750
yr 3: S = 0.750 × (1 − 6/30) = 0.600

One minus 0.600 gives a three-year relapse incidence of 40.0%.

The cumulative incidence function

The correct approach weights each year's relapse rate by the proportion still alive and relapse-free at the start of it, so people removed by the competing event cannot contribute relapses they were never going to have.

yr 1: 1.000 × 10/100 = 0.100
yr 2: 0.600 × 10/60 = 0.100, running total 0.200
yr 3: 0.300 × 6/30 = 0.060, running total 0.260

Three-year relapse incidence is 26.0%.

A 14 percentage point overstatement

40.0% against 26.0%, a factor of 1.54, from the same data. The naive figure is not a conservative approximation, it is an answer to a different question: what relapse incidence would be in a world where nobody died of anything else. That world does not exist, and no clinician plans for it.

When it matters, and what to use

✓It matters whenever the competing event is common relative to the event of interest. In older populations and long follow-up, that is most of the time.
✓For describing risk, report the cumulative incidence function rather than one minus Kaplan-Meier.
✓For modelling, a cause-specific Cox model and a Fine-Gray subdistribution model answer different questions. Cause-specific suits aetiology, Fine-Gray suits prediction.
✓In R this is cmprsk::cuminc() or survival::survfit() with a multi-state outcome; in Stata, stcompet and stcrreg.
✓Say explicitly in the methods which you used. A paper that reports relapse incidence without mentioning competing risks has usually not considered them.

Avoid these

Four errors that invalidate it

ErrorWhat goes wrong
Informative censoringPeople leave because of how they are doing. Survival is overstated and nothing in the output warns you.
Immortal time biasGroup membership is defined by something that could only happen to people who survived long enough to do it. The group then cannot have events early, by construction.
Comparing at one time pointPicking the moment where the gap looks largest. The log-rank test exists precisely to use all the follow-up.
Ignoring competing risksTreating death from another cause as ordinary censoring overstates the risk of the event of interest. Use a cumulative incidence function instead.

Immortal time bias is the subtlest and the most common in routine-data studies. If being in a group requires receiving a treatment at some point, everyone in it necessarily survived until that point, and they will appear to do better than a group with no such requirement.

Doing it

In R, SPSS and Stata

R

The survival package does the estimation and survminer produces the plot with numbers at risk.

library(survival); library(survminer)
fit <- survfit(Surv(time, status) ~ group, data = df)
summary(fit)
survdiff(Surv(time, status) ~ group, data = df)

summary(fit) prints the survival table exactly as calculated above, and survdiff() gives the log-rank test with observed and expected counts. For the plot, ggsurvplot(fit, risk.table = TRUE, conf.int = TRUE) adds the numbers at risk and confidence bands, both of which should be there every time.

SPSS

Kaplan-Meier is under Analyze, Survival, Kaplan-Meier.

KM time BY group
  /STATUS = status(1)
  /PRINT = TABLE MEAN
  /PLOT SURVIVAL
  /TEST = LOGRANK
  /COMPARE = OVERALL POOLED.

SPSS reports both mean and median survival. Treat the mean with suspicion: it is restricted to the longest observed time and will be misleading whenever substantial censoring remains at the end. SPSS does not add a numbers-at-risk table to the plot, so include one separately.

Stata

Declare the structure once with stset, then everything else is short.

stset time, failure(status == 1)
sts list, by(group)
sts graph, by(group) risktable ci
sts test group, logrank

sts list prints the product-limit table, risktable adds the numbers at risk beneath the plot, and sts test gives the log-rank with observed and expected events per group. For median survival with its interval, use stci, by(group).

Define time zero before you touch the software

Every command here needs a start point, and the choice is yours: date of diagnosis, date of randomisation, date of surgery. They give different answers and only one of them matches your research question. Deciding it after seeing the curves is how immortal time bias gets in.

Writing it up

How to report it

✓Define time zero and the event precisely. Both are choices, and both change the answer.
✓State how many were censored and why, and argue that censoring was non-informative.
✓Show the Kaplan-Meier plot with numbers at risk beneath it. Always.
✓Give median survival with a confidence interval, or say it was not reached.
✓Give the log-rank p-value and an effect size, not one without the other.
✓Report median follow-up as well as median survival. They are different numbers and both matter.

Common questions

Frequently asked questions

What is survival analysis used for?

Any question about time until an event where some observations are incomplete. Despite the name the event need not be death: it can be relapse, discharge, equipment failure, readmission or a customer leaving. What matters is that you are measuring duration and that some people have not had the event by the end of follow-up.

What is censoring?

An observation where you know the event had not happened by a certain time but not when, or whether, it eventually did. It usually arises because the study ended, the person withdrew, or they were lost to follow-up. Survival methods use that partial information rather than discarding it.

How do you read a Kaplan-Meier curve?

Each downward step is an event, tick marks are censored observations, and the numbers at risk beneath the axis tell you how much data supports each part of the curve. Read the left-hand side with confidence and the right-hand tail with caution, because it usually rests on very few people.

What does the log-rank test tell you?

Whether survival curves differ, using all event times across the whole of follow-up. It does not give an effect size, and it loses power when curves cross, because early and late differences in opposite directions cancel out. Pair it with median survival and a hazard ratio.

Why report median rather than mean survival?

Because the median can be estimated once half the group has had the event, while the mean requires everyone to have had it. With substantial censoring the mean is not estimable without assumptions about what happens beyond the data.

What is immortal time bias?

A bias arising when membership of a group requires surviving long enough to meet some condition, such as receiving a treatment. Those people cannot have had the event before that point, so the group appears to do better for reasons that are structural rather than real. It is common in studies built on routinely collected data.