expand_sample: Expand the Jump Chain representation

View source: R/expand_sample.R

expand_sampleR Documentation

Expand the Jump Chain representation

Description

The MCMC algorithms of bssm use a jump chain representation where we store the accepted values and the number of times we stayed in the current value. Although this saves bit memory and is especially convenient for IS-corrected MCMC, sometimes we want to have the usual sample paths (for example for drawing traceplots). Function expand_sample returns the expanded sample based on the counts (in form of coda::mcmc object. Note that for the IS-MCMC the expanded sample corresponds to the approximate posterior, i.e., the weights are ignored.

Usage

expand_sample(x, variable = "theta", times, states, by_states = TRUE)

Arguments

x

Output from run_mcmc.

variable

Expand parameters "theta" or states "states".

times

A vector of indices. In case of states, what time points to expand? Default is all.

states

A vector of indices. In case of states, what states to expand? Default is all.

by_states

If TRUE (default), return list by states. Otherwise by time.

Details

This functions is mostly for backwards compatibility, methods as.data.frame and as_draws produce likely more convenient output.

Value

An object of class "mcmc" of the coda package.

See Also

as.data.frame.mcmc_output and as_draws.mcmc_output.

Examples

set.seed(1)
n <- 50
x <- cumsum(rnorm(n))
y <- rnorm(n, x)
model <- bsm_lg(y, sd_y = gamma_prior(1, 2, 2), 
  sd_level = gamma_prior(1, 2, 2))
fit <- run_mcmc(model, iter = 1e4)
# Traceplots for theta
plot.ts(expand_sample(fit, variable = "theta"))
# Traceplot for x_5
plot.ts(expand_sample(fit, variable = "states", times = 5, 
  states = 1)$level)

bssm documentation built on Nov. 2, 2023, 6:25 p.m.