CalculatorsToolsGuides 日本語
BenchFig › Guides › How to do a log-rank test (worked example, 3+ groups, hazard ratio)
Statistics

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.

Updated: BenchFig日本語

What the log-rank test does

Key points
  • 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.

Only event times are listed. n is the number at risk in each group, d the events at that time, d = dA + dB, n = nA + nB. V = d·nA·nB·(n − d) / (n²(n − 1)).
TimenAnBdAdBEA = d·nA/nVariance V
255100.5000.250
445100.4440.247
625010.2860.204
724100.3330.222
913100.2500.188
1103010.0000.000
1302010.0000.000
Total431.8131.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.

χ² = (OA − EA)²V = (4 − 1.813)²1.111 = 4.30P = 0.038 (1 df). Using group B gives the same value, since OB − EB only changes sign.

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.

DataLog-rankGehan–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.

0204060801000102030405060Percent survivalDaysVehicleLow doseDrugLog-rank (overall) P = 0.021Number at riskVehicle161385000Low dose1615138320Drug1615149643
The app’s 3-group sample (made up, 16 animals per group). P from the overall log-rank test across the three groups (χ²(2) = 7.76).

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 PBonferroni (× 3)
Vehicle vs Low dose3.790.0520.155
Vehicle vs Drug6.180.0130.039
Low dose vs Drug0.340.5571.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):

HR = exp(O − EV)
95% CI = exp(O − EV ± 1.96√V)O, E and V are for the group being compared (here, Drug).

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.

The Cox values were computed separately from the partial likelihood on the same data (BenchFig does not fit Cox models).
MethodHR (Drug / Vehicle)95% CI
Mantel–Haenszel exp((O − E)/V)0.290.11–0.77
Ratio of O/E0.380.15–0.94
Reference: Cox proportional hazards (Efron)0.290.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).

See log-rank and hazard ratios on the sampleOpen the 3-group sample to get the overall log-rank test, pairwise Bonferroni-corrected tests and hazard ratios against the control you choose.
Open for free →

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.

Try the log-rank test on sample dataOpen a sample on the Survival screen to see the log-rank test (overall and pairwise with Bonferroni for 3+ groups), Gehan–Breslow–Wilcoxon, and hazard ratios with 95% CIs.
Open for free →
Your own data, all the way to the final figure.Loading your files, saving, and journal-ready export (PDF, TIFF, PNG, SVG) are in BenchFig Pro for Windows, updated based on researchers’ requests: ¥3,980/year (2 PCs), with lab and perpetual licenses too.
About the app →

Related guides

Product and journal names are trademarks or registered trademarks of their respective owners.