Extract summary information from MCMC output (mean, median, quantiles, Gelman-Rubin convergence statistic, number of effective samples, and specified custom metrics) for specific parameters of interest.
MCMCsummary(object, params = "all", excl = NULL, ISB = TRUE,
probs = c(0.025, 0.5, 0.975), hpd_prob = 0.95, HPD = FALSE,
digits = NULL, round = NULL, Rhat = TRUE, n.eff = TRUE,
func = NULL, func_name = NULL)
Object containing MCMC output. See DETAILS below.
Character string (or vector of character strings) denoting parameters to be returned in summary output.
Default 'all'
returns all parameters in summary output.
Character string (or vector of character strings) denoting parameters to exclude. Used in conjunction with params
argument to select parameters of interest.
Ignore Square Brackets (ISB). Logical specifying whether square brackets should be ignored in the params
and excl
arguments. If TRUE
, square brackets are ignored - input from params
and excl
are otherwise matched exactly. If FALSE
, square brackets are not ignored - input from params
and excl
are matched using grep, which can take arguments in regular expression format. This allows partial names to be used when specifying parameters of interest.
Numeric vector where each element in (0,1) representing probabilities used to calculate posterior sample quantiles for the selected parameters. Default is c(0.025, 0.5, 0.975).
Scalar in (0,1) representing probability used to calculate highest posterior density intervals for the selected parameters. Default is 0.95.
Logical specifying whether to calculate equal-tailed credible intervals (HPD = FALSE
) or highest posterior density intervals (HPD = TRUE
) for the selected parameters. Default is HPD = FALSE
.
Number of significant digits to include for posterior summary. All computed digits will be included by default. Note that Rhat is always rounded to 2 decimal places.
Number of decimal places to round to for posterior summary. Cannot be used in conjunction with digits
argument. Note that Rhat is always rounded to 2 decimal places.
Logical specifying whether to calculate and display the potential scale reduction statistic (Rhat). Values near 1 suggest convergence (Brooks and Gelman 1998). Rhat = FALSE
will prevent display of this column in summary output. Specifying Rhat = FALSE
, may increase function speed for very large mcmc.list
objects.
Logical specifying whether to calculate and display the number of effective samples for each parameter. n.eff = FALSE
will prevent display of this column in summary output. Specifying n.eff = FALSE
, may increase function speed for very large mcmc.list
objects. Default is n.eff = TRUE
.
Function to be performed on MCMC output. If a function is specified, it will be evaluated on posteriors for each specified parameter and returned as a column in the summary output (or multiple columns if the function returns more than one value).
Character string (or vector of character strings) specifying labels for output from func
argument. If func_name
is not specified, columns with func
argument will be labeled 'func'.
Function returns summary information (including parameter posterior mean, posterior sd, quantiles, potential scale reduction statistic (Rhat), number of effective samples, and other specified metrics) for specified parameters.
For mcmc.list
objects, the potential scale reduction statistic statistic (Rhat) is calculated using the gelman.diag
function in the coda
package (what is typically displayed in the summary output from models fit with JAGS). For stanfit
objects (as well as stanreg
and brmsfit
objects), Rhat is calculated using the rstan
package which computes a 'split chain' Rhat, which is thought to be a more conservative diagnostic (Stan Development Team 2018).
For mcmc.list
objects, the number of effective samples is calculated using the effectiveSize
function in the coda
package. For stanfit
objects (as well as stanreg
and brmsfit
objects), n.eff is calculated using the rstan
package which (in a similar way to the Rhat computation noted above) employs a slightly different (and more conservative) method of computation for the number of effective samples (Stan Development Team 2018).
object
argument can be a stanfit
object (rstan
package), a stanreg
object (rstanarm
package), a brmsfit
object (brms
package), an mcmc.list
object (coda
package), an R2jags
model object (R2jags
package), a jagsUI
model object (jagsUI
package), or a matrix containing MCMC chains (each column representing MCMC output for a single parameter, rows representing iterations in the chain). The function automatically detects the object type and proceeds accordingly.
Brooks, S. P., and A. Gelman. 1998. General methods for monitoring convergence of iterative simulations. Journal of Computational and Graphical Statistics 7:434.
Stan Development Team. 2018. Stan Modeling Language Users Guide and Reference Manual, Version 2.18.0. http://mc-stan.org
# NOT RUN {
#Load data
data(MCMC_data)
#Summary information for MCMC output - display 2 significant digits
MCMCsummary(MCMC_data, digits = 2)
#Just 'beta' parameters - round to 2 decimal places
MCMCsummary(MCMC_data, params = 'beta', round = 2)
#Just 'beta[1]', 'beta[4]', and 'alpha[3]'
#'params' takes regular expressions when ISB = FALSE, square brackets must be escaped with '\\'
MCMCsummary(MCMC_data, params = c('beta\\[1\\]', 'beta\\[4\\]', 'alpha\\[3\\]'), ISB = FALSE)
# }
Run the code above in your browser using DataLab