Tutorials › Joint Modeling of Longitudinal and Time-to-Event Data › Comparing Survival Curves With the Log-Rank Test

Survival Models Review · Tutorial 102 of 697

Comparing Survival Curves With the Log-Rank Test

Learn to calculate and interpret log-rank tests for two or more survival groups, including how risk sets and tied events determine the expected counts and variance.

Intermediate 12 min read

What You'll Learn

  • Define observed and expected event counts at each event time using group-specific risk sets.
  • Calculate the log-rank variance, including the finite-population correction for tied events.
  • Form and interpret the one-degree-of-freedom test for two groups.
  • Build the covariance matrix and chi-square test for three or more groups.
  • Check the censoring, event-time, and sample-size conditions behind the test.
  • Distinguish a global log-rank comparison from survival-curve descriptions and weighted alternatives.

Why Compare Survival Curves?

Kaplan–Meier curves describe survival over time, but a visual difference between curves does not by itself quantify the evidence against equal survival distributions. The log-rank test provides a formal comparison by asking, at each event time, how many events occurred in each group and how many would be expected if the groups had the same event-time distribution. It combines those comparisons across event times while accounting for the changing risk sets.

The test is a global comparison: it summarizes evidence over the observed follow-up rather than testing whether the curves differ at one selected time. Its calculation uses the event and censoring information that determines the Kaplan–Meier estimator, but its purpose is inferential rather than descriptive.

Definition: At distinct event time \(t_j\), let \(n_{gj}\) be the number of individuals in group \(g\) at risk immediately before \(t_j\), and let \(d_{gj}\) be the number of events in that group at \(t_j\). Set \(n_j=\sum_g n_{gj}\) and \(d_j=\sum_g d_{gj}\). The observed event count in group \(g\) is \(O_g=\sum_j d_{gj}\). Under the null hypothesis of equal survival distributions across groups, its expected count is \(E_g=\sum_j d_j(n_{gj}/n_j)\). The log-rank test compares \(O_g\) with \(E_g\), accounting for their sampling variance.

Risk Sets, Expected Counts, and Variance

An individual is at risk immediately before a time if they have entered observation and have not yet experienced the event or been censored. Someone censored at an event time is included in the risk set for that time; their censoring removes them from later risk sets. With delayed entry, only individuals who have entered by that time belong in the risk set. The earlier material on event times, censoring, and risk sets establishes this bookkeeping.

At an event time, the expected number of events in group \(g\) under equal survival distributions is the total number of events multiplied by the group’s share of the risk set. This accounts for unequal numbers at risk: if one group has twice as many people at risk, it is expected to contribute twice as many events at that time under the null.

When multiple events occur at the same time, the usual log-rank calculation treats the \(d_j\) events as a tied set. Conditional on \(d_j\) events among \(n_j\) people at risk, the number assigned to a group follows a hypergeometric allocation under the null. For two groups, the variance contribution for group 1 is therefore:

$$ V_{1j} = d_j\frac{n_j-d_j}{n_j-1} \left(\frac{n_{1j}}{n_j}\right) \left(1-\frac{n_{1j}}{n_j}\right). $$

The factor \((n_j-d_j)/(n_j-1)\) is the finite-population correction for sampling without replacement. If there is one event, the expression reduces to the variance of a single event’s group assignment. If all at-risk individuals experience the event at that time, \(d_j=n_j\), the allocation is fixed and the variance is zero. Event times with zero variance contribute no information to the test.

Forming the Test Statistic

For two groups, let \(U=O_1-E_1\) and let \(V_1=\sum_j V_{1j}\). Since the total observed and expected counts agree across groups, \(O_2-E_2=-U\); thus one group’s difference contains all the information. Under the null hypothesis and with enough information for a large-sample approximation, the standardized difference is approximately standard normal, and its square is approximately chi-square with one degree of freedom.

$$ H_0:S_1(t)=S_2(t)\text{ for all }t, \qquad H_a:S_1(t)\ne S_2(t)\text{ for at least one }t, \qquad X^2_{\mathrm{LR}}=\frac{(O_1-E_1)^2}{V_1} \ \dot{\sim}\ \chi^2_1. $$

The statistic is nonnegative and tests for a difference in either direction. The sign of \(O_1-E_1\), considered separately, indicates whether group 1 had more or fewer events than expected under the null; more events than expected generally point toward poorer survival in that group. The p-value is the probability, assuming the null hypothesis and test assumptions, of obtaining a statistic at least as large as the observed one. It is not the probability that the null hypothesis is true.

For \(G\) groups, define \(U_g=O_g-E_g\). The vector \(U\) sums to zero, so only \(G-1\) components are independent. At each event time the covariance matrix has diagonal and off-diagonal entries:

$$ V_{gh,j} = d_j\frac{n_j-d_j}{n_j-1} \left[ \mathbf{1}(g=h)\frac{n_{gj}}{n_j} - \frac{n_{gj}}{n_j}\frac{n_{hj}}{n_j} \right], \qquad V=\sum_j V_j. $$

