Draws from another sampler
external_draws.RdBrings posterior draws from Stan, PyMC, nutpie or any other sampler into
prospicio's diagnostics (#395). read_stan_csv() reads CmdStan's CSV
output, one file per chain: comments skipped, saved warmup draws dropped,
theta.1.2 named theta[1,2], lp__ kept, the sampler's other __
columns and each chain's divergent transitions kept as attributes.
mcmc_draws() takes draws already in R. draws_summary() and
draws_loo() then give the summary, R-hat and ESS as
posterior::summarise_draws() and PSIS-LOO as
loo::loo(log_lik, r_eff = loo::relative_eff(exp(log_lik))).
Usage
read_stan_csv(files)
mcmc_draws(x, divergences = NULL)
draws_summary(x)
draws_loo(x, log_lik = "log_lik")Arguments
- files
CmdStan CSV files, one per chain.
- x
For
mcmc_draws(): a 3-D array iteration x chain x variable with the variable names as its third dimnames (the layout ofposterior::draws_array), anyposteriordraws object (converted withposterior::as_draws_array()), an rstanstanfit(as.array()) or a cmdstanr fit ($draws(), with its divergences). Fordraws_summary()anddraws_loo(): draws fromread_stan_csv()ormcmc_draws(), or anythingmcmc_draws()takes.- divergences
Divergent transitions in each chain, if known.
- log_lik
The name of the pointwise log-likelihood variable, held as
log_lik[1],log_lik[2], ...
Value
read_stan_csv() and mcmc_draws(): a 3-D array iteration x
chain x variable, with attributes divergences (per chain) and, from
read_stan_csv(), sampler (the sampler's columns as another such
array) and warmup_dropped. draws_summary(): a data frame with
variable, mean, sd, q05, q50, q95, rhat, ess_bulk and
ess_tail. draws_loo(): a list as elpd_loo()'s, with r_eff per
observation.
Examples
set.seed(1)
x <- array(rnorm(4 * 200 * 2), c(200, 4, 2),
dimnames = list(NULL, NULL, c("mu", "sigma")))
draws_summary(mcmc_draws(x))
#> variable mean sd q05 q50 q95 rhat
#> 1 mu -0.016327961 1.026844 -1.665950 -0.05496689 1.682511 1.002138
#> 2 sigma -0.002474132 1.029644 -1.699394 -0.01304186 1.737531 1.003075
#> ess_bulk ess_tail
#> 1 876.3500 773.4439
#> 2 781.1723 813.5890