Extract posterior draws after MCMC or approximate posterior draws after variational approximation using formats provided by the posterior package.

The variables include the parameters, transformed parameters, and generated quantities from the Stan program as well as lp__, the total log probability (target) accumulated in the model block.

draws(
  variables = NULL,
  inc_warmup = FALSE,
  format = getOption("cmdstanr_draws_format")
)

Arguments

variables

(character vector) Optionally, the names of the variables (parameters, transformed parameters, and generated quantities) to read in.

  • If NULL (the default) then all variables are included.

  • If an empty string (variables="") then none are included.

  • For non-scalar variables all elements or specific elements can be selected:

    • variables = "theta" selects all elements of theta;

    • variables = c("theta[1]", "theta[3]") selects only the 1st and 3rd elements.

inc_warmup

(logical) Should warmup draws be included? Defaults to FALSE. Ignored except when used with CmdStanMCMC objects.

format

(string) The format of the returned draws or point estimates. Must be a valid format from the posterior package. The defaults are the following.

  • For sampling and generated quantities the default is "draws_array". This format keeps the chains separate. To combine the chains use any of the other formats (e.g. "draws_matrix").

  • For point estimates from optimization and approximate draws from variational inference the default is "draws_matrix".

To use a different format it can be specified as the full name of the format from the posterior package (e.g. format = "draws_df") or omitting the "draws_" prefix (e.g. format = "df").

Changing the default format: To change the default format for an entire R session use options(cmdstanr_draws_format = format), where format is the name (in quotes) of a valid format from the posterior package. For example options(cmdstanr_draws_format = "draws_df") will change the default to a data frame.

Note about efficiency: For models with a large number of parameters (20k+) we recommend using the "draws_list" format, which is the most efficient and RAM friendly when combining draws from multiple chains. If speed or memory is not a constraint we recommend selecting the format that most suits the coding style of the post processing phase.

Value

Depends on the value of format. The defaults are:

  • For MCMC, a 3-D draws_array object (iteration x chain x variable).

  • For standalone generated quantities, a 3-D draws_array object (iteration x chain x variable).

  • For variational inference, a 2-D draws_matrix object (draw x variable) because there are no chains. An additional variable lp_approx__ is also included, which is the log density of the variational approximation to the posterior evaluated at each of the draws.

  • For optimization, a 1-row draws_matrix with one column per variable. These are not actually draws, just point estimates stored in the draws_matrix format. See $mle() to extract them as a numeric vector.

Examples

# \dontrun{
# logistic regression with intercept alpha and coefficients beta
fit <- cmdstanr_example("logistic", method = "sample")

# returned as 3-D array (see ?posterior::draws_array)
draws <- fit$draws()
dim(draws)
#> [1] 1000    4  105
str(draws)
#>  'draws_array' num [1:1000, 1:4, 1:105] -65 -66.8 -64.9 -66.9 -66.5 ...
#>  - attr(*, "dimnames")=List of 3
#>   ..$ iteration: chr [1:1000] "1" "2" "3" "4" ...
#>   ..$ chain    : chr [1:4] "1" "2" "3" "4"
#>   ..$ variable : chr [1:105] "lp__" "alpha" "beta[1]" "beta[2]" ...

# can easily convert to other formats (data frame, matrix, list)
# using the posterior package
head(posterior::as_draws_matrix(draws))
#> # A draws_matrix: 6 iterations, 1 chains, and 105 variables
#>     variable
#> draw lp__  alpha beta[1] beta[2] beta[3] log_lik[1] log_lik[2] log_lik[3]
#>    1  -65  0.363   -0.70   -0.26    1.04      -0.53      -0.24      -0.40
#>    2  -67  0.522   -0.55   -0.61    1.18      -0.54      -0.30      -0.65
#>    3  -65  0.607   -0.57   -0.41    0.82      -0.46      -0.44      -0.49
#>    4  -67 -0.012   -0.52   -0.11    0.25      -0.65      -0.44      -0.59
#>    5  -67  0.715   -0.77   -0.34    1.14      -0.41      -0.29      -0.32
#>    6  -65  0.473   -0.42   -0.41    0.76      -0.52      -0.45      -0.60
#> # ... with 97 more variables

# or can specify 'format' argument to avoid manual conversion
# matrix format combines all chains
draws <- fit$draws(format = "matrix")
head(draws)
#> # A draws_matrix: 6 iterations, 1 chains, and 105 variables
#>     variable
#> draw lp__  alpha beta[1] beta[2] beta[3] log_lik[1] log_lik[2] log_lik[3]
#>    1  -65  0.363   -0.70   -0.26    1.04      -0.53      -0.24      -0.40
#>    2  -67  0.522   -0.55   -0.61    1.18      -0.54      -0.30      -0.65
#>    3  -65  0.607   -0.57   -0.41    0.82      -0.46      -0.44      -0.49
#>    4  -67 -0.012   -0.52   -0.11    0.25      -0.65      -0.44      -0.59
#>    5  -67  0.715   -0.77   -0.34    1.14      -0.41      -0.29      -0.32
#>    6  -65  0.473   -0.42   -0.41    0.76      -0.52      -0.45      -0.60
#> # ... with 97 more variables

