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.29k stars 187 forks source link

"Export" of redundant parameters with cmdstanr backend #1261

Closed fweber144 closed 10 months ago

fweber144 commented 3 years ago

Again, this is not urgent at all, but rather a cosmetic feature request for something I encountered while working on shinybrms. And, similar to #1251, this might not be easy to resolve or at least it might not be worth the effort.

When using the cmdstanr backend, there are some redundant parameters which are "exported" by as.matrix(<brmsfit_object>) and launch_shinystan(<brmsfit_object>), for example. At least there is an inconsistency with the rstan backend.

Reprex:

data(bacteria, package = "MASS")
bfit_rstan <- brms::brm(
  formula = y ~ 1 + week * trt + (1 | ID),
  data = bacteria,
  family = brms::bernoulli(),
  chains = 2,
  iter = 500,
  seed = 1234
)
bfit_cmdstanr <- brms::brm(
  formula = y ~ 1 + week * trt + (1 | ID),
  data = bacteria,
  family = brms::bernoulli(),
  chains = 2,
  iter = 500,
  seed = 1234,
  backend = "cmdstanr"
)

Now

setdiff(brms::variables(bfit_rstan), brms::variables(bfit_cmdstanr))

gives

character(0)

and

setdiff(brms::variables(bfit_cmdstanr), brms::variables(bfit_rstan))

gives

[1] "Intercept" "z_1[1,1]"  [...] "z_1[1,50]"

These parameters exported additionally when using the cmdstanr backend could be omitted, I think.

Session info:

> sessioninfo::session_info()
─ Session info  ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

 setting  value
 version  R version 4.1.2 (2021-11-01)
 os       Ubuntu 20.04.3 LTS
 system   x86_64, linux-gnu
 ui       RStudio
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       Europe/Berlin
 date     2021-11-03
 rstudio  2021.09.0+351 Ghost Orchid (desktop)
 pandoc   2.14.0.3 @ /usr/lib/rstudio/bin/pandoc/ (via rmarkdown)

