MCMCvis (version 0.13.5)

MCMCchains: Extract posterior chains from MCMC output

Description

Extract posterior chains from MCMC output for specific parameters of interest.

Usage

MCMCchains(object, params = "all", excl = NULL, ISB = TRUE,
  mcmc.list = FALSE, chain_num = NULL)

Arguments

object

Object containing MCMC output. See DETAILS below.

params

Character string (or vector of character strings) denoting parameters of interest.

Default 'all' returns chains for all parameters.

excl

Character string (or vector of character strings) denoting parameters to exclude. Used in conjunction with params argument to select parameters of interest.

ISB

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 regular expression format. This allows partial names to be used when specifying parameters of interest.

mcmc.list

Logical specifying whether to return an mcmc.list. If TRUE, an mcmc.list object is returned, rather than a matrix.

chain_num

Numeric - specifies posterior chain number. When a value is specified, posterior for only that chain is output. Useful for determining the last iteration for each parameter, to be used as initial values in a subsequent model, to effectively 'continue' a model run.

Details

Function returns matrix with one parameter per column (for specified parameters). Each iteration is represented as a row. Multiple chains for each parameter are combined to one posterior chain (unless chain_num is specified, in which case only the specified chain will be returned). Parameters are arranged in columns alphabetically.

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.

Examples

Run this code
# NOT RUN {
#Load data
data(MCMC_data)

#Extract MCMC chains
ex <- MCMCchains(MCMC_data)
apply(ex, 2, mean)

#Extract MCMC chains for just 'beta' parameters
ex2 <- MCMCchains(MCMC_data, params = 'beta')
apply(ex2, 2, mean)

#Just 'beta[1]', 'beta[4]', and 'alpha[3]'
#'params' takes regular expressions when ISB = FALSE, square brackets must be escaped with '\\'
ex3 <- MCMCchains(MCMC_data, params = c('beta\\[1\\]', 'beta\\[4\\]', 'alpha\\[3\\]'), ISB = FALSE)
apply(ex3, 2, sd)

# }

Run the code above in your browser using DataLab