A Python package for competing risks regression and cumulative incidence estimation. Implements and translates the methods from the R cmprsk package.
pip install CompRiskRegIn survival analysis with competing events, standard Kaplan-Meier and Cox regression can be misleading because they treat competing events as independent censoring. This package provides two core methods designed for this setting:
- Cumulative Incidence Function (CIF) — Aalen-Johansen estimator with Gray's K-sample test for comparing groups
- Fine-Gray regression — Subdistribution hazard model for covariate-adjusted inference on the CIF
Estimates the CIF for each group-cause combination and optionally tests equality across groups using Gray's K-sample test.
import numpy as np
from CompRiskReg import cuminc, timepoints
rng = np.random.default_rng(42)
n = 200
ftime = rng.exponential(scale=1.0, size=n)
fstatus = rng.choice([0, 1, 2], size=n, p=[0.3, 0.4, 0.3])
group = rng.choice(["A", "B"], size=n)
result = cuminc(ftime, fstatus, group=group)
# Access CIF estimates for group A, cause 1
curve = result["A 1"]
print(curve["time"]) # event times
print(curve["est"]) # CIF estimates
print(curve["var"]) # variance estimates
# Gray's test comparing groups (present when >1 group)
print(result["Tests"]) # columns: [test stat, p-value, df]Fits the Fine-Gray subdistribution hazard model.
from CompRiskReg import crr
n = 200
ftime = rng.exponential(scale=1.0, size=n)
fstatus = rng.choice([0, 1, 2], size=n, p=[0.3, 0.4, 0.3])
cov1 = rng.standard_normal(size=(n, 2))
fit = crr(ftime, fstatus, cov1=cov1, failcode=1, cencode=0)
print(fit.coef) # estimated coefficients
print(fit.converged) # whether Newton-Raphson converged
# Summary table (coef, exp(coef), SE, z, p-value)
summary = fit.summary()
print(summary["coef"])
print(summary["conf_int"])
# Predict CIF for new subjects
new_cov = np.array([[0.5, -1.0], [1.0, 0.0]])
pred = fit.predict(cov1=new_cov)crr also supports time-varying covariates via the cov2 and tf arguments, where tf is a callable that maps a vector of times to a covariate matrix.
- Python >= 3.8
- NumPy >= 1.20
- SciPy >= 1.8.0