as_draws-mcmc_output: Convert 'run_mcmc' Output to 'draws_df' Format

as_draws_df.mcmc_outputR Documentation

Convert run_mcmc Output to draws_df Format

Description

Converts MCMC output from run_mcmc call to a draws_df format of the posterior package. This enables the use of diagnostics and plotting methods of posterior and bayesplot packages.

Usage

## S3 method for class 'mcmc_output'
as_draws_df(x, times, states, ...)

## S3 method for class 'mcmc_output'
as_draws(x, times, states, ...)

Arguments

x

An object of class mcmc_output.

times

A vector of indices defining which time points to return? Default is all. If 0, no samples for the states are extracted.

states

A vector of indices defining which states to return. Default is all. If 0, no samples for the states are extracted.

...

Ignored.

Value

A draws_df object.

Note

The jump chain representation is automatically expanded by as_draws, but if run_mcmc used IS-MCMC method, the output contains additional weight column corresponding to the IS-weights (without counts), which is ignored by posterior and bayesplot, i.e. those results correspond to approximate MCMC.

Examples


model <- bsm_lg(Nile,
  sd_y = tnormal(init = 100, mean = 100, sd = 100, min = 0),
  sd_level = tnormal(init = 50, mean = 50, sd = 100, min = 0),
  a1 = 1000, P1 = 500^2)

fit1 <- run_mcmc(model, iter = 2000)
draws <- as_draws(fit1)
head(draws, 4)
estimate_ess(draws$sd_y)
summary(fit1, return_se = TRUE)

# More chains:
model$theta[] <- c(50, 150) # change initial value
fit2 <- run_mcmc(model, iter = 2000, verbose = FALSE)
model$theta[] <- c(150, 50) # change initial value
fit3 <- run_mcmc(model, iter = 2000, verbose = FALSE)

# it is actually enough to transform first mcmc_output to draws object,
# rest are transformed automatically inside bind_draws
draws <- posterior::bind_draws(as_draws(fit1),
  as_draws(fit2), as_draws(fit3), along = "chain")

posterior::rhat(draws$sd_y)


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