Signature
M = delta - H; r_D = M / abs(M) * sqrt(-2 * (M + delta * log(delta - M)))
| Inputs | Definition | Unit |
|---|---|---|
delta | 1 if the patient had the event at the observed time, 0 if censored | indicator |
H | Cumulative hazard the fitted model gives at the patient's event or censoring time, the Cox-Snell residual (HE-FM-CUMH-001) | events |
M | Event indicator minus the fitted cumulative hazard at the observed time | events |
|---|---|---|
r_D | Martingale residual transformed to be roughly symmetric about zero | none |
Function
Deviance residuals for finding poorly fitted patients in cost regressions and survival models
Turns each patient's contribution to a fitted model's deviance into a signed residual, so the patients a cost regression or a survival curve describes worst can be found before its estimates feed a cost-effectiveness model. The generalised linear model version takes the square root of the unit deviance with the sign of the raw residual; the survival version applies the same idea to the martingale residual, treating the event indicator as a Poisson count with mean equal to the fitted cumulative hazard. The Cox-Snell residual itself, the fitted cumulative hazard, is HE-FM-CUMH-001 on the Cumulative Hazard page. Notation follows the Deviance Residual article.
Computational function
Computational function: exponential survival fit with martingale and deviance residuals for every patient
Fits a constant hazard to patient-level survival times by maximum likelihood, then returns each patient's fitted cumulative hazard, martingale residual and deviance residual, the sum of the martingale residuals and the deviance of the exponential model, in input order. The inputs differ from the formula's: vectors of observed times and event indicators in place of a given cumulative hazard.
Inputs and outputs:
time: Observed event or censoring time of each patient. Unit: months (any unit, used throughout).;event: Event indicator, 1 for an event and 0 for censoring. Unit: indicator.;rate: Maximum likelihood constant hazard, events over total time at risk (HE-FM-AER-002). Unit: events per month.;cumhaz: Fitted cumulative hazard rate x time. Unit: events.;mart: Martingale residuals. Unit: events.;dev_res: Survival deviance residuals (HE-FM-DEVR-002). Unit: none.;sum_mart: Sum of the martingale residuals, zero at the maximum likelihood fit. Unit: events.;deviance: Sum of the squared deviance residuals. Unit: none.Assumption: One record per patient and a constant hazard over follow-up; for another parametric model the fitted cumulative hazard replaces rate x time.
Worked example (Four patients, article example): Times of 1, 3, 5 and 11 months with the 5-month patient censored give a hazard of 3 / 20 = 0.15 a month, martingale residuals of 0.85, 0.55, minus 0.75 and minus 0.65 that sum to zero, deviance residuals of +1.447, +0.705, minus 1.225 and minus 0.546 and a deviance of 4.390, as in the article.
time = 1, 3, 5, 11; event = 1, 1, 0, 1; rate = 0.15; deviance = 4.390Worked example (Three patients, one event): Times of 2, 4 and 6 months with only the 4-month patient having the event give a hazard of 1 / 12 = 0.0833 a month and deviance residuals of minus 0.577, +0.929 and minus 1.000 (computed here for illustration).
time = 2, 4, 6; event = 0, 1, 0; rate = 0.0833; deviance = 2.197Excel: With times in TimeVec and indicators in EventVec,
=SUM(EventVec)/SUM(TimeVec)returns ExpRate,=ExpRate*TimeVecthe cumulative hazards into CumHazVec,=EventVec-CumHazVecthe martingale residuals into MartVec and=SIGN(MartVec)*SQRT(-2*(MartVec+EventVec*LN(CumHazVec)))the deviance residuals into DevVec; LN(CumHazVec) equals LN(EventVec minus MartVec).R:
exp_dev <- function(time, event) { lam <- sum(event)/sum(time); H <- lam*time; M <- event-H; r <- sign(M)*sqrt(-2*(M+ifelse(event == 1, log(H), 0))); list(rate = lam, table = data.frame(time = time, event = event, cumhaz = H, mart = M, dev_res = r), sum_mart = sum(M), deviance = sum(r^2)) }Base R only;exp_dev(c(1, 3, 5, 11), c(1, 1, 0, 1))returns the article example.Python:
def exp_dev(time, event): lam = sum(event)/sum(time); H = [lam*t for t in time]; M = [e-x for e, x in zip(event, H)]; r = [math.copysign(math.sqrt(-2*(m+(math.log(x) if e == 1 else 0.0))), m) for m, x, e in zip(M, H, event)]; return {"rate": lam, "cumhaz": H, "mart": M, "dev_res": r, "sum_mart": sum(M), "deviance": sum(v*v for v in r)}Needsimport math; returns the same values as the R function.Test (Martingale residuals sum to zero at the exponential fit): At the maximum likelihood rate the events equal the rate times the total time at risk, so the martingale residuals in MartVec sum to zero. Expected result: TRUE. FALSE shows the rate computed from the time of patients with events only, 3 / 15 = 0.2, which gives a sum of minus 1. Excel check:
=ABS(SUM(MartVec))<1E-9*MAX(1,SUM(EventVec))Common error (Expecting the deviance residuals to sum to zero as well): The martingale residuals of the exponential fit sum to zero, but the deviance residuals of the article example sum to +0.381 (computed here for illustration); Stata's stcox postestimation manual notes that deviance residuals are expected to be symmetric about zero but do not necessarily sum to zero.
Source: StataCorp. Stata Survival Analysis Reference Manual, Release 19: streg postestimation. College Station, TX: Stata Press; 2025 (full text read). Methods and formulas; StataCorp. Stata Survival Analysis Reference Manual, Release 19: stcox postestimation. College Station, TX: Stata Press; 2025 (full text read). Methods and formulas.
rate = sum(event) / sum(time); cumhaz = rate * time; mart = event - cumhaz; dev_res = sign(mart) * sqrt(-2 * (mart + event * log(cumhaz)))
Try this function
Implementations
Excel
Martingale and survival deviance residuals from named event and hazard cells
With EventInd (1 or 0) and CumHaz named, the formulas return the martingale residual and the survival deviance residual, held in MartRes and SurvDevRes.
=EventInd-CumHaz; =SIGN(MartRes)*SQRT(-2*(MartRes+EventInd*LN(EventInd-MartRes)))
Assumptions
One record per patient with the cumulative hazard from the fitted model
H is the fitted cumulative hazard at the patient's own observed time, from a Cox model with its baseline hazard or from a parametric survival model, and each patient has a single record; with several records per patient the martingale residuals are first summed within the patient.
Positive cumulative hazard for the survival deviance residual
H is above zero, which holds for any patient followed for some time under a model with a positive hazard; the sign is written M / |M| in the Math line, so the calculator cannot show the case M = 0.
Worked examples
Patient who died at month 1 under an exponential hazard of 0.15 a month
The fitted cumulative hazard is 0.15, so M = 0.85 and the residual is +1.447, the largest of the four patients, as in the article.
delta = 1; H = 0.15; M = 0.85; r_D = 1.447
Patient censored at month 5 under an exponential hazard of 0.15 a month
The censored patient has H = 0.75, M = minus 0.75 and residual minus 1.225, minus the square root of 1.5, as in the article.
delta = 0; H = 0.75; M = -0.75; r_D = -1.225
Patient who died at month 11 under an exponential hazard of 0.15 a month
The late death has H = 1.65 and M = minus 0.65, further from zero than the month 3 death's +0.55 on the martingale scale, but its deviance residual, minus 0.546, is smaller in size than that patient's +0.705, as in the article.
delta = 1; H = 1.65; M = -0.65; r_D = -0.546
Common errors
Using normal cut-offs when censoring is heavy
Xue and Schifano report Therneau and colleagues' finding that the survival deviance residual is approximately normal with less than 25 per cent censoring, but that with more than 40 per cent too many points lie near 0, and Nardi and Schemper's finding that censoring often makes its distribution bimodal; a cut-off of plus or minus 1.96 is then unreliable for immature data.
Reading censored long survivors as poorly fitted
A censored patient's residual is minus the square root of 2H, which grows in size with follow-up by construction: the patient censored at month 5 has minus 1.225 without being an outlier, so large negative values among censored patients carry little information on their own.
Excluding a patient because the deviance residual is large
NICE DSU TSD 14 concludes that data points should be excluded only when they can be clearly shown to be erroneous outliers, so a large residual is a reason to check the record and the model, not to drop the patient from the survival analysis.
Sources
Stata martingale-like and deviance residuals after a parametric survival model
StataCorp. Stata Survival Analysis Reference Manual, Release 19: streg postestimation. College Station, TX: Stata Press; 2025 (full text read). Methods and formulas: the Cox-Snell residual is the estimated cumulative hazard H(t) = minus log S(t) at the observed time; the martingale-like residual is the failure indicator minus the Cox-Snell residual, takes values from minus infinity to 1 and is not symmetric about zero; the deviance residual is sign(M) times the square root of minus 2{M + d log(d minus M)}, a scaling intended to make it symmetric about zero.
Stata deviance residuals after a Cox model and their sum
StataCorp. Stata Survival Analysis Reference Manual, Release 19: stcox postestimation. College Station, TX: Stata Press; 2025 (full text read). Methods and formulas: martingale residuals lie in the range from minus infinity to 1; deviance residuals are calculated as sign(r_M) times the square root of minus 2{r_M + delta log(delta minus r_M)}, are expected to be symmetric about zero and do not necessarily sum to zero. Example 6: plots against the linear predictor, survival time, rank order of survival or observation number help identify aberrant observations.
Normality of survival deviance residuals under censoring
Xue Y, Schifano ED. Diagnostics for the Cox model. Communications for Statistical Applications and Methods. 2017;24(6):583-604. doi:10.29220/CSAM.2017.24.6.583 (full text read). Section 3.3 (Outlying observations): Therneau and colleagues concluded that with less than 25 per cent censoring the deviance residual is approximately normally distributed and that with censoring above 40 per cent too many points lie near 0, although the residuals remain symmetrised; Nardi and Schemper found its empirical distribution often becomes bimodal because of censoring.
TSD 14 on excluding survival data points as outliers
Latimer N. NICE DSU Technical Support Document 14: Survival analysis for economic evaluations alongside clinical trials, extrapolation with patient-level data. Sheffield: Decision Support Unit, ScHARR, University of Sheffield; 2011, last updated March 2013 (full text read). Section 5 (Review conclusions), conclusion 8: excluding data points should only be undertaken when it can be clearly demonstrated that certain points are erroneous outliers, and unless a very clear rationale is offered all data should be included in the survival analysis.
Canonical Identity
Stable URI · Machine-readable · Resolvable · CC BY 4.0