Skip to content

Commit b68a62a

Browse files
authored
Merge branch 'main' into fix-glmmtmb-homogeneity
2 parents 2425eea + 4ef3e39 commit b68a62a

4 files changed

Lines changed: 28 additions & 1 deletion

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: performance
33
Title: Assessment of Regression Models Performance
4-
Version: 0.17.1.1
4+
Version: 0.17.1.2
55
Authors@R:
66
c(person(given = "Daniel",
77
family = "Lüdecke",

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
* `check_collinearity()` now properly warns when the `vcov` matrix is rank
1010
deficient (#922).
1111

12+
* `r2()` (and hence `model_performance()`) no longer errors for `glmmTMB`
13+
negative-binomial models (`nbinom1`/`nbinom2`) without random effects, and now
14+
returns McFadden's R2 for them.
15+
1216
# performance 0.17.1
1317

1418
## Changes

R/r2.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,11 @@ r2.glmmTMB <- function(model, ci = NULL, tolerance = 1e-5, verbose = TRUE, ...)
544544
names(out$R2_Nagelkerke) <- "Nagelkerke's R2"
545545
attr(out, "model_type") <- "Generalized Linear"
546546
class(out) <- c("r2_pseudo", class(out))
547+
} else if (info$is_negbin && !info$is_zero_inflated) {
548+
# negative-binomial regression uses McFadden's R2. Nagelkerke's is unstable
549+
# here (the null-model refit finds a different dispersion, which can yield
550+
# nonsensical negative values), so mirror the beta-binomial branch above.
551+
out <- r2_mcfadden(model)
547552
} else if (info$is_zero_inflated) {
548553
# zero-inflated models use the default method
549554
out <- r2_zeroinflated(model)

tests/testthat/test-r2_mcfadden.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,21 @@ withr::with_environment(
6969
})
7070
}
7171
)
72+
73+
test_that("r2, glmmTMB negative-binomial without random effects", {
74+
skip_if_not_installed("glmmTMB")
75+
set.seed(101)
76+
dd <- data.frame(x = rnorm(200))
77+
dd$y <- rnbinom(200, mu = exp(0.5 + 1 * dd$x), size = 2)
78+
79+
# nbinom2 previously errored ("does not support models of class `glmmTMB`
80+
# without random effects and from nbinom2-family ..."); now returns McFadden's.
81+
m2 <- glmmTMB::glmmTMB(y ~ 1 + x, data = dd, family = glmmTMB::nbinom2())
82+
out <- r2(m2)
83+
expect_equal(out$R2, r2_mcfadden(m2)$R2, tolerance = 1e-4, ignore_attr = TRUE)
84+
expect_equal(out$R2, 0.1521543, tolerance = 1e-4, ignore_attr = TRUE)
85+
86+
# nbinom1 is handled by the same branch.
87+
m1 <- glmmTMB::glmmTMB(y ~ 1 + x, data = dd, family = glmmTMB::nbinom1())
88+
expect_equal(r2(m1)$R2, 0.1406573, tolerance = 1e-4, ignore_attr = TRUE)
89+
})

0 commit comments

Comments
 (0)