Here \(\mathbf{1}(g=h)\) is 1 when \(g=h\) and 0 otherwise. Because the components of \(U\) sum to zero, \(V\) is singular if all \(G\) components are retained. Use any \(G-1\) independent components and their corresponding covariance submatrix \(V^*\); the statistic \(U^{*T}(V^*)^{-1}U^*\) is approximately chi-square with \(G-1\) degrees of freedom when the large-sample approximation is adequate.

Why the Calculation Has This Form

The expected count follows from the null allocation of events among the risk set. Given \(d_j\) events, each of the \(n_j\) at-risk individuals has the same event probability in the tied-set calculation. A group containing \(n_{gj}\) of those individuals therefore has expected event count \(d_jn_{gj}/n_j\). This expectation adds across event times.

For two groups, let \(Y_j\) be the number of the \(d_j\) events assigned to group 1. Under the tied-set null, \(Y_j\) is hypergeometric with population size \(n_j\), \(n_{1j}\) group-1 individuals, and \(d_j\) event selections. Its mean is \(d_jn_{1j}/n_j\), and its variance is:

$$ \operatorname{Var}(Y_j) = d_j\left(\frac{n_{1j}}{n_j}\right) \left(1-\frac{n_{1j}}{n_j}\right) \frac{n_j-d_j}{n_j-1}. $$

When event times are distinct, the event-time contributions are accumulated across successive risk sets, giving \(U=O_1-E_1\) and \(V_1=\sum_j\operatorname{Var}(Y_j)\). Standardization by \(\sqrt{V_1}\) gives an approximately standard normal statistic under the null; squaring it gives the one-degree-of-freedom test. The same reasoning across groups yields the covariance matrix and the \(G-1\) degree-of-freedom test.

Worked Example: Two Groups With Tied Events

Suppose two hypothetical groups, A and B, each begin with 10 people. At event time \(t_1\), 4 events occur in A and none in B. At \(t_2\), 3 events occur in A and 1 in B. At \(t_3\), 2 occur in A and 1 in B. There is no censoring before these times. Calculate the log-rank statistic for comparing the groups.

At \(t_1\), the risk set is \(n_A=10,n_B=10\), so \(n=20\) and \(d=4\). The expected count for A is \(4(10/20)=2\). Its variance contribution is:

$$ V_{A1} = 4\frac{20-4}{20-1}(0.5)(0.5) = \frac{16}{19} \approx 0.8421. $$

After those events, 6 people in A and 10 in B remain at risk. At \(t_2\), \(n=16\), \(d=4\), and the group-A share of the risk set is \(6/16\). Thus:

$$ E_{A2}=4\left(\frac{6}{16}\right)=1.5, \qquad V_{A2} = 4\frac{16-4}{16-1}\left(\frac{6}{16}\right)\left(\frac{10}{16}\right) =0.75. $$

After \(t_2\), 3 people in A and 9 in B remain at risk. At \(t_3\), \(n=12\), \(d=3\), and:

$$ E_{A3}=3\left(\frac{3}{12}\right)=0.75, \qquad V_{A3} = 3\frac{12-3}{12-1}\left(\frac{3}{12}\right)\left(\frac{9}{12}\right) \approx 0.4602. $$

Group A had \(O_A=4+3+2=9\) observed events and \(E_A=2+1.5+0.75=4.25\) expected events. Its total variance is \(V_A=0.8421+0.75+0.4602=2.052\). Therefore:

$$ X^2_{\mathrm{LR}} = \frac{(9-4.25)^2}{2.052} \approx 10.99. $$

Against a chi-square distribution with 1 degree of freedom, this gives \(p\approx 0.0009\). Under the test assumptions, the data provide strong evidence against equal survival distributions. Group A had substantially more events than expected under the null, consistent with poorer survival in A over the observed follow-up. This comparison does not by itself estimate a hazard ratio or establish a causal effect.

Worked Example: A Log-Rank Test for Three Groups

At one hypothetical event time, suppose three groups each have 4 people at risk. Three tied events occur: 2 in group 1, 1 in group 2, and 0 in group 3. Calculate the multi-group statistic. Assume this event time is the only one contributing information.

Each group has one expected event, since \(d(n_g/n)=3(4/12)=1\). Thus \(O-E=(1,0,-1)\). The variance multiplier is \(d(n-d)/(n-1)=3(9/11)=27/11\), and each group’s risk-set share is \(1/3\). The covariance matrix is:

$$ V= \begin{pmatrix} 6/11 & -3/11 & -3/11\\ -3/11 & 6/11 & -3/11\\ -3/11 & -3/11 & 6/11 \end{pmatrix}. $$

Use the first two components of \(U\), giving \(U^*=(1,0)^T\) and the upper-left \(2\) by \(2\) covariance submatrix. Its determinant is \((36-9)/121=27/121\), so its inverse is:

$$ (V^*)^{-1} = \begin{pmatrix} 2/3 & 1/3\\ 1/3 & 2/3 \end{pmatrix}, \qquad U^{*T}(V^*)^{-1}U^*=\frac{2}{3}. $$

