Signature
d = 2 * ((y - mu) / mu - log(y / mu)); r_D = (y - mu) / abs(y - mu) * sqrt(d)
| Inputs | Definition | Unit |
|---|---|---|
y | Positive observed cost of the patient | pounds per year |
mu | Mean cost the fitted model predicts for the patient, from any link function and covariates | pounds per year |
d | Contribution of the patient to the model deviance, never negative | none |
|---|---|---|
r_D | Square root of the unit deviance carrying the sign of y minus mu | 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: gamma deviance residuals, unit deviances and model deviance for a set of patient costs
Computes the gamma deviance residuals for a whole set of patients, with the Pearson residuals, the unit deviances, the model deviance and the rank of each patient by the size of the deviance residual, so the worst-fitted patients can be listed in input order. The inputs differ from the formula's: a vector of positive costs and, optionally, a vector of fitted means; when the fitted means are omitted the intercept-only fit, the sample mean, is used.
Inputs and outputs:
y: Positive observed costs, one per patient. Unit: pounds per year.;mu: Fitted mean costs from the gamma GLM, one per patient (default: the sample mean). Unit: pounds per year.;pearson: Pearson residuals (y minus mu) / mu. Unit: none.;unit_dev: Gamma unit deviances (HE-FM-DEVR-001). Unit: none.;dev_res: Deviance residuals. Unit: none.;rank_abs_dev: Rank of each patient by the size of the deviance residual, 1 for the largest (R only). Unit: rank.;deviance: Model deviance, the sum of the unit deviances. Unit: none.;sum_dev_res: Sum of the deviance residuals. Unit: none.Assumption: Costs are positive and the fitted means come from the gamma model being checked, with prior weights of 1.
Worked example (Three annual costs, article example): Costs of 1,000, 2,000 and 6,000 pounds with the intercept-only fitted mean of 3,000 give Pearson residuals of minus 0.667, minus 0.333 and +1.000, deviance residuals of minus 0.929, minus 0.380 and +0.783 (ranks 1, 3 and 2), a model deviance of 1.622 and a sum of deviance residuals of minus 0.526, as in the article.
y = 1000, 2000, 6000; mu = 3000; deviance = 1.622; sum_dev_res = -0.526Worked example (Same costs against a fitted value of 2,000): If the fitted value were the median, 2,000 pounds, the Pearson residuals would be minus 0.5, 0 and +2.0 and would no longer sum to zero, the sign that the value is not the intercept-only gamma fit (computed here for illustration).
y = 1000, 2000, 6000; mu = 2000; pearson sum = 1.5Excel: With the costs in the range CostVec and the fitted means in FitVec, the array formula
=2*((CostVec-FitVec)/FitVec-LN(CostVec/FitVec))returns the unit deviances into UnitDevVec,=SIGN(CostVec-FitVec)*SQRT(UnitDevVec)the deviance residuals into DevResVec,=SUM(UnitDevVec)the model deviance and=XMATCH(ABS(DevResVec),SORT(ABS(DevResVec),,-1))the ranks (Excel 365).R:
gamma_dev <- function(y, mu = rep(mean(y), length(y))) { d <- 2*((y-mu)/mu-log(y/mu)); r <- sign(y-mu)*sqrt(d); list(table = data.frame(y = y, mu = mu, pearson = (y-mu)/mu, unit_dev = d, dev_res = r, rank_abs_dev = rank(-abs(r))), deviance = sum(d), sum_dev_res = sum(r)) }Base R only;gamma_dev(c(1000, 2000, 6000))returns the article example in input order.Python:
def gamma_dev(y, mu=None): mu = mu or [sum(y)/len(y)]*len(y); d = [2*((a-m)/m-math.log(a/m)) for a, m in zip(y, mu)]; r = [math.copysign(math.sqrt(x), a-m) if a != m else 0.0 for x, a, m in zip(d, y, mu)]; return {"y": list(y), "mu": list(mu), "pearson": [(a-m)/m for a, m in zip(y, mu)], "unit_dev": d, "dev_res": r, "deviance": sum(d), "sum_dev_res": sum(r)}Needsimport math; returns the same values as the R function apart from the ranks.Test (Intercept-only fitted value is the sample mean): For an intercept-only gamma fit the Pearson residuals sum to zero, so with FitVec holding the fitted value the sum of (CostVec minus FitVec) / FitVec is zero. Expected result: TRUE. FALSE shows the median, 2,000 pounds, used as the fitted value, which gives a sum of 1.5. Excel check:
=ABS(SUMPRODUCT((CostVec-FitVec)/FitVec))<1E-9*COUNT(CostVec)Common error (Comparing residuals across differently scaled fits): The residuals are unscaled, so a patient's value can be compared with other patients in the same fit but not with residuals from a model of a different outcome or family.
Source: StataCorp. Stata Base Reference Manual, Release 19: glm. College Station, TX: Stata Press; 2025 (full text read). Methods and formulas; StataCorp. Stata Base Reference Manual, Release 19: glm postestimation. College Station, TX: Stata Press; 2025 (full text read). Methods and formulas.
pearson = (y - mu) / mu; unit_dev = 2 * ((y - mu) / mu - log(y / mu)); dev_res = sign(y - mu) * sqrt(unit_dev); deviance = sum(unit_dev)
Try this function
Implementations
Excel
Gamma unit deviance and deviance residual from named cost cells
With CostObs and CostFit named, the formulas return the gamma unit deviance and the deviance residual, held in GamUnitDev and GamDevRes. SIGN returns 0 when the cost equals its fitted mean.
=2*((CostObs-CostFit)/CostFit-LN(CostObs/CostFit)); =SIGN(CostObs-CostFit)*SQRT(GamUnitDev)
Assumptions
Positive costs with fitted means from a gamma GLM
Both the observed cost and the fitted mean are positive; the log term is undefined for a cost of zero, so zero costs belong to the first part of a two-part model and the gamma GLM to the positive costs.
Unweighted and unscaled gamma deviance residuals
Every patient has a prior weight of 1 and the residual is not divided by the estimated dispersion, as in Stata's glm, whose overall deviance is the weighted sum of the unit deviances; residuals are therefore compared within one fit, not across models.
Worked examples
Patient costing 6,000 pounds against a fitted mean of 3,000
An intercept-only gamma GLM with a log link fits the sample mean, 3,000 pounds, to all three patients; for the 6,000 pound patient the unit deviance is 2 x (1 minus 0.6931) = 0.6137 and the residual +0.783, as in the article.
y = 6000; mu = 3000; d = 0.6137; r_D = 0.783
Patient costing 1,000 pounds against a fitted mean of 3,000
The cheapest patient has unit deviance 0.8639 and residual minus 0.929, the largest in size of the three although the Pearson residual, minus 0.667, is smaller in size than the expensive patient's +1.000, as in the article.
y = 1000; mu = 3000; d = 0.8639; r_D = -0.929
Patient costing 2,000 pounds against a fitted mean of 3,000
The middle patient has unit deviance 0.1443 and residual minus 0.380; the three unit deviances add to the model deviance of 1.622, as in the article.
y = 2000; mu = 3000; d = 0.1443; r_D = -0.380
Common errors
Ranking cost outliers by the Pearson residual
In the article's three-patient example the Pearson residual puts the 6,000 pound patient first (+1.000) and the deviance residual puts the 1,000 pound patient first (minus 0.929), because the gamma deviance judges the ratio of observed to fitted cost on roughly a log scale; Stata's glm postestimation manual notes that Pearson residuals often have markedly skewed distributions for non-normal families.
Computing gamma deviance residuals for zero costs
The log of y / mu is undefined at a cost of zero, so the residual cannot be computed; Mihaylova and colleagues describe two-part models in which a logit or probit first part estimates the probability of any cost and a GLM models the positive costs.
Expecting the gamma deviance residuals to sum to zero
The Pearson residuals of an intercept-only fit sum to zero, but in the article's example the deviance residuals sum to about minus 0.526, so a residual plot need not centre on zero and an offset alone does not signal misfit.
Sources
Stata gamma and Poisson unit deviances and the overall GLM deviance
StataCorp. Stata Base Reference Manual, Release 19: glm. College Station, TX: Stata Press; 2025 (full text read). Methods and formulas: the squared deviance residual for the gamma family is minus 2{ln(y/mu) minus (y minus mu)/mu}; for the Poisson family it is 2 mu when y is zero and 2{y ln(y/mu) minus (y minus mu)} otherwise; the overall deviance reported by glm is the weighted sum of the squared deviance residuals.
Stata definition of the GLM deviance residual and the skew of Pearson residuals
StataCorp. Stata Base Reference Manual, Release 19: glm postestimation. College Station, TX: Stata Press; 2025 (full text read). Methods and formulas: the deviance residual is sign(y minus mu) times the square root of the squared deviance residual, and the Pearson residual divides y minus mu by the square root of the family variance function. Options: deviance residuals are recommended by McCullagh and Nelder (1989) and are approximately normally distributed if the model is correct; Pearson residuals often have markedly skewed distributions for non-normal family distributions.
Gamma GLMs for costs and two-part models for zero costs
Mihaylova B, Briggs A, O'Hagan A, Thompson SG. Review of statistical methods for analysing healthcare resources and costs. Health Economics. 2011;20(8):897-916. doi:10.1002/hec.1653 (full text read). Section 1: cost and resource use data often show substantial positive skewness and heavy tails, with a mass at zero for non-users. Section 3.1.3: GLMs are used for costs (for example a gamma specification) and resource use (Poisson and negative binomial). Section 3.1.6: in a two-part model a logit or probit first part estimates the probability of any cost and the second part models costs among those with any.
Canonical Identity
Stable URI · Machine-readable · Resolvable · CC BY 4.0