Skip to content

PHReg: schoenfeld_residuals use Breslow means under ties="efron", and martingale_residuals omit each subject's own hazard jump #10288

Open

Description

@kangk1204

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).

  1. Schoenfeld residuals with ties="efron". schoenfeld_residuals subtracts the Breslow risk-set mean even when the model was fitted with ties="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. With ties="breslow" they match R exactly, including stratified models.
  2. Martingale residuals. martingale_residuals uses the cumulative hazard just before each subject's own time: baseline_cumulative_hazard computes cumhaz = 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

  1. added
    type-bug-wrong
    on Sep 28, 2026
  2. Chetansahney commented on Oct 7, 2026

    @Chetansahney
    Contributor

    Confirming this is fixed on current main (8278e2d), using the exact example and R reference values from the report.

    residual ties max abs diff vs R sum
    Schoenfeld efron 0.0000
    Schoenfeld breslow 0.0000
    martingale efron 0.0000 0.0000
    martingale breslow 0.0000 0.0000

    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, 93b5fa0 and 9e6cf64 in place. Looks ready to close.

  3. Kirill638849776 commented on Oct 8, 2026

    @Kirill638849776

    Hi @kangk1204, thanks for the detailed report!

    I reproduced this locally with the exact example you provided:

    • statsmodels 0.15.0, Python 3.11
    • R 4.5.2 with survival 3.8.6

    Confirmed the discrepancies:

    Schoenfeld residuals (event rows):

    residual ties statsmodels R diff
    Schoenfeld efron 0.7517 0.7176 +0.0341
    Schoenfeld breslow 0.7469 0.7469 0.0000 ✅

    Only the tied events at t = 1 and t = 4 differ with Efron ties, exactly as you described.

    Martingale residuals (sum):

    ties statsmodels R diff
    efron 3.3966 0.0000 +3.3966
    breslow 3.3967 0.0000 +3.3967

    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.c and coxmart.c looks correct.

    Would you be open to opening a PR against statsmodels with the fix? That would help move this forward.

  4. kangk1204 commented on Oct 8, 2026

    @kangk1204
    ContributorAuthor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugtype-bug-wrong

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions