Introduction
In our previous lessons, we've built a strong foundation in non-parametric survival estimation, learning to construct Kaplan-Meier survival curves and Nelson-Aalen cumulative hazard functions for a single population. While these are powerful descriptive tools, a common and critical task in any statistical analysis, from clinical trials to social research, is comparison. Today, we shift from description to inference by learning how to formally test for differences between the survival experiences of two or more groups.
This lesson addresses the learning outcome of performing the log-rank test. This widely used, non-parametric test allows us to assess whether observed differences between two or more survival curves are statistically significant or likely due to random chance. Given your background in statistics, you'll find the test's construction—rooted in the comparison of observed and expected counts—to be both intuitive and familiar.
The Hypotheses of Survival Comparison
Imagine you have two Kaplan-Meier curves, one for a control group and one for a treatment group. Visually, the treatment group's curve might appear higher, suggesting better survival. But is this difference real? The log-rank test provides the formal framework to answer this question.
The hypotheses are:
- Null Hypothesis (): There is no difference in the survival distributions among the groups. For two groups, this means for all time .
- Alternative Hypothesis (): There is a difference in the survival distributions. For two groups, for at least some time .

The test is non-parametric, meaning it makes no assumptions about the specific shape or family of the survival distributions (e.g., it doesn't assume survival follows a Weibull or exponential distribution). This makes it a robust and versatile tool.
The Intuition: A Series of Comparisons
The logic of the log-rank test is elegant. It proceeds through the data chronologically, focusing on each distinct time an event (e.g., death, failure) occurs. At each of these event times, it constructs a 2x2 contingency table to compare what was observed to what would be expected if the null hypothesis were true.
Let's say at a specific time , a total of events occur across all groups. The individuals at risk just before this time are . If the null hypothesis is true (i.e., all groups have the same survival), we would expect these events to be distributed among the groups in proportion to their representation in the at-risk pool .
For example, if Group A makes up 60% of the individuals at risk at time , we would expect Group A to account for 60% of the events that occur at that moment. The log-rank test systematically calculates the difference between the observed number of events () and this expected number () for each group at every event time, and then aggregates these differences to form a single test statistic.
The Log-Rank Test Calculation
To perform the test, we construct a table that tracks events and individuals at risk over time. The following video provides an excellent, clear walk-through of this calculation process.
Comparing Kaplan-Meier curves - the Log-rank test
The video "Comparing Kaplan-Meier curves - the Log-rank test" from TileStats clearly explains the setup and step-by-step calculation.
First, watch the section that introduces the test and hypotheses. Then, pay close attention to the detailed walkthrough of the manual calculation. Focus on how the table is built, specifically the columns for: Observed events (d_{ij}) Number at risk (n_{ij}) Expected events (E_{ij}) Variance (V_{ij}) Notice how the "Expected" value at each event time is calculated by apportioning the total deaths based on the at-risk numbers.
As demonstrated in the video, the core components for a specific group (let's say Group 1) at each event time are:
- Observed events: (the actual number of events in Group 1 at ).
- Expected events: , where is the total events at , is the number at risk in Group 1, and is the total number at risk.
- Variance: . This formula arises from the variance of a hypergeometric distribution, which is the underlying model for the 2x2 table at each event time.
To get the overall test statistic, we sum these components across all distinct event times:
- Total Observed:
- Total Expected:
- Total Variance:
The final log-rank test statistic is then calculated as:
This statistic approximately follows a chi-square distribution with degrees of freedom, where is the number of groups being compared. For two groups, this is 1 degree of freedom.
A Detailed Worked Example
Now, let's solidify this with a detailed reading that walks through a full calculation by hand and then shows the corresponding output in R. This will give you a chance to apply the formulas and connect them to practical software output.
What is survival analysis? Examples by hand and in R - Stats and R
This article from Stats and R provides a comprehensive example that is perfect for consolidating your understanding.
First, read the brief introduction to the log-rank test. Then, carefully work through the section "By hand". Follow how each column of the summary table is constructed. Finally, review the section "In R" to see how the survdiff() and ggsurvplot() functions are used to produce the same result.
The output of the log-rank test is typically a chi-square statistic and a p-value. If the p-value is below our chosen significance level (commonly ), we reject the null hypothesis.

Extensions of the Log-Rank Test
The standard log-rank test can be extended to handle more complex scenarios, which is crucial for real-world data analysis.
1. Comparing More Than Two Groups
The test naturally extends to compare groups. The null hypothesis becomes . The calculation methodology is similar, but the resulting test statistic follows a chi-square distribution with degrees of freedom. A significant result tells us that at least one group's survival distribution is different from the others, but it does not specify which one(s). To identify the specific differences, pairwise post-hoc tests (e.g., log-rank tests between pairs of groups) are typically performed, often with a correction for multiple comparisons (like the Bonferroni correction).
2. Stratified Log-Rank Test
In many studies, you may want to compare two treatments (e.g., treatment vs. placebo) while controlling for a confounding factor, such as gender or disease stage. The stratified log-rank test is used for this. The procedure is as follows:
- Stratify the data: Divide the subjects into strata based on the confounding variable (e.g., a stratum for males, a stratum for females).
- Calculate within strata: Within each stratum, calculate the observed (), expected (), and variance () values as you would for a standard log-rank test.
- Combine the results: Sum the , , and values across all strata to compute a single, overall test statistic.
where the sum is over the strata . This test assesses the difference between the main groups of interest after adjusting for the effect of the stratification variable.
Conclusion
In this lesson, you have learned how to use the log-rank test to move beyond describing survival curves to formally comparing them. This is a fundamental skill for anyone working with time-to-event data.
Key Takeaways:
- The log-rank test is a non-parametric statistical test used to compare the survival distributions of two or more groups.
- The null hypothesis () is that there is no difference in survival between the groups.
- The test works by summing the differences between Observed () and Expected () event counts at each event time, assuming is true.
- The test statistic follows a distribution with degrees of freedom, where is the number of groups.
- Extensions like the stratified log-rank test allow for comparison while controlling for confounding variables.
The log-rank test tells us if a significant difference exists. However, it doesn't quantify the size of the effect or tell us how specific factors (covariates) influence survival time. To answer those questions, we need to move from non-parametric tests to regression modeling. In our next module, we will do just that, beginning with the cornerstone of survival modeling: the Cox Proportional Hazards model.