extract_samples: Extract posterior samples from a model fit.

Description Usage Arguments Details Value Examples

View source: R/helper.R

Description

A helper function to extract posterior samples across multiple chains into a single data.frame.

Usage

1
extract_samples(fit, inc_warmup = FALSE, inc_lp = FALSE, as.list = FALSE)

Arguments

fit

A list returned by sample_tmb or sample_admb.

inc_warmup

Whether to extract the warmup samples or not (default). Warmup samples should never be used for inference, but may be useful for diagnostics.

inc_lp

Whether to include a column for the log posterior density (last column). For diagnostics it can be useful.

as.list

Whether to return the samples as a list (one element per chain). This could then be converted to a CODA mcmc object.

Details

This function is loosely based on the rstan function extract. Merging samples across chains should only be used for inference after appropriate diagnostic checks. Do not calculate diagnostics like Rhat or effective sample size after using this function, instead, use monitor. Likewise, warmup samples are not valid and should never be used for inference, but may be useful in some cases for diagnosing issues.

Value

If as.list is FALSE, an invisible data.frame containing samples (rows) of each parameter (columns). If multiple chains exist they will be rbinded together, maintaining order within each chain. If as.list is TRUE, samples are returned as a list of matrices.

Examples

1
2
3
4
## A previously run fitted TMB model
fit <- readRDS(system.file('examples', 'fit_tmb.RDS', package='adnuts'))
post <- extract_samples(fit)
tail(apply(post, 2, median))

colemonnahan/adnuts documentation built on Feb. 13, 2018, 4 p.m.