-
-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test get_variance for zero-inflation models #893
Conversation
What would be expected as R2 result for zero inflated models in comparison to non zero inflated? Would R2 be higher or lower for simple Poisson as opposed to zero inflated Poisson, for example? |
Revisiting this code, the question is whether the last line should be # For zero-inflated poisson models, the
# distributional variance is based on Zuur et al. 2012
# ----------------------------------------------
.variance_zip <- function(model, faminfo, family_var) {
if (inherits(model, "glmmTMB")) {
p <- stats::predict(model, type = "zprob")
mu <- stats::predict(model, type = "conditional")
pvar <- (1 - p) * (mu + p * mu^2)
}
mean(pvar) # var() better here?
} Using # using mean()
data(fish, package = "insight")
model <- glmmTMB::glmmTMB(
count ~ child + camper + (1 | persons),
ziformula = ~ child + camper,
data = fish,
family = poisson()
)
insight::get_variance(model)
#> $var.fixed
#> [1] 1.141135
#>
#> $var.random
#> [1] 1.133478
#>
#> $var.residual
#> [1] 1.03198
#>
#> $var.distribution
#> [1] 1.03198
#>
#> $var.dispersion
#> [1] 0
#>
#> $var.intercept
#> persons
#> 1.133478
performance::r2_nakagawa(model)
#> # R2 for Mixed Models
#>
#> Conditional R2: 0.688
#> Marginal R2: 0.345 Using # using var()
data(fish, package = "insight")
model <- glmmTMB::glmmTMB(
count ~ child + camper + (1 | persons),
ziformula = ~ child + camper,
data = fish,
family = poisson()
)
insight::get_variance(model)
#> $var.fixed
#> [1] 1.141135
#>
#> $var.random
#> [1] 1.133478
#>
#> $var.residual
#> [1] 4.911707
#>
#> $var.distribution
#> [1] 4.911707
#>
#> $var.dispersion
#> [1] 0
#>
#> $var.intercept
#> persons
#> 1.133478
performance::r2_nakagawa(model)
#> # R2 for Mixed Models
#>
#> Conditional R2: 0.317
#> Marginal R2: 0.159 The same model fitted with brms returns (using |
I think, p <- stats::predict(model, type = "zprob")
mu <- stats::predict(model, type = "conditional")
pvar <- (1 - p) * (mu + p * mu^2)
mean(pvar)
# similar to
p <- mean(stats::predict(model, type = "zprob"))
mu <- mean(stats::predict(model, type = "conditional"))
pvar <- (1 - p) * (mu + p * mu^2)
pvar |
Related to #889
TL;DR
We have two questions:
You can ignore the detailed changes in this PR - just the comments and examples shown here are relevant for the discussion!
What did this PR change?
In the current main branch, the dispersion parameter for Poisson and ZI Poisson is set to 1. This PR changes the behaviour for ZI Poisson, returning a different dispersion / variance:
Taking following model, for this particular example, this PR comes closer to a Bayesian model than insight from the main branch.
Current main branch returns
This PR returns
brms returns (marginal R2 only)
Any ideas how to validate the results? @bbolker @bwiernik ?