knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-"
)

library(mmcc)

mmcc

R-CMD-check Codecov test coverage

Tidying up MCMC output can be a real pain. There are plenty of packages that help with summarising MCMC and providing their own summaries, but sometimes you just want a tidy data structure so you can do your own thing. And quickly.

mmcc provides tidying functions that return tidy data structure from mcmc.list objects. It uses data.table as the backend for speediness, it also provides broom tidiers to assist in some quick summaries.

Installation

Install from github using:

# install.packages("remotes")
remotes::install_github("njtierney/mmcc")

Using mmcc

mcmc_to_dt takes an mcmc.list object and turns it into a data.table of the format:

library(coda)
data(line)

head(data.frame(line$line1))
library(mmcc)

mcmc_dt <- mcmc_to_dt(line)

mcmc_dt

tidy.mcmc.list takes an mcmc.list, turns it into a data.table and summarises it in terms of each parameter's mean, median, standard deviation and credible interval with level given by conf.level:

``` {r show-tidy-method} tidy(line)

We can also optionally ask for a subset of the parameters with a vector of `colnames` and summarise for each chain:

``` {r show-tidy-options}
tidy(line, 
     chain = TRUE, 
     colnames=c("alpha"))

This may be useful if we want to make a plot that shows how a given parameter varies from chain to chain.

``` {r show-tidy-ggplot, fig.height=3, fig.width=7} library(ggplot2)

line_tidy <- tidy(line, chain = TRUE)

ggplot(data = line_tidy, aes(x = factor(chain), y = mean)) + geom_pointrange(aes(ymin = 2.5%, ymax = 97.5%)) + facet_wrap(~parameter, nrow = 1, scales = "free_y") + theme_bw() + xlab("Chain") + ylab("Value")

# Helpers

`mmcc` provides helpers to extract the number of chains, iterations, simulations, and variables in an MCMC object:

```r
n_chain(line)
n_iter(line)
n_sims(line)
n_var(line)

glance helper

General model summary providing information on:

glance(line)

Diagnostics

diag_autocorr(line)
diag_ess(line)
diag_mc_stderr(line)

Thinning

thin_dt(mcmc_to_dt(line), thin = 10)

Why the name, "mmcc"?

Full credit does to Sam Clifford for the name.

To quote Sam:

...it's all about reshaping and manipulating mcmc chains...therefore, mmcc

Future work

Code of Conduct

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.



njtierney/mmcc documentation built on Oct. 5, 2021, 12:14 a.m.