Visualizing and wrangling MCMC output in R with `MCMCvis`

Model results can be thought of as a reward for the many hours of model design, troubleshooting, re-design, etc. that analyses often require. Following the potentially exhausting mental exercise to acquire these results, I think we’d all like the interpretation to be as straightforward as possible. Analyzing MCMC output from Bayesian analyses, which may include hundreds of parameters and/or derived quantities, however can often require a fair amount of code and (more importantly) time.

The MCMCvis package was designed to alleviate this problem, and streamline the analysis of Bayesian model results. The latest version (0.7.1) is now available on CRAN with bug fixes, speed improvements, and added functionality.


Why MCMCvis?

Using MCMCvis provides three principal benefits:

1) MCMC output fit with Stan, JAGS, or other MCMC samplers can be fed into all package functions as an argument with no further manipulation needed. No need to specify the type of object or how it was fit; the package does all of that behind the scenes.

2) Specific parameters or derived quantities of interest can be specified within each function, to avoid additional steps of data processing. This works using a grep like call for optimal efficiency.

3) The package creates ‘publication-ready’ posterior estimate visualizations (below). Parameters can now be plotted vertically or horizontally.


The package has four functions for basic MCMC output tasks:

MCMCsummary – summarize MCMC output for particular parameters of interest

MCMCtrace – create trace and density plots of MCMC chains for particular parameters of interest

MCMCchains – easily extract posterior chains from MCMC output for particular parameters of interest

MCMCplot – create caterpillar plots from MCMC output for particular parameters of interest

The vignette can be found here.


An example workflow may go as follows:

– summarize posterior estimates for just beta parameters

#install package

#load package

#load example data

#run summary function
             params = 'beta')
##            mean   2.5%    50% 97.5% Rhat
## beta[1]    0.16   0.06   0.15  0.25    1
## beta[2]   -7.77 -25.82  -7.68  9.78    1
## beta[3]   -5.64 -28.53  -5.76 17.23    1
## beta[4]  -10.39 -25.98 -10.63  5.27    1
## beta[5]    7.52   6.03   7.52  9.05    1
## beta[6]   10.89  10.10  10.89 11.68    1
## beta[7]   -1.91  -4.83  -1.92  1.08    1
## beta[8]    5.38  -6.86   5.45 17.67    1
## beta[9]   13.39   3.28  13.38 23.60    1
## beta[10]  17.63  14.41  17.63 20.86    1

– check posteriors for convergence

           params = c('beta[1]', 'beta[2]', 'beta[3]'), 
           ind = TRUE)


– extract chains for beta parameters so that they can be manipulated directly

ex <- MCMCchains(MCMC_data, params = 'beta')
#find 22nd quantile for all beta parameters
apply(ex, 2, function(x){round(quantile(x, probs = 0.22), digits = 2)})
##  beta[1]  beta[2]  beta[3]  beta[4]  beta[5]  beta[6]  beta[7]  beta[8]  beta[9] beta[10] 
##    0.12   -14.86   -14.80   -16.48     6.91    10.58    -3.09     0.68     9.29    16.36

– create caterpillar plots for posterior estimates. Shading represents whether 50% CI (gray with open circle), 95% CI (gray with closed circle), or neither (black) overlap 0. This option can be turned off (as below). A variety of options exist, including the ability to plot posteriors vertically rather than horizontally

          params = 'beta', 
          horiz = FALSE,
          rank = TRUE,
          ref_ovl = FALSE,
          xlab = 'My x-axis label', 
          main = 'MCMCvis plot', 
          labels = c('First param', 'Second param', 'Third param', 
          'Fourth param', 'Fifth param', 'Sixth param', 'Seventh param', 
          'Eighth param', 'Nineth param', 'Tenth param'), 
          labels_sz = 1.5, med_sz = 2, thick_sz = 7, thin_sz = 3, 
          ax_sz = 4, main_text_sz = 2)


Follow Casey Youngflesh on Twitter @caseyyoungflesh. The MCMCvis source code can be found on GitHub.

NASA press release for Lynch Lab-developed penguin monitoring tool

Latest press release from NASA about the Mapping Application for Penguin Populations and Projected Dynamics (MAPPPD) tool, developed as a part of a NASA-funded project. The development of MAPPPD was led by lab PI Heather Lynch and NASA Goddard Space Flight Center Scientist Mathew Schwaller, with the goal of making scientific data more accessible to Antarctic scientists and decision makers. The tool will provide necessary information for key conservation-decisions in the region.

MAPPPD allows users to explore population trends in different regions of Antarctic by selecting an area of interest. It is also designed to incorporate information from citizen scientists in order to ‘fill in the gaps’ in knowledge in the Antarctic.

Learn more about MAPPPD here!

R package `MCMCvis` now on CRAN

MCMCvis, an R package for visualizing, manipulating, and summarizing MCMC output is now available on CRAN!

It can be installed with: install.packages('MCMCvis')

The vignette (tutorial) can be run with: vignette('MCMCvis')

MCMCvis was designed to perform key functions for MCMC analysis using minimal code, in order to free up time/brainpower for interpretation of analysis results. Functions support simple and straightforward subsetting of model parameters within the calls, and produce presentable and ‘publication-ready’ output. Model output can be from JAGS, Stan, or other MCMC samplers. The package deals with different data types behind the scenes so you don’t have to think about it. You can specify which parameters you want to visualize or extract within the functions.

For example, this plot can be made using just one line of code (after loading package/data, of course)!

library('MCMCvis') #load package

data(MCMC_data) #load example data

MCMCplot(MCMC_data, params = 'beta') #create plot



More information can be found within the package vignette. The package was created and authored by Lynch Lab PhD candidate, Casey Youngflesh. He can be found here on Github and here on Twitter.

Dr. Lynch at ESA – Ecology on the Runway

Lab PI, Dr. Heather Lynch will be participating in an Ecology-inspired fashion show at this year’s Ecological Society of America (ESA) meeting in Ft. Lauderdale! Sneak peak of her runway skirt below. Check out the (sure to be incredible) fashion show Monday, August 8 at 10:15AM in the Grand Floridian Ballroom B.

Penguin skirt

Write up about the event from the University of Utah Press office:

At the Ecological Society of America’s 2016 meeting in Ft. Lauderdale, Florida, 13 ecologists will be wearing their hearts and their research on their sleeves. To raise awareness of growing ecological challenges, the ecologists – scientists who study relationships between plants, animals, humans and their environments – will take to the runway in a special session of the meeting, showing off custom-made clothing that displays the plants, animals, microbes and landscapes that they study.

University of Utah biology professor Nalini Nadkarni and National Science Foundation ecologist Doug Levey will host the eco-fashion show. Nadkarni and Levey collaborated with fashion designer and Utah theatre professor Brenda Van der Wiel to create the clothing worn in the show, with the goal of demonstrating innovative and surprising ways that ecologists can raise awareness of their discipline. The theme of the show is “Ecology is Everywhere.”

Designer Van der Wiel and tailor Eugene Tachinni will describe their experiences interacting with researchers and their efforts to translate scientific research into a wearable statement. Ecologists in the audience will be encouraged to share, through Twitter and Facebook, their own innovative ideas for engaging with the public. This demonstrates how something as seemingly non-academic as fashion clothing can serve as a vector for outreach and education about ecology.

A booklet describing the ecologists’ discoveries and interests can be found here.