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] -64.2 -65.3 -65.3 -64.7 -64.8 ...
#>  - 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  -64  0.28   -0.73   -0.16    0.73      -0.53      -0.30      -0.39
#>    2  -65  0.19   -0.42   -0.13    0.37      -0.59      -0.48      -0.53
#>    3  -65  0.61   -0.93   -0.25    0.83      -0.40      -0.34      -0.30
#>    4  -65  0.42   -0.81   -0.32    0.94      -0.50      -0.28      -0.41
#>    5  -65  0.36   -0.41   -0.20    0.39      -0.52      -0.56      -0.52
#>    6  -67  0.55   -0.24   -0.43    0.24      -0.49      -0.87      -0.75
#> # ... 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  -64  0.28   -0.73   -0.16    0.73      -0.53      -0.30      -0.39
#>    2  -65  0.19   -0.42   -0.13    0.37      -0.59      -0.48      -0.53
#>    3  -65  0.61   -0.93   -0.25    0.83      -0.40      -0.34      -0.30
#>    4  -65  0.42   -0.81   -0.32    0.94      -0.50      -0.28      -0.41
#>    5  -65  0.36   -0.41   -0.20    0.39      -0.52      -0.56      -0.52
#>    6  -67  0.55   -0.24   -0.43    0.24      -0.49      -0.87      -0.75
#> # ... 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.28 0.27 0.62 0.10
#>         2 0.19 0.70 0.49 0.71
#>         3 0.61 0.26 0.89 0.14
#>         4 0.42 0.39 0.58 0.39
#>         5 0.36 0.39 0.58 0.48
#> 
#> # ... 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.73 -0.46 -0.82 -0.55
#>         2 -0.42 -0.92 -1.18 -1.08
#>         3 -0.93 -0.52 -0.63 -0.42
#>         4 -0.81 -0.86 -0.30 -0.72
#>         5 -0.41 -0.86 -0.30 -0.86
#> 
#> , , variable = beta[2]
#> 
#>          chain
#> iteration     1     2     3     4
#>         1 -0.16 -0.31 -0.88 -0.18
#>         2 -0.13 -0.27 -0.69 -0.21
#>         3 -0.25 -0.39 -0.38 -0.37
#>         4 -0.32 -0.43 -0.52 -0.36
#>         5 -0.20 -0.43 -0.52 -0.42
#> 
#> , , variable = beta[3]
#> 
#>          chain
#> iteration    1    2    3    4
#>         1 0.73 0.28 0.87 0.93
#>         2 0.37 1.26 0.75 0.45
#>         3 0.83 0.28 0.88 0.83
#>         4 0.94 0.64 0.71 0.48
#>         5 0.39 0.64 0.71 0.41
#> 
#> # ... 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.28 0.27 0.62 0.10
#>         2 0.19 0.70 0.49 0.71
#>         3 0.61 0.26 0.89 0.14
#>         4 0.42 0.39 0.58 0.39
#>         5 0.36 0.39 0.58 0.48
#> 
#> , , variable = beta[2]
#> 
#>          chain
#> iteration     1     2     3     4
#>         1 -0.16 -0.31 -0.88 -0.18
#>         2 -0.13 -0.27 -0.69 -0.21
#>         3 -0.25 -0.39 -0.38 -0.37
#>         4 -0.32 -0.43 -0.52 -0.36
#>         5 -0.20 -0.43 -0.52 -0.42
#> 
#> # ... 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.62  -0.485    0.63
#>    2   -0.34   0.188    0.77
#>    3   -0.63  -0.236    0.90
#>    4   -0.65  -0.392    0.76
#>    5   -0.57   0.028    0.44
#>    6   -0.58  -0.299    0.69
head(fit$draws("beta", format = "df"))
#> # A draws_df: 6 iterations, 1 chains, and 3 variables
#>   beta[1] beta[2] beta[3]
#> 1   -0.62  -0.485    0.63
#> 2   -0.34   0.188    0.77
#> 3   -0.63  -0.236    0.90
#> 4   -0.65  -0.392    0.76
#> 5   -0.57   0.028    0.44
#> 6   -0.58  -0.299    0.69
#> # ... hidden reserved variables {'.chain', '.iteration', '.draw'}
# }