How to do a log-rank test (worked example, 3+ groups, hazard ratio)
The log-rank test is the standard way to test for a difference between Kaplan–Meier survival curves. At every event it works out how many events each group would have had if there were no difference (the expected number E) and compares this with what happened (the observed number O). This guide builds the table for a 5 vs 5 example, then covers 3+ groups, hazard ratios, and when to use Gehan–Breslow–Wilcoxon instead.
What the log-rank test does
- The null hypothesis is that the groups have the same survival (hazard); it compares the whole curves
- At each event time it counts observed (O) and expected (E) events per group and sums O − E
- Every event time gets the same weight, so it is most powerful when the hazard ratio is constant (proportional hazards)
- With 3+ groups, run the overall test (df = groups − 1), then pairwise tests with a multiple-comparison correction if needed
Unlike t tests or the Mann–Whitney U test, it handles censored data. How to draw the curves and handle censoring is covered in Kaplan–Meier survival curves.
Computing O and E (5 vs 5 example)
Compare group A, 2, 4, 5+, 7, 9 days, with group B, 6, 8+, 11, 13, 15+ days (“+” = censored; made up). At each event time, count the number at risk n just before it and the number of events d. With no difference, events would split between the groups in proportion to the numbers at risk, so the expected number in A is EA = d × nA / n.
| Time | nA | nB | dA | dB | EA = d·nA/n | Variance V |
|---|---|---|---|---|---|---|
| 2 | 5 | 5 | 1 | 0 | 0.500 | 0.250 |
| 4 | 4 | 5 | 1 | 0 | 0.444 | 0.247 |
| 6 | 2 | 5 | 0 | 1 | 0.286 | 0.204 |
| 7 | 2 | 4 | 1 | 0 | 0.333 | 0.222 |
| 9 | 1 | 3 | 1 | 0 | 0.250 | 0.188 |
| 11 | 0 | 3 | 0 | 1 | 0.000 | 0.000 |
| 13 | 0 | 2 | 0 | 1 | 0.000 | 0.000 |
| Total | 4 | 3 | 1.813 | 1.111 |
Group A had 4 events (OA) but would have had 1.81 with no difference (EA). The difference squared, divided by its variance V, is the log-rank statistic, which follows a χ² distribution with 1 degree of freedom.
Some textbooks use the shortcut Σ(O − E)²/E (summed over groups) instead of the variance. Here it gives 3.56 (P = 0.059), smaller and now above 0.05. The shortcut is a conservative approximation; R’s survdiff, SciPy’s logrank and BenchFig use the variance formula above.
How Gehan–Breslow–Wilcoxon differs
The Gehan–Breslow–Wilcoxon (generalized Wilcoxon) test weights each time’s O − E by the number at risk at that time. Early events, when many animals are at risk, count more; late events, when few remain, count less.
| Data | Log-rank | Gehan–Breslow–Wilcoxon |
|---|---|---|
| 5 vs 5 example | χ² = 4.30, P = 0.038 | χ² = 3.41, P = 0.065 |
| Sample Vehicle vs Drug (16 each) | χ² = 6.18, P = 0.013 | χ² = 5.82, P = 0.016 |
In the 5 vs 5 example the same data give P < 0.05 by log-rank but P > 0.05 by Gehan–Breslow–Wilcoxon. Because the choice can change the conclusion, decide on the test before the analysis and name it in the Methods. Despite the name, it is not the Mann–Whitney (Wilcoxon rank-sum) or the Wilcoxon signed-rank test.
Three or more groups and multiple comparisons
With three or more groups, one log-rank test covers them all (df = number of groups − 1), using every group’s O − E and their variance–covariance matrix.
Across the three groups, χ²(2) = 7.76, P = 0.021: there is a difference somewhere. To find where, run log-rank tests on each pair and correct for the number of comparisons.
| Comparison | χ² | Unadjusted P | Bonferroni (× 3) |
|---|---|---|---|
| Vehicle vs Low dose | 3.79 | 0.052 | 0.155 |
| Vehicle vs Drug | 6.18 | 0.013 | 0.039 |
| Low dose vs Drug | 0.34 | 0.557 | 1.000 |
After Bonferroni correction (unadjusted P × 3 comparisons), only Vehicle vs Drug is significant. If you only care about each group versus control, limiting the comparisons to those two in advance means multiplying by 2 instead. A log-rank test for trend (whether survival increases with dose order) is not available in BenchFig.
A quick hazard ratio, and how it differs from Cox
The hazard ratio (HR) is the ratio of the instantaneous event rates (hazards) among animals still alive. HR = 0.5 means events occur at half the rate of the control group. The log-rank quantities give it directly (Mantel–Haenszel form):
For the Drug group in the sample, O = 9, E = 13.95 and V = 3.97, so HR = 0.29 (95% CI 0.11 to 0.77). The other shortcut, (OB/EB) / (OA/EA), gives 0.38 (0.15 to 0.94), closer to 1.
| Method | HR (Drug / Vehicle) | 95% CI |
|---|---|---|
| Mantel–Haenszel exp((O − E)/V) | 0.29 | 0.11–0.77 |
| Ratio of O/E | 0.38 | 0.15–0.94 |
| Reference: Cox proportional hazards (Efron) | 0.29 | 0.11–0.80 |
When only groups are compared, the Mantel–Haenszel value agrees well with Cox regression (the gap grows when the HR is far from 1 or group sizes are very unequal). Cox regression’s strength is adjusting for covariates such as age, weight or sex at the same time; if you need that, use Cox regression and say so in the Methods. Both hazard ratios summarize the data under the assumption that the HR is constant over time (proportional hazards).
How to write it in a paper
Methods: “Survival curves were estimated by the Kaplan–Meier method and compared with the log-rank test. For three or more groups, pairwise log-rank tests were performed with Bonferroni correction. Hazard ratios and 95% confidence intervals were calculated by the Mantel–Haenszel method from the observed and expected numbers of the log-rank test.”
Results: “Drug treatment prolonged survival compared with vehicle (median 46 vs 20 days; HR 0.29, 95% CI 0.11–0.77; log-rank P = 0.013).” Adding χ² and its degrees of freedom helps readers check the result. General formats are in how to report statistics.
FAQ
Log-rank or Gehan–Breslow–Wilcoxon?
Use the log-rank test unless you have a reason not to. It is most powerful when the hazard ratio is constant over time (proportional hazards) and is the standard in papers. Use Gehan–Breslow–Wilcoxon when you decided in advance that early differences matter most. Do not compute both and report the smaller P.
My survival curves cross. Can I still use the log-rank test?
You can compute it, but differences in opposite directions early and late cancel out, so a real difference is easily missed. Show the curves with numbers at risk and describe what you actually want to compare, such as survival at a given time or median survival.
Does the log-rank test compare survival at a specific time (say, day 30)?
No. It compares the curves over the whole follow-up. Comparing survival at one time point is a different question, answered with the survival estimates and their confidence intervals at that time.
Can BenchFig fit Cox regression?
No. For comparing groups, BenchFig reports the Mantel–Haenszel hazard ratio from the log-rank O and E, with its 95% CI. If you need to adjust for covariates such as age or weight, use Cox regression in R, SPSS, Prism or similar.
Related guides
Product and journal names are trademarks or registered trademarks of their respective owners.