Skip to content

Scale dev_gamma() by shape so deviance residuals have unit scaling (#170) - #171

Merged
nehill197 merged 1 commit into
mainfrom
fix-dev-gamma
Oct 23, 2026
Merged

nehill197 merged 1 commit into
mainfrom
fix-dev-gamma

Conversation

@nehill197

Copy link
Copy Markdown
Member

Summary

  • dev_gamma() computed the classic Gamma unit deviance, 2 * [(x - mu)/mu - log(x/mu)], which omits shape by construction — it matches residuals.glm(type = "deviance"), but leaves res_gamma(type = "dev") residuals smaller than dev_norm()/dev_student()'s by a factor of sqrt(shape), making a standard ±2 screening threshold meaningless for Gamma models.
  • Fixes this by multiplying the deviance by shape, recovering the full 2 * (log_lik_saturated - log_lik_fitted) — consistent with how every other dev_*() function in the package is computed, and verified independently against base R's glm()/rstandard() machinery.
  • Updated the hardcoded expected values in test-dev.R/test-res.R accordingly, including removing a # hack to divide multiply by 1 / shape workaround in a test that had already noticed this mismatch.

Closes #170.

Test plan

  • devtools::test() — full suite passes (3455/3455)
  • Verified the new formula matches shape * 2 * (log_lik_gamma(x, shape, shape/x) - log_lik_gamma(x, shape, rate)) to floating-point precision
  • Simulated under a correctly-specified Gamma model and confirmed sd(res_gamma(x, shape, rate, type = "dev")) ≈ 1, matching dev_norm()/dev_student()'s behavior

🤖 Generated with Claude Code

dev_gamma() omitted shape from the unit deviance, leaving res_gamma(type =
"dev") residuals smaller than dev_norm()/dev_student()'s by ~sqrt(shape) and
unusable for a standard +/-2 screening threshold. Multiplying by shape
recovers the full 2 * (log_lik_sat - log_lik_fit), consistent with the rest
of the dev_*() family.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@nehill197 nehill197 changed the title Scale dev_gamma() by shape so deviance residuals match dev_norm() (#170) Scale dev_gamma() by shape so deviance residuals have unit scaling (#170) Oct 23, 2026
@nehill197
nehill197 requested a review from joethorley October 23, 2026 18:52
@nehill197
nehill197 merged commit 647c0b4 into main Oct 23, 2026
11 checks passed
@nehill197
nehill197 deleted the fix-dev-gamma branch October 23, 2026 22:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dev_gamma() leaves out the shape, so Gamma deviance residuals are not on the same scale as dev_norm()

2 participants