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 list, matrix, stanfit, bugs, rjags.

Details

Accepted classes are produced by several packages, including but probably not limited to:

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

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")

[Package postpack version 0.5.4 Index]