Skip to main content

21  Survival curves and the log-rank test

Many outcomes in medicine are times: the time to death, to relapse or to discharge. A study ends before every patient has the event, so some times are incomplete. Kaplan and Meier gave an estimate of the survival curve for such data (Journal of the American Statistical Association, 1958). The estimate also uses the patients whose follow-up stopped early. The log-rank test then compares two curves. Chapter 1 tells the story of both methods.

21.1 The problem

Patients with acute myelogenous leukaemia (AML) get a course of chemotherapy. The question at the time was whether to extend the course with more cycles, called maintenance chemotherapy. The aml data in the survival package come from Miller’s book (Survival Analysis, Wiley, 1997). The times are in weeks.

aml
   time status             x
1     9      1    Maintained
2    13      1    Maintained
3    13      0    Maintained
4    18      1    Maintained
5    23      1    Maintained
6    28      0    Maintained
7    31      1    Maintained
8    34      1    Maintained
9    45      0    Maintained
10   48      1    Maintained
11  161      0    Maintained
12    5      1 Nonmaintained
13    5      1 Nonmaintained
14    8      1 Nonmaintained
15    8      1 Nonmaintained
16   12      1 Nonmaintained
17   16      0 Nonmaintained
18   23      1 Nonmaintained
19   27      1 Nonmaintained
20   30      1 Nonmaintained
21   33      1 Nonmaintained
22   43      1 Nonmaintained
23   45      1 Nonmaintained

There are 23 patients: 11 with maintenance and 12 without. status = 1 marks an event at time.

status = 0 marks a patient whose follow-up stopped at time without an event. This is a censored time. We know only that the true time is longer. Of the 23 times, 5 are censored.

Show code
aml |>
  arrange(x, time) |>
  mutate(patient = row_number(), status = factor(status, 0:1, c("censored", "event"))) |>
  ggplot(aes(y = patient, colour = x)) +
  geom_segment(aes(x = 0, xend = time, yend = patient)) +
  geom_point(aes(x = time, shape = status), size = 2.5, fill = "white") +
  scale_shape_manual(values = c(censored = 21, event = 16)) +
  scale_colour_manual(values = km_colours) +
  coord_cartesian(xlim = c(0, 60)) +
  labs(x = "weeks", y = "patient", colour = NULL, shape = NULL)
Figure 21.1: The follow-up of each patient, by group. A filled point is an event, and an open point is a censored time. The longest follow-up goes past the right edge of the plot.

Does maintenance chemotherapy lengthen the time to the event?

21.1.1 Simple summaries fail

Three simple summaries are possible. Each one is wrong for censored data.

naive <- aml |>
  group_by(x) |>
  summarise(mean_all = mean(time), mean_events = mean(time[status == 1]),
            proportion_event = mean(status))
naive
# A tibble: 2 × 4
  x             mean_all mean_events proportion_event
  <fct>            <dbl>       <dbl>            <dbl>
1 Maintained        38.5        25.1            0.636
2 Nonmaintained     21.2        21.7            0.917
  • mean_all: the mean of all times. It treats a censored time as an event time. A censored time is only a lower limit, so this mean is too short.
  • mean_events: the mean of the event times only. It removes the patients who did well. The patient with 161 weeks of follow-up drops out.
  • proportion_event: the proportion with an event. It ignores when the events happened, and the patients had different lengths of follow-up.

21.2 Intuitive solution

21.2.1 Survive one step at a time

To be event-free at week 20, a patient must pass each earlier event time without an event. At each event time, we ask: of the patients still at risk, what proportion passed it? The probability that a patient is event-free at week 20 is the product of these proportions.

A censored patient counts in the group at risk until the time of censoring. After that time, the patient leaves the group without an event. In this way, each patient gives information for as long as we follow the patient.

