<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/ridgelines-in-bayesplot-1-5-0/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://tjmahr.github.io/ridgelines-in-bayesplot-1-5-0-release/"> Higher Order Functions</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>At the end of March, Jonah Gabry and I released<br>
<a rel="nofollow" href="http://mc-stan.org/bayesplot/index.html" target="_blank">bayesplot 1.5.0</a>. The major additions<br>
to the package were visualizations using ridgelines and a new plot for PIT<br>
diagnostics from LOO validation. I don’t know what that LOO PIT thing is yet, so<br>
I’ll talk about how ridgelines have been incorporated into bayesplot instead.</p>
<h2>Behind the scenes of <code>mcmc_areas()</code></h2>
<p>I’m going to start with a less obvious use of ridgelines.</p>
<p><code>mcmc_areas()</code> visualizes the posterior distribution of model parameters. It<br>
helps us see where the peak values are. By default, it will plot the complete<br>
range of the distribution, shade the middle 50% of the distribution, and draw a<br>
line at the median.</p>
<p>For this release, I <a rel="nofollow" href="https://github.com/stan-dev/bayesplot/pull/115" target="_blank">rewrote the function</a><br>
so that it uses the <a rel="nofollow" href="https://cran.r-project.org/web/packages/ggridges/vignettes/introduction.html" target="_blank">ggridges</a> package. I had noticed that the shapes it<br>
produced had different areas. Here’s an example of how the plots have changed.</p>
<div>
<div>
<pre><code><span>library</span><span>(</span><span>ggplot2</span><span>)</span><span>
</span><span># Temporarily install the previous version of bayesplot and </span><span>
</span><span># make an mcmc_areas() plot.</span><span>
</span><span>withr</span><span>::</span><span>with_temp_libpaths</span><span>({</span><span>
</span><span>versions</span><span>::</span><span>install.versions</span><span>(</span><span>&quot;bayesplot&quot;</span><span>,</span><span> </span><span>&quot;1.4.0&quot;</span><span>,</span><span> </span><span>quiet</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>example_data</span><span> </span><span>&lt;-</span><span> </span><span>bayesplot</span><span>::</span><span>example_mcmc_draws</span><span>()</span><span>
</span><span>p_a</span><span> </span><span>&lt;-</span><span> </span><span>bayesplot</span><span>::</span><span>mcmc_areas</span><span>(</span><span>example_data</span><span>,</span><span> </span><span>pars</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;beta[1]&quot;</span><span>,</span><span> </span><span>&quot;beta[2]&quot;</span><span>))</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Version 1.4.0&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>bayesplot</span><span>::</span><span>theme_default</span><span>()</span><span>
</span><span>})</span><span>
</span><span>#> package 'bayesplot' successfully unpacked and MD5 sums checked</span><span>
</span><span>library</span><span>(</span><span>bayesplot</span><span>)</span><span>
</span><span>#> This is bayesplot version 1.5.0</span><span>
</span><span>#> - Plotting theme set to bayesplot::theme_default()</span><span>
</span><span>#> - Online documentation at mc-stan.org/bayesplot</span><span>
</span><span>p_b</span><span> </span><span>&lt;-</span><span> </span><span>mcmc_areas</span><span>(</span><span>example_data</span><span>,</span><span> </span><span>c</span><span>(</span><span>&quot;beta[1]&quot;</span><span>,</span><span> </span><span>&quot;beta[2]&quot;</span><span>))</span><span> </span><span>+</span><span> </span><span>ggtitle</span><span>(</span><span>&quot;Version 1.5.0&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>bayesplot</span><span>::</span><span>theme_default</span><span>()</span><span>
</span><span># Show the two together</span><span>
</span><span>cowplot</span><span>::</span><span>plot_grid</span><span>(</span><span>p_a</span><span>,</span><span> </span><span>p_b</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p><img alt="Comparison of mcmc_areas() in bayesplot 1.4.0 and 1.5.0" title="Comparison of mcmc_areas() in bayesplot 1.4.0 and 1.5.0" src="https://i0.wp.com/tjmahr.github.io/figs//2018-04-03-ridgelines-in-bayesplot-1-5-0-release/version-differences-1.png?w=80%25&ssl=1" data-recalc-dims="1"></p>
<p>In the plot on the left, the shapes have different areas. Eyeballing it, it<br>
looks like <code>beta[2]</code> would fit inside <code>beta[1]</code>. Area is width times height.<br>
These shapes have different widths because they cover different values—that<br>
makes sense. Therefore, the problem is the height—namely, the fact that the<br>
shapes have the same height. To make the areas more similar, we adjust the<br>
heights, and that’s what’s happening in the plot on the right. The distribution<br>
for <code>beta[2]</code> is concentrated to a more narrow range, and that feature manifests<br>
in that shape’s much higher peak.</p>
<h3>Three plots in one</h3>
<p>This plot is built using the ggridges packages which handles drawing the shapes<br>
at different heights. It took a bit of trickery, however, to do the annotation<br>
for the shaded 50% interval and the median. Here is a high-level overview of<br>
what’s happening.</p>
<p><code>mcmc_areas_data()</code> tidies the MCMC samples into a dataframe suitable for<br>
plotting. It computes the density of the parameter values, and it returns the<br>
densities over <em>three</em> intervals: 1) an outer interval, 2) an inner interval to<br>
be shaded, and 3) a narrow “point” interval.</p>
<div>
<div>
<pre><code><span>library</span><span>(</span><span>dplyr</span><span>,</span><span> </span><span>warn.conflicts</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span><span>d</span><span> </span><span>&lt;-</span><span> </span><span>mcmc_areas_data</span><span>(</span><span>example_data</span><span>,</span><span> </span><span>c</span><span>(</span><span>&quot;beta[1]&quot;</span><span>,</span><span> </span><span>&quot;beta[2]&quot;</span><span>))</span><span> </span><span>%>%</span><span> </span><span>mutate</span><span>(</span><span>
</span><span>interval</span><span> </span><span>=</span><span> </span><span>factor</span><span>(</span><span>interval</span><span>,</span><span> </span><span>c</span><span>(</span><span>&quot;outer&quot;</span><span>,</span><span> </span><span>&quot;inner&quot;</span><span>,</span><span> </span><span>&quot;point&quot;</span><span>)))</span><span>
</span><span>d</span><span>
</span><span>#> # A tibble: 4,238 x 5</span><span>
</span><span>#> parameter interval interval_width x density</span><span>
</span><span>#> </span></code></pre></div></div>