MCMCchains {MCMCvis} | R Documentation |
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,
exact = 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 |
excl |
Character string (or vector of character strings) denoting parameters to exclude. Used in conjunction with |
ISB |
Ignore Square Brackets (ISB). Logical specifying whether square brackets should be ignored in the |
exact |
Logical specifying whether input from |
mcmc.list |
Logical specifying whether to return an mcmc.list. If |
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 CmdStanMCMC
object (cmdstanr
package), a stanreg
object (rstanarm
package), a brmsfit
object (brms
package), an mcmc.list
object (coda
and rjags
packages), mcmc
object (coda
and nimble
packages), list
object (nimble
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
#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]'
ex3 <- MCMCchains(MCMC_data, params = c('beta[1]', 'beta[4]', 'alpha[3]'),
ISB = FALSE, exact = TRUE)
apply(ex3, 2, sd)