FTime to event · Guide test 240
Log rank or Mantel–Cox test
Compares survival curves through observed versus expected events at event times.
Do it in CodeSplash
In the app, set it up in Analyze with one of these methods:
- Kaplan–Meier survival curves — How long until the event happens, and does survival differ between groups?
- Log-rank test (compare survival curves) — Do survival curves differ between groups?
Do it in CodeSplash Run this test on your own data, in Python or R.
Bench
Change the design, drag the data, move α. The statistic, its reference distribution and the verdict follow.
Bench · Log-rank test
Group 1's median event time divided by group 2's; 1 means equal medians.
Lognormal σ; unequal spreads make the survival curves cross.
Censoring times are uniform on (0, follow-up); group 2's median event time is 1.
Your data
Simulated with known medians and spreadsKaplan–Meier curves; ticks mark censored follow-up. Rows under the axis: numbers still at risk. Shaded: the weight each event time gets, w(t) = 1.
If H₀ were true
- The reference: how χ² varies across samples when H₀ is true.
- Your statistic, from the data above.
- The p-value: the area at least as extreme as yours (p = .161).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The formula
With your numbers
- events and numbers at risk in group 1 at event time j
- events expected in group 1 if the groups shared one hazard
- 1 at every event time: the log-rank weights
The verdict
χ²(1) = 1.97, p = .161
p > α = .050: compatible with H₀
Most powerful when the hazard ratio stays constant; crossing curves can cancel and hide a real difference. Censoring must be unrelated to prognosis.
- Statistic
- 1.966
- p-value
- 0.1609
Log-rank test on 40 + 40 subjects with 40 events: χ²(1) = 1.97, p = .161. The p-value is above α = .050, so the data are compatible with H₀.
Question and hypotheses
H0: the groups have equal survival/event hazard functions over follow-up.
Assumptions
Independent subjects and independent/noninformative censoring within groups, conditional on the modeled design. Standard weights and variances presume the stated risk-set process. Proportional hazards is a power/interpretation condition for logrank, not necessary for validity under identical survival curves.
Formula
Parameters and notation
At event time tj, Ygj is group g at-risk count, dgj its event count; Yj,dj are pooled totals. For more than two groups use a score vector and its covariance, with χ²k−1. wj=1.
Structure of the method
Construct pooled event-time risk sets, compare observed group events with their risk-set-proportional expectations, sum the unweighted score, and use its hypergeometric covariance. For multiple groups, remove the redundant score direction or use a rank-aware covariance calculation before forming the omnibus statistic.
Estimated and nuisance parameters
Estimate event-time risk sets and covariance, with optional Kaplan–Meier curves for interpretation; no hazard ratio is estimated by the test itself. A Cox hazard ratio, median survival, or restricted mean survival difference requires a separate estimator. Censoring information is part of the observed risk-set process.
Settings and design choices
Choose groups, stratification, follow-up horizon, event coding, tie/time-rounding conventions, and one/two-sided inference before analysis. Delayed entry, clustering, or time-varying group assignment may require a more suitable survival-model interface than the simple demonstration. Weight one is a design choice, not a fitted parameter.
Fitting and adaptation
Adapt by constructing correct event/censoring records and a valid at-risk process, not by tuning coefficients to lower a p value. If stratified randomization is relevant, use a prespecified stratified test. For bootstrap/permutation inference, preserve the design and censoring assumptions rather than freely shuffling every recorded time/status pair.
Sensitivity and robustness
Inspect numbers at risk, censoring imbalance, sparse late events, crossing hazards, and follow-up differences. Proportional hazards affects power and hazard-ratio interpretation, not validity under identical survival functions. Report an interpretable survival effect with uncertainty alongside the test; statistical significance alone does not quantify clinical or practical benefit.
How to use and calibrate
Use asymptotic normal tails for Z, or χ²1 for Z². Report the weight rule, risk-set conventions and censoring assumptions.
Example application
Compare survival experience between randomized treatment arms.
Interpretation and pitfalls
Watch out
Crossing hazards can make positive and negative contributions cancel.
What changed in edition 2
- New sections: structure of the method, estimated and nuisance parameters, settings and design choices, fitting and adaptation, sensitivity and robustness.
Python and R
Right-censored survival, independent groups and noninformative censoring. rho=0 is log-rank; rho=1 is Peto–Peto, not ordinary Gehan–Breslow weighting.
Not run — Executed end to end through Rscript · needs Python standard library + Rscript; R packages: survival
# Python launcher for the complete R implementation below.
# Requires Rscript on PATH and the listed R packages.
import shutil
import subprocess
rscript = shutil.which("Rscript")
if rscript is None:
raise RuntimeError("Install R and make Rscript available on PATH.")
r_code = r"""
set.seed(2026)
library(survival)
n<-120;g<-factor(rep(c("A","B"),each=60));event_time<-rexp(n,rate=ifelse(g=="A",.1,.14));censor_time<-rexp(n,.04);d<-data.frame(time=pmin(event_time,censor_time),status=as.integer(event_time<=censor_time),g=g)
print(survdiff(Surv(time,status)~g,data=d,rho=0))
"""
# No shell interpolation. A failed R execution raises an error in Python.
result = subprocess.run([rscript, "--vanilla", "-"], input=r_code,
text=True, capture_output=True)
print(result.stdout)
if result.stderr:
print(result.stderr)
result.check_returncode()
Validated — Executed successfully in the validation environment · needs survival
set.seed(2026)
library(survival)
n<-120;g<-factor(rep(c("A","B"),each=60));event_time<-rexp(n,rate=ifelse(g=="A",.1,.14));censor_time<-rexp(n,.04);d<-data.frame(time=pmin(event_time,censor_time),status=as.integer(event_time<=censor_time),g=g)
print(survdiff(Surv(time,status)~g,data=d,rho=0))
Output
Call:
survdiff(formula = Surv(time, status) ~ g, data = d, rho = 0)
N Observed Expected (O-E)^2/E (O-E)^2/V
g=A 60 44 51.1 0.994 2.66
g=B 60 41 33.9 1.500 2.66
Chisq= 2.7 on 1 degrees of freedom, p= 0.1Sources
- survival — survival · survdiffAuthor documentation/source reviewed: survival 3.8-12.search.r-project.org
- survdiffsearch.r-project.org
- Statistics guide v4 (historical):
- Python: Executed end to end through Rscript · R: Executed successfully in the validation environment
- Reference's record:
- Edition 2 review: Python: skip · R: not executed (Rscript unavailable); why not run: Python: Python-to-R bridge; Rscript is unavailable. R code was not executed. Python: Not run in the reference's review: Python-to-R bridge; Rscript is unavailable. R code was not executed. R: Reviewed but not run: R wasn't available to the reference's authors.
From the CodeSplash statistics guide (v4), entry 240, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT240.