km_by_hand <- function(time, status) {
  tibble(time, status) |>
    group_by(time) |>
    summarise(d = sum(status), censored = sum(1 - status)) |>   # events and censored at each time
    mutate(n = rev(cumsum(rev(d + censored))),                 # at risk just before each time
           pass = 1 - d / n,
           surv = cumprod(pass))
}
maintained <- aml |> filter(x == "Maintained")
km_m <- km_by_hand(maintained$time, maintained$status)
km_m
# A tibble: 10 × 6
    time     d censored     n  pass  surv
   <dbl> <dbl>    <dbl> <dbl> <dbl> <dbl>
 1     9     1        0    11 0.909 0.909
 2    13     1        1    10 0.9   0.818
 3    18     1        0     8 0.875 0.716
 4    23     1        0     7 0.857 0.614
 5    28     0        1     6 1     0.614
 6    31     1        0     5 0.8   0.491
 7    34     1        0     4 0.75  0.368
 8    45     0        1     3 1     0.368
 9    48     1        0     2 0.5   0.184
10   161     0        1     1 1     0.184

At week 9, 11 patients are at risk, and 1 has the event. The proportion that passes week 9 is 10/11. At week 13, 10 patients are at risk. One has the event and one is censored. At week 18, only 8 patients are at risk, because the censored patient left.

The column surv multiplies the proportions in pass. Rows with no event have pass = 1, so a censored time does not lower the curve.

Show code
nonmaintained <- aml |> filter(x == "Nonmaintained")
km_both <- bind_rows(
  km_m |> mutate(group = "Maintained"),
  km_by_hand(nonmaintained$time, nonmaintained$status) |> mutate(group = "Nonmaintained")
)
curves <- km_both |>
  group_by(group) |>
  group_modify(\(df, key) bind_rows(tibble(time = 0, surv = 1, censored = 0), df)) |>
  ungroup()
ggplot(curves, aes(time, surv, colour = group)) +
  geom_step(linewidth = 1) +
  geom_point(data = filter(curves, censored > 0), shape = 3, size = 3) +
  scale_colour_manual(values = km_colours) +
  coord_cartesian(xlim = c(0, 60)) +
  labs(x = "weeks", y = "proportion event-free", colour = NULL)
Figure 21.2: The probability that a patient is event-free, from the step-by-step product, for each group. The ticks mark censored times.

The curve of the maintained group stays above the other curve at most times. The curve of the maintained group falls to 0.5 or below at week 31. With 23 patients, we must ask if chance alone can give a gap of this size.

21.3 Formal solution

21.3.1 The survival function and censoring

Let \(T\) be the time to the event. The survival function is \(S(t) = \Pr(T > t)\), the probability that a patient is event-free after time \(t\). It starts at 1 and falls with time.

The methods of this chapter need one assumption about censoring. A patient censored at time \(t\) must have the same future risk as the patients that we still follow at \(t\). This is non-informative censoring. Suppose that the sickest patients leave the study because they are too ill to attend. Then their censoring carries information about their risk, and the estimate is too optimistic.

21.3.2 The Kaplan–Meier estimate

Let \(t_1 < t_2 < \dots\) be the distinct event times. At \(t_j\), \(n_j\) patients are at risk and \(d_j\) have the event. The Kaplan–Meier estimate is the product of the intuitive solution:

\[ \hat S(t) = \prod_{t_j \le t} \left(1 - \frac{d_j}{n_j}\right). \]

Its standard error (SE) comes from Greenwood’s formula:

\[ \widehat{\operatorname{Var}}\big(\hat S(t)\big) = \hat S(t)^2 \sum_{t_j \le t} \frac{d_j}{n_j (n_j - d_j)}. \]

The section on the theory of the estimate below derives both formulae. In R, Surv(time, status) combines the time and the event indicator, and survfit() makes the estimate.

