R/bayesplot-extractors.R
bayesplot-extractors.Rd
Generics and methods for extracting quantities needed for plotting from various types of model objects. Currently methods are provided for stanfit (rstan), CmdStanMCMC (cmdstanr), and stanreg (rstanarm) objects, but adding new methods should be relatively straightforward.
log_posterior(object, ...)
nuts_params(object, ...)
rhat(object, ...)
neff_ratio(object, ...)
# S3 method for stanfit
log_posterior(object, inc_warmup = FALSE, ...)
# S3 method for stanreg
log_posterior(object, inc_warmup = FALSE, ...)
# S3 method for CmdStanMCMC
log_posterior(object, inc_warmup = FALSE, ...)
# S3 method for stanfit
nuts_params(object, pars = NULL, inc_warmup = FALSE, ...)
# S3 method for stanreg
nuts_params(object, pars = NULL, inc_warmup = FALSE, ...)
# S3 method for list
nuts_params(object, pars = NULL, ...)
# S3 method for CmdStanMCMC
nuts_params(object, pars = NULL, ...)
# S3 method for stanfit
rhat(object, pars = NULL, ...)
# S3 method for stanreg
rhat(object, pars = NULL, regex_pars = NULL, ...)
# S3 method for CmdStanMCMC
rhat(object, pars = NULL, ...)
# S3 method for stanfit
neff_ratio(object, pars = NULL, ...)
# S3 method for stanreg
neff_ratio(object, pars = NULL, regex_pars = NULL, ...)
# S3 method for CmdStanMCMC
neff_ratio(object, pars = NULL, ...)
The object to use.
Arguments passed to individual methods.
A logical scalar (defaulting to FALSE
) indicating
whether to include warmup draws, if applicable.
An optional character vector of parameter names. For
nuts_params()
these will be NUTS sampler parameter names rather than
model parameters. If pars
is omitted all parameters are included.
An optional regular expression to use for
parameter selection. Can be specified instead of pars
or in addition to
pars
. When using pars
for tidy parameter selection, the regex_pars
argument is ignored since select helpers
perform a similar function.
log_posterior()
log_posterior()
methods return a molten data frame (see reshape2::melt()
).
The data frame should have columns "Iteration"
(integer), "Chain"
(integer), and "Value"
(numeric). See Examples, below.
nuts_params()
nuts_params()
methods return a molten data frame (see reshape2::melt()
).
The data frame should have columns "Parameter"
(factor), "Iteration"
(integer), "Chain"
(integer), and "Value"
(numeric). See Examples, below.
rhat()
, neff_ratio()
Methods return (named) vectors.
# \dontrun{
library(rstanarm)
fit <- stan_glm(mpg ~ wt, data = mtcars, refresh = 0)
np <- nuts_params(fit)
head(np)
#> Chain Iteration Parameter Value
#> 1 1 1 accept_stat__ 0.9959800
#> 2 1 2 accept_stat__ 0.9990818
#> 3 1 3 accept_stat__ 0.9922949
#> 4 1 4 accept_stat__ 0.9592249
#> 5 1 5 accept_stat__ 0.9959713
#> 6 1 6 accept_stat__ 0.9846108
tail(np)
#> Chain Iteration Parameter Value
#> 23995 4 995 energy__ 86.09516
#> 23996 4 996 energy__ 86.63987
#> 23997 4 997 energy__ 87.05197
#> 23998 4 998 energy__ 91.53823
#> 23999 4 999 energy__ 88.10087
#> 24000 4 1000 energy__ 89.14512
lp <- log_posterior(fit)
head(lp)
#> Chain Iteration Value
#> 1 1 1 -86.46143
#> 2 1 2 -86.45492
#> 3 1 3 -86.55465
#> 4 1 4 -87.06081
#> 5 1 5 -86.32876
#> 6 1 6 -86.77675
tail(lp)
#> Chain Iteration Value
#> 3995 4 995 -85.93512
#> 3996 4 996 -86.07147
#> 3997 4 997 -86.54743
#> 3998 4 998 -87.97370
#> 3999 4 999 -86.85226
#> 4000 4 1000 -88.45662
# }