<!DOCTYPE html>
<!-- Generated by pkgdown: do not edit by hand --><html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Extracting and visualizing tidy draws from rstanarm models • tidybayes</title>
<!-- favicons --><link rel="icon" type="image/png" sizes="16x16" href="../favicon-16x16.png">
<link rel="icon" type="image/png" sizes="32x32" href="../favicon-32x32.png">
<link rel="apple-touch-icon" type="image/png" sizes="180x180" href="../apple-touch-icon.png">
<link rel="apple-touch-icon" type="image/png" sizes="120x120" href="../apple-touch-icon-120x120.png">
<link rel="apple-touch-icon" type="image/png" sizes="76x76" href="../apple-touch-icon-76x76.png">
<link rel="apple-touch-icon" type="image/png" sizes="60x60" href="../apple-touch-icon-60x60.png">
<!-- jquery --><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js" integrity="sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g==" crossorigin="anonymous" referrerpolicy="no-referrer"></script><!-- Bootstrap --><link href="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/3.4.0/paper/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha256-nuL8/2cJ5NDSSwnKD8VqreErSWHtnEP9E7AySL+1ev4=" crossorigin="anonymous"></script><!-- bootstrap-toc --><link rel="stylesheet" href="../bootstrap-toc.css">
<script src="../bootstrap-toc.js"></script><!-- Font Awesome icons --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/all.min.css" integrity="sha256-mmgLkCYLUQbXn0B1SRqzHar6dCnv9oZFPEC1g1cwlkk=" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/v4-shims.min.css" integrity="sha256-wZjR52fzng1pJHwx4aV2AO3yyTOXrcDW7jBpJtTwVxw=" crossorigin="anonymous">
<!-- clipboard.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script><!-- headroom.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/headroom.min.js" integrity="sha256-AsUX4SJE1+yuDu5+mAVzJbuYNPHj/WroHuZ8Ir/CkE0=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/jQuery.headroom.min.js" integrity="sha256-ZX/yNShbjqsohH1k95liqY9Gd8uOiE1S4vZc+9KQ1K4=" crossorigin="anonymous"></script><!-- pkgdown --><link href="../pkgdown.css" rel="stylesheet">
<script src="../pkgdown.js"></script><link href="../extra.css" rel="stylesheet">
<meta property="og:title" content="Extracting and visualizing tidy draws from rstanarm models">
<!-- mathjax --><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-AMS-MML_HTMLorMML.js" integrity="sha256-84DKXVJXs0/F8OTMzX4UR909+jtl4G7SPypPavF+GfA=" crossorigin="anonymous"></script><!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]--><!-- Global site tag (gtag.js) - Google Analytics --><script async src="https://www.googletagmanager.com/gtag/js?id=UA-93322-5"></script><script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-93322-5');
</script>
</head>
<body data-spy="scroll" data-target="#toc">


    <div class="container template-article">
      <header><div class="navbar navbar-default navbar-fixed-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <span class="navbar-brand">
        <a class="navbar-link" href="../index.html">tidybayes</a>
        <span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="">3.0.7</span>
      </span>
    </div>

    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
<li>
  <a href="../articles/tidybayes.html">Get started</a>
</li>
<li>
  <a href="../reference/index.html">Reference</a>
</li>
<li class="dropdown">
  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
    Articles

    <span class="caret"></span>
  </a>
  <ul class="dropdown-menu" role="menu">
<li>
      <a href="../articles/tidy-brms.html">Extracting and visualizing tidy draws from brms models</a>
    </li>
    <li>
      <a href="../articles/tidy-posterior.html">Using tidybayes with the posterior package</a>
    </li>
    <li>
      <a href="../articles/tidy-rstanarm.html">Extracting and visualizing tidy draws from rstanarm models</a>
    </li>
    <li>
      <a href="../articles/tidybayes-residuals.html">Extracting and visualizing tidy residuals from Bayesian models</a>
    </li>
  </ul>
</li>
<li>
  <a href="../news/index.html">Changelog</a>
</li>
      </ul>
<ul class="nav navbar-nav navbar-right">
<li>
  <a href="https://github.com/mjskay/tidybayes/" class="external-link">
    <span class="fab fa-github fa-lg"></span>

  </a>
</li>
      </ul>
</div>
<!--/.nav-collapse -->
  </div>
<!--/.container -->
</div>
<!--/.navbar -->



      </header><div class="row">
  <div class="col-md-9 contents">
    <div class="page-header toc-ignore">
      <h1 data-toc-skip>Extracting and visualizing tidy draws from
rstanarm models</h1>
                        <h4 data-toc-skip class="author">Matthew
Kay</h4>
            
            <h4 data-toc-skip class="date">2024-09-14</h4>
      
      <small class="dont-index">Source: <a href="https://github.com/mjskay/tidybayes/blob/HEAD/vignettes/tidy-rstanarm.Rmd" class="external-link"><code>vignettes/tidy-rstanarm.Rmd</code></a></small>
      <div class="hidden name"><code>tidy-rstanarm.Rmd</code></div>

    </div>

    
    
