New posterior::extract_list_of_variable_arrays()

Recently people have been refering to @andrjohnspost saying

github version of posterior package has now variant of the code

  • existing extract_variable_array(), which extracts just one variable, has now argument with_chains, with the default value TRUE for backward compatibility
  • new extract_list_of_variable_arrays() extracts all variables, and it works with all posterior object types (no need for the fit object)

These are helpful for extracting arrays of multivariate variables

Pinging also @GabrielR and @Michael_Peck who liked @andrjohns’ solution

edit: added sub-category

3 Likes

Awesome!

+1 to that. This has been super useful in CmdStanPy. It’s also nice to be able to extract just one variable, too.

@avehtari

I think there is a small incorrectness in the documentation:

Usage

extract_list_of_variable_arrays(x, variables = NULL, ...)

The parameter is variable, getting an error message:

> extr2<-extract_variable_array(fit,variables=c("beta"))

Error in extract_variable_array.default(fit, variables = c(“beta”)) :
argument “variable” is missing, with no default

Further, trying to extract all parameters the following error occurs:

extr2<-extract_variable_array(fit,variable=NULL)

Error: Cannot coerce ‘variable’ to a single character value.

extr2<-extract_variable_array(fit$draws())

results in the same message.

> sessionInfo()
R version 4.5.2 (2025-10-31)
Platform: x86_64-pc-linux-gnu
Running under: Manjaro Linux

Matrix products: default
BLAS/LAPACK: /usr/lib/libopenblas.so.0.3; LAPACK version 3.12.0

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

time zone: Asia/Bangkok
tzcode source: system (glibc)

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

other attached packages:
[1] posterior_1.6.1.9000

loaded via a namespace (and not attached):
[1] tensorA_0.36.2.1 generics_0.1.4 lattice_0.22-7 magrittr_2.0.4 ROracle_1.4-1 evaluate_1.0.3
[7] grid_4.5.2 RColorBrewer_1.1-3 pkgload_1.4.0 Matrix_1.7-4 jsonlite_2.0.0 processx_3.8.6
[13] pkgbuild_1.4.7 backports_1.5.0 DBI_1.2.3 ps_1.9.1 gridExtra_2.3 mgcv_1.9-3
[19] QuickJSR_1.7.0 scales_1.4.0 codetools_0.2-20 abind_1.4-8 cli_3.6.5 rlang_1.1.7
[25] cmdstanr_0.9.0 splines_4.5.2 remotes_2.5.0 StanHeaders_2.36.0.9000 tools_4.5.2 rstan_2.36.0.9000
[31] inline_0.3.21 parallel_4.5.2 checkmate_2.3.3 dplyr_1.1.4 ggplot2_4.0.1 curl_6.2.2
[37] vctrs_0.7.1 R6_2.6.1 matrixStats_1.5.0 stats4_4.5.2 lifecycle_1.0.5 callr_3.7.6
[43] desc_1.4.3 pkgconfig_2.0.3 RcppParallel_5.1.10 pillar_1.11.1 gtable_0.3.6 loo_2.8.0
[49] glue_1.8.0 Rcpp_1.1.0 xfun_0.54 tibble_3.3.1 tidyselect_1.2.1 rstudioapi_0.17.1
[55] knitr_1.50 farver_2.1.2 nlme_3.1-168 compiler_4.5.2 S7_0.2.1 distributional_0.6.0

The documentation is correct. For extract_list_of_variable_arrays() the argument is variables and for extract_variable_array() the argument is variable. In one way this makes sense as the first one is usually used for several variables and the second one can be used only for one variable. However, for example, subset_draws() uses variable also for multiple variables, and it would be logical to change the documentation to say variable. Due to how R arguments behave, the current version works with both variable and variables.

Yes, because you are trying to use extract_variable_array() instead of extract_list_of_variable_arrays()

The same reason.