km <- survfit(Surv(time, status) ~ x, data = aml)
summary(km)
Call: survfit(formula = Surv(time, status) ~ x, data = aml)

                x=Maintained 
 time n.risk n.event survival std.err lower 95% CI upper 95% CI
    9     11       1    0.909  0.0867       0.7541        1.000
   13     10       1    0.818  0.1163       0.6192        1.000
   18      8       1    0.716  0.1397       0.4884        1.000
   23      7       1    0.614  0.1526       0.3769        0.999
   31      5       1    0.491  0.1642       0.2549        0.946
   34      4       1    0.368  0.1627       0.1549        0.875
   48      2       1    0.184  0.1535       0.0359        0.944

                x=Nonmaintained 
 time n.risk n.event survival std.err lower 95% CI upper 95% CI
    5     12       2   0.8333  0.1076       0.6470        1.000
    8     10       2   0.6667  0.1361       0.4468        0.995
   12      8       1   0.5833  0.1423       0.3616        0.941
   23      6       1   0.4861  0.1481       0.2675        0.883
   27      5       1   0.3889  0.1470       0.1854        0.816
   30      4       1   0.2917  0.1387       0.1148        0.741
   33      3       1   0.1944  0.1219       0.0569        0.664
   43      2       1   0.0972  0.0919       0.0153        0.620
   45      1       1   0.0000     NaN           NA           NA
  • time: the event times. R lists only the times with an event.
  • n.risk: \(n_j\), the patients at risk just before time.
  • n.event: \(d_j\), the events at time.
  • survival: \(\hat S(t)\). These values agree with the column surv of our hand calculation.
  • std.err: the Greenwood SE of \(\hat S(t)\). At week 9 it is \(0.909 \sqrt{1 / (11 \times 10)} =\) 0.0867.
  • lower 95% CI, upper 95% CI: a 95% confidence interval. R calculates it on the scale of \(\log \hat S\) and transforms back, so the interval stays below 1.
km
Call: survfit(formula = Surv(time, status) ~ x, data = aml)

                 n events median 0.95LCL 0.95UCL
x=Maintained    11      7     31      18      NA
x=Nonmaintained 12     11     23       8      NA

The median survival time is the first time at which \(\hat S(t)\) falls to 0.5 or below. It is 31 weeks with maintenance and 23 weeks without. The upper confidence limits are NA. Each upper confidence limit of the curve stays above 0.5 to the end of follow-up. So the data cannot fix an upper limit for the median.

21.3.3 The log-rank test

\(H_0\): the two groups have the same survival function.
\(H_1\): the survival functions differ.

At each event time \(t_j\), the patients at risk form a 2 × 2 table: group against event or no event. Under \(H_0\), the \(d_j\) events fall at random among the \(n_j\) patients at risk. Suppose that \(n_{1j}\) of them are in group 1. With the margins fixed, the number of events \(d_{1j}\) in group 1 is hypergeometric, as in Fisher’s exact test (Chapter 9). Its expected value and variance are

\[ E_{1j} = d_j \frac{n_{1j}}{n_j}, \qquad V_{1j} = d_j \frac{n_{1j}}{n_j} \left(1 - \frac{n_{1j}}{n_j}\right) \frac{n_j - d_j}{n_j - 1}. \]

The log-rank statistic adds the observed minus the expected events over all event times:

\[ X^2 = \frac{\left(\sum_j (d_{1j} - E_{1j})\right)^2}{\sum_j V_{1j}}. \]

Under \(H_0\), \(X^2\) follows a chi-square distribution with 1 degree of freedom (df). We calculate it by hand.

event_times <- sort(unique(aml$time[aml$status == 1]))
in_1 <- aml$x == "Maintained"
lr <- map_dfr(event_times, \(t) {
  at_risk <- aml$time >= t
  died <- aml$time == t & aml$status == 1
  tibble(time = t, n = sum(at_risk), n1 = sum(at_risk & in_1), d = sum(died), d1 = sum(died & in_1))
}) |>
  mutate(E1 = d * n1 / n,
         V1 = ifelse(n > 1, d * (n1 / n) * (1 - n1 / n) * (n - d) / (n - 1), 0))
