post_convert: Convert MCMC samples to mcmc.list format

Description Usage Arguments Details Value Note See Also Examples

View source: R/post_convert.R

Description

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

Usage

1

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
## 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")

postpack documentation built on June 3, 2021, 1:07 a.m.