Skip to content

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.

Runs in CodeSplashPython (calls R) example not runR example validated

Do it in CodeSplash

In the app, set it up in Analyze with this method:

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

40
40
1.00

Group 1's median event time divided by group 2's; 1 means equal medians.

1.00

Lognormal σ; unequal spreads make the survival curves cross.

1.00
4.0

Censoring times are uniform on (0, follow-up); group 2's median event time is 1.

0.70

The rest are the competing cause.

0.40
0.050

Your data

Simulated with known medians and causes
G14017400G24018941P(cause 1 by t)

Cumulative incidence of the cause of interest; the competing cause removes people from risk without counting as the event.

If H₀ were true

χ² = 8.50
  1. The reference: how χ² varies across samples when H₀ is true.
  2. Your statistic, from the data above.
  3. The p-value: the area at least as extreme as yours (p = .004).
  4. The hatched rejection region at α = .050: statistics landing here reject H₀.

The formula

U=∫w(t)[dN1(c)(t)−Y1∗(t)Y∗(t)dN(c)(t)],χ2=U⊤V^−1UU=\int w(t)\Big[dN_1^{(c)}(t)-\frac{Y_1^*(t)}{Y^*(t)}dN^{(c)}(t)\Big],\qquad \chi^2=U^\top\hat V^{-1}U

With your numbers

33 cause-1 and 17 competing events,U=8.09,χ2=8.5033\text{ cause-1 and }17\text{ competing events},\quad U = 8.09,\quad \chi^2 = 8.50
N(c)N^{(c)}
counting process of the cause of interest
Y∗Y^*
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 H0.

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

U=∫w(t)[dN1(c)(t)−Y1∗(t)Y∗(t)dN(c)(t)]Q=UTV^−1U\begin{gathered}U=\int w(t)\left[dN_1^{(c)}(t)-\frac{Y_1^*(t)}{Y^*(t)}dN^{(c)}(t)\right]\\[5pt] Q=U^T\widehat V^{-1}U\end{gathered}

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.

Python · invokes R

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()
R

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

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.