<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/visualizing-and-wrangling-mcmc-output-in-r-with-mcmcvis/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://lynchlab.com/2017/02/10/visualizing-and-wrangling-mcmc-output-in-r-with-mcmcvis/"> R – Lynch Lab</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>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.</p>
<p>The <code>MCMCvis</code> 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 <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=MCMCvis">CRAN </a>with bug fixes, speed improvements, and added functionality.</p>
<p> </p>
<h2>Why <code>MCMCvis</code>?</h2>
<p>Using <code>MCMCvis</code> provides three principal benefits:</p>
<p>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.</p>
<p>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 <code>grep</code> like call for optimal efficiency.</p>
<p>3) The package creates ‘publication-ready’ posterior estimate visualizations (below). Parameters can now be plotted vertically or horizontally.</p>
<p><img sizes="(max-width: 524px) 100vw, 524px" data-image-description="" data-orig-file="https://i2.wp.com/lynchlab.files.wordpress.com/2017/01/rplot.jpeg?w=456&amp;ssl=1" data-orig-size="524,473" data-image-title="rplot" class="wp-image-1013" data-comments-opened="0" data-recalc-dims="1" data-attachment-id="1013" data-permalink="https://lynchlab.com/2017/02/10/visualizing-and-wrangling-mcmc-output-in-r-with-mcmcvis/rplot-2/" alt="rplot" data-medium-file="https://i2.wp.com/lynchlab.files.wordpress.com/2017/01/rplot.jpeg?w=456&amp;ssl=1?w=300" data-large-file="https://i2.wp.com/lynchlab.files.wordpress.com/2017/01/rplot.jpeg?w=456&amp;ssl=1?w=524" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i2.wp.com/lynchlab.files.wordpress.com/2017/01/rplot.jpeg?w=456&ssl=1"></p>
<h3></h3>
<h2>The package has four functions for basic MCMC output tasks:</h2>
<p><code>MCMCsummary</code> – summarize MCMC output for particular parameters of interest</p>
<p><code>MCMCtrace</code> – create trace and density plots of MCMC chains for particular parameters of interest</p>
<p><code>MCMCchains</code> – easily extract posterior chains from MCMC output for particular parameters of interest</p>
<p><code>MCMCplot</code> – create caterpillar plots from MCMC output for particular parameters of interest</p>
<p>The vignette can be found <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/MCMCvis/vignettes/MCMCvis.html">here</a>.</p>
<p> </p>
<h2>An example workflow may go as follows:</h2>
<p><strong>– summarize posterior estimates for just beta parameters</strong></p>
<pre>#install package
install.packages('MCMCvis')

#load package
require(MCMCvis)

#load example data
data(MCMC_data)

#run summary function
MCMCsummary(MCMC_data, 
             params = 'beta')
</pre>
<pre>##            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</pre>
<p><strong>– check posteriors for convergence</strong></p>
<pre>MCMCtrace(MCMC_data, 
           params = c('beta[1]', 'beta[2]', 'beta[3]'), 
           ind = TRUE)
</pre>
<p><img sizes="(max-width: 524px) 100vw, 524px" data-image-description="" data-orig-file="https://i1.wp.com/lynchlab.files.wordpress.com/2017/01/rplot01.jpeg?w=456&amp;ssl=1" data-orig-size="524,473" data-image-title="rplot01" data-comments-opened="0" class="wp-image-1022" data-recalc-dims="1" data-attachment-id="1022" data-permalink="https://lynchlab.com/2017/02/10/visualizing-and-wrangling-mcmc-output-in-r-with-mcmcvis/rplot01/" alt="rplot01" data-medium-file="https://i1.wp.com/lynchlab.files.wordpress.com/2017/01/rplot01.jpeg?w=456&amp;ssl=1?w=300" data-large-file="https://i1.wp.com/lynchlab.files.wordpress.com/2017/01/rplot01.jpeg?w=456&amp;ssl=1?w=524" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i1.wp.com/lynchlab.files.wordpress.com/2017/01/rplot01.jpeg?w=456&ssl=1"></p>
<p><strong>– extract chains for beta parameters so that they can be manipulated directly</strong></p>
<pre>ex &lt;- 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)})
</pre>
<pre>##  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</pre>
<p><strong>– 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</strong></p>
<pre>MCMCplot(MCMC_data, 
          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)
</pre>
<h3> <img sizes="(max-width: 584px) 100vw, 584px" data-image-description="" data-orig-file="https://i1.wp.com/lynchlab.files.wordpress.com/2017/01/rplot03.jpeg?w=456&amp;ssl=1" data-orig-size="646,450" data-image-title="rplot03" data-comments-opened="0" class="wp-image-1051" data-recalc-dims="1" data-attachment-id="1051" data-permalink="https://lynchlab.com/2017/02/10/visualizing-and-wrangling-mcmc-output-in-r-with-mcmcvis/rplot03/" alt="rplot03" data-medium-file="https://i1.wp.com/lynchlab.files.wordpress.com/2017/01/rplot03.jpeg?w=456&amp;ssl=1?w=300" data-large-file="https://i1.wp.com/lynchlab.files.wordpress.com/2017/01/rplot03.jpeg?w=456&amp;ssl=1?w=584" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i1.wp.com/lynchlab.files.wordpress.com/2017/01/rplot03.jpeg?w=456&ssl=1"></h3>
<h2>Follow <a target="_blank" rel="nofollow" href="http://caseyyoungflesh.weebly.com/">Casey Youngflesh</a> on Twitter <a target="_blank" rel="nofollow" href="https://twitter.com/CaseyYoungflesh">@caseyyoungflesh</a>. The <code></code><code>MCMCvis</code> source code can be found on <a target="_blank" rel="nofollow" href="https://github.com/caseyyoungflesh/MCMCvis">GitHub</a>.</h2>
<p><img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://lynchlab.com/2017/02/10/visualizing-and-wrangling-mcmc-output-in-r-with-mcmcvis/"> R – Lynch Lab</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/LFG6cJRnOak"></a></div>