Skip to content
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

null_model() for zero-inflated models. #891

Closed
strengejacke opened this issue Jun 14, 2024 · 2 comments · Fixed by #892
Closed

null_model() for zero-inflated models. #891

strengejacke opened this issue Jun 14, 2024 · 2 comments · Fixed by #892
Assignees
Labels
3 investigators ❔❓ Need to look further into this issue

Comments

@strengejacke
Copy link
Member

strengejacke commented Jun 14, 2024

Presumably it would be

formula = count ~ 1 + (1 | persons),
 ziformula =    ~1 + (1 | persons)

?

Originally posted by @bbolker in #889 (comment)

@strengejacke strengejacke added the 3 investigators ❔❓ Need to look further into this issue label Jun 14, 2024
@strengejacke
Copy link
Member Author

This code might work:

data(fish, package = "insight")
m1 <- glmmTMB::glmmTMB(
  count ~ child + camper + (1 | persons),
  ziformula = ~ child + camper + (1 | persons),
  data = fish,
  family = poisson()
)

args <- lapply(insight::get_call(m1), deparse)[-1]
formula_args <- endsWith(names(args), "formula")

resp <- insight::find_response(m1)
args[formula_args] <- lapply(names(args[formula_args]), function(f_names) {
  f <- args[[f_names]]
  re_string <- sapply(insight:::.findbars(stats::as.formula(f)), insight::safe_deparse)
  if (!insight::is_empty_object(re_string)) {
    if (startsWith(f_names, "zi")) {
      stats::reformulate(paste0("(", re_string, ")"), response = NULL)
    } else {
      stats::reformulate(paste0("(", re_string, ")"), response = resp)
    }
  } else {
    stats::as.formula(f)
  }
})

args[!formula_args] <- lapply(args[!formula_args], str2lang)
args
#> $formula
#> count ~ (1 | persons)
#> <environment: 0x0000022c396853f8>
#> 
#> $data
#> fish
#> 
#> $family
#> poisson()
#> 
#> $ziformula
#> ~(1 | persons)
#> <environment: 0x0000022c3965f588>
#> 
#> $dispformula
#> ~1
#> <environment: 0x0000022c39118cf8>
do.call(glmmTMB::glmmTMB, args)
#> Formula:          count ~ (1 | persons)
#> Zero inflation:         ~(1 | persons)
#> Data: fish
#>       AIC       BIC    logLik  df.resid 
#> 1880.6558 1894.7417 -936.3279       246 
#> Random-effects (co)variances:
#> 
#> Conditional model:
#>  Groups  Name        Std.Dev.
#>  persons (Intercept) 0.8114  
#> 
#> Zero-inflation model:
#>  Groups  Name        Std.Dev.
#>  persons (Intercept) 8.98e-05
#> 
#> Number of obs: 250 / Conditional model: persons, 4 / Zero-inflation model: persons, 4
#> 
#> Fixed Effects:
#> 
#> Conditional model:
#> (Intercept)  
#>       1.599  
#> 
#> Zero-inflation model:
#> (Intercept)  
#>      0.1763

Created on 2024-06-14 with reprex v2.1.0

@bwiernik
Copy link
Contributor

Lgtm

strengejacke added a commit that referenced this issue Jun 14, 2024
* `null_model()` for zero-inflated models.
Fixes #891

* also work for no random effects in zi part

* fix tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3 investigators ❔❓ Need to look further into this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants