Repository navigation
Description
Describe the bug
On data with tied event times, two residual types of PHRegResults disagree with R's survival::residuals.coxph (survival 3.8.6).
- Schoenfeld residuals with
ties="efron".schoenfeld_residualssubtracts the Breslow risk-set mean even when the model was fitted withties="efron". The residuals of tied events therefore differ from R's. Anything built on them is shifted as well, for example a Grambsch–Therneau proportional-hazards test. Withties="breslow"they match R exactly, including stratified models. - Martingale residuals.
martingale_residualsuses the cumulative hazard just before each subject's own time:baseline_cumulative_hazardcomputescumhaz = np.cumsum(h0) - h0. Each subject's own event-time jump is therefore left out, and the residuals do not sum to zero. They differ from R's for both tie methods. An event at the first event time always gets exactly 1.
Code sample
import numpy as np
from statsmodels.duration.hazard_regression import PHReg
time = np.array([1, 1, 2, 2, 3, 4, 4, 5, 6, 7, 8, 9], float)
status = np.array([1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1])
x = np.array([0.5, -1.2, 0.3, 1.1, -0.7, 0.8, -0.2, 1.5, -1.0, 0.4, 0.9, -0.3])
for ties in ["efron", "breslow"]:
res = PHReg(time, x[:, None], status=status, ties=ties).fit()
print(ties, res.params, res.schoenfeld_residuals[:, 0], res.martingale_residuals)library(survival)
d <- data.frame(time = c(1, 1, 2, 2, 3, 4, 4, 5, 6, 7, 8, 9),
status = c(1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1),
x = c(0.5, -1.2, 0.3, 1.1, -0.7, 0.8, -0.2, 1.5, -1.0, 0.4, 0.9, -0.3))
for (ties in c("efron", "breslow")) {
f <- coxph(Surv(time, status) ~ x, data = d, ties = ties)
print(coef(f)); print(resid(f, "schoenfeld")); print(resid(f, "martingale"))
}The coefficients agree: −0.634674 (Efron) and −0.627133 (Breslow). The residuals differ as follows.
Schoenfeld residuals, event rows
| statsmodels 0.15.0 | R survival 3.8.6 | |
|---|---|---|
| Efron | 0.7517 −0.9483 0.3946 −0.4920 0.8911 −0.1089 −0.6775 0.2213 0.0000 | 0.7176 −0.9824 0.3946 −0.4920 0.9092 −0.0908 −0.6775 0.2213 0.0000 |
| Breslow | 0.7469 −0.9531 0.3904 −0.4960 0.8867 −0.1133 −0.6811 0.2195 0.0000 | identical |
With Efron ties, only the tied events at t = 1 and t = 4 differ.
Martingale residuals, all rows
| statsmodels 0.15.0 | R survival 3.8.6 | |
|---|---|---|
| Efron | 1.0000 1.0000 0.8657 −0.0808 0.5816 0.7644 0.5555 −0.1511 −0.3136 0.2849 −0.5206 −0.5894 (sum 3.3966) | 0.9074 0.7276 0.7693 −0.1388 0.3728 0.6133 0.2706 −0.2819 −0.8029 −0.0456 −0.7613 −1.6305 (sum 0.0000) |
| Breslow | 1.0000 1.0000 0.8652 −0.0816 0.5832 0.7626 0.5556 −0.1530 −0.3052 0.2817 −0.5249 −0.5868 (sum 3.3967) | 0.8811 0.6546 0.7774 −0.1348 0.3919 0.5779 0.2097 −0.2721 −0.7282 −0.0230 −0.7476 −1.5868 (sum 0.0000) |
Expected behaviour
Schoenfeld residuals (Efron ties). R's coxscho.c computes, for d events tied at time t, the expected covariate of each tied event as
(1/d) · Σₖ₌₀^{d−1} (S1 − (k/d)·S1_tied) / (S0 − (k/d)·S0_tied)
where S0 and S1 sum exp(η) and exp(η)·x over the risk set, and S0_tied and S1_tied sum them over the tied events.
Martingale residuals. R's coxmart.c includes the jump at each subject's own time. With Efron ties, each of the d tied events receives the share Σₖ (1 − k/d) / (S0 − (k/d)·S0_tied) of that jump; everyone else still at risk receives Σₖ 1 / (S0 − (k/d)·S0_tied).
We compute both residuals this way in SurvStudio and match R to about 1e-9, stratified or not: _efron_tie_groups, _efron_schoenfeld_residuals and _efron_martingale_residuals in https://github.com/kangk1204/SurvStudio/blob/e29ddd0ed91693e922de5d5032ca67e51b11155f/src/survival_toolkit/analysis.py#L2278-L2419. We are happy to help with a PR if useful.
We did not find an existing issue for this; #2386 (exact tie handling) and #8518 (survival function) are related but different.
Versions
statsmodels 0.15.0, NumPy 2.4.6, Python 3.11 (Linux aarch64); R 4.5.2 with survival 3.8.6.
Activity
BUG: Use Efron-adjusted means in PHReg Schoenfeld residuals
BUG: Fix the score residuals and the robust covariance of PHReg for E…
BUG: Fix the martingale residuals of PHReg
Chetansahney commentedon Oct 7, 2026
Confirming this is fixed on current main (
8278e2d), using the exact example and R reference values from the report.Both of the reported problems are gone. The Efron Schoenfeld residuals at the tied times t = 1 and t = 4 now agree with R, and the martingale residuals include each subject's own hazard jump, so they sum to zero instead of 3.3966.
Verified with
3c3bd86,813c9e0,93b5fa0and9e6cf64in place. Looks ready to close.Kirill638849776 commentedon Oct 8, 2026
Hi @kangk1204, thanks for the detailed report!
I reproduced this locally with the exact example you provided:
Confirmed the discrepancies:
Schoenfeld residuals (event rows):
Only the tied events at t = 1 and t = 4 differ with Efron ties, exactly as you described.
Martingale residuals (sum):
The Schoenfeld issue with
ties="efron"and the martingale residual sum issue are both real and reproducible.Thanks for the reference implementation in SurvStudio — the approach following R's
coxscho.candcoxmart.clooks correct.Would you be open to opening a PR against statsmodels with the fix? That would help move this forward.
kangk1204 commentedon Oct 8, 2026
DOC: clarify PHReg entry-time risk-set boundary