strengejacke / ggeffects

Estimated Marginal Means and Marginal Effects from Regression Models for ggplot2
https://strengejacke.github.io/ggeffects
Other
556 stars 35 forks source link

Non-conformable errors wbm-models #266

Closed edvinsyk closed 6 months ago

edvinsyk commented 2 years ago

I've been playing around with different panelr::wbm models.

I keep getting a number of different errors with both

ggeffect (mostly Can't compute marginal effects, 'effects::Effect()' returned an error."

and

ggpredict (Error in complete.cases(data[[variable]]) : no input has determined the number of cases)

I've included a small reproducible example using the chick weight dataset (and some output).

library(panelr)
library(ggeffects)
library(lme4)
library(data.table)
set.seed(1)

dt <- setDT(datasets::ChickWeight)
dt[, x := runif(.N, 0, 10), by = Chick]
dt <- dt[complete.cases(dt), Diet := factor(Diet)] 

pss <- panel_data(dt, id = Chick, wave = Time)

m0 <- wbm(weight ~ Time, data = pss)
summary(m0)  # Model works fine 

ggeffect(m0)
Can't compute marginal effects, 'effects::Effect()' returned an error.
Reason: non-conformable arguments
You may try 'ggpredict()' or 'ggemmeans()'.
NULL

ggpredict(m0) # Works 
m1 <- wbm(weight ~ x | Diet, data = pss)

ggeffect(m1)

Can't compute marginal effects, 'effects::Effect()' returned an error.

Reason: non-conformable arguments
You may try 'ggpredict()' or 'ggemmeans()'.

Can't compute marginal effects, 'effects::Effect()' returned an error.

Reason: the following predictor is not in the model: Diet
You may try 'ggpredict()' or 'ggemmeans()'.

NULL

ggpredict(m1)
Error in `vectbl_as_col_location()`:
! Can't subset columns that don't exist.
✖ Columns `Diet2`, `Diet3`, and `Diet4` don't exist.
Backtrace:
  1. ggeffects::ggpredict(m1)
  2. base::lapply(...)
  3. ggeffects (local) FUN(X[[i]], ...)
  4. ggeffects:::ggpredict_helper(...)
  5. ggeffects:::select_prediction_method(...)
     ...
 10. panelr:::predict.wbm(...)
 11. panelr:::process_nonraw_newdata(object, newdata, re.form)
 12. panelr::model_frame(mf_form, newdata)
 21. tibble:::`[.tbl_df`(...)
 22. tibble:::vectbl_as_col_location(j, length(x), names(x), j_arg = j_arg, assign = FALSE)
sessionInfo()
R version 4.2.1 (2022-06-23)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Monterey 12.4

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib

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

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

other attached packages:
[1] data.table_1.14.2 ggeffects_1.1.3.2 panelr_0.7.6      lme4_1.1-30       Matrix_1.4-1     

loaded via a namespace (and not attached):
 [1] sjlabelled_1.2.0    tidyselect_1.1.2    purrr_0.3.4         pander_0.6.5        mitools_2.4         haven_2.5.0         splines_4.2.1      
 [8] lmerTest_3.1-3      lattice_0.20-45     carData_3.0-5       colorspace_2.0-3    vctrs_0.4.1         generics_0.1.3      utf8_1.2.2         
[15] survival_3.3-1      rlang_1.0.4         nloptr_2.0.3        pillar_1.8.0        glue_1.6.2          DBI_1.1.3           jtools_2.2.0       
[22] lifecycle_1.0.1     stringr_1.4.0       effects_4.2-2       munsell_0.5.0       gtable_0.3.0        forcats_0.5.1       fansi_1.0.3        
[29] Rcpp_1.0.9          renv_0.15.5         scales_1.2.0        jsonlite_1.8.0      hms_1.1.1           ggplot2_3.3.6       digest_0.6.29      
[36] stringi_1.7.8       insight_0.18.2      dplyr_1.0.9         survey_4.1-1        numDeriv_2016.8-1.1 grid_4.2.1          cli_3.3.0          
[43] tools_4.2.1         magrittr_2.0.3      tibble_3.1.7        Formula_1.2-4       crayon_1.5.1        pkgconfig_2.0.3     MASS_7.3-57        
[50] ellipsis_0.3.2      estimability_1.4.1  assertthat_0.2.1    minqa_1.2.4         R6_2.5.1            boot_1.3-28         nnet_7.3-17        
[57] nlme_3.1-158        compiler_4.2.1     
strengejacke commented 2 years ago

Yes, the predict() method from panelr is a bit tricky, as it either requires a panel_data object, or a data.frame that already did all the transformations that is done internally in wbm().

I post this code example for me as reminder.

library(panelr)
library(lme4)
library(data.table)
set.seed(1)

dt <- setDT(datasets::ChickWeight)
dt[, x := runif(.N, 0, 10), by = Chick]
dt <- dt[complete.cases(dt), Diet := factor(Diet)]

pss <- panel_data(dt, id = Chick, wave = Time)
m1 <- wbm(weight ~ x | Diet, data = pss)

datagrid <- insight::get_datagrid(m1, at = "x", range = "grid", length = 5)
datagrid
#>               x   weight Diet Chick     Time
#> 1 -5.412990e+00 121.8183    1    18 10.71799
#> 2 -2.706495e+00 121.8183    1    18 10.71799
#> 3 -2.540409e-17 121.8183    1    18 10.71799
#> 4  2.706495e+00 121.8183    1    18 10.71799
#> 5  5.412990e+00 121.8183    1    18 10.71799

predict(m1, newdata = datagrid)
#> Error in `is.data.frame()`:
#> ! Can't subset columns that don't exist.
#> ✖ Columns `Diet2`, `Diet3`, and `Diet4` don't exist.

Created on 2022-08-21 by the reprex package (v2.0.1)

strengejacke commented 2 years ago

maybe @jacob-long has also ideas how to address this?