─ Packages ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 package        * version  date (UTC) lib source
 abind            1.4-5    2016-07-21 [1] CRAN (R 4.0.4)
 assertthat       0.2.1    2019-03-21 [1] CRAN (R 4.0.4)
 backports        1.3.0    2021-10-27 [1] CRAN (R 4.1.1)
 base64enc        0.1-3    2015-07-28 [1] CRAN (R 4.0.4)
 bayesplot        1.8.1    2021-06-14 [1] CRAN (R 4.1.0)
 boot             1.3-28   2021-05-03 [4] CRAN (R 4.0.5)
 bridgesampling   1.1-2    2021-04-16 [1] CRAN (R 4.0.5)
 brms             2.16.1   2021-08-23 [1] CRAN (R 4.1.1)
 Brobdingnag      1.2-6    2018-08-13 [1] CRAN (R 4.0.4)
 callr            3.7.0    2021-04-20 [1] CRAN (R 4.0.5)
 checkmate        2.0.0    2020-02-06 [1] CRAN (R 4.0.4)
 cli              3.1.0    2021-10-27 [1] CRAN (R 4.1.1)
 cmdstanr         0.4.0    2021-04-16 [1] local
 coda             0.19-4   2020-09-30 [1] CRAN (R 4.0.4)
 codetools        0.2-18   2020-11-04 [4] CRAN (R 4.0.3)
 colorspace       2.0-2    2021-06-24 [1] CRAN (R 4.1.0)
 colourpicker     1.1.1    2021-10-04 [1] CRAN (R 4.1.1)
 crayon           1.4.2    2021-10-29 [1] CRAN (R 4.1.1)
 crosstalk        1.1.1    2021-01-12 [1] CRAN (R 4.0.4)
 curl             4.3.2    2021-06-23 [1] CRAN (R 4.1.0)
 data.table       1.14.2   2021-09-27 [1] CRAN (R 4.1.1)
 DBI              1.1.1    2021-01-15 [1] CRAN (R 4.0.4)
 digest           0.6.28   2021-09-23 [1] CRAN (R 4.1.1)
 distributional   0.2.2    2021-02-02 [1] CRAN (R 4.0.4)
 dplyr            1.0.7    2021-06-18 [1] CRAN (R 4.1.0)
 DT               0.19     2021-09-02 [1] CRAN (R 4.1.1)
 dygraphs         1.1.1.6  2018-07-11 [1] CRAN (R 4.0.4)
 ellipsis         0.3.2    2021-04-29 [1] CRAN (R 4.0.5)
 emmeans          1.7.0    2021-09-29 [1] CRAN (R 4.1.1)
 estimability     1.3      2018-02-11 [1] CRAN (R 4.0.4)
 evaluate         0.14     2019-05-28 [1] CRAN (R 4.0.4)
 fansi            0.5.0    2021-05-25 [1] CRAN (R 4.1.0)
 farver           2.1.0    2021-02-28 [1] CRAN (R 4.0.4)
 fastmap          1.1.0    2021-01-25 [1] CRAN (R 4.0.4)
 gamm4            0.2-6    2020-04-03 [1] CRAN (R 4.0.4)
 generics         0.1.1    2021-10-25 [1] CRAN (R 4.1.1)
 ggplot2          3.3.5    2021-06-25 [1] CRAN (R 4.1.0)
 ggridges         0.5.3    2021-01-08 [1] CRAN (R 4.0.4)
 glue             1.4.2    2020-08-27 [1] CRAN (R 4.0.4)
 gridExtra        2.3      2017-09-09 [1] CRAN (R 4.0.4)
 gtable           0.3.0    2019-03-25 [1] CRAN (R 4.0.4)
 gtools           3.9.2    2021-06-06 [1] CRAN (R 4.1.0)
 htmltools        0.5.2    2021-08-25 [1] CRAN (R 4.1.1)
 htmlwidgets      1.5.4    2021-09-08 [1] CRAN (R 4.1.1)
 httpuv           1.6.3    2021-09-09 [1] CRAN (R 4.1.1)
 igraph           1.2.7    2021-10-15 [1] CRAN (R 4.1.1)
 inline           0.3.19   2021-05-31 [1] CRAN (R 4.1.0)
 jsonlite         1.7.2    2020-12-09 [1] CRAN (R 4.0.4)
 knitr            1.36     2021-09-29 [1] CRAN (R 4.1.1)
 later            1.3.0    2021-08-18 [1] CRAN (R 4.1.1)
 lattice          0.20-45  2021-09-22 [4] CRAN (R 4.1.1)
 lifecycle        1.0.1    2021-09-24 [1] CRAN (R 4.1.1)
 lme4             1.1-27.1 2021-06-22 [1] CRAN (R 4.1.0)
 loo              2.4.1    2020-12-09 [1] CRAN (R 4.1.1)
 magrittr         2.0.1    2020-11-17 [1] CRAN (R 4.0.4)
 markdown         1.1      2019-08-07 [1] CRAN (R 4.0.4)
 MASS             7.3-54   2021-05-03 [4] CRAN (R 4.0.5)
 Matrix           1.3-4    2021-06-01 [4] CRAN (R 4.1.0)
 matrixStats      0.61.0   2021-09-17 [1] CRAN (R 4.1.1)
 mgcv             1.8-38   2021-10-06 [4] CRAN (R 4.1.1)
 mime             0.12     2021-09-28 [1] CRAN (R 4.1.1)
 miniUI           0.1.1.1  2018-05-18 [1] CRAN (R 4.0.4)
 minqa            1.2.4    2014-10-09 [1] CRAN (R 4.0.4)
 multcomp         1.4-17   2021-04-29 [1] CRAN (R 4.1.0)
 munsell          0.5.0    2018-06-12 [1] CRAN (R 4.0.4)
 mvtnorm          1.1-3    2021-10-08 [1] CRAN (R 4.1.1)
 nlme             3.1-152  2021-02-04 [4] CRAN (R 4.0.3)
 nloptr           1.2.2.3  2021-11-02 [1] CRAN (R 4.1.2)
 pillar           1.6.4    2021-10-18 [1] CRAN (R 4.1.1)
 pkgbuild         1.2.0    2020-12-15 [1] CRAN (R 4.0.4)
 pkgconfig        2.0.3    2019-09-22 [1] CRAN (R 4.0.4)
 plyr             1.8.6    2020-03-03 [1] CRAN (R 4.0.4)
 posterior        1.1.0    2021-09-09 [1] CRAN (R 4.1.1)
 prettyunits      1.1.1    2020-01-24 [1] CRAN (R 4.0.4)
 processx         3.5.2    2021-04-30 [1] CRAN (R 4.0.5)
 projpred         2.0.2    2020-10-28 [1] CRAN (R 4.1.1)
 promises         1.2.0.1  2021-02-11 [1] CRAN (R 4.0.4)
 ps               1.6.0    2021-02-28 [1] CRAN (R 4.0.4)
 purrr            0.3.4    2020-04-17 [1] CRAN (R 4.0.4)
 R6               2.5.1    2021-08-19 [1] CRAN (R 4.1.1)
 Rcpp             1.0.7    2021-07-07 [1] CRAN (R 4.1.0)
 RcppParallel     5.1.4    2021-05-04 [1] CRAN (R 4.0.5)
 reshape2         1.4.4    2020-04-09 [1] CRAN (R 4.0.4)
 rlang            0.4.12   2021-10-18 [1] CRAN (R 4.1.1)
 rmarkdown        2.11     2021-09-14 [1] CRAN (R 4.1.1)
 rsconnect        0.8.24   2021-08-05 [1] CRAN (R 4.1.0)
 rstan            2.26.3   2021-08-09 [1] local
 rstantools       2.1.1    2020-07-06 [1] CRAN (R 4.0.4)
 sandwich         3.0-1    2021-05-18 [1] CRAN (R 4.1.0)
 scales           1.1.1    2020-05-11 [1] CRAN (R 4.0.4)
 sessioninfo      1.2.1    2021-11-02 [1] CRAN (R 4.1.2)
 shiny            1.7.1    2021-10-02 [1] CRAN (R 4.1.1)
 shinyjs          2.0.0    2020-09-09 [1] CRAN (R 4.0.4)
 shinystan        2.5.0    2018-05-01 [1] CRAN (R 4.0.4)
 shinythemes      1.2.0    2021-01-25 [1] CRAN (R 4.0.4)
 StanHeaders      2.26.3   2021-08-09 [1] local
 stringi          1.7.5    2021-10-04 [1] CRAN (R 4.1.1)
 stringr          1.4.0    2019-02-10 [1] CRAN (R 4.0.4)
 survival         3.2-13   2021-08-24 [4] CRAN (R 4.1.1)
 tensorA          0.36.2   2020-11-19 [1] CRAN (R 4.0.4)
 TH.data          1.1-0    2021-09-27 [1] CRAN (R 4.1.1)
 threejs          0.3.3    2020-01-21 [1] CRAN (R 4.0.4)
 tibble           3.1.5    2021-09-30 [1] CRAN (R 4.1.1)
 tidyselect       1.1.1    2021-04-30 [1] CRAN (R 4.0.5)
 utf8             1.2.2    2021-07-24 [1] CRAN (R 4.1.0)
 V8               3.4.2    2021-05-01 [1] CRAN (R 4.0.5)
 vctrs            0.3.8    2021-04-29 [1] CRAN (R 4.0.5)
 xfun             0.27     2021-10-18 [1] CRAN (R 4.1.1)
 xtable           1.8-4    2019-04-21 [1] CRAN (R 4.0.4)
 xts              0.12.1   2020-09-09 [1] CRAN (R 4.0.4)
 yaml             2.2.1    2020-02-01 [1] CRAN (R 4.0.4)
 zoo              1.8-9    2021-03-09 [1] CRAN (R 4.0.4)

CmdStan version:

> cmdstanr::cmdstan_version()
[1] "2.28.1"
paul-buerkner commented 3 years ago

I agree, and already have a TODO in the code. I don't have time to work on this at the moment but would appreciate a PR if anyone has time to work on it.

paul-buerkner commented 10 months ago

This is actually implemented already.

fweber144 commented 10 months ago

Thanks!