vincentarelbundock / marginaleffects

R package to compute and plot predictions, slopes, marginal means, and comparisons (contrasts, risk ratios, odds, etc.) for over 100 classes of statistical and ML models. Conduct linear and non-linear hypothesis tests, or equivalence tests. Calculate uncertainty estimates using the delta method, bootstrapping, or simulation-based inference
https://marginaleffects.com
Other
432 stars 45 forks source link

Issue with multimembership grouping terms? #1048

Closed andreifoldes closed 5 months ago

andreifoldes commented 6 months ago

Greetings,

  1. Concise description of the bug I have issues running marginaleffect's datagrid and predictions functions on my wacky regression model; see error messages below.

  2. Minimal reproducible example using publicly available data and the bare minimum code and libraries needed to reproduce the bug.

    • Consider using the mtcars dataset which is distributed by default with R, or one of the [CSV files from the RDatasets archive.]
mtcars$w <- 1
test_mod <- brm(mpg  ~ 1 + hp  + (1|cyl)  +
                (1|carb:am :hp ) +
                (1|mm(carb, am , weights = cbind(w, w), scale=FALSE)) +
                (1|mm(carb:cyl, am :cyl, weights = cbind(w, w), scale=FALSE)),
              data = mtcars, family = distY, chains = chains,
              iter = iterations, control = list(adapt_delta = 0.99, max_treedepth = 15),
              cores = 4, seed = 12345)

library(marginaleffects)
datagrid(model=test_mod, hp=1)
> datagrid(model=test_mod, hp=1)
Error in if (grepl("gr\\((.*)\\)", re)) { : the condition has length > 1

predictions(test_mod, type = "prediction", ndraws = 10, re_formula = NA)
> Error: Unable to extract the data from model of class `brmsfit`. This can happen in a variety of cases, such as when a
`marginaleffects` package function is called from inside a user-defined function, or using an `*apply()`-style operation on
a list. Please supply a data frame explicitly via the `newdata` argument.
  1. sessionInfo() output
    
    R version 4.2.2 (2022-10-31 ucrt)
    Platform: x86_64-w64-mingw32/x64 (64-bit)
    Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale: [1] LC_COLLATE=English_United Kingdom.utf8 LC_CTYPE=English_United Kingdom.utf8 LC_MONETARY=English_United Kingdom.utf8 [4] LC_NUMERIC=C LC_TIME=English_United Kingdom.utf8

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

other attached packages: [1] brmsmargins_0.2.0 marginaleffects_0.16.0 ggcorrplot_0.1.4.1 glue_1.6.2 brms_2.20.4
[6] Rcpp_1.0.10 lubridate_1.9.1 forcats_1.0.0 stringr_1.5.0 dplyr_1.1.4
[11] purrr_1.0.2 readr_2.1.3 tidyr_1.3.0 tibble_3.2.1 ggplot2_3.5.0
[16] tidyverse_2.0.0

loaded via a namespace (and not attached): [1] backports_1.4.1 plyr_1.8.8 igraph_1.5.1 splines_4.2.2 gmp_0.7-2 listenv_0.9.0
[7] crosstalk_1.2.0 TH.data_1.1-2 rstantools_2.3.1.1 inline_0.3.19 digest_0.6.31 htmltools_0.5.4
[13] fansi_1.0.4 magrittr_2.0.3 checkmate_2.3.0 extraoperators_0.3.0 tzdb_0.3.0 globals_0.16.2
[19] modelr_0.1.11 RcppParallel_5.1.7 matrixStats_1.0.0 vroom_1.6.3 xts_0.13.1 sandwich_3.0-2
[25] timechange_0.2.0 prettyunits_1.2.0 colorspace_2.1-0 xfun_0.39 callr_3.7.3 crayon_1.5.2
[31] jsonlite_1.8.5 lme4_1.1-31 survival_3.5-5 zoo_1.8-11 gtable_0.3.4 emmeans_1.8.9
[37] V8_4.3.2 sjstats_0.18.2 sjmisc_2.8.9 distributional_0.3.2 car_3.1-2 pkgbuild_1.4.2
[43] rstan_2.32.3 Rmpfr_0.9-3 abind_1.4-5 scales_1.3.0 mvtnorm_1.1-3 rstatix_0.7.2
[49] ggeffects_1.3.2 miniUI_0.1.1.1 xtable_1.8-4 performance_0.10.8 bit_4.0.5 stats4_4.2.2
[55] StanHeaders_2.26.28 DT_0.30 collapse_2.0.10 htmlwidgets_1.6.2 threejs_0.3.3 posterior_1.5.0
[61] ellipsis_0.3.2 pkgconfig_2.0.3 loo_2.6.0 farver_2.1.1 utf8_1.2.3 tidyselect_1.2.0
[67] rlang_1.1.2 reshape2_1.4.4 later_1.3.1 munsell_0.5.0 tools_4.2.2 cli_3.6.1
[73] generics_0.1.3 sjlabelled_1.2.0 broom_1.0.5 evaluate_0.23 fastmap_1.1.1 yaml_2.3.7
[79] bit64_4.0.5 processx_3.8.1 knitr_1.45 future_1.33.0 nlme_3.1-162 mime_0.12
[85] compiler_4.2.2 bayesplot_1.10.0 shinythemes_1.2.0 rstudioapi_0.15.0 curl_5.2.0 ggsignif_0.6.4
[91] stringi_1.7.12 ps_1.7.5 Brobdingnag_1.2-9 lattice_0.21-8 Matrix_1.6-1.1 nloptr_2.0.3
[97] markdown_1.11 shinyjs_2.1.0 tensorA_0.36.2 vctrs_0.6.4 pillar_1.9.0 lifecycle_1.0.4
[103] furrr_0.3.1 bridgesampling_1.1-2 estimability_1.4.1 data.table_1.14.8 cowplot_1.1.3 insight_0.19.8
[109] httpuv_1.6.11 QuickJSR_1.0.7 R6_2.5.1 promises_1.2.0.1 gridExtra_2.3 parallelly_1.36.0
[115] codetools_0.2-19 boot_1.3-28.1 colourpicker_1.3.0 MASS_7.3-60 gtools_3.9.4 withr_3.0.0
[121] shinystan_2.6.0 multcomp_1.4-25 broom.mixed_0.2.9.4 hms_1.1.3 bayestestR_0.13.1 parallel_4.2.2
[127] grid_4.2.2 sjPlot_2.8.15 coda_0.19-4 minqa_1.2.5 rmarkdown_2.25 carData_3.0-5
[133] ggpubr_0.6.0 shiny_1.7.5.1 base64enc_0.1-3 dygraphs_1.1.1.6



> packageVersion("marginaleffects")
[1] ‘0.16.0’
vincentarelbundock commented 5 months ago

Thanks a lot for the report. This is an upstream bug in the insight package. I have escalated here:

https://github.com/easystats/insight/issues/859

vincentarelbundock commented 5 months ago

FYI, this should now be fixed if you install the development version of insight from Github and restart R:

https://github.com/easystats/insight/issues/859

andreifoldes commented 5 months ago

Much appreciated+

strengejacke commented 5 months ago

@andreifoldes Once insight is built on r-universe (approx. ~1 hour after this post), you can use:

install.packages("insight", repos = "https://easystats.r-universe.dev")

and install insight version 0.19.9.1 (make sure you have that version using packageVersion("insight")). Then it should work.