marginaleffects icon indicating copy to clipboard operation
marginaleffects copied to clipboard

`inferences()` with `glmer()` fails.

Open davidaarmstrong opened this issue 11 months ago • 2 comments

I came across a bug when using inferences() after agv_comparisons() for a model estimated with lme4::glmer(). A minimal reproducible example is below:

library(dplyr)
library(lme4)
library(marginaleffects)
gm1 <- glmer(cbind(incidence, size - incidence) ~ period + (1 | herd),
             data = cbpp, family = binomial)

avg_comparisons(gm1, variables = "period") 
#> 
#>    Term Contrast Estimate Std. Error     z Pr(>|z|)    S  2.5 %  97.5 %
#>  period    2 - 1   -0.118     0.0354 -3.33   <0.001 10.2 -0.187 -0.0484
#>  period    3 - 1   -0.128     0.0356 -3.60   <0.001 11.6 -0.198 -0.0586
#>  period    4 - 1   -0.156     0.0369 -4.24   <0.001 15.5 -0.229 -0.0841
#> 
#> Columns: term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high 
#> Type:  response

avg_comparisons(gm1, variables = "period") %>% inferences(method="simulation")
#> Warning in class(model) <- c("inferences_simulation", class(model)): Setting
#> class(x) to multiple strings ("inferences_simulation", "glmerMod", ...); result
#> will no longer be an S4 object
#> Warning: Parameters can't be retrieved for objects of class
#>   `notSubsettableError`.
#> Warning: Unable to extract a variance-covariance matrix from this model.
#> Error in array(x, c(length(x), 1L), if (!is.null(names(x))) list(names(x), : 'data' must be of a vector type, was 'NULL'

Created on 2024-03-20 with reprex v2.0.2

Output from sessionInfo():

R version 4.3.3 (2024-02-29)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Sonoma 14.4

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/Toronto
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] vctrs_0.6.5       cli_3.6.2         knitr_1.44        rlang_1.1.3       xfun_0.40         processx_3.8.3    purrr_1.0.2       styler_1.10.2     glue_1.7.0        clipr_0.8.0      
[11] htmltools_0.5.6.1 ps_1.7.6          fansi_1.0.6       rmarkdown_2.25    R.cache_0.16.0    evaluate_0.23     tibble_3.2.1      fastmap_1.1.1     yaml_2.3.7        lifecycle_1.0.4  
[21] compiler_4.3.3    fs_1.6.3          pkgconfig_2.0.3   rstudioapi_0.15.0 R.oo_1.25.0       R.utils_2.12.2    digest_0.6.34     R6_2.5.1          reprex_2.0.2      utf8_1.2.4       
[31] pillar_1.9.0      callr_3.7.5       magrittr_2.0.3    R.methodsS3_1.8.2 tools_4.3.3       withr_3.0.0      

davidaarmstrong avatar Mar 20 '24 13:03 davidaarmstrong