lr
# A tibble: 15 × 7
    time     n    n1     d    d1    E1    V1
   <dbl> <int> <int> <int> <int> <dbl> <dbl>
 1     5    23    11     2     0 0.957 0.476
 2     8    21    11     2     0 1.05  0.474
 3     9    19    11     1     1 0.579 0.244
 4    12    18    10     1     0 0.556 0.247
 5    13    17    10     1     1 0.588 0.242
 6    18    14     8     1     1 0.571 0.245
 7    23    13     7     2     1 1.08  0.456
 8    27    11     6     1     0 0.545 0.248
 9    30     9     5     1     0 0.556 0.247
10    31     8     5     1     1 0.625 0.234
11    33     7     4     1     0 0.571 0.245
12    34     6     4     1     1 0.667 0.222
13    43     5     3     1     0 0.6   0.24 
14    45     4     3     1     0 0.75  0.188
15    48     2     2     1     1 1     0    
X2_hand <- sum(lr$d1 - lr$E1)^2 / sum(lr$V1)
c(O1 = sum(lr$d1), E1 = sum(lr$E1), V1 = sum(lr$V1), X2 = X2_hand)
       O1        E1        V1        X2 
 7.000000 10.689336  4.007551  3.396389 

The maintained group had 7 events. Under \(H_0\) it is expected to have 10.69. survdiff() does the same calculation.

lrt <- survdiff(Surv(time, status) ~ x, data = aml)
lrt
Call:
survdiff(formula = Surv(time, status) ~ x, data = aml)

                 N Observed Expected (O-E)^2/E (O-E)^2/V
x=Maintained    11        7    10.69      1.27       3.4
x=Nonmaintained 12       11     7.31      1.86       3.4

 Chisq= 3.4  on 1 degrees of freedom, p= 0.07 
  • N: the patients in each group.
  • Observed: the events in each group, \(\sum_j d_{1j}\).
  • Expected: the events expected under \(H_0\), \(\sum_j E_{1j}\). The two rows add to the total number of events.
  • (O-E)^2/E: a Pearson-type contribution of each group, as in Chapter 8. R prints it for reference.
  • (O-E)^2/V: the log-rank statistic. It uses the hypergeometric variance \(\sum_j V_{1j}\).
  • Chisq: 3.4 on 1 df, the same as our \(X^2 =\) 3.4.
  • p: 0.065.

The maintained group had fewer events than expected, but the p-value is 0.065. With 23 patients, the data give only weak evidence that maintenance lengthens the time to the event.

The log-rank test gives the same weight to every event time. It has the most power when the ratio of the event rates in the two groups is constant over time. If the curves cross, the early and late differences cancel, and the test can miss a real difference.

21.4 The hazard

The hazard \(h(t)\) is the event rate at time \(t\) among the patients who are still event-free. At an event time, the estimate of the hazard is \(d_j / n_j\). The cumulative hazard adds these steps:

\[ \hat H(t) = \sum_{t_j \le t} \frac{d_j}{n_j}. \]

This is the Nelson–Aalen estimate. For a continuous time, \(S(t) = \exp(-H(t))\), so \(-\log \hat S(t)\) is a second estimate of \(H(t)\).

Show code
haz <- km_both |>
  filter(d > 0) |>
  group_by(group) |>
  mutate(nelson_aalen = cumsum(d / n), minus_log_km = -log(surv)) |>
  ungroup() |>
  filter(is.finite(minus_log_km))
ggplot(haz, aes(time, colour = group)) +
  geom_step(aes(y = nelson_aalen), linewidth = 1) +
  geom_step(aes(y = minus_log_km), linewidth = 1, linetype = "dashed") +
  scale_colour_manual(values = km_colours) +
  labs(x = "weeks", y = "cumulative hazard", colour = NULL)
Figure 21.3: The cumulative hazard of each group: the Nelson–Aalen estimate (solid) and minus the log of the Kaplan–Meier estimate (dashed). The two estimates are close until few patients remain at risk.

