Predict error when using subset for the outcome and mi for a covariate

Predicting a response when the outcome is subset and a covariate has missingness handled by mi throws an error
“Error in as_draws_matrix(sdata[[paste0(“Y_”, vmi)]], dim) :
length(x) %in% c(1, dim[2]) is not TRUE”

This is true for both the cran release and the dev branch as of today. Issue ticket here

Here is a minimum reproducible example

#remotes::install_github("paul-buerkner/brms")
library(brms)
xy <- mtcars
xy$cyl[1:10] <- NA #set first 10 to NA
xy$disp[c(T,F)] <- NA
table(xy$disp, useNA='always') #16 NAs
xy$cyl_nona <- !is.na(xy$cyl) #create an index to subset on
dim(xy) #32
bf_dv <- bf(cyl | subset(cyl_nona) ~ 0 + Intercept + mi(disp)) 
bf_disp <- bf(disp | mi() ~ 0  )
fit1 <- brm(bf_dv + bf_disp, data = xy, chains = 1, iter = 2000, warmup = 1000, thin = 1, cores = 128,  backend = "cmdstanr", seed = 42) 
y_hat <- predict(fit1, resp="cyl", summary=F)

This came up in a production model for covid that has count outcomes and so can’t be handled with MI and require subsetting. The model aggregates lots of different count data with different missingness and so it’s not easily refactored to get around this.

sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.1 LTS

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0

locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=en_US.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

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

other attached packages:
[1] brms_2.14.6 Rcpp_1.0.5

loaded via a namespace (and not attached):
[1] nlme_3.1-151 matrixStats_0.57.0 xts_0.12.1 lubridate_1.7.9.2 threejs_0.3.3 rstan_2.21.2 backports_1.2.1 tools_4.0.3 DT_0.16 rgdal_1.5-18 R6_2.5.0 mgcv_1.8-33
[13] projpred_2.0.2 colorspace_2.0-0 withr_2.3.0 sp_1.4-4 tidyselect_1.1.0 gridExtra_2.3 prettyunits_1.1.1 processx_3.4.5 Brobdingnag_1.2-6 curl_4.3 compiler_4.0.3 cli_2.2.0
[25] shinyjs_2.0.0 posterior_0.1.3 colourpicker_1.1.0 checkmate_2.0.0 scales_1.1.1 dygraphs_1.1.1.6 mvtnorm_1.1-1 ggridges_0.5.2 callr_3.5.1 stringr_1.4.0 digest_0.6.27 StanHeaders_2.21.0-7
[37] minqa_1.2.4 base64enc_0.1-3 pkgconfig_2.0.3 htmltools_0.5.0 lme4_1.1-26 fastmap_1.0.1 htmlwidgets_1.5.3 rlang_0.4.9 rstudioapi_0.13 shiny_1.5.0 generics_0.1.0 zoo_1.8-8
[49] jsonlite_1.7.2 gtools_3.8.2 crosstalk_1.1.0.1 dplyr_1.0.2 inline_0.3.17 magrittr_2.0.1 loo_2.4.1 bayesplot_1.7.2 Matrix_1.2-18 munsell_0.5.0 fansi_0.4.1 abind_1.4-5
[61] lifecycle_0.2.0 stringi_1.5.3 MASS_7.3-53 pkgbuild_1.2.0 plyr_1.8.6 grid_4.0.3 parallel_4.0.3 promises_1.1.1 crayon_1.3.4 miniUI_0.1.1.1 lattice_0.20-41 splines_4.0.3
[73] ps_1.5.0 pillar_1.4.7 igraph_1.2.6 boot_1.3-25 markdown_1.1 shinystan_2.5.0 reshape2_1.4.4 codetools_0.2-18 stats4_4.0.3 rstantools_2.1.1 glue_1.4.2 V8_3.4.0
[85] data.table_1.13.4 RcppParallel_5.0.2 vctrs_0.3.5 nloptr_1.2.2.2 httpuv_1.5.4 gtable_0.3.0 purrr_0.3.4 assertthat_0.2.1 ggplot2_3.3.2 mime_0.9 xtable_1.8-4 coda_0.19-4
[97] later_1.1.0.1 rsconnect_0.8.16 tibble_3.0.4 shinythemes_1.1.2 gamm4_0.2-6 statmod_1.4.35 ellipsis_0.3.1 cmdstanr_0.2.2 bridgesampling_1.0-0

2 Likes

Thanks for the test case! I don’t know enough about brms to try to fix this but can confirm the same behavior on my computer.

1 Like

Looks like Paul is on it: https://github.com/paul-buerkner/brms/issues/1063

Thanks for reporting this @rexdouglass.