# can select specific parameters
fit$draws("alpha")
#> # A draws_array: 1000 iterations, 4 chains, and 1 variables
#> , , variable = alpha
#> 
#>          chain
#> iteration      1    2    3       4
#>         1  0.363 0.52 0.50 -0.0071
#>         2  0.522 0.50 0.46  0.3258
#>         3  0.607 0.19 0.25  0.2788
#>         4 -0.012 0.13 0.41  0.2299
#>         5  0.715 0.48 0.40  0.6308
#> 
#> # ... with 995 more iterations
fit$draws("beta")  # selects entire vector beta
#> # A draws_array: 1000 iterations, 4 chains, and 3 variables
#> , , variable = beta[1]
#> 
#>          chain
#> iteration     1     2     3     4
#>         1 -0.70 -0.74 -0.87 -0.37
#>         2 -0.55 -0.71 -0.58 -0.84
#>         3 -0.57 -0.85 -0.59 -0.37
#>         4 -0.52 -0.66 -0.60 -0.53
#>         5 -0.77 -0.40 -0.55 -0.68
#> 
#> , , variable = beta[2]
#> 
#>          chain
#> iteration     1     2     3      4
#>         1 -0.26 -0.12 -0.28 -0.474
#>         2 -0.61 -0.12 -0.51 -0.487
#>         3 -0.41 -0.10 -0.22 -0.069
#>         4 -0.11 -0.14 -0.13 -0.094
#>         5 -0.34 -0.08 -0.58 -0.328
#> 
#> , , variable = beta[3]
#> 
#>          chain
#> iteration    1    2    3    4
#>         1 1.04 0.96 0.78 0.75
#>         2 1.18 0.90 0.40 0.81
#>         3 0.82 0.29 1.27 0.64
#>         4 0.25 0.45 0.97 0.64
#>         5 1.14 0.18 0.54 0.66
#> 
#> # ... with 995 more iterations
fit$draws(c("alpha", "beta[2]"))
#> # A draws_array: 1000 iterations, 4 chains, and 2 variables
#> , , variable = alpha
#> 
#>          chain
#> iteration      1    2    3       4
#>         1  0.363 0.52 0.50 -0.0071
#>         2  0.522 0.50 0.46  0.3258
#>         3  0.607 0.19 0.25  0.2788
#>         4 -0.012 0.13 0.41  0.2299
#>         5  0.715 0.48 0.40  0.6308
#> 
#> , , variable = beta[2]
#> 
#>          chain
#> iteration     1     2     3      4
#>         1 -0.26 -0.12 -0.28 -0.474
#>         2 -0.61 -0.12 -0.51 -0.487
#>         3 -0.41 -0.10 -0.22 -0.069
#>         4 -0.11 -0.14 -0.13 -0.094
#>         5 -0.34 -0.08 -0.58 -0.328
#> 
#> # ... with 995 more iterations

# can be passed directly to bayesplot plotting functions
bayesplot::color_scheme_set("brightblue")
bayesplot::mcmc_dens(fit$draws(c("alpha", "beta")))

bayesplot::mcmc_scatter(fit$draws(c("beta[1]", "beta[2]")), alpha = 0.3)



# example using variational inference
fit <- cmdstanr_example("logistic", method = "variational")
head(fit$draws("beta")) # a matrix by default
#> # A draws_matrix: 6 iterations, 1 chains, and 3 variables
#>     variable
#> draw beta[1] beta[2] beta[3]
#>    1   -0.80   -0.25    0.98
#>    2   -0.56   -0.31    0.57
#>    3   -0.98   -0.35    0.66
#>    4   -0.67   -0.78    0.49
#>    5   -0.98   -0.38    0.75
#>    6   -0.52   -0.52    0.37
head(fit$draws("beta", format = "df"))
#> # A draws_df: 6 iterations, 1 chains, and 3 variables
#>   beta[1] beta[2] beta[3]
#> 1   -0.80   -0.25    0.98
#> 2   -0.56   -0.31    0.57
#> 3   -0.98   -0.35    0.66
#> 4   -0.67   -0.78    0.49
#> 5   -0.98   -0.38    0.75
#> 6   -0.52   -0.52    0.37
#> # ... hidden reserved variables {'.chain', '.iteration', '.draw'}
# }