The two estimates differ most when \(d_j / n_j\) is large, at the end of each curve. The last event of the non-maintained group brings \(\hat S\) to 0, so \(-\log \hat S\) is infinite there, and the plot leaves it out. The hazard is the scale of the Cox model in Chapter 22.

21.5 Competing risks

Some patients can have a different event that prevents the event of interest. A patient who dies of a heart attack cannot die of cancer later. This is a competing risk, and it is not censoring. A censored patient can still have the event after the end of follow-up. A patient who died of another cause cannot.

We simulate patients with two causes of death and a random end of follow-up.

set.seed(1958)
n_sim <- 2000
rate_cancer <- 0.10; rate_other <- 0.15                          # per year
t_cancer <- rexp(n_sim, rate_cancer)
t_other  <- rexp(n_sim, rate_other)
t_cens   <- runif(n_sim, 0, 10)
time  <- pmin(t_cancer, t_other, t_cens)
cause <- case_when(time == t_cancer ~ "cancer", time == t_other ~ "other", .default = "censored")
km_wrong <- survfit(Surv(time, cause == "cancer") ~ 1)            # other deaths treated as censored
aj <- survfit(Surv(time, factor(cause, c("censored", "cancer", "other"))) ~ 1)

There are 2,000 patients. The rate of death from cancer is 0.10 per year, and the rate of death from other causes is 0.15 per year. Follow-up stops at a random time up to 10 years.

1 - km_wrong treats a death from another cause as censored. It estimates the probability of death from cancer in a world where nobody dies of other causes: \(1 - e^{-0.10 t}\). The factor status gives the Aalen–Johansen estimate of the cumulative incidence. This is the probability of death from cancer by time \(t\) in the real world, where other deaths occur. For two constant rates \(\lambda_1\) and \(\lambda_2\), the true cumulative incidence is

\[ F_1(t) = \frac{\lambda_1}{\lambda_1 + \lambda_2}\left(1 - e^{-(\lambda_1 + \lambda_2) t}\right). \]

Show code
grid <- seq(0, 10, by = 0.1)
tibble(t = grid,
       one_minus_km = 1 - summary(km_wrong, times = grid, extend = TRUE)$surv,
       aalen_johansen = summary(aj, times = grid, extend = TRUE)$pstate[, "cancer"],
       truth = F1(grid)) |>
  ggplot(aes(t)) +
  geom_line(aes(y = truth), colour = "grey50", linetype = "dashed", linewidth = 1) +
  geom_step(aes(y = one_minus_km), colour = "firebrick", linewidth = 1) +
  geom_step(aes(y = aalen_johansen), colour = "steelblue", linewidth = 1) +
  labs(x = "years", y = "probability of death from cancer")
Figure 21.4: The estimated probability of death from cancer in the simulation: one minus the Kaplan–Meier estimate with other deaths censored (red), and the Aalen–Johansen cumulative incidence (blue). The dashed grey curve is the true cumulative incidence.

At 5 years, one minus Kaplan–Meier gives 0.375, close to \(1 - e^{-0.10 \times 5} =\) 0.393. The Aalen–Johansen estimate gives 0.272, and the true value is 0.285. One minus Kaplan–Meier overestimates the risk, because it counts deaths from cancer in patients who died of other causes first. With competing risks, report the cumulative incidence.

21.6 The theory of the Kaplan–Meier estimate

21.6.1 A maximum likelihood estimate

Suppose that the hazard is a probability \(h_j\) of an event at each event time \(t_j\), and 0 at other times. A patient at risk at \(t_j\) has the event with the probability \(h_j\), or passes with the probability \(1 - h_j\). A censored patient passes every event time up to the censoring time. Censoring is non-informative, so it adds no factor that depends on the \(h_j\). The likelihood is

\[ L(h_1, h_2, \dots) = \prod_j h_j^{d_j} (1 - h_j)^{n_j - d_j}. \]