The statistic is approximately chi-square with \(3-1=2\) degrees of freedom, giving \(p\approx 0.7165\). These data do not provide evidence against equal survival distributions at conventional significance levels. The test is an overall comparison: it does not identify which group might differ from another.

Worked Example: Accounting for Censoring Between Event Times

In a hypothetical study, groups A and B each begin with 5 people. At the first event time, 2 events occur in A and none in B. One additional person in B is then censored before the next event time. At that next time, 2 events occur in B and none in A. Calculate the two-group statistic.

At the first event time, \(n=10,d=2\), and A’s share of the risk set is \(5/10\). Therefore \(E_{A1}=1\) and \(V_{A1}=2(8/9)(0.5)(0.5)=4/9\). After the events and the later censoring, the second risk set has 3 people in A and 4 in B. The censoring changes that risk set but is not counted as an event. At the second event time:

$$ E_{A2}=2\left(\frac{3}{7}\right)=\frac{6}{7}, \qquad V_{A2}=2\frac{7-2}{7-1}\left(\frac{3}{7}\right)\left(\frac{4}{7}\right) =\frac{20}{49}. $$

Here \(O_A=2\), \(E_A=1+6/7=13/7\), and \(V_A=4/9+20/49=376/441\). Thus \(X^2_{\mathrm{LR}}=(1/7)^2/(376/441)=9/376\approx 0.0239\), with \(p\approx 0.877\) on 1 degree of freedom. The observed difference is small relative to its variance. Correctly updating the risk set for the censoring is essential to this calculation.

Assumptions, Diagnostics, and Alternatives

The usual interpretation of the log-rank p-value relies on several conditions. Individuals’ event outcomes should be independent across subjects, and censoring should be independent of the event time within groups, at least conditional on factors used to define the analysis. If people with a higher near-term event risk are more likely to be censored, the observed risk sets may no longer represent those still capable of experiencing the event. Groups should also be defined consistently from a common time origin and eligibility rule. If delayed entry occurs, risk sets must include only individuals who have entered observation and remain event-free.

The chi-square reference distribution is an approximation. It can be unreliable when there are very few events, sparse group risk sets, or expected event counts that are too small for a normal approximation. In such settings, consider an exact or simulation-based procedure appropriate to the design, and report the limited information rather than treating the asymptotic p-value as definitive. Tied event times should be handled with a stated convention; the hypergeometric variance above is the standard tied-set calculation.

A practical check is to inspect the Kaplan–Meier curves and the risk-set counts alongside the test. Look for implausible sudden changes, unexpectedly empty risk sets, and discrepancies between the event records and the reported numbers at risk. Such checks can reveal data or bookkeeping errors, but a visual inspection cannot verify independent censoring. Also inspect whether the curves cross: the log-rank test remains a test of equality under its assumptions, but crossing patterns can cause early and late differences to offset, reducing power.

The log-rank test is closely related to, but distinct from, several alternatives. Kaplan–Meier curves describe survival and can show when differences occur; they do not replace a test of the global null. Weighted log-rank tests give different emphasis to event times and may be more sensitive to particular patterns, but the choice of weights should be justified rather than selected after viewing results. A Cox proportional hazards model can estimate covariate associations and is a different modeling approach; its treatment is separate from this test.

Key takeaway: The log-rank test compares observed with expected events across correctly constructed risk sets. Its validity depends on independent subjects and appropriate censoring, and its chi-square calibration requires enough event information. A small p-value signals evidence of unequal survival distributions, not a causal effect or a particular effect size.

Common Pitfall

A frequent mistake is to count everyone originally assigned to a group in every risk set, or to count censored observations as events. Neither is correct. The expected count at time \(t_j\) uses only those at risk immediately before that time, and only actual events contribute to \(d_j\). For example, in the third worked example the censored person in B is included in the first risk set, then excluded from the second; that person contributes no event. Using the original group sizes at the second time would give the wrong expected count and variance.

Check Your Understanding

Try each question, then compare your reasoning with the worked solutions below.

  1. At an event time, \(n_1=8\), \(n_2=12\), and \(d=5\). What is the expected number of events in group 1 under the log-rank null?
  2. Why does the variance for tied events include the factor \((n-d)/(n-1)\)?
  3. For two groups, \(O_1=14\), \(E_1=10\), and \(V_1=4\). Calculate the statistic and state its approximate degrees of freedom.
  4. Name two assumptions or data conditions that can undermine the usual interpretation of a log-rank test.

Solutions. (1) The total risk set is \(n=8+12=20\). Thus \(E_1=d(n_1/n)=5(8/20)=2\) events. (2) Given the number of tied events, individuals are selected without replacement from the risk set. The factor is the hypergeometric finite-population correction; it reduces the variance compared with independent event assignments. (3) \(X^2_{\mathrm{LR}}=(14-10)^2/4=4\), approximately chi-square with 1 degree of freedom. (4) Examples include dependent event outcomes across subjects, informative censoring within a group, incorrect risk sets, or too few events for the chi-square approximation to be dependable.

Clinical Biostats