paul-buerkner / brms

brms R package for Bayesian generalized multivariate non-linear multilevel models using Stan
https://paul-buerkner.github.io/brms/
GNU General Public License v2.0
1.28k stars 181 forks source link

"NAs are not allowed in grouping variables" error with re_formula=NULL in conditional_effects() #1355

Closed eipi10 closed 2 years ago

eipi10 commented 2 years ago

I'm getting the error NAs are not allowed in grouping variables when I run conditional_effects(mymodel, re_formula=NULL). conditional_effects works normally with the default re_formula=NA. Below are two reproducible examples with different datasets.

library(lme4) # For sleepstudy data
library(vdemdata) # For vdem data
library(tidyverse)
library(brms)
# Example 1. sleepstudy data
m1 = brm(Reaction ~ Days + (1|Subject),
        data=sleepstudy,
        refresh=0, silent=2,
        cores=4, backend="cmdstanr")

conditional_effects(m1)

conditional_effects(m1, re_formula=NULL)
#> Error: NAs are not allowed in grouping variables.

conditional_effects(m1, re_formula= ~ (1|Subject))
#> Error: NAs are not allowed in grouping variables.
# Example 2. vdem data 
# Source: https://www.andrewheiss.com/blog/2021/11/10/ame-bayes-re-guide/
d = vdem %>% 
  select(country_name, country_text_id, year, region = e_regionpol_6C,
         media_index = v2xme_altinf, party_autonomy_ord = v2psoppaut_ord,
         polyarchy = v2x_polyarchy, civil_liberties = v2x_civlib) %>% 
  filter(year == 2015) %>% 
  mutate(party_autonomy = party_autonomy_ord >= 3,
         party_autonomy = ifelse(is.na(party_autonomy), FALSE, party_autonomy)) %>% 
  mutate(region = factor(region, 
                         labels = c("Eastern Europe and Central Asia",
                                    "Latin America and the Caribbean",
                                    "Middle East and North Africa",
                                    "Sub-Saharan Africa",
                                    "Western Europe and North America",
                                    "Asia and Pacific")))
m2 = brm(
  bf(media_index ~ party_autonomy + civil_liberties + (1 | region),
     phi ~ (1 | region)),
  data = d,
  family = Beta(),
  control = list(adapt_delta = 0.95),
  chains = 4, iter = 2000, warmup = 1000,
  cores = 4, seed = 12345,
  backend = "cmdstanr"
)

plot(conditional_effects(m2), plot=FALSE, ask=FALSE)

plot(conditional_effects(m2, re_formula=NULL), plot=FALSE, ask=FALSE)
#> Error: NAs are not allowed in grouping variables.

plot(conditional_effects(m2, re_formula= ~ (1|region)), plot=FALSE, ask=FALSE)
#> Error: NAs are not allowed in grouping variables.

Created on 2022-05-13 by the reprex package (v2.0.1)

Session Info

