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
433 stars 45 forks source link

`bife` Support Not Working Properly #949

Closed mreece13 closed 9 months ago

mreece13 commented 10 months ago

bife models are not properly supported. I would be happy to submit an extension but it looks like the methods already exist in R/methods_bife.R, they are just not working correctly. Below is a working reprex using the included data and included example from the bife package.

library(bife)
library(marginaleffects)
dataset <- psid

# Fit a static logit model
mod <- bife(LFP ~ I(AGE^2) + log(INCH) + KID1 + KID2 + KID3 + factor(TIME) | ID, dataset)
slopes(mod)
#> Error: Models of class "bife" are not supported. Supported model classes
#>   include:
#>   
#>   afex_aov, amest, betareg, bglmerMod, bigglm, biglm, blmerMod, bracl,
#>   brglmFit, brmsfit, brnb, clm, clogit, coxph, crch, fixest, flac, flic,
#>   gam, Gam, gamlss, geeglm, glimML, glm, glmerMod, glmmPQL, glmmTMB,
#>   glmrob, glmx, gls, hetprob, hurdle, hxlr, iv_robust, ivpml, ivreg,
#>   Learner, lm, lm_robust, lme, lmerMod, lmerModLmerTest, lmrob, lmRob,
#>   loess, logistf, lrm, mblogit, mclogit, MCMCglmm, mhurdle, mira, mlogit,
#>   model_fit, multinom, negbin, nls, ols, oohbchoice, orm, phyloglm,
#>   phylolm, plm, polr, Rchoice, rlmerMod, rq, scam, selection, speedglm,
#>   speedlm, stanreg, survreg, svyolr, tobit, tobit1, truncreg, workflow,
#>   zeroinfl
#>   
#>   New modeling packages can usually be supported by `marginaleffects` if
#>   they include a working `predict()` method. If you believe that this is
#>   the case, please file a feature request on Github:
#>   https://github.com/vincentarelbundock/marginaleffects/issues

Created on 2023-11-03 with reprex v2.0.2

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.2.2 (2022-10-31) #> os macOS 14.0 #> system aarch64, darwin20 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz America/New_York #> date 2023-11-03 #> pandoc 3.1.1 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> backports 1.4.1 2021-12-13 [1] CRAN (R 4.2.0) #> bife * 0.7.2 2022-08-11 [1] CRAN (R 4.2.0) #> checkmate 2.3.0 2023-10-25 [1] CRAN (R 4.2.2) #> cli 3.6.1 2023-03-23 [1] CRAN (R 4.2.0) #> data.table 1.14.9 2023-07-07 [1] local #> digest 0.6.33 2023-07-07 [1] CRAN (R 4.2.0) #> evaluate 0.22 2023-09-29 [1] CRAN (R 4.2.0) #> fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.2.0) #> Formula 1.2-5 2023-02-24 [1] CRAN (R 4.2.0) #> fs 1.6.3 2023-07-20 [1] CRAN (R 4.2.0) #> generics 0.1.3 2022-07-05 [1] CRAN (R 4.2.0) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.2.0) #> htmltools 0.5.6.1 2023-10-06 [1] CRAN (R 4.2.0) #> insight 0.19.6 2023-10-12 [1] CRAN (R 4.2.0) #> knitr 1.44 2023-09-11 [1] CRAN (R 4.2.0) #> lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.2.0) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.2.0) #> marginaleffects * 0.16.0 2023-10-19 [1] CRAN (R 4.2.0) #> purrr 1.0.2 2023-08-10 [1] CRAN (R 4.2.0) #> R.cache 0.16.0 2022-07-21 [1] CRAN (R 4.2.0) #> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.2.0) #> R.oo 1.25.0 2022-06-12 [1] CRAN (R 4.2.0) #> R.utils 2.12.2 2022-11-11 [1] CRAN (R 4.2.0) #> Rcpp 1.0.11 2023-07-06 [1] CRAN (R 4.2.0) #> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.2.0) #> rlang 1.1.1 2023-04-28 [1] CRAN (R 4.2.0) #> rmarkdown 2.25 2023-09-18 [1] CRAN (R 4.2.0) #> rstudioapi 0.15.0 2023-07-07 [1] CRAN (R 4.2.0) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.2.0) #> styler 1.10.2 2023-08-29 [1] CRAN (R 4.2.0) #> vctrs 0.6.4 2023-10-12 [1] CRAN (R 4.2.0) #> withr 2.5.1 2023-09-26 [1] CRAN (R 4.2.0) #> xfun 0.40 2023-08-09 [1] CRAN (R 4.2.0) #> yaml 2.3.7 2023-01-23 [1] CRAN (R 4.2.0) #> #> [1] /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library #> #> ────────────────────────────────────────────────────────────────────────────── ```
vincentarelbundock commented 10 months ago

Thanks for raising the issue. This used to be supported, but it was deprecated in 0.14.0 with this note:

bife models are no longer supported pending investigation in weird results in the tests. Looking for volunteers write more thorough tests.

I think that reinstating support would just require uncommenting this line: https://github.com/vincentarelbundock/marginaleffects/blob/main/R/sanity_model.R#L34

If you want to do that feel free to create a Pull Request, but I would really appreciate it if you could investigate the current tests, and maybe write a few ones to see if everything looks as it should. (I don't know this package well, so I couldn't do a proper investigation.)

mreece13 commented 10 months ago

Sure, I can look into that. What is the recommended guidance for how to run the tests and see their output? I've found the tests in inst/tinytest/test-pkg-bife.R, just not sure what the next step is. Running the recommended run_test_file("test-pkg-bife.R") from tinytest seems to be the wrong choice since it fails to automatically import a bunch of helper functions.

vincentarelbundock commented 10 months ago

Thanks!

In principle, you should be able to do this from an R session in the root directory of the repo:

pkgload::load_all()
tinytest::run_test_file("inst/tinytest/test-pkg-bife.R")

If you want to load the helper functions manually, you can just:

source("inst/tinytest/helpers.R")

Then, you can run all lines in the code manually as in a normal R session. That's the beauty of tinytest: it's just a normal script.

vincentarelbundock commented 9 months ago

I am still very interested in this, so I added it to the consolidated list of models we want to support. Closing this issue to keep the repo clean, but feel free to comment again. I will monitor it.