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)

Arguments

variables

(character vector) The variables to read in. If NULL (the default) then all variables are included.

inc_warmup

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

Value

  • 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). 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.

See also

Examples

# \dontrun{ library(posterior) library(bayesplot) color_scheme_set("brightblue") # logistic regression with intercept alpha and coefficients beta fit <- cmdstanr_example("logistic")
#> Model executable is up to date!
# 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] -67.7 -67.3 -66.9 -67.2 -68.1 ... #> - 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) as_draws_df(draws) # see also as_draws_matrix, as_draws_list
#> # A draws_df: 1000 iterations, 4 chains, and 105 variables #> lp__ alpha beta[1] beta[2] beta[3] log_lik[1] log_lik[2] log_lik[3] #> 1 -68 0.56 -1.19 -0.581 0.65 -0.41 -0.42 -0.51 #> 2 -67 0.15 -0.85 -0.681 0.46 -0.61 -0.46 -1.00 #> 3 -67 0.60 -0.54 0.191 0.66 -0.40 -0.39 -0.18 #> 4 -67 0.14 -0.71 -0.748 0.97 -0.69 -0.27 -1.01 #> 5 -68 0.43 -0.64 0.340 0.73 -0.43 -0.28 -0.14 #> 6 -66 0.42 -0.75 0.116 0.79 -0.45 -0.27 -0.20 #> 7 -65 0.58 -0.56 -0.056 0.78 -0.43 -0.38 -0.27 #> 8 -64 0.42 -0.75 -0.122 0.82 -0.48 -0.30 -0.31 #> 9 -65 0.34 -0.40 -0.508 0.46 -0.57 -0.59 -0.84 #> 10 -65 0.65 -0.59 -0.022 0.64 -0.39 -0.46 -0.25 #> # ... with 3990 more draws, and 97 more variables #> # ... hidden reserved variables {'.chain', '.iteration', '.draw'}
# 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.56 0.634 0.36 0.57 #> 2 0.15 0.097 0.53 0.16 #> 3 0.60 0.943 0.36 0.53 #> 4 0.14 0.481 0.30 0.46 #> 5 0.43 0.275 0.15 0.52 #> #> # ... 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 -1.19 -0.42 -1.11 -0.59 #> 2 -0.85 -0.81 -1.09 -0.40 #> 3 -0.54 -0.62 -0.23 -1.11 #> 4 -0.71 -0.35 -1.15 -0.83 #> 5 -0.64 -0.86 -0.70 -0.76 #> #> , , variable = beta[2] #> #> chain #> iteration 1 2 3 4 #> 1 -0.58 -0.293 -0.40 -0.671 #> 2 -0.68 -0.325 -0.62 -0.088 #> 3 0.19 -0.244 -0.12 -0.436 #> 4 -0.75 -0.402 -0.45 -0.222 #> 5 0.34 -0.074 -0.52 -0.390 #> #> , , variable = beta[3] #> #> chain #> iteration 1 2 3 4 #> 1 0.65 0.42 0.54 0.91 #> 2 0.46 1.00 0.79 0.84 #> 3 0.66 0.85 0.69 0.54 #> 4 0.97 0.82 0.73 0.81 #> 5 0.73 0.29 1.34 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.56 0.634 0.36 0.57 #> 2 0.15 0.097 0.53 0.16 #> 3 0.60 0.943 0.36 0.53 #> 4 0.14 0.481 0.30 0.46 #> 5 0.43 0.275 0.15 0.52 #> #> , , variable = beta[2] #> #> chain #> iteration 1 2 3 4 #> 1 -0.58 -0.293 -0.40 -0.671 #> 2 -0.68 -0.325 -0.62 -0.088 #> 3 0.19 -0.244 -0.12 -0.436 #> 4 -0.75 -0.402 -0.45 -0.222 #> 5 0.34 -0.074 -0.52 -0.390 #> #> # ... with 995 more iterations
# can be passed directly to bayesplot plotting functions mcmc_dens(fit$draws(c("alpha", "beta")))
mcmc_scatter(fit$draws(c("beta[1]", "beta[2]")), alpha = 0.3)
# }