R version 4.2.0 (2022-04-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.7

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.2/Resources/lib/libRlapack.dylib

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

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

other attached packages:
 [1] brms_2.17.0     Rcpp_1.0.8.3    forcats_0.5.1   stringr_1.4.0   dplyr_1.0.9     purrr_0.3.4     readr_2.1.2     tidyr_1.2.0    
 [9] tibble_3.1.7    ggplot2_3.3.6   tidyverse_1.3.1 vdemdata_3.0    lme4_1.1-29     Matrix_1.4-1   

loaded via a namespace (and not attached):
  [1] readxl_1.4.0         backports_1.4.1      plyr_1.8.7           igraph_1.3.1         splines_4.2.0        crosstalk_1.2.0     
  [7] TH.data_1.1-1        rstantools_2.2.0     inline_0.3.19        digest_0.6.29        htmltools_0.5.2      fansi_1.0.3         
 [13] magrittr_2.0.3       checkmate_2.1.0      tzdb_0.3.0           modelr_0.1.8         RcppParallel_5.1.5   matrixStats_0.62.0  
 [19] R.utils_2.11.0       xts_0.12.1           sandwich_3.0-1       prettyunits_1.1.1    colorspace_2.0-3     rvest_1.0.2         
 [25] haven_2.5.0          xfun_0.30            callr_3.7.0          crayon_1.5.1         jsonlite_1.8.0       survival_3.3-1      
 [31] zoo_1.8-10           glue_1.6.2           gtable_0.3.0         emmeans_1.7.3        distributional_0.3.0 clipr_0.8.0         
 [37] R.cache_0.15.0       pkgbuild_1.3.1       rstan_2.21.5         abind_1.4-5          scales_1.2.0         mvtnorm_1.1-3       
 [43] DBI_1.1.2            miniUI_0.1.1.1       xtable_1.8-4         stats4_4.2.0         StanHeaders_2.21.0-7 DT_0.22             
 [49] htmlwidgets_1.5.4    httr_1.4.3           threejs_0.3.3        posterior_1.2.1      ellipsis_0.3.2       pkgconfig_2.0.3     
 [55] loo_2.5.1            R.methodsS3_1.8.1    farver_2.1.0         dbplyr_2.1.1         utf8_1.2.2           tidyselect_1.1.2    
 [61] labeling_0.4.2       rlang_1.0.2          reshape2_1.4.4       later_1.3.0          munsell_0.5.0        cellranger_1.1.0    
 [67] tools_4.2.0          cli_3.3.0            generics_0.1.2       broom_0.8.0          ggridges_0.5.3       evaluate_0.15       
 [73] fastmap_1.1.0        yaml_2.3.5           processx_3.5.3       knitr_1.39           fs_1.5.2             nlme_3.1-157        
 [79] mime_0.12            projpred_2.1.1       R.oo_1.24.0          xml2_1.3.3           compiler_4.2.0       bayesplot_1.9.0     
 [85] shinythemes_1.2.0    rstudioapi_0.13      gamm4_0.2-6          reprex_2.0.1         stringi_1.7.6        highr_0.9           
 [91] ps_1.7.0             Brobdingnag_1.2-7    lattice_0.20-45      styler_1.7.0         nloptr_2.0.1         markdown_1.1        
 [97] shinyjs_2.1.0        tensorA_0.36.2       vctrs_0.4.1          pillar_1.7.0         lifecycle_1.0.1      bridgesampling_1.1-2
[103] estimability_1.3     data.table_1.14.2    httpuv_1.6.5         R6_2.5.1             promises_1.2.0.1     gridExtra_2.3       
[109] codetools_0.2-18     boot_1.3-28          colourpicker_1.1.1   MASS_7.3-56          gtools_3.9.2         assertthat_0.2.1    
[115] withr_2.5.0          shinystan_2.6.0      multcomp_1.4-19      mgcv_1.8-40          parallel_4.2.0       hms_1.1.1           
[121] grid_4.2.0           coda_0.19-4          minqa_1.2.4          rmarkdown_2.14       cmdstanr_0.5.2       shiny_1.7.1         
[127] lubridate_1.8.0      base64enc_0.1-3      dygraphs_1.1.1.6    
paul-buerkner commented 2 years ago

Do you still see the error with the latest github version of brms?

eipi10 commented 2 years ago

Yes. I just installed brms 2.17.4 from github and I'm still getting the error.

paul-buerkner commented 2 years ago

You are right. It is unfortunate that this problem slipped my test cases earlier. Should now be fixed.

eipi10 commented 2 years ago

Great! Thanks Paul.

paul-buerkner commented 2 years ago

Can you check if the same error occurs also with the latest github version of brms?

Joel Schwartz @.***> schrieb am Sa., 14. Mai 2022, 00:40:

I'm getting the error NAs are not allowed in grouping variables when I run conditional_effects(mymodel, re_formula=NULL). conditional_effects works normally with the default re_formula. Below are two reproducible examples with different datasets.

library(lme4) # For sleepstudy data library(vdemdata) # For vdem data library(tidyverse) library(brms)

Example 1. sleepstudy datam1 = brm(Reaction ~ Days + (1|Subject),

    data=sleepstudy,
    refresh=0, silent=2,
    cores=4, backend="cmdstanr")

conditional_effects(m1)

https://camo.githubusercontent.com/238ab2b74d248f04e7aeedf1ce001be890c6d7f4f541c96a1b967bf1ba9f09d7/68747470733a2f2f692e696d6775722e636f6d2f73367842434d692e706e67

conditional_effects(m1, re_formula=NULL)#> Error: NAs are not allowed in grouping variables.

conditional_effects(m1, re_formula= ~ (1|Subject))#> Error: NAs are not allowed in grouping variables.

Example 2. vdem data # Source: https://www.andrewheiss.com/blog/2021/11/10/ame-bayes-re-guide/d = vdem %>%

select(country_name, country_text_id, year, region = e_regionpol_6C, media_index = v2xme_altinf, party_autonomy_ord = v2psoppaut_ord, polyarchy = v2x_polyarchy, civil_liberties = v2x_civlib) %>% filter(year == 2015) %>% mutate(party_autonomy = party_autonomy_ord >= 3, party_autonomy = ifelse(is.na(party_autonomy), FALSE, party_autonomy)) %>% mutate(region = factor(region, labels = c("Eastern Europe and Central Asia", "Latin America and the Caribbean", "Middle East and North Africa", "Sub-Saharan Africa", "Western Europe and North America", "Asia and Pacific")))m2 = brm( bf(media_index ~ party_autonomy + civil_liberties + (1 | region), phi ~ (1 | region)), data = d, family = Beta(), control = list(adapt_delta = 0.95), chains = 4, iter = 2000, warmup = 1000, cores = 4, seed = 12345, backend = "cmdstanr" )

plot(conditional_effects(m2), plot=FALSE, ask=FALSE)

plot(conditional_effects(m2, re_formula=NULL), plot=FALSE, ask=FALSE)#> Error: NAs are not allowed in grouping variables.

plot(conditional_effects(m2, re_formula= ~ (1|region)), plot=FALSE, ask=FALSE)#> Error: NAs are not allowed in grouping variables.

Created on 2022-05-13 by the reprex package https://reprex.tidyverse.org (v2.0.1)

Session Info

R version 4.2.0 (2022-04-22) Platform: x86_64-apple-darwin17.0 (64-bit) Running under: macOS Catalina 10.15.7

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.2/Resources/lib/libRlapack.dylib

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

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

other attached packages: [1] brms_2.17.0 Rcpp_1.0.8.3 forcats_0.5.1 stringr_1.4.0 dplyr_1.0.9 purrr_0.3.4 readr_2.1.2 tidyr_1.2.0 [9] tibble_3.1.7 ggplot2_3.3.6 tidyverse_1.3.1 vdemdata_3.0 lme4_1.1-29 Matrix_1.4-1

loaded via a namespace (and not attached): [1] readxl_1.4.0 backports_1.4.1 plyr_1.8.7 igraph_1.3.1 splines_4.2.0 crosstalk_1.2.0 [7] TH.data_1.1-1 rstantools_2.2.0 inline_0.3.19 digest_0.6.29 htmltools_0.5.2 fansi_1.0.3 [13] magrittr_2.0.3 checkmate_2.1.0 tzdb_0.3.0 modelr_0.1.8 RcppParallel_5.1.5 matrixStats_0.62.0 [19] R.utils_2.11.0 xts_0.12.1 sandwich_3.0-1 prettyunits_1.1.1 colorspace_2.0-3 rvest_1.0.2 [25] haven_2.5.0 xfun_0.30 callr_3.7.0 crayon_1.5.1 jsonlite_1.8.0 survival_3.3-1 [31] zoo_1.8-10 glue_1.6.2 gtable_0.3.0 emmeans_1.7.3 distributional_0.3.0 clipr_0.8.0 [37] R.cache_0.15.0 pkgbuild_1.3.1 rstan_2.21.5 abind_1.4-5 scales_1.2.0 mvtnorm_1.1-3 [43] DBI_1.1.2 miniUI_0.1.1.1 xtable_1.8-4 stats4_4.2.0 StanHeaders_2.21.0-7 DT_0.22 [49] htmlwidgets_1.5.4 httr_1.4.3 threejs_0.3.3 posterior_1.2.1 ellipsis_0.3.2 pkgconfig_2.0.3 [55] loo_2.5.1 R.methodsS3_1.8.1 farver_2.1.0 dbplyr_2.1.1 utf8_1.2.2 tidyselect_1.1.2 [61] labeling_0.4.2 rlang_1.0.2 reshape2_1.4.4 later_1.3.0 munsell_0.5.0 cellranger_1.1.0 [67] tools_4.2.0 cli_3.3.0 generics_0.1.2 broom_0.8.0 ggridges_0.5.3 evaluate_0.15 [73] fastmap_1.1.0 yaml_2.3.5 processx_3.5.3 knitr_1.39 fs_1.5.2 nlme_3.1-157 [79] mime_0.12 projpred_2.1.1 R.oo_1.24.0 xml2_1.3.3 compiler_4.2.0 bayesplot_1.9.0 [85] shinythemes_1.2.0 rstudioapi_0.13 gamm4_0.2-6 reprex_2.0.1 stringi_1.7.6 highr_0.9 [91] ps_1.7.0 Brobdingnag_1.2-7 lattice_0.20-45 styler_1.7.0 nloptr_2.0.1 markdown_1.1 [97] shinyjs_2.1.0 tensorA_0.36.2 vctrs_0.4.1 pillar_1.7.0 lifecycle_1.0.1 bridgesampling_1.1-2 [103] estimability_1.3 data.table_1.14.2 httpuv_1.6.5 R6_2.5.1 promises_1.2.0.1 gridExtra_2.3 [109] codetools_0.2-18 boot_1.3-28 colourpicker_1.1.1 MASS_7.3-56 gtools_3.9.2 assertthat_0.2.1 [115] withr_2.5.0 shinystan_2.6.0 multcomp_1.4-19 mgcv_1.8-40 parallel_4.2.0 hms_1.1.1 [121] grid_4.2.0 coda_0.19-4 minqa_1.2.4 rmarkdown_2.14 cmdstanr_0.5.2 shiny_1.7.1 [127] lubridate_1.8.0 base64enc_0.1-3 dygraphs_1.1.1.6

— Reply to this email directly, view it on GitHub https://github.com/paul-buerkner/brms/issues/1355, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADCW2AHYQJO5RD2QVPJTQI3VJ3K7RANCNFSM5V4U6F3Q . You are receiving this because you are subscribed to this thread.Message ID: @.***>

eipi10 commented 2 years ago

Both examples ran without error.