FTime to event · Guide test 247
Grambsch–Therneau proportional hazards test
Uses scaled Schoenfeld residual patterns to test the proportional hazards assumption in a Cox model.
Do it in CodeSplash
In the app, set it up in Analyze with one of these methods:
- Cox proportional hazards regression — Which factors are associated with the hazard of the event?
- 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 · Grambsch–Therneau 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.
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 = < .001).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The formula
With your numbers
- the Kaplan–Meier time scale cox.zph uses by default
- score for a coefficient drifting with g(t), from the scaled Schoenfeld residuals
The verdict
χ²(1) = 21.64, p < .001, HR = 1.40
p ≤ α = .050: reject H₀
Effect size: average hazard ratio (group 1 vs 2) HR = 1.403
A small p says the hazard ratio drifts: report it over time or use a time-varying effect. With many events it flags drifts too small to matter, so look at the residual plot.
- Statistic
- 21.644
- p-value
- 0.0000
Grambsch–Therneau test on 40 + 40 subjects with 50 events: χ²(1) = 21.64, p < .001, HR = 1.40. The p-value is below α = .050, so the data are hard to reconcile with H₀.
Question and hypotheses
H0: specified Cox model coefficients are constant over time (proportional hazards).
Assumptions
Correct Cox risk-set model apart from tested time dependence; independent/noninformative censoring and valid covariate specification.
Formula
Parameters and notation
g(t) is a selected time transform. The score is computed from appropriately scaled Schoenfeld residuals; test individual coefficients or a joint global vector.
Structure of the method
Fit the Cox model, obtain event-time Schoenfeld score information, associate coefficient behavior with a specified transformed time, and form individual-term and global score tests for added time dependence. Scaled residual plots provide a related diagnostic view but are not interchangeable with an arbitrary correlation test.
Estimated and nuisance parameters
Estimate baseline Cox coefficients and information/covariance quantities; the tested time-interaction coefficients are evaluated at their null values. The baseline hazard is a nuisance component handled by the Cox framework. Multi-level factors can be assessed as whole terms or separate coordinates, changing degrees of freedom.
Settings and design choices
Choose time transformation, term-versus-coefficient tests, global test inclusion, tie method, and plot smoother. A smoother is a visual diagnostic setting, not the score test’s fitted time-varying effect model. Choosing transforms only after observing residual patterns creates additional selection beyond a single nominal p value.
Fitting and adaptation
Refit the Cox model after addressing nonlinear covariate effects, stratification, or time-dependent effects, then diagnose the revised model with appropriate caution. The x=TRUE option in the demonstration retains the design matrix. Random-effects/frailty or complex dependence features may need specialized diagnostics rather than assuming the basic interface tests everything.
Sensitivity and robustness
Inspect the number and timing of events, covariate misspecification, influential observations, censoring, and sparse late follow-up. Low power can conceal nonproportionality; significance can reflect a small time trend in a large sample. A fitted time-varying effect requires explicit modeling and uncertainty, not only a plotted residual smoother.
How to use and calibrate
Use the score chi square reference and inspect residual-versus-time plots. State time transform and terms tested.
Example application
Assess whether a hazard ratio changes during follow-up.
Interpretation and pitfalls
Watch out
A nonsignificant test does not prove proportional hazards; time transform, sample size and covariate misspecification affect detection.
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
Scaled-Schoenfeld-residual test of time-varying effects. Read both covariate and global tests; proportional-hazards diagnostics should also inspect residual plots.
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)
fit<-coxph(Surv(time,status)~g,data=d,x=TRUE);print(cox.zph(fit,transform="km"))
"""
# 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)
fit<-coxph(Surv(time,status)~g,data=d,x=TRUE);print(cox.zph(fit,transform="km"))
Output
chisq df p g 0.717 1 0.4 GLOBAL 0.717 1 0.4
Sources
- survival — survival · cox.zphAuthor documentation/source reviewed: survival 3.8-12.search.r-project.org
- cox.zphsearch.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 247, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT247.