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.27k stars 182 forks source link

Interval censoring fails due to differing dimensions of resp_cens arguments #1622

Closed StaffanBetner closed 6 months ago

StaffanBetner commented 6 months ago

I have a very simple interval censoring example that fails ~for an unknown reason~, due to differing dimensions of the resp_cens arguments x and y2. Here is the reprex:

library(brms)
#> Loading required package: Rcpp
#> Loading 'brms' package (version 2.20.17). Useful instructions
#> can be found by typing help('brms'). A more detailed introduction
#> to the package is available through vignette('brms_overview').
#> 
#> Attaching package: 'brms'
#> The following object is masked from 'package:stats':
#> 
#>     ar

structure(list(y = c(188L, 184L, 180L, 175L, 170L), 
               p = c(0.9, 0.75, 0.5, 0.25, 0.1), 
               q = c(1.2815515655446, 0.674489750196082, 0, -0.674489750196082, -1.2815515655446), 
               y1 = c(5.23377884541047, 5.21221466749463, 5.19017520792833, 5.16192474164248, 5.1328529268205), 
               y2 = c(5.23909800688807, 5.21764946348058, 5.19573077777294, 5.16763904290592, 5.13873529672357)), 
          row.names = c(NA, -5L), 
          class = c("data.frame")) -> 
  testdata

brm(formula = y1 | cens("interval", y2 = y2) ~ 1  + q, data = testdata) -> lognormal_test
#> Compiling Stan program...
#> Start sampling
#> Error in eval(expr, envir, enclos) : 
#>   Exception: mismatch in dimension declared and found in context; processing stage=data initialization; variable name=cens; position=0; dims declared=(5); dims found=(1) (in 'string', line 7, column 2 to column 38)
#> failed to create the sampler; sampling not done

sessionInfo()
#> R version 4.3.2 (2023-10-31 ucrt)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19044)
#> 
#> Matrix products: default
#> 
#> 
#> locale:
#> [1] LC_COLLATE=Swedish_Sweden.utf8  LC_CTYPE=Swedish_Sweden.utf8   
#> [3] LC_MONETARY=Swedish_Sweden.utf8 LC_NUMERIC=C                   
#> [5] LC_TIME=Swedish_Sweden.utf8    
#> 
#> time zone: Europe/Stockholm
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] brms_2.20.17 Rcpp_1.0.12 
#> 
#> loaded via a namespace (and not attached):
#>   [1] tidyselect_1.2.1     dplyr_1.1.4          loo_2.7.0           
#>   [4] R.utils_2.12.3       fastmap_1.1.1        TH.data_1.1-2       
#>   [7] tensorA_0.36.2.1     shinystan_2.6.0      promises_1.2.1      
#>  [10] shinyjs_2.1.0        reprex_2.1.0         digest_0.6.35       
#>  [13] estimability_1.5     mime_0.12            lifecycle_1.0.4     
#>  [16] StanHeaders_2.32.6   ellipsis_0.3.2       survival_3.5-8      
#>  [19] magrittr_2.0.3       posterior_1.5.0      compiler_4.3.2      
#>  [22] rlang_1.1.3          tools_4.3.2          igraph_2.0.3        
#>  [25] utf8_1.2.4           yaml_2.3.8           knitr_1.45          
#>  [28] bridgesampling_1.1-2 htmlwidgets_1.6.4    curl_5.2.1          
#>  [31] pkgbuild_1.4.3       plyr_1.8.9           dygraphs_1.1.1.6    
#>  [34] multcomp_1.4-25      abind_1.4-5          miniUI_0.1.1.1      
#>  [37] R.cache_0.16.0       withr_3.0.0          purrr_1.0.2         
#>  [40] R.oo_1.26.0          grid_4.3.2           stats4_4.3.2        
#>  [43] fansi_1.0.6          xts_0.13.2           inline_0.3.19       
#>  [46] xtable_1.8-4         colorspace_2.1-0     ggplot2_3.5.0       
#>  [49] MASS_7.3-60.0.1      emmeans_1.10.0       scales_1.3.0        
#>  [52] gtools_3.9.5         cli_3.6.2            mvtnorm_1.2-4       
#>  [55] rmarkdown_2.26       generics_0.1.3       RcppParallel_5.1.7  
#>  [58] rstudioapi_0.15.0    reshape2_1.4.4       rstan_2.32.6        
#>  [61] stringr_1.5.1        splines_4.3.2        shinythemes_1.2.0   
#>  [64] bayesplot_1.11.1     parallel_4.3.2       matrixStats_1.2.0   
#>  [67] base64enc_0.1-3      vctrs_0.6.5          sandwich_3.1-0      
#>  [70] V8_4.4.2             Matrix_1.6-5         jsonlite_1.8.8      
#>  [73] crosstalk_1.2.1      glue_1.7.0           codetools_0.2-19    
#>  [76] DT_0.32              distributional_0.4.0 stringi_1.8.3       
#>  [79] gtable_0.3.4         QuickJSR_1.1.3       later_1.3.2         
#>  [82] munsell_0.5.0        tibble_3.2.1         colourpicker_1.3.0  
#>  [85] styler_1.10.2        pillar_1.9.0         htmltools_0.5.7     
#>  [88] Brobdingnag_1.2-9    R6_2.5.1             evaluate_0.23       
#>  [91] shiny_1.8.0          lattice_0.22-5       markdown_1.12       
#>  [94] R.methodsS3_1.8.2    backports_1.4.1      threejs_0.3.3       
#>  [97] httpuv_1.6.14        rstantools_2.4.0     coda_0.19-4.1       
#> [100] gridExtra_2.3        nlme_3.1-164         checkmate_2.3.1     
#> [103] xfun_0.42            fs_1.6.3             zoo_1.8-12          
#> [106] pkgconfig_2.0.3

Created on 2024-03-15 with reprex v2.1.0

StaffanBetner commented 6 months ago

A fairly reasonable guess is that resp_cens assumes that x and y2 are of the same dimension.

EDIT:

Yep,

brm(formula = y1 | cens(rep("interval", 5), y2 = y2) ~ 1  + q, data = testdata) -> lognormal_test

works.

paul-buerkner commented 6 months ago

Thanks! fixed :-)