Fix r2() error for glmmTMB negative-binomial models without random effects - #924
Open
raffaelemancuso wants to merge 1 commit into
Open
Fix r2() error for glmmTMB negative-binomial models without random effects#924raffaelemancuso wants to merge 1 commit into
r2() error for glmmTMB negative-binomial models without random effects#924raffaelemancuso wants to merge 1 commit into
Conversation
Contributor
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
r2.glmmTMB() had branches for the linear, logit, beta-binomial, binomial, Poisson, zero-inflated, ordered-beta and beta families, but none for the negative-binomial family. A non-mixed nbinom1/nbinom2 model therefore fell through to the final else and stopped with "`r2()` does not support models of class `glmmTMB` without random effects and from nbinom2-family with log-link-function", which also broke model_performance(). Add a negative-binomial branch that returns McFadden's R2 (mirroring the beta-binomial branch); Nagelkerke's is unstable here because the null-model refit finds a different dispersion and can yield negative values. The branch is guarded with !is_zero_inflated so zero-inflated negbin models still route to r2_zeroinflated().
raffaelemancuso
force-pushed
the
fix/r2-glmmtmb-negbin-no-ranef
branch
from
July 23, 2026 10:06
68fb0d8 to
013a033
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes performance::r2() for glmmTMB negative-binomial (nbinom1/nbinom2) models without random effects, which previously errored due to falling through the non-mixed glmmTMB family dispatch. The fix adds an explicit negative-binomial branch that uses McFadden’s pseudo-R² (via r2_mcfadden()), keeping zero-inflated models routed to r2_zeroinflated().
Changes:
- Add a non-mixed
glmmTMBnegative-binomial branch inr2.glmmTMB()to return McFadden’s R². - Add a regression test covering
nbinom2(previously failing) andnbinom1. - Document the behavior change in
NEWS.md.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| R/r2.R | Adds a negative-binomial (non-zero-inflated) handling branch for non-mixed glmmTMB models, returning r2_mcfadden(). |
| tests/testthat/test-r2_mcfadden.R | Adds regression coverage ensuring r2() works for glmmTMB nbinom1/nbinom2 without random effects and matches expected outputs. |
| NEWS.md | Notes that r2()/model_performance() no longer errors for these models and returns McFadden’s R². |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
r2()(and thereforemodel_performance()) errors for aglmmTMBnegative-binomial model (
nbinom1/nbinom2) that has no random effects:Cause
For non-mixed models,
r2.glmmTMB()walks anif / else ifchain with branchesfor the linear, logit, beta-binomial, binomial, Poisson, zero-inflated,
ordered-beta and beta families. There is no branch for the negative-binomial
family, so a non-mixed negbin model falls through to the final
else, whichraises the error above.
Fix
Add a negative-binomial branch that returns McFadden's R² (mirroring the
existing beta-binomial branch). Nagelkerke's R² — used by the Poisson branch —
is unstable here: the null-model refit lands on a different dispersion parameter
and can return nonsensical negative values.
The branch is guarded with
!info$is_zero_inflatedso zero-inflated negbinmodels keep routing to
r2_zeroinflated().Tests
Added a regression test in
test-r2_mcfadden.Rcoveringnbinom2(whichpreviously errored) and
nbinom1, checking thatr2()agrees withr2_mcfadden()and pinning the expected values.Existing behaviour is unchanged and verified:
test-r2.Randtest-r2_mcfadden.Rpass in full.