FTime to event · Guide test 246
Gray competing risks test
Compares cumulative incidence functions for a specified event type in the presence of competing events.
Do it in CodeSplash
In the app, set it up in Analyze with this method:
- Cumulative incidence with competing risks — What is the probability of each type of event over time when other events compete?
In Cumulative incidence with competing risks, choose Compare groups by = Gray's test (the curves).
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 · Gray competing risks 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.
The rest are the competing cause.
Your data
Simulated with known medians and causesCumulative incidence of the cause of interest; the competing cause removes people from risk without counting as the event.
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 = .004).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The formula
With your numbers
- counting process of the cause of interest
- Gray's risk set: people who had a competing event stay in it, weighted by the censoring distribution
The verdict
χ²(1) = 8.50, p = .004
p ≤ α = .050: reject H₀
Compares cumulative incidence, which mixes the cause's own hazard with the competing hazards; a cause-specific log-rank test answers a different question.
- Statistic
- 8.502
- p-value
- 0.0035
Gray competing risks test on 40 + 40 subjects with 50 events: χ²(1) = 8.50, p = .004. The p-value is below α = .050, so the data are hard to reconcile with H₀.
Question and hypotheses
H0: groups have the same cumulative incidence function for a specified event cause.
Assumptions
Independent subjects; censoring independent of failure causes within the modeled strata; competing events observed and correctly classified.
Formula
Parameters and notation
For cause c, subdistribution risk sets Y* retain subjects with competing events using censoring weights; a common contribution after a competing event at Ti is Ĝ(t)/Ĝ(Ti). Gray weights often use [1−F̂c(t−)]^ρ.
Structure of the method
Construct cause-specific counting processes and a censoring-adjusted subdistribution risk process that retains appropriate contributions after competing events, accumulate a weighted group contrast, and use Gray’s covariance. The target is equality of cause-specific cumulative incidence, not equality of a cause-specific hazard alone.
Estimated and nuisance parameters
Estimate cumulative incidence and censoring-related quantities needed by the test, along with its covariance. No Fine–Gray regression coefficient is fitted by cuminc. The event-of-interest code, competing-event codes, and censoring code define different observable outcomes and cannot be collapsed without changing the estimand.
Settings and design choices
Choose event cause, grouping/stratification, rho weight, censoring code, and follow-up range before testing. Multiple causes yield multiple comparisons. The supplied example’s random status generation is a computational illustration; a substantive analysis needs coherent event times and cause/censoring classification.
Fitting and adaptation
Refit the nonparametric incidence/censoring components whenever the analysis population changes. If covariate adjustment is needed, use an appropriate regression or standardization procedure rather than claiming the unadjusted test provides it. Bootstrap designs must preserve competing-event and censoring structure and recompute all estimated nuisance quantities.
Sensitivity and robustness
Inspect sparse cause counts, competing-event imbalance, dependent censoring, and miscoded outcomes. Treating competing events as ordinary censoring estimates a different quantity and can mislead absolute-risk comparisons. Report cumulative-incidence curves and effect summaries, because a rejection does not isolate which underlying cause-specific hazard mechanism differs.
How to use and calibrate
Use Gray’s influence-function covariance, including censoring estimation, and χ²k−1. Use a competing-risks implementation.
Example application
Compare incidence of relapse when death prevents relapse.
Interpretation and pitfalls
Watch out
Treating competing deaths as ordinary censoring changes the estimand. Do not substitute ordinary logrank variance for Gray’s covariance.
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
Status 0 denotes censoring, 1/2 competing causes. Gray tests cumulative-incidence differences separately by cause, accounting for competing events.
Not run — Executed end to end through Rscript · needs Python standard library + Rscript; R packages: cmprsk
# 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)
n<-150;g<-factor(rep(1:2,each=75));time<-rexp(n);status<-sample(0:2,n,replace=TRUE,prob=c(.2,.5,.3));fit<-cmprsk::cuminc(time,status,g,cencode=0,rho=0);print(fit$Tests)
"""
# 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 cmprsk
set.seed(2026)
n<-150;g<-factor(rep(1:2,each=75));time<-rexp(n);status<-sample(0:2,n,replace=TRUE,prob=c(.2,.5,.3));fit<-cmprsk::cuminc(time,status,g,cencode=0,rho=0);print(fit$Tests)
Output
stat pv df 1 0.7422955 0.3889265 1 2 0.4057317 0.5241439 1
Sources
- cmprsk — cmprsk · cumincAuthor documentation/source reviewed: cmprsk 2.2-12.search.r-project.org
- cumincsearch.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 246, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT246.