post_convert | R Documentation |
Wrapper around several ways of converting objects to mcmc.list
format,
automated based on the input object class.
post_convert(obj)
obj |
An object storing posterior samples from an MCMC algorithm.
Accepted classes are |
Accepted classes are produced by several packages, including but probably not limited to:
stanfit
objects are created by rstan::stan()
, which also provides rstan::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 a stanfit
object.
bugs
objects are created by R2WinBUGS::bugs()
and R2OpenBUGS::bugs()
.
rjags
objects are created by R2jags::jags()
.
list
objects are created by nimble::runMCMC()
, 'MCMCpack' functions, or custom MCMC algorithms.
matrix
objects are created by post_subset()
and is often the format of posterior quantities derived from monitored nodes.
mcmc.list
objects are created by rjags::coda.samples()
, jagsUI::jags.basic()
, and jagsUI::jags()
$samples
. If a mcmc.list
object is passed to obj
, 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.
The same information as passed in the obj
argument, but formatted as mcmc.list
class.
If samples are stored in a list
object, the individual elements must be matrix
or mcmc
class, storing the samples (rows) across parameters (columns, with names) for each chain (list
elements). If list
elements are in matrix
format, they will be coerced to mcmc
format, and thinning, start, and end intervals may be inaccurate.
If samples are stored in a matrix
object, rows should store samples and columns should store nodes. Multiple chains should be combined using base::rbind()
. Two additional columns must be present: "CHAIN"
and "ITER"
, which denote the MCMC chain and iteration numbers, respectively.
coda::as.mcmc.list()
, coda::as.mcmc()
## 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")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.