Each \(h_j\) appears in one factor only, so we maximise each factor alone. The derivative of the log of factor \(j\) is

\[ \frac{d_j}{h_j} - \frac{n_j - d_j}{1 - h_j} = 0 \quad \Rightarrow \quad \hat h_j = \frac{d_j}{n_j}. \]

The survival function is \(S(t) = \prod_{t_j \le t} (1 - h_j)\). The maximum likelihood estimate of a function of the parameters is the same function of their estimates. So \(\hat S(t) = \prod_{t_j \le t} (1 - d_j / n_j)\), the Kaplan–Meier estimate. A hazard above 0 between the event times only lowers the likelihood, so the estimate puts all its steps at the observed event times.

21.6.2 Greenwood’s formula

Given \(n_j\), the count \(d_j\) is binomial with \(n_j\) trials and probability \(h_j\). So \(\operatorname{Var}(\hat h_j) = h_j (1 - h_j)/n_j\). Take the log of the estimate:

\[ \log \hat S(t) = \sum_{t_j \le t} \log(1 - \hat h_j). \]

The delta method says that \(\operatorname{Var}(g(\hat h)) \approx g'(h)^2 \operatorname{Var}(\hat h)\). With \(g(h) = \log(1 - h)\), \(g'(h) = -1/(1 - h)\), and

\[ \operatorname{Var}\big(\log(1 - \hat h_j)\big) \approx \frac{h_j}{n_j (1 - h_j)} \approx \frac{d_j}{n_j (n_j - d_j)}, \]

when we put \(\hat h_j = d_j / n_j\) in place of \(h_j\). The error \(\hat h_j - h_j\) has mean 0 given the earlier event times. Errors of this kind are uncorrelated, so the variances of the terms add. A last use of the delta method, with \(g(x) = e^x\), gives \(\operatorname{Var}(\hat S) \approx \hat S^2 \operatorname{Var}(\log \hat S)\). This is Greenwood’s formula.

21.6.3 A simulation of Greenwood’s formula

We test the formula by simulation. Each data set has 50 patients with a constant hazard of 0.1 per week, so the true \(S(10) = e^{-1}\). Censoring times are uniform from 0 to 30 weeks. We repeat the study many times and compare the spread of \(\hat S(10)\) with the mean Greenwood SE.

set.seed(1926)
n_rep <- 2000
sims <- map_dfr(1:n_rep, \(i) {
  event <- rexp(50, 0.1)
  cens  <- runif(50, 0, 30)
  fit <- survfit(Surv(pmin(event, cens), as.numeric(event <= cens)) ~ 1)
  s <- summary(fit, times = 10)
  tibble(surv = s$surv, se = s$std.err, lower = s$lower, upper = s$upper)
})
cov95 <- mean(sims$lower <= exp(-1) & exp(-1) <= sims$upper)
c(true_S = exp(-1), mean_estimate = mean(sims$surv), sd_estimate = sd(sims$surv),
  mean_greenwood_se = mean(sims$se), coverage = cov95)
           true_S     mean_estimate       sd_estimate mean_greenwood_se 
       0.36787944        0.37014975        0.07549826        0.07577240 
         coverage 
       0.94750000 

Over the 2,000 studies, the mean estimate is 0.370, close to the true 0.368. The standard deviation (SD) of the estimates is 0.0755, and the mean Greenwood SE is 0.0758. The 95% confidence interval contains the true value in 94.8% of the studies.

21.7 Summary

  • The Kaplan–Meier estimate multiplies, over the event times, the proportion of patients at risk who pass each time. Censored patients count as at risk until they leave.
  • Non-informative censoring is the main assumption: a censored patient must have the same future risk as the patients still in follow-up.
  • The log-rank test adds the observed minus the expected events in one group over all event times. It has a chi-square distribution with 1 df.
  • Competing events are not censoring. With competing risks, one minus Kaplan–Meier overestimates the risk, and the cumulative incidence is the correct estimate.