Skip to content

Commit

Permalink
add nbinom2
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Jun 12, 2024
1 parent bfd87e3 commit 9529246
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
18 changes: 13 additions & 5 deletions R/compute_variances.R
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,8 @@
beta = mu,
ordbeta = stats::plogis(mu),
poisson = ,
nbinom1 = sqrt(exp(mu + 0.5 * as.vector(revar_null))),
nbinom1 = ,
nbinom2 = exp(mu + 0.5 * as.vector(revar_null)),
exp(mu)
)
}
Expand All @@ -701,11 +702,11 @@

# (zero-inflated) negative binomial ----
# --------------------------------------
nbinom1 = sig,
nbinom1 = ,
nbinom2 = sig,
`zero-inflated negative binomial` = ,
`negative binomial` = ,
genpois = ,
nbinom2 = .variance_family_nbinom(x, mu, sig, faminfo),
genpois = .variance_family_nbinom(x, mu, sig, faminfo),
truncated_nbinom2 = stats::family(x)$variance(mu, sig),

# other distributions ----
Expand All @@ -726,7 +727,14 @@
"Model's distribution-specific variance is negative. Results are not reliable."
)
}
vv / mu^2

# now compute cvsquared
switch(faminfo$family,
nbinom2 = (1 / mu) + (1 / sig),
poisson = ,
nbinom1 = vv / mu,
vv / mu^2
)
},
error = function(x) {
if (verbose) {
Expand Down
36 changes: 35 additions & 1 deletion tests/testthat/test-r2_nakagawa_MuMIn.R
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ test_that("lme4, Poisson", {


# ==============================================================================
# Poisson mixed models, glmmTMB
# neg-binomial1 mixed models, glmmTMB
# ==============================================================================

test_that("glmmTMB, Nbinom1", {
Expand Down Expand Up @@ -428,3 +428,37 @@ test_that("glmmTMB, Nbinom1", {
expect_equal(out1[1, "R2m"], out2$R2_marginal, ignore_attr = TRUE, tolerance = 1e-4)
expect_equal(out1[1, "R2c"], out2$R2_conditional, ignore_attr = TRUE, tolerance = 1e-4)
})


# ==============================================================================
# neg-binomial2 mixed models, glmmTMB
# ==============================================================================

test_that("glmmTMB, Nbinom2", {
# dataset ---------------------------------
data(Salamanders, package = "glmmTMB")

# no random slopes
m <- glmmTMB::glmmTMB(
count ~ mined + spp + (1 | site),
family = glmmTMB::nbinom2(),
data = Salamanders
)
out1 <- suppressWarnings(MuMIn::r.squaredGLMM(m))
out2 <- performance::r2_nakagawa(m)
# matches theoretical values
expect_equal(out1[2, "R2m"], out2$R2_marginal, ignore_attr = TRUE, tolerance = 1e-4)
expect_equal(out1[2, "R2c"], out2$R2_conditional, ignore_attr = TRUE, tolerance = 1e-4)

# with random slopes
m <- suppressWarnings(glmmTMB::glmmTMB(
count ~ mined + spp + cover + (1 + cover | site),
family = glmmTMB::nbinom2(),
data = Salamanders
))
out1 <- suppressWarnings(MuMIn::r.squaredGLMM(m))
out2 <- performance::r2_nakagawa(m, tolerance = 1e-8)
# matches theoretical values
expect_equal(out1[2, "R2m"], out2$R2_marginal, ignore_attr = TRUE, tolerance = 1e-1)
expect_equal(out1[2, "R2c"], out2$R2_conditional, ignore_attr = TRUE, tolerance = 1e-1)
})

0 comments on commit 9529246

Please sign in to comment.