fit-method-draws | R Documentation |
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")
)
variables |
(character vector) Optionally, the names of the variables (parameters, transformed parameters, and generated quantities) to read in.
|
inc_warmup |
(logical) Should warmup draws be included? Defaults to
|
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.
To use a different format it can be specified as the full name of the
format from the posterior package (e.g. Changing the default format: To change the default format for an entire
R session use Note about efficiency: For models with a large number of parameters
(20k+) we recommend using the |
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.
CmdStanMCMC
, CmdStanMLE
, CmdStanVB
, CmdStanGQ
## Not run:
# 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)
str(draws)
# can easily convert to other formats (data frame, matrix, list)
# using the posterior package
head(posterior::as_draws_matrix(draws))
# or can specify 'format' argument to avoid manual conversion
# matrix format combines all chains
draws <- fit$draws(format = "matrix")
head(draws)
# can select specific parameters
fit$draws("alpha")
fit$draws("beta") # selects entire vector beta
fit$draws(c("alpha", "beta[2]"))
# 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
head(fit$draws("beta", format = "df"))
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.