Skip to content

Fix r2() error for glmmTMB negative-binomial models without random effects - #924

Open
raffaelemancuso wants to merge 1 commit into
easystats:mainfrom
raffaelemancuso:fix/r2-glmmtmb-negbin-no-ranef
Open

Fix r2() error for glmmTMB negative-binomial models without random effects#924
raffaelemancuso wants to merge 1 commit into
easystats:mainfrom
raffaelemancuso:fix/r2-glmmtmb-negbin-no-ranef

Conversation

@raffaelemancuso

Copy link
Copy Markdown

Problem

r2() (and therefore model_performance()) errors for a glmmTMB
negative-binomial model (nbinom1 / nbinom2) that has no random effects:

library(glmmTMB)
m <- glmmTMB(count ~ x1 + x2, data = df, family = nbinom2(link = "log"))
performance::r2(m)
#> Error: `r2()` does not support models of class `glmmTMB` without random
#>   effects and from nbinom2-family with log-link-function.

Cause

For non-mixed models, r2.glmmTMB() walks an if / else if chain with branches
for 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, which
raises 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_inflated so zero-inflated negbin
models keep routing to r2_zeroinflated().

} else if (info$is_negbin && !info$is_zero_inflated) {
  out <- r2_mcfadden(model)
}

Tests

Added a regression test in test-r2_mcfadden.R covering nbinom2 (which
previously errored) and nbinom1, checking that r2() agrees with
r2_mcfadden() and pinning the expected values.

Existing behaviour is unchanged and verified:

  • Poisson without random effects still uses Nagelkerke's R².
  • Mixed negbin models still use Nakagawa's marginal/conditional R².
  • test-r2.R and test-r2_mcfadden.R pass in full.

@gemini-code-assist

Copy link
Copy Markdown
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().

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 glmmTMB negative-binomial branch in r2.glmmTMB() to return McFadden’s R².
  • Add a regression test covering nbinom2 (previously failing) and nbinom1.
  • 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.

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.

2 participants