The packages mmcc creates tidy summaries of Bayesian models, in the fashion of broom, with one important difference - mmcc uses a data.table instead of a tibble, due to the size of the output that is all too easily possible in Bayesian models.

The aim of this vignette is to demonstrate how to use the two key functions of mmcc, mcmc_to_dt() and tidy (which actually calls mmcc:::tidy.mcmc.list under the hood).

First, we simulate some data to fit a Bayesian model to.

Then, we simulate some values for predicting

Next, we fit the model, specified as

and then generate the mcmc_object with the rjags package.

We draw burn-in samples and posterior inference samples for all terms in the model.

We can now convert the posterior samples to a data.table and summarise the regression parameters. A data.table object is very useful in this case when you have many samples for many parameters.

Summarise the line of best fit, mu, and the predictions, y_pred,

For plotting, we add the prediction \(\boldsymbol{x}\) values to these data tables.

Now we’ll generate a plot that shows the data, a 95% credible interval for the predictions, \({\hat{\bm{y}}}_{pred}\), and a 95% credible interval for their means, \({\hat{\bm{\mu}}}_{pred}\).

If we tidy the samples object, we can look at the distribution of values

We can also thin to create trace plots and plot per chain

tidy_samples_10 <- thin_dt(tidy_samples, thin = 10)

ggplot(data=tidy_samples_10, aes(x=iteration, y=value)) +
    geom_line(aes(group=chain, color=factor(chain))) +
    facet_wrap( ~ parameter, ncol=1, scales="free_y") +
    theme_bw() +
    theme(legend.position = "bottom") +
    scale_color_discrete(name="Chain")