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
455 stars 47 forks source link

False alarm regarding factor warning #1212

Closed mattansb closed 1 month ago

mattansb commented 1 month ago

Here that cyl column is pre-transformed into a factor, but the warning still remains.

data("mtcars")
mtcars$cyl <- factor(mtcars$cyl)
mod <- rstanarm::stan_glm(mpg ~ cyl + hp * am, data = mtcars, refresh = 0)

marginaleffects::comparisons(mod,
                             variables = "cyl",
                             newdata = data.frame(hp = 100, am = 0)
)
#> Warning: The `cyl` variable is treated as a categorical (factor) variable, but
#>   the original data is of class NULL. It is safer and faster to convert
#>   such variables to factor before fitting the model and calling a
#>   `marginaleffects` function.
#>   
#>   This warning appears once per session.
#>   FALSE
#> 
#>  Contrast Estimate 2.5 % 97.5 %  hp am
#>     6 - 4    -3.96 -7.04 -0.631 100  0
#>     8 - 4    -3.52 -9.15  2.223 100  0
#> 
#> Term: cyl
#> Type:  response 
#> Columns: rowid, term, contrast, estimate, conf.low, conf.high, predicted_lo, predicted_hi, predicted, tmp_idx, hp, am, mpg

Created on 2024-09-05 with reprex v2.1.0

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.3.2 (2023-10-31 ucrt) #> os Windows 11 x64 (build 22631) #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate English_Israel.utf8 #> ctype English_Israel.utf8 #> tz Asia/Jerusalem #> date 2024-09-05 #> pandoc 3.1.11 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> ! package * version date (UTC) lib source #> abind 1.4-5 2016-07-21 [1] CRAN (R 4.3.0) #> backports 1.4.1 2021-12-13 [1] CRAN (R 4.3.0) #> base64enc 0.1-3 2015-07-28 [1] CRAN (R 4.3.0) #> bayesplot 1.11.1 2024-02-15 [1] CRAN (R 4.3.2) #> boot 1.3-28.1 2022-11-22 [2] CRAN (R 4.3.2) #> checkmate 2.3.1 2023-12-04 [1] CRAN (R 4.3.2) #> cli 3.6.2 2023-12-11 [1] CRAN (R 4.3.2) #> codetools 0.2-19 2023-02-01 [2] CRAN (R 4.3.2) #> collapse 2.0.13 2024-04-13 [1] CRAN (R 4.3.3) #> colorspace 2.1-0 2023-01-23 [1] CRAN (R 4.3.1) #> colourpicker 1.3.0 2023-08-21 [1] CRAN (R 4.3.1) #> crosstalk 1.2.1 2023-11-23 [1] CRAN (R 4.3.2) #> curl 5.2.1 2024-03-01 [1] CRAN (R 4.3.3) #> data.table 1.15.4 2024-03-30 [1] CRAN (R 4.3.3) #> digest 0.6.35 2024-03-11 [1] CRAN (R 4.3.3) #> distributional 0.4.0 2024-02-07 [1] CRAN (R 4.3.2) #> dplyr 1.1.4 2023-11-17 [1] CRAN (R 4.3.2) #> DT 0.33 2024-04-04 [1] CRAN (R 4.3.3) #> dygraphs 1.1.1.6 2018-07-11 [1] CRAN (R 4.3.1) #> evaluate 0.23 2023-11-01 [1] CRAN (R 4.3.2) #> fansi 1.0.6 2023-12-08 [1] CRAN (R 4.3.2) #> fastmap 1.2.0 2024-05-15 [1] CRAN (R 4.3.3) #> fs 1.6.4 2024-04-25 [1] CRAN (R 4.3.3) #> generics 0.1.3 2022-07-05 [1] CRAN (R 4.3.1) #> ggplot2 3.5.1 2024-04-23 [1] CRAN (R 4.3.3) #> glue 1.7.0 2024-01-09 [1] CRAN (R 4.3.2) #> gridExtra 2.3 2017-09-09 [1] CRAN (R 4.3.1) #> gtable 0.3.5 2024-04-22 [1] CRAN (R 4.3.3) #> gtools 3.9.5 2023-11-20 [1] CRAN (R 4.3.2) #> htmltools 0.5.8.1 2024-04-04 [1] CRAN (R 4.3.3) #> htmlwidgets 1.6.4 2023-12-06 [1] CRAN (R 4.3.2) #> httpuv 1.6.15 2024-03-26 [1] CRAN (R 4.3.3) #> igraph 2.0.3 2024-03-13 [1] CRAN (R 4.3.2) #> inline 0.3.19 2021-05-31 [1] CRAN (R 4.3.1) #> insight 0.20.4.2 2024-09-04 [1] local #> jsonlite 1.8.8 2023-12-04 [1] CRAN (R 4.3.2) #> knitr 1.46 2024-04-06 [1] CRAN (R 4.3.3) #> later 1.3.2 2023-12-06 [1] CRAN (R 4.3.2) #> lattice 0.21-9 2023-10-01 [2] CRAN (R 4.3.2) #> lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.3.2) #> lme4 1.1-35.3 2024-04-16 [1] CRAN (R 4.3.3) #> loo 2.7.0 2024-02-24 [1] CRAN (R 4.3.2) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.3.1) #> marginaleffects 0.22.0 2024-08-31 [1] CRAN (R 4.3.3) #> markdown 1.12 2023-12-06 [1] CRAN (R 4.3.2) #> MASS 7.3-60.0.1 2024-01-13 [1] CRAN (R 4.3.2) #> Matrix 1.6-5 2024-01-11 [1] CRAN (R 4.3.2) #> matrixStats 1.3.0 2024-04-11 [1] CRAN (R 4.3.3) #> mime 0.12 2021-09-28 [1] CRAN (R 4.3.0) #> miniUI 0.1.1.1 2018-05-18 [1] CRAN (R 4.3.1) #> minqa 1.2.6 2023-09-11 [1] CRAN (R 4.3.1) #> munsell 0.5.1 2024-04-01 [1] CRAN (R 4.3.3) #> nlme 3.1-163 2023-08-09 [2] CRAN (R 4.3.2) #> nloptr 2.0.3 2022-05-26 [1] CRAN (R 4.3.1) #> pillar 1.9.0 2023-03-22 [1] CRAN (R 4.3.1) #> pkgbuild 1.4.4 2024-03-17 [1] CRAN (R 4.3.3) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.3.1) #> plyr 1.8.9 2023-10-02 [1] CRAN (R 4.3.1) #> posterior 1.5.0 2023-10-31 [1] CRAN (R 4.3.2) #> promises 1.3.0 2024-04-05 [1] CRAN (R 4.3.3) #> purrr 1.0.2 2023-08-10 [1] CRAN (R 4.3.1) #> QuickJSR 1.1.3 2024-01-31 [1] CRAN (R 4.3.2) #> R.cache 0.16.0 2022-07-21 [1] CRAN (R 4.3.1) #> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.3.0) #> R.oo 1.26.0 2024-01-24 [1] CRAN (R 4.3.2) #> R.utils 2.12.3 2023-11-18 [1] CRAN (R 4.3.2) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.3.1) #> Rcpp 1.0.12 2024-01-09 [1] CRAN (R 4.3.2) #> D RcppParallel 5.1.7 2023-02-27 [1] CRAN (R 4.3.1) #> reprex 2.1.0 2024-01-11 [1] CRAN (R 4.3.2) #> reshape2 1.4.4 2020-04-09 [1] CRAN (R 4.3.1) #> rlang 1.1.3 2024-01-10 [1] CRAN (R 4.3.2) #> rmarkdown 2.27 2024-05-17 [1] CRAN (R 4.3.3) #> rstan 2.32.6 2024-03-05 [1] CRAN (R 4.3.3) #> rstanarm 2.32.1 2024-01-18 [1] CRAN (R 4.3.2) #> rstantools 2.4.0 2024-01-31 [1] CRAN (R 4.3.2) #> rstudioapi 0.16.0 2024-03-24 [1] CRAN (R 4.3.3) #> scales 1.3.0 2023-11-28 [1] CRAN (R 4.3.2) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.3.1) #> shiny 1.8.1.1 2024-04-02 [1] CRAN (R 4.3.3) #> shinyjs 2.1.0 2021-12-23 [1] CRAN (R 4.3.1) #> shinystan 2.6.0 2022-03-03 [1] CRAN (R 4.3.1) #> shinythemes 1.2.0 2021-01-25 [1] CRAN (R 4.3.1) #> StanHeaders 2.32.7 2024-04-25 [1] CRAN (R 4.3.3) #> stringi 1.8.4 2024-05-06 [1] CRAN (R 4.3.3) #> stringr 1.5.1 2023-11-14 [1] CRAN (R 4.3.2) #> styler 1.10.3 2024-04-07 [1] CRAN (R 4.3.3) #> survival 3.5-7 2023-08-14 [2] CRAN (R 4.3.2) #> tensorA 0.36.2.1 2023-12-13 [1] CRAN (R 4.3.2) #> threejs 0.3.3 2020-01-21 [1] CRAN (R 4.3.1) #> tibble 3.2.1 2023-03-20 [1] CRAN (R 4.3.1) #> tidyselect 1.2.1 2024-03-11 [1] CRAN (R 4.3.3) #> utf8 1.2.4 2023-10-22 [1] CRAN (R 4.3.1) #> V8 4.4.2 2024-02-15 [1] CRAN (R 4.3.3) #> vctrs 0.6.5 2023-12-01 [1] CRAN (R 4.3.2) #> withr 3.0.0 2024-01-16 [1] CRAN (R 4.3.2) #> xfun 0.44 2024-05-15 [1] CRAN (R 4.3.3) #> xtable 1.8-4 2019-04-21 [1] CRAN (R 4.3.1) #> xts 0.13.2 2024-01-21 [1] CRAN (R 4.3.2) #> yaml 2.3.8 2023-12-11 [1] CRAN (R 4.3.2) #> zoo 1.8-12 2023-04-13 [1] CRAN (R 4.3.1) #> #> [1] C:/Users/user/AppData/Local/R/win-library/4.3 #> [2] C:/Program Files/R/R-4.3.2/library #> #> D ── DLL MD5 mismatch, broken installation. #> #> ────────────────────────────────────────────────────────────────────────────── ```
vincentarelbundock commented 1 month ago

Your code is incorrect. The message says that cyl is NULL because it is not a column in the data frame that you supplied to newdata.

Instead of newdata=data.frame(), you should use newdata=datagrid(), which is smart enough to create a grid with the appropriate cyl variable when you don't specify it explicitly.

mattansb commented 1 month ago

🤦‍♂️ Thanks