<style type="text/css">
.kable-table table {
  margin-left: 0;
}
img {
  border: none;
}
</style>
<div class="section level2">
<h2 id="introduction">Introduction<a class="anchor" aria-label="anchor" href="#introduction"></a>
</h2>
<p>This vignette describes how to use the <code>tidybayes</code> and
<code>ggdist</code> packages to extract and visualize <a href="https://dx.doi.org/10.18637/jss.v059.i10" class="external-link">tidy</a> data frames of
draws from posterior distributions of model variables, means, and
predictions from <code>rstanarm</code>. For a more general introduction
to <code>tidybayes</code> and its use on general-purpose Bayesian
modeling languages (like Stan and JAGS), see
<code><a href="../articles/tidybayes.html">vignette("tidybayes")</a></code>.</p>
</div>
<div class="section level2">
<h2 id="setup">Setup<a class="anchor" aria-label="anchor" href="#setup"></a>
</h2>
<p>The following libraries are required to run this vignette:</p>
<div class="sourceCode" id="cb1"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://magrittr.tidyverse.org" class="external-link">magrittr</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://dplyr.tidyverse.org" class="external-link">dplyr</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://purrr.tidyverse.org/" class="external-link">purrr</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://forcats.tidyverse.org/" class="external-link">forcats</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://tidyr.tidyverse.org" class="external-link">tidyr</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://modelr.tidyverse.org" class="external-link">modelr</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://mjskay.github.io/ggdist/" class="external-link">ggdist</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://mjskay.github.io/tidybayes/">tidybayes</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://ggplot2.tidyverse.org" class="external-link">ggplot2</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://wilkelab.org/cowplot/" class="external-link">cowplot</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://mc-stan.org/rstan/" class="external-link">rstan</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://mc-stan.org/rstanarm/" class="external-link">rstanarm</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va">RColorBrewer</span><span class="op">)</span></span>
<span></span>
<span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/theme_get.html" class="external-link">theme_set</a></span><span class="op">(</span><span class="fu"><a href="http://mjskay.github.io/ggdist/reference/theme_ggdist.html" class="external-link">theme_tidybayes</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://wilkelab.org/cowplot/reference/panel_border.html" class="external-link">panel_border</a></span><span class="op">(</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p>These options help Stan run faster:</p>
<div class="sourceCode" id="cb2"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://mc-stan.org/rstan/reference/rstan_options.html" class="external-link">rstan_options</a></span><span class="op">(</span>auto_write <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/options.html" class="external-link">options</a></span><span class="op">(</span>mc.cores <span class="op">=</span> <span class="fu">parallel</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/r/parallel/detectCores.html" class="external-link">detectCores</a></span><span class="op">(</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
</div>
<div class="section level2">
<h2 id="example-dataset">Example dataset<a class="anchor" aria-label="anchor" href="#example-dataset"></a>
</h2>
<p>To demonstrate <code>tidybayes</code>, we will use a simple dataset
with 10 observations from 5 conditions each:</p>
<div class="sourceCode" id="cb3"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/base/Random.html" class="external-link">set.seed</a></span><span class="op">(</span><span class="fl">5</span><span class="op">)</span></span>
<span><span class="va">n</span> <span class="op">=</span> <span class="fl">10</span></span>
<span><span class="va">n_condition</span> <span class="op">=</span> <span class="fl">5</span></span>
<span><span class="va">ABC</span> <span class="op">=</span></span>
<span>  <span class="fu"><a href="https://tibble.tidyverse.org/reference/tibble.html" class="external-link">tibble</a></span><span class="op">(</span></span>
<span>    condition <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/rep.html" class="external-link">rep</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"A"</span>,<span class="st">"B"</span>,<span class="st">"C"</span>,<span class="st">"D"</span>,<span class="st">"E"</span><span class="op">)</span>, <span class="va">n</span><span class="op">)</span>,</span>
<span>    response <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/Normal.html" class="external-link">rnorm</a></span><span class="op">(</span><span class="va">n</span> <span class="op">*</span> <span class="fl">5</span>, <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">0</span>,<span class="fl">1</span>,<span class="fl">2</span>,<span class="fl">1</span>,<span class="op">-</span><span class="fl">1</span><span class="op">)</span>, <span class="fl">0.5</span><span class="op">)</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p>A snapshot of the data looks like this:</p>
<div class="sourceCode" id="cb4"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/utils/head.html" class="external-link">head</a></span><span class="op">(</span><span class="va">ABC</span>, <span class="fl">10</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 10 × 2</span></span></span>
<span><span class="co">##    condition response</span></span>
<span><span class="co">##    <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>        <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 1</span> A           -<span style="color: #BB0000;">0.420</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 2</span> B            1.69 </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 3</span> C            1.37 </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 4</span> D            1.04 </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 5</span> E           -<span style="color: #BB0000;">0.144</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 6</span> A           -<span style="color: #BB0000;">0.301</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 7</span> B            0.764</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 8</span> C            1.68 </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 9</span> D            0.857</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">10</span> E           -<span style="color: #BB0000;">0.931</span></span></span></code></pre>
<p>This is a typical tidy format data frame: one observation per row.
Graphically:</p>
<div class="sourceCode" id="cb6"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">ABC</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">condition</span>, x <span class="op">=</span> <span class="va">response</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html" class="external-link">geom_point</a></span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-rstanarm_files/figure-html/unnamed-chunk-4-1.png" width="648"></p>
</div>
<div class="section level2">
<h2 id="model">Model<a class="anchor" aria-label="anchor" href="#model"></a>
</h2>
<p>Let’s fit a hierarchical model with shrinkage towards a global
mean:</p>
<div class="sourceCode" id="cb7"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op">=</span> <span class="fu"><a href="https://mc-stan.org/rstanarm/reference/stan_glmer.html" class="external-link">stan_lmer</a></span><span class="op">(</span><span class="va">response</span> <span class="op">~</span> <span class="op">(</span><span class="fl">1</span><span class="op">|</span><span class="va">condition</span><span class="op">)</span>, data <span class="op">=</span> <span class="va">ABC</span>, </span>
<span>  prior <span class="op">=</span> <span class="fu"><a href="https://mc-stan.org/rstanarm/reference/priors.html" class="external-link">normal</a></span><span class="op">(</span><span class="fl">0</span>, <span class="fl">1</span>, autoscale <span class="op">=</span> <span class="cn">FALSE</span><span class="op">)</span>,</span>
<span>  prior_aux <span class="op">=</span> <span class="fu"><a href="https://mc-stan.org/rstanarm/reference/priors.html" class="external-link">student_t</a></span><span class="op">(</span><span class="fl">3</span>, <span class="fl">0</span>, <span class="fl">1</span>, autoscale <span class="op">=</span> <span class="cn">FALSE</span><span class="op">)</span>,</span>
<span>  adapt_delta <span class="op">=</span> <span class="fl">.99</span><span class="op">)</span></span></code></pre></div>
<p>The results look like this:</p>
<div class="sourceCode" id="cb8"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span></span></code></pre></div>
<pre><code><span><span class="co">## stan_lmer</span></span>
<span><span class="co">##  family:       gaussian [identity]</span></span>
<span><span class="co">##  formula:      response ~ (1 | condition)</span></span>
<span><span class="co">##  observations: 50</span></span>
<span><span class="co">## ------</span></span>
<span><span class="co">##             Median MAD_SD</span></span>
<span><span class="co">## (Intercept) 0.6    0.5   </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## Auxiliary parameter(s):</span></span>
<span><span class="co">##       Median MAD_SD</span></span>
<span><span class="co">## sigma 0.6    0.1   </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## Error terms:</span></span>
<span><span class="co">##  Groups    Name        Std.Dev.</span></span>
<span><span class="co">##  condition (Intercept) 1.14    </span></span>
<span><span class="co">##  Residual              0.56    </span></span>
<span><span class="co">## Num. levels: condition 5 </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## ------</span></span>
<span><span class="co">## * For help interpreting the printed output see ?print.stanreg</span></span>
<span><span class="co">## * For info on the priors used see ?prior_summary.stanreg</span></span></code></pre>
</div>
<div class="section level2">
<h2 id="extracting-draws-from-a-fit-in-tidy-format-using-spread_draws">Extracting draws from a fit in tidy-format using
<code>spread_draws</code><a class="anchor" aria-label="anchor" href="#extracting-draws-from-a-fit-in-tidy-format-using-spread_draws"></a>
</h2>
<p>Now that we have our results, the fun begins: getting the draws out
in a tidy format! First, we’ll use the <code><a href="../reference/get_variables.html">get_variables()</a></code>
function to get a list of raw model variables names so that we know what
variables we can extract from the model:</p>
<div class="sourceCode" id="cb10"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="../reference/get_variables.html">get_variables</a></span><span class="op">(</span><span class="va">m</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">##  [1] "(Intercept)"                              "b[(Intercept) condition:A]"              </span></span>
<span><span class="co">##  [3] "b[(Intercept) condition:B]"               "b[(Intercept) condition:C]"              </span></span>
<span><span class="co">##  [5] "b[(Intercept) condition:D]"               "b[(Intercept) condition:E]"              </span></span>
<span><span class="co">##  [7] "sigma"                                    "Sigma[condition:(Intercept),(Intercept)]"</span></span>
<span><span class="co">##  [9] "accept_stat__"                            "stepsize__"                              </span></span>
<span><span class="co">## [11] "treedepth__"                              "n_leapfrog__"                            </span></span>
<span><span class="co">## [13] "divergent__"                              "energy__"</span></span></code></pre>
<p>Here, <code>(Intercept)</code> is the global mean, and the
<code>b</code> parameters are offsets from that mean for each condition.
Given these parameters:</p>
<ul>
<li><code>b[(Intercept) condition:A]</code></li>
<li><code>b[(Intercept) condition:B]</code></li>
<li><code>b[(Intercept) condition:C]</code></li>
<li><code>b[(Intercept) condition:D]</code></li>
<li><code>b[(Intercept) condition:E]</code></li>
</ul>
<p>We might want a data frame where each row is a draw from either
<code>b[(Intercept) condition:A]</code>,
<code>b[(Intercept) condition:B]</code>, <code>...:C]</code>,
<code>...:D]</code>, or <code>...:E]</code>, and where we have columns
indexing which chain/iteration/draw the row came from and which
condition (<code>A</code> to <code>E</code>) it is for. That would allow
us to easily compute quantities grouped by condition, or generate plots
by condition using ggplot, or even merge draws with the original data to
plot data and posteriors.</p>
<p>The workhorse of <code>tidybayes</code> is the
<code><a href="../reference/spread_draws.html">spread_draws()</a></code> function, which does this extraction for us.
It includes a simple specification format that we can use to extract
model variables and their indices into tidy-format data frames.</p>
<div class="section level3">
<h3 id="gathering-variable-indices-into-a-separate-column-in-a-tidy-format-data-frame">Gathering variable indices into a separate column in a tidy format
data frame<a class="anchor" aria-label="anchor" href="#gathering-variable-indices-into-a-separate-column-in-a-tidy-format-data-frame"></a>
</h3>
<p>Given a parameter like this:</p>
<p><code>b[(Intercept) condition:D]</code></p>
<p>We can provide <code><a href="../reference/spread_draws.html">spread_draws()</a></code> with a column
specification like this:</p>
<p><code>b[term,group]</code></p>
<p>Where <code>term</code> corresponds to <code>(Intercept)</code> and
<code>group</code> to <code>condition:D</code>. There is nothing too
magical about what <code><a href="../reference/spread_draws.html">spread_draws()</a></code> does with this
specification: under the hood, it splits the parameter indices by commas
and spaces (you can split by other characters by changing the
<code>sep</code> argument). It lets you assign columns to the resulting
indices in order. So <code>b[(Intercept) condition:D]</code> has indices
<code>(Intercept)</code> and <code>condition:D</code>, and
<code><a href="../reference/spread_draws.html">spread_draws()</a></code> lets us extract these indices as columns in
the resulting tidy data frame of draws from <code>b</code>:</p>
<div class="sourceCode" id="cb12"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">b</span><span class="op">[</span><span class="va">term</span>,<span class="va">group</span><span class="op">]</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://rdrr.io/r/utils/head.html" class="external-link">head</a></span><span class="op">(</span><span class="fl">10</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 10 × 6</span></span></span>
<span><span class="co">## <span style="color: #949494;"># Groups:   term, group [1]</span></span></span>
<span><span class="co">##    term        group             b .chain .iteration .draw</span></span>
<span><span class="co">##    <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>       <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>         <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;int&gt;</span>      <span style="color: #949494; font-style: italic;">&lt;int&gt;</span> <span style="color: #949494; font-style: italic;">&lt;int&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 1</span> (Intercept) condition:A -<span style="color: #BB0000;">1.27</span>        1          1     1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 2</span> (Intercept) condition:A -<span style="color: #BB0000;">0.213</span>       1          2     2</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 3</span> (Intercept) condition:A  0.019<span style="text-decoration: underline;">2</span>      1          3     3</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 4</span> (Intercept) condition:A -<span style="color: #BB0000;">0.423</span>       1          4     4</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 5</span> (Intercept) condition:A -<span style="color: #BB0000;">0.401</span>       1          5     5</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 6</span> (Intercept) condition:A  0.027<span style="text-decoration: underline;">5</span>      1          6     6</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 7</span> (Intercept) condition:A -<span style="color: #BB0000;">0.318</span>       1          7     7</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 8</span> (Intercept) condition:A -<span style="color: #BB0000;">0.415</span>       1          8     8</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 9</span> (Intercept) condition:A -<span style="color: #BB0000;">0.564</span>       1          9     9</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">10</span> (Intercept) condition:A -<span style="color: #BB0000;">1.15</span>        1         10    10</span></span></code></pre>
<p>We can choose whatever names we want for the index columns; e.g.:</p>
<div class="sourceCode" id="cb14"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">b</span><span class="op">[</span><span class="va">t</span>,<span class="va">g</span><span class="op">]</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://rdrr.io/r/utils/head.html" class="external-link">head</a></span><span class="op">(</span><span class="fl">10</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 10 × 6</span></span></span>
<span><span class="co">## <span style="color: #949494;"># Groups:   t, g [1]</span></span></span>
<span><span class="co">##    t           g                 b .chain .iteration .draw</span></span>
<span><span class="co">##    <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>       <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>         <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;int&gt;</span>      <span style="color: #949494; font-style: italic;">&lt;int&gt;</span> <span style="color: #949494; font-style: italic;">&lt;int&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 1</span> (Intercept) condition:A -<span style="color: #BB0000;">1.27</span>        1          1     1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 2</span> (Intercept) condition:A -<span style="color: #BB0000;">0.213</span>       1          2     2</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 3</span> (Intercept) condition:A  0.019<span style="text-decoration: underline;">2</span>      1          3     3</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 4</span> (Intercept) condition:A -<span style="color: #BB0000;">0.423</span>       1          4     4</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 5</span> (Intercept) condition:A -<span style="color: #BB0000;">0.401</span>       1          5     5</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 6</span> (Intercept) condition:A  0.027<span style="text-decoration: underline;">5</span>      1          6     6</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 7</span> (Intercept) condition:A -<span style="color: #BB0000;">0.318</span>       1          7     7</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 8</span> (Intercept) condition:A -<span style="color: #BB0000;">0.415</span>       1          8     8</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 9</span> (Intercept) condition:A -<span style="color: #BB0000;">0.564</span>       1          9     9</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">10</span> (Intercept) condition:A -<span style="color: #BB0000;">1.15</span>        1         10    10</span></span></code></pre>
<p>But the more descriptive and less cryptic names from the previous
example are probably preferable.</p>
<p>In this particular model, there is only one term
(<code>(Intercept)</code>), thus we could omit that index altogether to
just get each <code>group</code> and the value of <code>b</code> for the
corresponding condition:</p>
<div class="sourceCode" id="cb16"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">b</span><span class="op">[</span>,<span class="va">group</span><span class="op">]</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://rdrr.io/r/utils/head.html" class="external-link">head</a></span><span class="op">(</span><span class="fl">10</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 10 × 5</span></span></span>
<span><span class="co">## <span style="color: #949494;"># Groups:   group [1]</span></span></span>
<span><span class="co">##    group             b .chain .iteration .draw</span></span>
<span><span class="co">##    <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>         <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;int&gt;</span>      <span style="color: #949494; font-style: italic;">&lt;int&gt;</span> <span style="color: #949494; font-style: italic;">&lt;int&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 1</span> condition:A -<span style="color: #BB0000;">1.27</span>        1          1     1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 2</span> condition:A -<span style="color: #BB0000;">0.213</span>       1          2     2</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 3</span> condition:A  0.019<span style="text-decoration: underline;">2</span>      1          3     3</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 4</span> condition:A -<span style="color: #BB0000;">0.423</span>       1          4     4</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 5</span> condition:A -<span style="color: #BB0000;">0.401</span>       1          5     5</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 6</span> condition:A  0.027<span style="text-decoration: underline;">5</span>      1          6     6</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 7</span> condition:A -<span style="color: #BB0000;">0.318</span>       1          7     7</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 8</span> condition:A -<span style="color: #BB0000;">0.415</span>       1          8     8</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 9</span> condition:A -<span style="color: #BB0000;">0.564</span>       1          9     9</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">10</span> condition:A -<span style="color: #BB0000;">1.15</span>        1         10    10</span></span></code></pre>
<p>Since all the groups in this case are from the <code>condition</code>
factor, we may also want to separate out a column just containing the
corresponding condition (<code>A</code>, <code>B</code>, <code>C</code>,
etc). We can do that using <code><a href="https://tidyr.tidyverse.org/reference/separate.html" class="external-link">tidyr::separate</a></code>:</p>
<div class="sourceCode" id="cb18"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span> </span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">b</span><span class="op">[</span>,<span class="va">group</span><span class="op">]</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://tidyr.tidyverse.org/reference/separate.html" class="external-link">separate</a></span><span class="op">(</span><span class="va">group</span>, <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"group"</span>, <span class="st">"condition"</span><span class="op">)</span>, <span class="st">":"</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://rdrr.io/r/utils/head.html" class="external-link">head</a></span><span class="op">(</span><span class="fl">10</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 10 × 6</span></span></span>
<span><span class="co">##    group     condition       b .chain .iteration .draw</span></span>
<span><span class="co">##    <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>     <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>       <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;int&gt;</span>      <span style="color: #949494; font-style: italic;">&lt;int&gt;</span> <span style="color: #949494; font-style: italic;">&lt;int&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 1</span> condition A         -<span style="color: #BB0000;">1.27</span>        1          1     1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 2</span> condition A         -<span style="color: #BB0000;">0.213</span>       1          2     2</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 3</span> condition A          0.019<span style="text-decoration: underline;">2</span>      1          3     3</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 4</span> condition A         -<span style="color: #BB0000;">0.423</span>       1          4     4</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 5</span> condition A         -<span style="color: #BB0000;">0.401</span>       1          5     5</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 6</span> condition A          0.027<span style="text-decoration: underline;">5</span>      1          6     6</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 7</span> condition A         -<span style="color: #BB0000;">0.318</span>       1          7     7</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 8</span> condition A         -<span style="color: #BB0000;">0.415</span>       1          8     8</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 9</span> condition A         -<span style="color: #BB0000;">0.564</span>       1          9     9</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">10</span> condition A         -<span style="color: #BB0000;">1.15</span>        1         10    10</span></span></code></pre>
<p>Alternatively, we could change the <code>sep</code> argument to
<code><a href="../reference/spread_draws.html">spread_draws()</a></code> to also split on <code>:</code>
(<code>sep</code> is a regular expression). <strong>Note:</strong> This
works in this example, but will not work well on rstanarm models where
interactions between factors are used as grouping levels in a multilevel
model, thus <code>:</code> is not included in the default
separators.</p>
<div class="sourceCode" id="cb20"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span> </span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">b</span><span class="op">[</span>,<span class="va">group</span>,<span class="va">condition</span><span class="op">]</span>, sep <span class="op">=</span> <span class="st">"[, :]"</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://rdrr.io/r/utils/head.html" class="external-link">head</a></span><span class="op">(</span><span class="fl">10</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 10 × 6</span></span></span>
<span><span class="co">## <span style="color: #949494;"># Groups:   group, condition [1]</span></span></span>
<span><span class="co">##    group     condition       b .chain .iteration .draw</span></span>
<span><span class="co">##    <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>     <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>       <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;int&gt;</span>      <span style="color: #949494; font-style: italic;">&lt;int&gt;</span> <span style="color: #949494; font-style: italic;">&lt;int&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 1</span> condition A         -<span style="color: #BB0000;">1.27</span>        1          1     1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 2</span> condition A         -<span style="color: #BB0000;">0.213</span>       1          2     2</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 3</span> condition A          0.019<span style="text-decoration: underline;">2</span>      1          3     3</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 4</span> condition A         -<span style="color: #BB0000;">0.423</span>       1          4     4</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 5</span> condition A         -<span style="color: #BB0000;">0.401</span>       1          5     5</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 6</span> condition A          0.027<span style="text-decoration: underline;">5</span>      1          6     6</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 7</span> condition A         -<span style="color: #BB0000;">0.318</span>       1          7     7</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 8</span> condition A         -<span style="color: #BB0000;">0.415</span>       1          8     8</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 9</span> condition A         -<span style="color: #BB0000;">0.564</span>       1          9     9</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">10</span> condition A         -<span style="color: #BB0000;">1.15</span>        1         10    10</span></span></code></pre>
<p><strong>Note:</strong> If you have used <code><a href="../reference/spread_draws.html">spread_draws()</a></code>
with a raw sample from Stan or JAGS, you may be used to using
<code><a href="../reference/recover_types.html">recover_types()</a></code> before <code><a href="../reference/spread_draws.html">spread_draws()</a></code> to get
index column values back (e.g. if the index was a factor). This is not
necessary when using <code><a href="../reference/spread_draws.html">spread_draws()</a></code> on
<code>rstanarm</code> models, because those models already contain that
information in their variable names. For more on
<code><a href="../reference/recover_types.html">recover_types()</a></code>, see
<code><a href="../articles/tidybayes.html">vignette("tidybayes")</a></code>.</p>
</div>
</div>
<div class="section level2">
<h2 id="point-summaries-and-intervals">Point summaries and intervals<a class="anchor" aria-label="anchor" href="#point-summaries-and-intervals"></a>
</h2>
<div class="section level3">
<h3 id="with-simple-model-variables">With simple model variables<a class="anchor" aria-label="anchor" href="#with-simple-model-variables"></a>
</h3>
<p><code>tidybayes</code> provides a family of functions for generating
point summaries and intervals from draws in a tidy format. These
functions follow the naming scheme
<code>[median|mean|mode]_[qi|hdi]</code>, for example,
<code><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi()</a></code>, <code><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">mean_qi()</a></code>,
<code><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">mode_hdi()</a></code>, and so on. The first name (before the
<code>_</code>) indicates the type of point summary, and the second name
indicates the type of interval. <code>qi</code> yields a quantile
interval (a.k.a. equi-tailed interval, central interval, or percentile
interval) and <code>hdi</code> yields a highest (posterior) density
interval. Custom point or interval functions can also be applied using
the <code><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">point_interval()</a></code> function.</p>
<p>For example, we might extract the draws corresponding to the
posterior distributions of the overall mean and standard deviation of
observations:</p>
<div class="sourceCode" id="cb22"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">`(Intercept)`</span>, <span class="va">sigma</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://rdrr.io/r/utils/head.html" class="external-link">head</a></span><span class="op">(</span><span class="fl">10</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 10 × 5</span></span></span>
<span><span class="co">##    .chain .iteration .draw `(Intercept)` sigma</span></span>
<span><span class="co">##     <span style="color: #949494; font-style: italic;">&lt;int&gt;</span>      <span style="color: #949494; font-style: italic;">&lt;int&gt;</span> <span style="color: #949494; font-style: italic;">&lt;int&gt;</span>         <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 1</span>      1          1     1         1.47  0.749</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 2</span>      1          2     2         0.295 0.508</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 3</span>      1          3     3         0.330 0.564</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 4</span>      1          4     4         0.587 0.564</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 5</span>      1          5     5         0.522 0.533</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 6</span>      1          6     6         0.604 0.544</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 7</span>      1          7     7         0.336 0.623</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 8</span>      1          8     8         0.582 0.559</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 9</span>      1          9     9         1.07  0.660</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">10</span>      1         10    10         1.19  0.506</span></span></code></pre>
<p>Like with <code>b[term,group]</code>, this gives us a tidy data
frame. If we want the median and 95% quantile interval of the variables,
we can apply <code><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi()</a></code>:</p>
<div class="sourceCode" id="cb24"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">`(Intercept)`</span>, <span class="va">sigma</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi</a></span><span class="op">(</span><span class="va">`(Intercept)`</span>, <span class="va">sigma</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">##   (Intercept) (Intercept).lower (Intercept).upper     sigma sigma.lower sigma.upper .width .point .interval</span></span>
<span><span class="co">## 1   0.6068899         -0.524025          1.576127 0.5602622   0.4564012   0.6969076   0.95 median        qi</span></span></code></pre>
<p>We can specify the columns we want to get medians and intervals from,
as above, or if we omit the list of columns, <code><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi()</a></code>
will use every column that is not a grouping column or a special column
(like <code>.chain</code>, <code>.iteration</code>, or
<code>.draw</code>). Thus in the above example, <code>(Intercept)</code>
and <code>sigma</code> are redundant arguments to
<code><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi()</a></code> because they are also the only columns we
gathered from the model. So we can simplify this to:</p>
<div class="sourceCode" id="cb26"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">`(Intercept)`</span>, <span class="va">sigma</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi</a></span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">##   (Intercept) (Intercept).lower (Intercept).upper     sigma sigma.lower sigma.upper .width .point .interval</span></span>
<span><span class="co">## 1   0.6068899         -0.524025          1.576127 0.5602622   0.4564012   0.6969076   0.95 median        qi</span></span></code></pre>
<p>If you would rather have a long-format list of intervals, use
<code><a href="../reference/spread_draws.html">gather_draws()</a></code> instead:</p>
<div class="sourceCode" id="cb28"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">gather_draws</a></span><span class="op">(</span><span class="va">`(Intercept)`</span>, <span class="va">sigma</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi</a></span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 2 × 7</span></span></span>
<span><span class="co">##   .variable   .value .lower .upper .width .point .interval</span></span>
<span><span class="co">##   <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>        <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>    </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span> (Intercept)  0.607 -<span style="color: #BB0000;">0.524</span>  1.58    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span> sigma        0.560  0.456  0.697   0.95 median qi</span></span></code></pre>
<p>For more on <code><a href="../reference/spread_draws.html">gather_draws()</a></code>, see
<code><a href="../articles/tidybayes.html">vignette("tidybayes")</a></code>.</p>
</div>
<div class="section level3">
<h3 id="with-indexed-variables">With indexed variables<a class="anchor" aria-label="anchor" href="#with-indexed-variables"></a>
</h3>
<p>When we have a model variable with one or more indices, such as
<code>b</code>, we can apply <code><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi()</a></code> (or other
functions in the <code><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">point_interval()</a></code> family) as we did
before:</p>
<div class="sourceCode" id="cb30"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">b</span><span class="op">[</span>,<span class="va">group</span><span class="op">]</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi</a></span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 5 × 7</span></span></span>
<span><span class="co">##   group            b .lower .upper .width .point .interval</span></span>
<span><span class="co">##   <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>        <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>    </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span> condition:A -<span style="color: #BB0000;">0.410</span> -<span style="color: #BB0000;">1.42</span>   0.705   0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span> condition:B  0.390 -<span style="color: #BB0000;">0.630</span>  1.49    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">3</span> condition:C  1.20   0.257  2.37    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">4</span> condition:D  0.402 -<span style="color: #BB0000;">0.589</span>  1.55    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">5</span> condition:E -<span style="color: #BB0000;">1.49</span>  -<span style="color: #BB0000;">2.52</span>  -<span style="color: #BB0000;">0.367</span>   0.95 median qi</span></span></code></pre>
<p>How did <code><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi()</a></code> know what to aggregate? Data frames
returned by <code><a href="../reference/spread_draws.html">spread_draws()</a></code> are automatically grouped by all
index variables you pass to it; in this case, that means
<code><a href="../reference/spread_draws.html">spread_draws()</a></code> groups its results by <code>group</code>.
<code><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi()</a></code> respects those groups, and calculates the point
summaries and intervals within all groups. Then, because no columns were
passed to <code><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi()</a></code>, it acts on the only non-special
(<code>.</code>-prefixed) and non-group column, <code>b</code>. So the
above shortened syntax is equivalent to this more verbose call:</p>
<div class="sourceCode" id="cb32"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">b</span><span class="op">[</span>,<span class="va">group</span><span class="op">]</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/group_by.html" class="external-link">group_by</a></span><span class="op">(</span><span class="va">group</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span>       <span class="co"># this line not necessary (done by spread_draws)</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi</a></span><span class="op">(</span><span class="va">b</span><span class="op">)</span>                <span class="co"># b is not necessary (it is the only non-group column)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 5 × 7</span></span></span>
<span><span class="co">##   group            b .lower .upper .width .point .interval</span></span>
<span><span class="co">##   <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>        <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>    </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span> condition:A -<span style="color: #BB0000;">0.410</span> -<span style="color: #BB0000;">1.42</span>   0.705   0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span> condition:B  0.390 -<span style="color: #BB0000;">0.630</span>  1.49    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">3</span> condition:C  1.20   0.257  2.37    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">4</span> condition:D  0.402 -<span style="color: #BB0000;">0.589</span>  1.55    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">5</span> condition:E -<span style="color: #BB0000;">1.49</span>  -<span style="color: #BB0000;">2.52</span>  -<span style="color: #BB0000;">0.367</span>   0.95 median qi</span></span></code></pre>
<p><code>tidybayes</code> also provides an implementation of
<code><a href="https://mc-stan.org/posterior/reference/draws_summary.html" class="external-link">posterior::summarise_draws()</a></code> for grouped data frames
(<code>tidybayes::summaries_draws.grouped_df()</code>), which you can
use to quickly get convergence diagnostics:</p>
<div class="sourceCode" id="cb34"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">b</span><span class="op">[</span>,<span class="va">group</span><span class="op">]</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://mc-stan.org/posterior/reference/draws_summary.html" class="external-link">summarise_draws</a></span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 5 × 11</span></span></span>
<span><span class="co">## <span style="color: #949494;"># Groups:   group [5]</span></span></span>
<span><span class="co">##   group       variable   mean median    sd   mad     q5    q95  rhat ess_bulk ess_tail</span></span>
<span><span class="co">##   <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>       <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>     <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>    <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>    <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span> condition:A b        -<span style="color: #BB0000;">0.406</span> -<span style="color: #BB0000;">0.410</span> 0.521 0.474 -<span style="color: #BB0000;">1.22</span>   0.429  1.00     991.    <span style="text-decoration: underline;">1</span>027.</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span> condition:B b         0.396  0.390 0.521 0.470 -<span style="color: #BB0000;">0.421</span>  1.25   1.00    <span style="text-decoration: underline;">1</span>001.    <span style="text-decoration: underline;">1</span>102.</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">3</span> condition:C b         1.23   1.20  0.520 0.472  0.433  2.10   1.00     978.    <span style="text-decoration: underline;">1</span>217.</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">4</span> condition:D b         0.411  0.402 0.523 0.473 -<span style="color: #BB0000;">0.412</span>  1.27   1.00     999.    <span style="text-decoration: underline;">1</span>187.</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">5</span> condition:E b        -<span style="color: #BB0000;">1.48</span>  -<span style="color: #BB0000;">1.49</span>  0.524 0.474 -<span style="color: #BB0000;">2.32</span>  -<span style="color: #BB0000;">0.636</span>  1.00    <span style="text-decoration: underline;">1</span>010.    <span style="text-decoration: underline;">1</span>155.</span></span></code></pre>
</div>
</div>
<div class="section level2">
<h2 id="combining-variables-with-different-indices-in-a-single-tidy-format-data-frame">Combining variables with different indices in a single tidy format
data frame<a class="anchor" aria-label="anchor" href="#combining-variables-with-different-indices-in-a-single-tidy-format-data-frame"></a>
</h2>
<p><code><a href="../reference/spread_draws.html">spread_draws()</a></code> and <code><a href="../reference/spread_draws.html">gather_draws()</a></code> support
extracting variables that have different indices into the same data
frame. Indices with the same name are automatically matched up, and
values are duplicated as necessary to produce one row per all
combination of levels of all indices. For example, we might want to
calculate the mean within each condition (call this
<code>condition_mean</code>). In this model, that mean is the intercept
(<code>(Intercept)</code>) plus the effect for a given condition
(<code>b</code>).</p>
<p>We can gather draws from <code>(Intercept)</code> and <code>b</code>
together in a single data frame:</p>
<div class="sourceCode" id="cb36"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span> </span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">`(Intercept)`</span>, <span class="va">b</span><span class="op">[</span>,<span class="va">group</span><span class="op">]</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://rdrr.io/r/utils/head.html" class="external-link">head</a></span><span class="op">(</span><span class="fl">10</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 10 × 6</span></span></span>
<span><span class="co">## <span style="color: #949494;"># Groups:   group [5]</span></span></span>
<span><span class="co">##    .chain .iteration .draw `(Intercept)` group            b</span></span>
<span><span class="co">##     <span style="color: #949494; font-style: italic;">&lt;int&gt;</span>      <span style="color: #949494; font-style: italic;">&lt;int&gt;</span> <span style="color: #949494; font-style: italic;">&lt;int&gt;</span>         <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>        <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 1</span>      1          1     1         1.47  condition:A -<span style="color: #BB0000;">1.27</span> </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 2</span>      1          1     1         1.47  condition:B -<span style="color: #BB0000;">0.178</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 3</span>      1          1     1         1.47  condition:C  0.437</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 4</span>      1          1     1         1.47  condition:D -<span style="color: #BB0000;">0.413</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 5</span>      1          1     1         1.47  condition:E -<span style="color: #BB0000;">2.15</span> </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 6</span>      1          2     2         0.295 condition:A -<span style="color: #BB0000;">0.213</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 7</span>      1          2     2         0.295 condition:B  0.886</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 8</span>      1          2     2         0.295 condition:C  1.66 </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 9</span>      1          2     2         0.295 condition:D  0.713</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">10</span>      1          2     2         0.295 condition:E -<span style="color: #BB0000;">1.26</span></span></span></code></pre>
<p>Within each draw, <code>(Intercept)</code> is repeated as necessary
to correspond to every index of <code>b</code>. Thus, the
<code>mutate</code> function from dplyr can be used to find their sum,
<code>condition_mean</code> (which is the mean for each condition):</p>
<div class="sourceCode" id="cb38"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">`(Intercept)`</span>, <span class="va">b</span><span class="op">[</span>,<span class="va">group</span><span class="op">]</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/mutate.html" class="external-link">mutate</a></span><span class="op">(</span>condition_mean <span class="op">=</span> <span class="va">`(Intercept)`</span> <span class="op">+</span> <span class="va">b</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi</a></span><span class="op">(</span><span class="va">condition_mean</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 5 × 7</span></span></span>
<span><span class="co">##   group       condition_mean .lower .upper .width .point .interval</span></span>
<span><span class="co">##   <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>                <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>    </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span> condition:A          0.200 -<span style="color: #BB0000;">0.151</span>  0.538   0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span> condition:B          0.994  0.654  1.36    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">3</span> condition:C          1.83   1.48   2.18    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">4</span> condition:D          1.01   0.666  1.38    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">5</span> condition:E         -<span style="color: #BB0000;">0.878</span> -<span style="color: #BB0000;">1.24</span>  -<span style="color: #BB0000;">0.521</span>   0.95 median qi</span></span></code></pre>
<p><code><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi()</a></code> uses tidy evaluation (see
<code>vignette("tidy-evaluation", package = "rlang")</code>), so it can
take column expressions, not just column names. Thus, we can simplify
the above example by moving the calculation of
<code>condition_mean</code> from <code>mutate</code> into
<code><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi()</a></code>:</p>
<div class="sourceCode" id="cb40"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">`(Intercept)`</span>, <span class="va">b</span><span class="op">[</span>,<span class="va">group</span><span class="op">]</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi</a></span><span class="op">(</span>condition_mean <span class="op">=</span> <span class="va">`(Intercept)`</span> <span class="op">+</span> <span class="va">b</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 5 × 7</span></span></span>
<span><span class="co">##   group       condition_mean .lower .upper .width .point .interval</span></span>
<span><span class="co">##   <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>                <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>    </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span> condition:A          0.200 -<span style="color: #BB0000;">0.151</span>  0.538   0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span> condition:B          0.994  0.654  1.36    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">3</span> condition:C          1.83   1.48   2.18    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">4</span> condition:D          1.01   0.666  1.38    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">5</span> condition:E         -<span style="color: #BB0000;">0.878</span> -<span style="color: #BB0000;">1.24</span>  -<span style="color: #BB0000;">0.521</span>   0.95 median qi</span></span></code></pre>
</div>
<div class="section level2">
<h2 id="plotting-intervals-with-multiple-probability-levels">Plotting intervals with multiple probability levels<a class="anchor" aria-label="anchor" href="#plotting-intervals-with-multiple-probability-levels"></a>
</h2>
<p><code><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi()</a></code> and its sister functions can produce an
arbitrary number of probability intervals by setting the
<code>.width =</code> argument:</p>
<div class="sourceCode" id="cb42"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">`(Intercept)`</span>, <span class="va">b</span><span class="op">[</span>,<span class="va">group</span><span class="op">]</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi</a></span><span class="op">(</span>condition_mean <span class="op">=</span> <span class="va">`(Intercept)`</span> <span class="op">+</span> <span class="va">b</span>, .width <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">.95</span>, <span class="fl">.8</span>, <span class="fl">.5</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 15 × 7</span></span></span>
<span><span class="co">##    group       condition_mean  .lower .upper .width .point .interval</span></span>
<span><span class="co">##    <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>                <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>   <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>    </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 1</span> condition:A          0.200 -<span style="color: #BB0000;">0.151</span>   0.538   0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 2</span> condition:B          0.994  0.654   1.36    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 3</span> condition:C          1.83   1.48    2.18    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 4</span> condition:D          1.01   0.666   1.38    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 5</span> condition:E         -<span style="color: #BB0000;">0.878</span> -<span style="color: #BB0000;">1.24</span>   -<span style="color: #BB0000;">0.521</span>   0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 6</span> condition:A          0.200 -<span style="color: #BB0000;">0.026</span><span style="color: #BB0000; text-decoration: underline;">6</span>  0.410   0.8  median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 7</span> condition:B          0.994  0.769   1.23    0.8  median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 8</span> condition:C          1.83   1.60    2.06    0.8  median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 9</span> condition:D          1.01   0.779   1.24    0.8  median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">10</span> condition:E         -<span style="color: #BB0000;">0.878</span> -<span style="color: #BB0000;">1.11</span>   -<span style="color: #BB0000;">0.652</span>   0.8  median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">11</span> condition:A          0.200  0.084<span style="text-decoration: underline;">2</span>  0.310   0.5  median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">12</span> condition:B          0.994  0.875   1.12    0.5  median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">13</span> condition:C          1.83   1.71    1.95    0.5  median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">14</span> condition:D          1.01   0.889   1.13    0.5  median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">15</span> condition:E         -<span style="color: #BB0000;">0.878</span> -<span style="color: #BB0000;">1.00</span>   -<span style="color: #BB0000;">0.759</span>   0.5  median qi</span></span></code></pre>
<p>The results are in a tidy format: one row per group and uncertainty
interval width (<code>.width</code>). This facilitates plotting. For
example, assigning <code>-.width</code> to the <code>linewidth</code>
aesthetic will show all intervals, making thicker lines correspond to
smaller intervals. The <code><a href="http://mjskay.github.io/ggdist/reference/geom_pointinterval.html" class="external-link">ggdist::geom_pointinterval()</a></code> geom
automatically sets the <code>linewidth</code> aesthetic appropriately
based on the <code>.width</code> columns in the data to produce plots of
points with multiple probability levels:</p>
<div class="sourceCode" id="cb44"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">`(Intercept)`</span>, <span class="va">b</span><span class="op">[</span>,<span class="va">group</span><span class="op">]</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi</a></span><span class="op">(</span>condition_mean <span class="op">=</span> <span class="va">`(Intercept)`</span> <span class="op">+</span> <span class="va">b</span>, .width <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">.95</span>, <span class="fl">.66</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">group</span>, x <span class="op">=</span> <span class="va">condition_mean</span>, xmin <span class="op">=</span> <span class="va">.lower</span>, xmax <span class="op">=</span> <span class="va">.upper</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/geom_pointinterval.html" class="external-link">geom_pointinterval</a></span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-rstanarm_files/figure-html/unnamed-chunk-24-1.png" width="648"></p>
</div>
<div class="section level2">
<h2 id="intervals-with-densities">Intervals with densities<a class="anchor" aria-label="anchor" href="#intervals-with-densities"></a>
</h2>
<p>To see the density along with the intervals, we can use
<code><a href="http://mjskay.github.io/ggdist/reference/stat_eye.html" class="external-link">ggdist::stat_eye()</a></code> (“eye plots”, which combine intervals
with violin plots), or <code><a href="http://mjskay.github.io/ggdist/reference/stat_halfeye.html" class="external-link">ggdist::stat_halfeye()</a></code> (interval +
density plots):</p>
<div class="sourceCode" id="cb45"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">`(Intercept)`</span>, <span class="va">b</span><span class="op">[</span>,<span class="va">group</span><span class="op">]</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/mutate.html" class="external-link">mutate</a></span><span class="op">(</span>condition_mean <span class="op">=</span> <span class="va">`(Intercept)`</span> <span class="op">+</span> <span class="va">b</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">group</span>, x <span class="op">=</span> <span class="va">condition_mean</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/stat_halfeye.html" class="external-link">stat_halfeye</a></span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-rstanarm_files/figure-html/unnamed-chunk-25-1.png" width="648"></p>
<p>Or say you want to annotate portions of the densities in color; the
<code>fill</code> aesthetic can vary within a slab in all geoms and
stats in the <code><a href="http://mjskay.github.io/ggdist/reference/geom_slabinterval.html" class="external-link">ggdist::geom_slabinterval()</a></code> family, including
<code><a href="http://mjskay.github.io/ggdist/reference/stat_halfeye.html" class="external-link">ggdist::stat_halfeye()</a></code>. For example, if you want to
annotate a domain-specific region of practical equivalence (ROPE), you
could do something like this:</p>
<div class="sourceCode" id="cb46"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">`(Intercept)`</span>, <span class="va">b</span><span class="op">[</span>,<span class="va">group</span><span class="op">]</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/mutate.html" class="external-link">mutate</a></span><span class="op">(</span>condition_mean <span class="op">=</span> <span class="va">`(Intercept)`</span> <span class="op">+</span> <span class="va">b</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">group</span>, x <span class="op">=</span> <span class="va">condition_mean</span>, fill <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/MathFun.html" class="external-link">abs</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span> <span class="op">&lt;</span> <span class="fl">.8</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/stat_halfeye.html" class="external-link">stat_halfeye</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_abline.html" class="external-link">geom_vline</a></span><span class="op">(</span>xintercept <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="op">-</span><span class="fl">.8</span>, <span class="fl">.8</span><span class="op">)</span>, linetype <span class="op">=</span> <span class="st">"dashed"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_manual.html" class="external-link">scale_fill_manual</a></span><span class="op">(</span>values <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"gray80"</span>, <span class="st">"skyblue"</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-rstanarm_files/figure-html/unnamed-chunk-26-1.png" width="648"></p>
</div>
<div class="section level2">
<h2 id="other-visualizations-of-distributions-stat_slabinterval">Other visualizations of distributions:
<code>stat_slabinterval</code><a class="anchor" aria-label="anchor" href="#other-visualizations-of-distributions-stat_slabinterval"></a>
</h2>
<p>There are a variety of additional stats for visualizing distributions
in the <code><a href="http://mjskay.github.io/ggdist/reference/geom_slabinterval.html" class="external-link">ggdist::geom_slabinterval()</a></code> family of stats and
geoms:</p>
<p><img src="slabinterval_family.png" width="700" alt="The slabinterval family of geoms and stats"></p>
<p>See <code><a href="http://mjskay.github.io/ggdist/articles/slabinterval.html" class="external-link">vignette("slabinterval", package = "ggdist")</a></code> for an
overview.</p>
</div>
<div class="section level2">
<h2 id="posterior-means-and-predictions">Posterior means and predictions<a class="anchor" aria-label="anchor" href="#posterior-means-and-predictions"></a>
</h2>
<p>Rather than calculating conditional means manually as in the previous
example, we could use <code><a href="../reference/add_predicted_draws.html">add_epred_draws()</a></code>, which is analogous
to <code><a href="https://mc-stan.org/rstantools/reference/posterior_epred.html" class="external-link">rstanarm::posterior_epred()</a></code> (giving posterior draws from
the expectation of the posterior predictive; i.e. posterior
distributions of conditional means), but uses a tidy data format. We can
combine it with <code><a href="https://modelr.tidyverse.org/reference/data_grid.html" class="external-link">modelr::data_grid()</a></code> to first generate a
grid describing the predictions we want, then transform that grid into a
long-format data frame of draws from conditional means:</p>
<div class="sourceCode" id="cb47"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">ABC</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://modelr.tidyverse.org/reference/data_grid.html" class="external-link">data_grid</a></span><span class="op">(</span><span class="va">condition</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_epred_draws</a></span><span class="op">(</span><span class="va">m</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://rdrr.io/r/utils/head.html" class="external-link">head</a></span><span class="op">(</span><span class="fl">10</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 10 × 6</span></span></span>
<span><span class="co">## <span style="color: #949494;"># Groups:   condition, .row [1]</span></span></span>
<span><span class="co">##    condition  .row .chain .iteration .draw .epred</span></span>
<span><span class="co">##    <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>     <span style="color: #949494; font-style: italic;">&lt;int&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;int&gt;</span>      <span style="color: #949494; font-style: italic;">&lt;int&gt;</span> <span style="color: #949494; font-style: italic;">&lt;int&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 1</span> A             1     <span style="color: #BB0000;">NA</span>         <span style="color: #BB0000;">NA</span>     1 0.203 </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 2</span> A             1     <span style="color: #BB0000;">NA</span>         <span style="color: #BB0000;">NA</span>     2 0.081<span style="text-decoration: underline;">5</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 3</span> A             1     <span style="color: #BB0000;">NA</span>         <span style="color: #BB0000;">NA</span>     3 0.349 </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 4</span> A             1     <span style="color: #BB0000;">NA</span>         <span style="color: #BB0000;">NA</span>     4 0.164 </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 5</span> A             1     <span style="color: #BB0000;">NA</span>         <span style="color: #BB0000;">NA</span>     5 0.121 </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 6</span> A             1     <span style="color: #BB0000;">NA</span>         <span style="color: #BB0000;">NA</span>     6 0.632 </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 7</span> A             1     <span style="color: #BB0000;">NA</span>         <span style="color: #BB0000;">NA</span>     7 0.017<span style="text-decoration: underline;">2</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 8</span> A             1     <span style="color: #BB0000;">NA</span>         <span style="color: #BB0000;">NA</span>     8 0.168 </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 9</span> A             1     <span style="color: #BB0000;">NA</span>         <span style="color: #BB0000;">NA</span>     9 0.502 </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">10</span> A             1     <span style="color: #BB0000;">NA</span>         <span style="color: #BB0000;">NA</span>    10 0.039<span style="text-decoration: underline;">5</span></span></span></code></pre>
<p>To plot this example, we’ll also show the use of
<code><a href="http://mjskay.github.io/ggdist/reference/stat_pointinterval.html" class="external-link">ggdist::stat_pointinterval()</a></code> instead of
<code><a href="http://mjskay.github.io/ggdist/reference/geom_pointinterval.html" class="external-link">ggdist::geom_pointinterval()</a></code>, which summarizes draws into
point summaries and intervals within ggplot:</p>
<div class="sourceCode" id="cb49"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">ABC</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://modelr.tidyverse.org/reference/data_grid.html" class="external-link">data_grid</a></span><span class="op">(</span><span class="va">condition</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_epred_draws</a></span><span class="op">(</span><span class="va">m</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">.epred</span>, y <span class="op">=</span> <span class="va">condition</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/stat_pointinterval.html" class="external-link">stat_pointinterval</a></span><span class="op">(</span>.width <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">.66</span>, <span class="fl">.95</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-rstanarm_files/figure-html/unnamed-chunk-28-1.png" width="648"></p>
</div>
<div class="section level2">
<h2 id="quantile-dotplots">Quantile dotplots<a class="anchor" aria-label="anchor" href="#quantile-dotplots"></a>
</h2>
<p>Intervals are nice if the alpha level happens to line up with
whatever decision you are trying to make, but getting a shape of the
posterior is better (hence eye plots, above). On the other hand, making
inferences from density plots is imprecise (estimating the area of one
shape as a proportion of another is a hard perceptual task). Reasoning
about probability in frequency formats is easier, motivating <a href="https://github.com/mjskay/when-ish-is-my-bus/blob/master/quantile-dotplots.md" class="external-link">quantile
dotplots</a> (<a href="https://doi.org/10.1145/2858036.2858558" class="external-link">Kay et
al. 2016</a>, <a href="https://doi.org/10.1145/3173574.3173718" class="external-link">Fernandes et
al. 2018</a>), which also allow precise estimation of arbitrary
intervals (down to the dot resolution of the plot, 100 in the example
below).</p>
<p>Within the slabinterval family of geoms in tidybayes is the
<code>dots</code> and <code>dotsinterval</code> family, which
automatically determine appropriate bin sizes for dotplots and can
calculate quantiles from samples to construct quantile dotplots.
<code><a href="http://mjskay.github.io/ggdist/reference/stat_dotsinterval.html" class="external-link">ggdist::stat_dotsinterval()</a></code> is the horizontal variant
designed for use on samples:</p>
<div class="sourceCode" id="cb50"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">ABC</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://modelr.tidyverse.org/reference/data_grid.html" class="external-link">data_grid</a></span><span class="op">(</span><span class="va">condition</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_epred_draws</a></span><span class="op">(</span><span class="va">m</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">.epred</span>, y <span class="op">=</span> <span class="va">condition</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/stat_dotsinterval.html" class="external-link">stat_dotsinterval</a></span><span class="op">(</span>quantiles <span class="op">=</span> <span class="fl">100</span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-rstanarm_files/figure-html/unnamed-chunk-29-1.png" width="648"></p>
<p>The idea is to get away from thinking about the posterior as
indicating one canonical point or interval, but instead to represent it
as (say) 100 approximately equally likely points.</p>
</div>
<div class="section level2">
<h2 id="posterior-predictions">Posterior predictions<a class="anchor" aria-label="anchor" href="#posterior-predictions"></a>
</h2>
<p>Where <code><a href="../reference/add_predicted_draws.html">add_epred_draws()</a></code> is analogous to
<code><a href="https://mc-stan.org/rstantools/reference/posterior_epred.html" class="external-link">rstanarm::posterior_epred()</a></code>,
<code><a href="../reference/add_predicted_draws.html">add_predicted_draws()</a></code> is analogous to
<code><a href="https://mc-stan.org/rstantools/reference/posterior_predict.html" class="external-link">rstanarm::posterior_predict()</a></code>, giving draws from the
posterior predictive distribution.</p>
<p>We could use <code><a href="http://mjskay.github.io/ggdist/reference/stat_interval.html" class="external-link">tidybayes::stat_interval()</a></code> to plot
predictive bands alongside the data and posterior distributions of the
means:</p>
<div class="sourceCode" id="cb51"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">grid</span> <span class="op">=</span> <span class="va">ABC</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://modelr.tidyverse.org/reference/data_grid.html" class="external-link">data_grid</a></span><span class="op">(</span><span class="va">condition</span><span class="op">)</span></span>
<span></span>
<span><span class="va">means</span> <span class="op">=</span> <span class="va">grid</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_epred_draws</a></span><span class="op">(</span><span class="va">m</span><span class="op">)</span></span>
<span></span>
<span><span class="va">preds</span> <span class="op">=</span> <span class="va">grid</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_predicted_draws</a></span><span class="op">(</span><span class="va">m</span><span class="op">)</span></span>
<span></span>
<span><span class="va">ABC</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">condition</span>, x <span class="op">=</span> <span class="va">response</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/stat_interval.html" class="external-link">stat_interval</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">.prediction</span><span class="op">)</span>, data <span class="op">=</span> <span class="va">preds</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/stat_pointinterval.html" class="external-link">stat_pointinterval</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">.epred</span><span class="op">)</span>, data <span class="op">=</span> <span class="va">means</span>, .width <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">.66</span>, <span class="fl">.95</span><span class="op">)</span>, position <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/position_nudge.html" class="external-link">position_nudge</a></span><span class="op">(</span>y <span class="op">=</span> <span class="op">-</span><span class="fl">0.3</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html" class="external-link">geom_point</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_color_brewer</a></span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-rstanarm_files/figure-html/unnamed-chunk-30-1.png" width="648"></p>
</div>
<div class="section level2">
<h2 id="fitprediction-curves">Fit/prediction curves<a class="anchor" aria-label="anchor" href="#fitprediction-curves"></a>
</h2>
<p>To demonstrate drawing fit curves with uncertainty, let’s fit a
slightly naive model to part of the <code>mtcars</code> dataset:</p>
<div class="sourceCode" id="cb52"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m_mpg</span> <span class="op">=</span> <span class="fu"><a href="https://mc-stan.org/rstanarm/reference/stan_glm.html" class="external-link">stan_glm</a></span><span class="op">(</span><span class="va">mpg</span> <span class="op">~</span> <span class="va">hp</span> <span class="op">*</span> <span class="va">cyl</span>, data <span class="op">=</span> <span class="va">mtcars</span><span class="op">)</span></span></code></pre></div>
<p>We can plot fit curves with probability bands:</p>
<div class="sourceCode" id="cb53"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">mtcars</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/group_by.html" class="external-link">group_by</a></span><span class="op">(</span><span class="va">cyl</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://modelr.tidyverse.org/reference/data_grid.html" class="external-link">data_grid</a></span><span class="op">(</span>hp <span class="op">=</span> <span class="fu"><a href="https://modelr.tidyverse.org/reference/seq_range.html" class="external-link">seq_range</a></span><span class="op">(</span><span class="va">hp</span>, n <span class="op">=</span> <span class="fl">51</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_epred_draws</a></span><span class="op">(</span><span class="va">m_mpg</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">hp</span>, y <span class="op">=</span> <span class="va">mpg</span>, color <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html" class="external-link">ordered</a></span><span class="op">(</span><span class="va">cyl</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/stat_lineribbon.html" class="external-link">stat_lineribbon</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">.epred</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html" class="external-link">geom_point</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_fill_brewer</a></span><span class="op">(</span>palette <span class="op">=</span> <span class="st">"Greys"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_color_brewer</a></span><span class="op">(</span>palette <span class="op">=</span> <span class="st">"Set2"</span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-rstanarm_files/figure-html/unnamed-chunk-31-1.png" width="648"></p>
<p>Or we can sample a reasonable number of fit lines (say 100) and
overplot them:</p>
<div class="sourceCode" id="cb54"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">mtcars</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/group_by.html" class="external-link">group_by</a></span><span class="op">(</span><span class="va">cyl</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://modelr.tidyverse.org/reference/data_grid.html" class="external-link">data_grid</a></span><span class="op">(</span>hp <span class="op">=</span> <span class="fu"><a href="https://modelr.tidyverse.org/reference/seq_range.html" class="external-link">seq_range</a></span><span class="op">(</span><span class="va">hp</span>, n <span class="op">=</span> <span class="fl">101</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="co"># NOTE: this shows the use of ndraws to subsample within add_epred_draws()</span></span>
<span>  <span class="co"># ONLY do this IF you are planning to make spaghetti plots, etc.</span></span>
<span>  <span class="co"># NEVER subsample to a small sample to plot intervals, densities, etc.</span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_epred_draws</a></span><span class="op">(</span><span class="va">m_mpg</span>, ndraws <span class="op">=</span> <span class="fl">100</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">hp</span>, y <span class="op">=</span> <span class="va">mpg</span>, color <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html" class="external-link">ordered</a></span><span class="op">(</span><span class="va">cyl</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_path.html" class="external-link">geom_line</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">.epred</span>, group <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/paste.html" class="external-link">paste</a></span><span class="op">(</span><span class="va">cyl</span>, <span class="va">.draw</span><span class="op">)</span><span class="op">)</span>, alpha <span class="op">=</span> <span class="fl">.1</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html" class="external-link">geom_point</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_color_brewer</a></span><span class="op">(</span>palette <span class="op">=</span> <span class="st">"Dark2"</span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-rstanarm_files/figure-html/unnamed-chunk-32-1.png" width="648"></p>
<p>Or we could plot posterior predictions (instead of means). For this
example we’ll also use <code>alpha</code> to make it easier to see
overlapping bands:</p>
<div class="sourceCode" id="cb55"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">mtcars</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/group_by.html" class="external-link">group_by</a></span><span class="op">(</span><span class="va">cyl</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://modelr.tidyverse.org/reference/data_grid.html" class="external-link">data_grid</a></span><span class="op">(</span>hp <span class="op">=</span> <span class="fu"><a href="https://modelr.tidyverse.org/reference/seq_range.html" class="external-link">seq_range</a></span><span class="op">(</span><span class="va">hp</span>, n <span class="op">=</span> <span class="fl">101</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_predicted_draws</a></span><span class="op">(</span><span class="va">m_mpg</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">hp</span>, y <span class="op">=</span> <span class="va">mpg</span>, color <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html" class="external-link">ordered</a></span><span class="op">(</span><span class="va">cyl</span><span class="op">)</span>, fill <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html" class="external-link">ordered</a></span><span class="op">(</span><span class="va">cyl</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/stat_lineribbon.html" class="external-link">stat_lineribbon</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">.prediction</span><span class="op">)</span>, .width <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">.95</span>, <span class="fl">.80</span>, <span class="fl">.50</span><span class="op">)</span>, alpha <span class="op">=</span> <span class="fl">1</span><span class="op">/</span><span class="fl">4</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html" class="external-link">geom_point</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_fill_brewer</a></span><span class="op">(</span>palette <span class="op">=</span> <span class="st">"Set2"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_color_brewer</a></span><span class="op">(</span>palette <span class="op">=</span> <span class="st">"Dark2"</span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-rstanarm_files/figure-html/unnamed-chunk-33-1.png" width="648"></p>
<p>See <code><a href="../articles/tidy-brms.html">vignette("tidy-brms")</a></code> for additional examples of fit
lines, including animated <a href="https://mucollective.northwestern.edu/project/hops-trends" class="external-link">hypothetical
outcome plots (HOPs)</a>.</p>
</div>
<div class="section level2">
<h2 id="comparing-levels-of-a-factor">Comparing levels of a factor<a class="anchor" aria-label="anchor" href="#comparing-levels-of-a-factor"></a>
</h2>
<p>If we wish compare the means from each condition,
<code><a href="../reference/compare_levels.html">compare_levels()</a></code> facilitates comparisons of the value of
some variable across levels of a factor. By default it computes all
pairwise differences.</p>
<p>Let’s demonstrate <code><a href="../reference/compare_levels.html">compare_levels()</a></code> with
<code><a href="http://mjskay.github.io/ggdist/reference/stat_halfeye.html" class="external-link">ggdist::stat_halfeye()</a></code>. We’ll also re-order by the mean of
the difference:</p>
<div class="sourceCode" id="cb56"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">b</span><span class="op">[</span>,,<span class="va">condition</span><span class="op">]</span>, sep <span class="op">=</span> <span class="st">"[, :]"</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/compare_levels.html">compare_levels</a></span><span class="op">(</span><span class="va">b</span>, by <span class="op">=</span> <span class="va">condition</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/group_by.html" class="external-link">ungroup</a></span><span class="op">(</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/mutate.html" class="external-link">mutate</a></span><span class="op">(</span>condition <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/reorder.factor.html" class="external-link">reorder</a></span><span class="op">(</span><span class="va">condition</span>, <span class="va">b</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">condition</span>, x <span class="op">=</span> <span class="va">b</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/stat_halfeye.html" class="external-link">stat_halfeye</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_abline.html" class="external-link">geom_vline</a></span><span class="op">(</span>xintercept <span class="op">=</span> <span class="fl">0</span>, linetype <span class="op">=</span> <span class="st">"dashed"</span><span class="op">)</span> </span></code></pre></div>
<p><img src="tidy-rstanarm_files/figure-html/unnamed-chunk-34-1.png" width="648"></p>
</div>
<div class="section level2">
<h2 id="ordinal-models">Ordinal models<a class="anchor" aria-label="anchor" href="#ordinal-models"></a>
</h2>
<div class="section level3">
<h3 id="ordinal-model-with-categorical-predictor">Ordinal model with categorical predictor<a class="anchor" aria-label="anchor" href="#ordinal-model-with-categorical-predictor"></a>
</h3>
<p>Here’s an ordinal model with a categorical predictor:</p>
<div class="sourceCode" id="cb57"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/utils/data.html" class="external-link">data</a></span><span class="op">(</span><span class="va">esoph</span><span class="op">)</span></span>
<span><span class="va">m_esoph_rs</span> <span class="op">=</span> <span class="fu"><a href="https://mc-stan.org/rstanarm/reference/stan_polr.html" class="external-link">stan_polr</a></span><span class="op">(</span><span class="va">tobgp</span> <span class="op">~</span> <span class="va">agegp</span>, data <span class="op">=</span> <span class="va">esoph</span>, prior <span class="op">=</span> <span class="fu"><a href="https://mc-stan.org/rstanarm/reference/priors.html" class="external-link">R2</a></span><span class="op">(</span><span class="fl">0.25</span><span class="op">)</span>, prior_counts <span class="op">=</span> <span class="fu">rstanarm</span><span class="fu">::</span><span class="fu"><a href="https://mc-stan.org/rstanarm/reference/priors.html" class="external-link">dirichlet</a></span><span class="op">(</span><span class="fl">1</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p>The <code><a href="https://mc-stan.org/rstantools/reference/posterior_linpred.html" class="external-link">rstanarm::posterior_linpred()</a></code> function for ordinal
regression models in rstanarm returns the link-level prediction for each
draw (in contrast to <code><a href="https://mc-stan.org/rstantools/reference/posterior_epred.html" class="external-link">brms::posterior_epred()</a></code>, which returns
one prediction per category for ordinal models, see the ordinal
regression examples in <code><a href="../articles/tidy-brms.html">vignette("tidy-brms")</a></code>).
Unfortunately, <code><a href="https://mc-stan.org/rstantools/reference/posterior_epred.html" class="external-link">rstanarm::posterior_epred()</a></code> does not provide
this format. The philosophy of <code>tidybayes</code> is to tidy
whatever format is output by a model, so in keeping with that
philosophy, when applied to ordinal <code>rstanarm</code> models, we
will use examples with <code><a href="../reference/add_predicted_draws.html">add_linpred_draws()</a></code> and show how to
transform them into predicted per-category probabilities.</p>
<p>For example, here is a plot of the link-level fit:</p>
<div class="sourceCode" id="cb58"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">esoph</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://modelr.tidyverse.org/reference/data_grid.html" class="external-link">data_grid</a></span><span class="op">(</span><span class="va">agegp</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_linpred_draws</a></span><span class="op">(</span><span class="va">m_esoph_rs</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/numeric.html" class="external-link">as.numeric</a></span><span class="op">(</span><span class="va">agegp</span><span class="op">)</span>, y <span class="op">=</span> <span class="va">.linpred</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/stat_lineribbon.html" class="external-link">stat_lineribbon</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_fill_brewer</a></span><span class="op">(</span>palette <span class="op">=</span> <span class="st">"Greys"</span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-rstanarm_files/figure-html/unnamed-chunk-35-1.png" width="648"></p>
<p>This can be hard to interpret. To turn this into predicted
probabilities on a per-category basis, we have to use the fact that an
ordinal logistic regression defines the probability of an outcome in
category
<math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mi>j</mi><annotation encoding="application/x-tex">j</annotation></semantics></math><strong>or less</strong> as:</p>
<p><math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext mathvariant="normal">logit</mtext><mrow><mo stretchy="true" form="prefix">[</mo><mi>P</mi><mi>r</mi><mrow><mo stretchy="true" form="prefix">(</mo><mi>Y</mi><mo>≤</mo><mi>j</mi><mo stretchy="true" form="postfix">)</mo></mrow><mo stretchy="true" form="postfix">]</mo></mrow><mo>=</mo><msub><mi>α</mi><mi>j</mi></msub><mo>−</mo><mi>β</mi><mi>x</mi></mrow><annotation encoding="application/x-tex">
\textrm{logit}\left[Pr(Y\le j)\right] = \alpha_j - \beta x
</annotation></semantics></math></p>
<p>Thus, the probability of category
<math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mi>j</mi><annotation encoding="application/x-tex">j</annotation></semantics></math>
is:</p>
<p><math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mtable><mtr><mtd columnalign="right" style="text-align: right"><mi>P</mi><mi>r</mi><mrow><mo stretchy="true" form="prefix">(</mo><mi>Y</mi><mo>=</mo><mi>j</mi><mo stretchy="true" form="postfix">)</mo></mrow></mtd><mtd columnalign="left" style="text-align: left"><mo>=</mo><mi>P</mi><mi>r</mi><mrow><mo stretchy="true" form="prefix">(</mo><mi>Y</mi><mo>≤</mo><mi>j</mi><mo stretchy="true" form="postfix">)</mo></mrow><mo>−</mo><mi>P</mi><mi>r</mi><mrow><mo stretchy="true" form="prefix">(</mo><mi>Y</mi><mo>≤</mo><mi>j</mi><mo>−</mo><mn>1</mn><mo stretchy="true" form="postfix">)</mo></mrow></mtd></mtr><mtr><mtd columnalign="right" style="text-align: right"></mtd><mtd columnalign="left" style="text-align: left"><mo>=</mo><msup><mtext mathvariant="normal">logit</mtext><mrow><mo>−</mo><mn>1</mn></mrow></msup><mrow><mo stretchy="true" form="prefix">(</mo><msub><mi>α</mi><mi>j</mi></msub><mo>−</mo><mi>β</mi><mi>x</mi><mo stretchy="true" form="postfix">)</mo></mrow><mo>−</mo><msup><mtext mathvariant="normal">logit</mtext><mrow><mo>−</mo><mn>1</mn></mrow></msup><mrow><mo stretchy="true" form="prefix">(</mo><msub><mi>α</mi><mrow><mi>j</mi><mo>−</mo><mn>1</mn></mrow></msub><mo>−</mo><mi>β</mi><mi>x</mi><mo stretchy="true" form="postfix">)</mo></mrow></mtd></mtr></mtable><annotation encoding="application/x-tex">
\begin{align}
Pr(Y = j) &amp;= Pr(Y \le j) - Pr(Y \le j - 1)\\
&amp;= \textrm{logit}^{-1}(\alpha_j - \beta x) - \textrm{logit}^{-1}(\alpha_{j-1} - \beta x)
\end{align}
</annotation></semantics></math></p>
<p>To derive these values, we need two things:</p>
<ul>
<li><p>The
<math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><semantics><msub><mi>α</mi><mi>j</mi></msub><annotation encoding="application/x-tex">\alpha_j</annotation></semantics></math>
values. These are threshold parameters fitted by the model. For
convenience, if there are
<math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mi>k</mi><annotation encoding="application/x-tex">k</annotation></semantics></math>
levels, we will take
<math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>α</mi><mi>k</mi></msub><mo>=</mo><mo>+</mo><mi>∞</mi></mrow><annotation encoding="application/x-tex">\alpha_k = +\infty</annotation></semantics></math>,
since the probability of being in the top level or below it is
1.</p></li>
<li><p>The
<math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>β</mi><mi>x</mi></mrow><annotation encoding="application/x-tex">\beta x</annotation></semantics></math>
values. These are just the <code>.linpred</code> values returned by
<code><a href="../reference/add_predicted_draws.html">add_linpred_draws()</a></code>.</p></li>
</ul>
<p>The thresholds in <code>rstanarm</code> are coefficients with names
containing <code>|</code>, indicating which categories they are
thresholds between. We can see those parameters in the list of variables
in the model:</p>
<div class="sourceCode" id="cb59"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="../reference/get_variables.html">get_variables</a></span><span class="op">(</span><span class="va">m_esoph_rs</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">##  [1] "agegp.L"        "agegp.Q"        "agegp.C"        "agegp^4"        "agegp^5"        "0-9g/day|10-19"</span></span>
<span><span class="co">##  [7] "10-19|20-29"    "20-29|30+"      "accept_stat__"  "stepsize__"     "treedepth__"    "n_leapfrog__"  </span></span>
<span><span class="co">## [13] "divergent__"    "energy__"</span></span></code></pre>
<p>We can extract those automatically by using the
<code>regex = TRUE</code> argument to <code><a href="../reference/spread_draws.html">gather_draws()</a></code> to
find all variables containing a <code>|</code> character. We will then
use <code>dplyr::summarise_all(list)</code> to turn these thresholds
into a list column, and add a final threshold equal to
<math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo>+</mo><mi>∞</mi></mrow><annotation encoding="application/x-tex">+\infty</annotation></semantics></math>
(to represent the highest category):</p>
<div class="sourceCode" id="cb61"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">thresholds</span> <span class="op">=</span> <span class="va">m_esoph_rs</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">gather_draws</a></span><span class="op">(</span><span class="va">`.*[|].*`</span>, regex <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/group_by.html" class="external-link">group_by</a></span><span class="op">(</span><span class="va">.draw</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/select.html" class="external-link">select</a></span><span class="op">(</span><span class="va">.draw</span>, threshold <span class="op">=</span> <span class="va">.value</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/summarise_all.html" class="external-link">summarise_all</a></span><span class="op">(</span><span class="va">list</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/mutate.html" class="external-link">mutate</a></span><span class="op">(</span>threshold <span class="op">=</span> <span class="fu"><a href="https://purrr.tidyverse.org/reference/map.html" class="external-link">map</a></span><span class="op">(</span><span class="va">threshold</span>, <span class="op">~</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="va">.</span>, <span class="cn">Inf</span><span class="op">)</span><span class="op">)</span><span class="op">)</span></span>
<span></span>
<span><span class="fu"><a href="https://rdrr.io/r/utils/head.html" class="external-link">head</a></span><span class="op">(</span><span class="va">thresholds</span>, <span class="fl">10</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 10 × 2</span></span></span>
<span><span class="co">##    .draw threshold</span></span>
<span><span class="co">##    <span style="color: #949494; font-style: italic;">&lt;int&gt;</span> <span style="color: #949494; font-style: italic;">&lt;list&gt;</span>   </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 1</span>     1 <span style="color: #949494;">&lt;dbl [4]&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 2</span>     2 <span style="color: #949494;">&lt;dbl [4]&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 3</span>     3 <span style="color: #949494;">&lt;dbl [4]&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 4</span>     4 <span style="color: #949494;">&lt;dbl [4]&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 5</span>     5 <span style="color: #949494;">&lt;dbl [4]&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 6</span>     6 <span style="color: #949494;">&lt;dbl [4]&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 7</span>     7 <span style="color: #949494;">&lt;dbl [4]&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 8</span>     8 <span style="color: #949494;">&lt;dbl [4]&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 9</span>     9 <span style="color: #949494;">&lt;dbl [4]&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">10</span>    10 <span style="color: #949494;">&lt;dbl [4]&gt;</span></span></span></code></pre>
<p>For example, the threshold vector from one row of this data frame
(i.e., from one draw from the posterior) looks like this:</p>
<div class="sourceCode" id="cb63"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">thresholds</span><span class="op">[</span><span class="fl">1</span>,<span class="op">]</span><span class="op">$</span><span class="va">threshold</span></span></code></pre></div>
<pre><code><span><span class="co">## [[1]]</span></span>
<span><span class="co">## [1] -0.9705395  0.1546626  1.0081490        Inf</span></span></code></pre>
<p>We can combine those thresholds (the
<math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><semantics><msub><mi>α</mi><mi>j</mi></msub><annotation encoding="application/x-tex">\alpha_j</annotation></semantics></math>
values from the above formula) with the <code>.linpred</code> column
from <code><a href="../reference/add_predicted_draws.html">add_linpred_draws()</a></code>
(<math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>β</mi><mi>x</mi></mrow><annotation encoding="application/x-tex">\beta x</annotation></semantics></math>
from the above formula) to calculate per-category probabilities:</p>
<div class="sourceCode" id="cb65"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">esoph</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://modelr.tidyverse.org/reference/data_grid.html" class="external-link">data_grid</a></span><span class="op">(</span><span class="va">agegp</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_linpred_draws</a></span><span class="op">(</span><span class="va">m_esoph_rs</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/mutate-joins.html" class="external-link">inner_join</a></span><span class="op">(</span><span class="va">thresholds</span>, by <span class="op">=</span> <span class="st">".draw"</span>, multiple <span class="op">=</span> <span class="st">"all"</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/mutate.html" class="external-link">mutate</a></span><span class="op">(</span>`P(Y = category)` <span class="op">=</span> <span class="fu"><a href="https://purrr.tidyverse.org/reference/map2.html" class="external-link">map2</a></span><span class="op">(</span><span class="va">threshold</span>, <span class="va">.linpred</span>, <span class="kw">function</span><span class="op">(</span><span class="va">alpha</span>, <span class="va">beta_x</span><span class="op">)</span></span>
<span>      <span class="co"># this part is logit^-1(alpha_j - beta*x) - logit^-1(alpha_j-1 - beta*x)</span></span>
<span>      <span class="fu"><a href="https://rdrr.io/r/stats/Logistic.html" class="external-link">plogis</a></span><span class="op">(</span><span class="va">alpha</span> <span class="op">-</span> <span class="va">beta_x</span><span class="op">)</span> <span class="op">-</span> </span>
<span>      <span class="fu"><a href="https://rdrr.io/r/stats/Logistic.html" class="external-link">plogis</a></span><span class="op">(</span><span class="fu"><a href="https://dplyr.tidyverse.org/reference/lead-lag.html" class="external-link">lag</a></span><span class="op">(</span><span class="va">alpha</span>, default <span class="op">=</span> <span class="op">-</span><span class="cn">Inf</span><span class="op">)</span> <span class="op">-</span> <span class="va">beta_x</span><span class="op">)</span></span>
<span>    <span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/mutate.html" class="external-link">mutate</a></span><span class="op">(</span>.category <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/list.html" class="external-link">list</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/levels.html" class="external-link">levels</a></span><span class="op">(</span><span class="va">esoph</span><span class="op">$</span><span class="va">tobgp</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://tidyr.tidyverse.org/reference/unnest.html" class="external-link">unnest</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="va">threshold</span>, <span class="va">`P(Y = category)`</span>, <span class="va">.category</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">agegp</span>, y <span class="op">=</span> <span class="va">`P(Y = category)`</span>, color <span class="op">=</span> <span class="va">.category</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/stat_pointinterval.html" class="external-link">stat_pointinterval</a></span><span class="op">(</span>position <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/position_dodge.html" class="external-link">position_dodge</a></span><span class="op">(</span>width <span class="op">=</span> <span class="fl">.4</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_size.html" class="external-link">scale_size_continuous</a></span><span class="op">(</span>guide <span class="op">=</span> <span class="st">"none"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_manual.html" class="external-link">scale_color_manual</a></span><span class="op">(</span>values <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/RColorBrewer/man/ColorBrewer.html" class="external-link">brewer.pal</a></span><span class="op">(</span><span class="fl">6</span>, <span class="st">"Blues"</span><span class="op">)</span><span class="op">[</span><span class="op">-</span><span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">1</span>,<span class="fl">2</span><span class="op">)</span><span class="op">]</span><span class="op">)</span> </span></code></pre></div>
<p><img src="tidy-rstanarm_files/figure-html/unnamed-chunk-39-1.png" width="648"></p>
<p>It is hard to see the changes in categories in the above plot; let’s
try something that gives a better gist of the distribution within each
year:</p>
<div class="sourceCode" id="cb66"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">esoph_plot</span> <span class="op">=</span> <span class="va">esoph</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://modelr.tidyverse.org/reference/data_grid.html" class="external-link">data_grid</a></span><span class="op">(</span><span class="va">agegp</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_linpred_draws</a></span><span class="op">(</span><span class="va">m_esoph_rs</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/mutate-joins.html" class="external-link">inner_join</a></span><span class="op">(</span><span class="va">thresholds</span>, by <span class="op">=</span> <span class="st">".draw"</span>, multiple <span class="op">=</span> <span class="st">"all"</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/mutate.html" class="external-link">mutate</a></span><span class="op">(</span>`P(Y = category)` <span class="op">=</span> <span class="fu"><a href="https://purrr.tidyverse.org/reference/map2.html" class="external-link">map2</a></span><span class="op">(</span><span class="va">threshold</span>, <span class="va">.linpred</span>, <span class="kw">function</span><span class="op">(</span><span class="va">alpha</span>, <span class="va">beta_x</span><span class="op">)</span></span>
<span>      <span class="co"># this part is logit^-1(alpha_j - beta*x) - logit^-1(alpha_j-1 - beta*x)</span></span>
<span>      <span class="fu"><a href="https://rdrr.io/r/stats/Logistic.html" class="external-link">plogis</a></span><span class="op">(</span><span class="va">alpha</span> <span class="op">-</span> <span class="va">beta_x</span><span class="op">)</span> <span class="op">-</span> </span>
<span>      <span class="fu"><a href="https://rdrr.io/r/stats/Logistic.html" class="external-link">plogis</a></span><span class="op">(</span><span class="fu"><a href="https://dplyr.tidyverse.org/reference/lead-lag.html" class="external-link">lag</a></span><span class="op">(</span><span class="va">alpha</span>, default <span class="op">=</span> <span class="op">-</span><span class="cn">Inf</span><span class="op">)</span> <span class="op">-</span> <span class="va">beta_x</span><span class="op">)</span></span>
<span>    <span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/mutate.html" class="external-link">mutate</a></span><span class="op">(</span>.category <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/list.html" class="external-link">list</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/levels.html" class="external-link">levels</a></span><span class="op">(</span><span class="va">esoph</span><span class="op">$</span><span class="va">tobgp</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://tidyr.tidyverse.org/reference/unnest.html" class="external-link">unnest</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="va">threshold</span>, <span class="va">`P(Y = category)`</span>, <span class="va">.category</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">`P(Y = category)`</span>, y <span class="op">=</span> <span class="va">.category</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/coord_cartesian.html" class="external-link">coord_cartesian</a></span><span class="op">(</span>expand <span class="op">=</span> <span class="cn">FALSE</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/facet_grid.html" class="external-link">facet_grid</a></span><span class="op">(</span><span class="va">.</span> <span class="op">~</span> <span class="va">agegp</span>, switch <span class="op">=</span> <span class="st">"x"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggtheme.html" class="external-link">theme_classic</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/theme.html" class="external-link">theme</a></span><span class="op">(</span>strip.background <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/element.html" class="external-link">element_blank</a></span><span class="op">(</span><span class="op">)</span>, strip.placement <span class="op">=</span> <span class="st">"outside"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span><span class="st">"P(tobacco consumption category | age group)"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">xlab</a></span><span class="op">(</span><span class="st">"age group"</span><span class="op">)</span></span>
<span></span>
<span><span class="va">esoph_plot</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/stat_summary.html" class="external-link">stat_summary</a></span><span class="op">(</span>fun <span class="op">=</span> <span class="va">median</span>, geom <span class="op">=</span> <span class="st">"bar"</span>, fill <span class="op">=</span> <span class="st">"gray65"</span>, width <span class="op">=</span> <span class="fl">1</span>, color <span class="op">=</span> <span class="st">"white"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/stat_pointinterval.html" class="external-link">stat_pointinterval</a></span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-rstanarm_files/figure-html/unnamed-chunk-40-1.png" width="648"></p>
<p>The bars in this case might present a false sense of precision, so we
could also try CCDF barplots instead:</p>
<div class="sourceCode" id="cb67"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">esoph_plot</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/stat_ccdfinterval.html" class="external-link">stat_ccdfinterval</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/expand_limits.html" class="external-link">expand_limits</a></span><span class="op">(</span>x <span class="op">=</span> <span class="fl">0</span><span class="op">)</span> <span class="co">#ensure bars go to 0</span></span></code></pre></div>
<p><img src="tidy-rstanarm_files/figure-html/unnamed-chunk-41-1.png" width="648"></p>
<p>This output should be very similar to the output from the
corresponding <code>m_esoph_brm</code> model in
<code><a href="../articles/tidy-brms.html">vignette("tidy-brms")</a></code> (modulo different priors), though it
takes a bit more work to produce in <code>rstanarm</code> compared to
<code>brms</code>.</p>
</div>
</div>
  </div>

  <div class="col-md-3 hidden-xs hidden-sm" id="pkgdown-sidebar">

      </div>

</div>



      <footer><div class="copyright">
  <p></p>
<p>Developed by <a href="https://www.mjskay.com" class="external-link">Matthew Kay</a>.</p>
</div>

<div class="pkgdown">
  <p></p>
<p>Site built with <a href="https://pkgdown.r-lib.org/" class="external-link">pkgdown</a> 2.1.0.</p>
</div>

      </footer>
</div>






  </body>
</html>
