Wrapper around several ways of converting objects to mcmc.list format, automated based on the input object class.

post_convert(obj)

Arguments

obj

An object storing posterior samples from an MCMC algorithm. Accepted classes are list, matrix, stanfit, bugs, rjags.

Value

The same information as passed in the obj argument, but formatted as mcmc.list class.

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.

Note

  • 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.

See also

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))
#> [1] "mcmc.list"
## 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")
#> param1 param2 param3 param4 param5 #> mean -0.2405483 -0.02659635 0.14732366 0.009101542 0.3700505 #> sd 1.0889150 0.90802879 1.01711781 1.044506489 1.0530227 #> 50% -0.2597395 0.06917890 0.05828312 0.034392545 0.4834371 #> 2.5% -2.2597993 -1.62761125 -1.72047568 -2.006158943 -1.5991835 #> 97.5% 2.0974833 1.76265081 2.29968269 2.032693411 2.0556730
## 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")
#> param1 param2 param3 param4 param5 #> mean 0.05067511 -0.04485533 0.05532735 -0.1874525 0.06014872 #> sd 1.05573920 1.03113795 1.02712251 0.9799174 0.93816522 #> 50% 0.06089166 0.16803656 -0.13232099 -0.3274543 -0.02304236 #> 2.5% -1.69592511 -2.41449420 -1.50040514 -1.9526691 -1.99189283 #> 97.5% 2.04088203 1.43784392 1.82063153 1.7346364 1.86971692