| post_convert {postpack} | R Documentation |
Convert MCMC samples to mcmc.list format
Description
Wrapper around several ways of converting objects to mcmc.list format,
automated based on the input object class.
Usage
post_convert(obj)
Arguments
obj |
An object storing posterior samples from an MCMC algorithm.
Accepted classes are |
Details
Accepted classes are produced by several packages, including but probably not limited to:
-
stanfitobjects are created byrstan::stan(), which also providesrstan::As.mcmc.list(). Rather than requiring users to have 'rstan' installed to use 'postpack',post_convert()will instruct users to use this function if supplied astanfitobject. -
bugsobjects are created byR2WinBUGS::bugs()andR2OpenBUGS::bugs(). -
rjagsobjects are created byR2jags::jags(). -
listobjects are created bynimble::runMCMC(), 'MCMCpack' functions, or custom MCMC algorithms. -
matrixobjects are created bypost_subset()and is often the format of posterior quantities derived from monitored nodes. -
mcmc.listobjects are created byrjags::coda.samples(),jagsUI::jags.basic(), andjagsUI::jags()$samples. If amcmc.listobject is passed toobj, an error will be returned telling the user this function is not necessary.
If you find that a critical class conversion is missing, please submit an issue requesting its addition with a minimum working example of how it can be created.
Value
The same information as passed in the obj argument, but formatted as mcmc.list class.
Note
If samples are stored in a
listobject, the individual elements must bematrixormcmcclass, storing the samples (rows) across parameters (columns, with names) for each chain (listelements). Iflistelements are inmatrixformat, they will be coerced tomcmcformat, and thinning, start, and end intervals may be inaccurate.If samples are stored in a
matrixobject, rows should store samples and columns should store nodes. Multiple chains should be combined usingbase::rbind(). Two additional columns must be present:"CHAIN"and"ITER", which denote the MCMC chain and iteration numbers, respectively.
See Also
coda::as.mcmc.list(), coda::as.mcmc()
Examples
## EXAMPLE 1
# load example mcmc.list
data(cjs)
# take a subset from cjs as a matrix, retain chain and iter ids
cjs_sub = post_subset(cjs, "^B", matrix = TRUE, chains = TRUE, iters = TRUE)
# convert back to mcmc.list
class(post_convert(cjs_sub))
## EXAMPLE 2: create mcmc.list from hypothetical MCMC samples; chains are list elements
# create hypothetical samples; can't use postpack on this - not an mcmc.list
samps = lapply(1:3, function(i) {
m = matrix(rnorm(100), 20, 5)
colnames(m) = paste0("param", 1:5)
m
})
# convert
samps_new = post_convert(samps)
# can use postpack now
post_summ(samps_new, "param")
## EXAMPLE 3: create mcmc.list from hypothetical MCMC samples; chains rbind-ed matrices
# create samples
f = function() {
m = matrix(rnorm(100), 20, 5)
colnames(m) = paste0("param", 1:5)
m
}
samps = rbind(f(), f(), f())
# assign chain and iter IDs to each sample
samps = cbind(CHAIN = rep(1:3, each = 20), ITER = rep(1:20, 3), samps)
# convert
samps_new = post_convert(samps)
# can use postpack now
post_summ(samps_new, "param")