<!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>Using tidybayes with the posterior package • 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="Using tidybayes with the posterior package">
<!-- 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>Using tidybayes with the posterior package</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-posterior.Rmd" class="external-link"><code>vignettes/tidy-posterior.Rmd</code></a></small>
      <div class="hidden name"><code>tidy-posterior.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 along with the <code>posterior</code>
package (and particularly the <code><a href="https://mc-stan.org/posterior/reference/rvar.html" class="external-link">posterior::rvar()</a></code> datatype)
to extract and visualize <a href="https://dx.doi.org/10.18637/jss.v059.i10" class="external-link">tidy</a> data frames of
<code>rvar</code>s from posterior distributions of model variables,
fits, and predictions.</p>
<p>This workflow is a “long-data-frame-of-<code>rvar</code>s” workflow,
which is bit different from the “long-data-frame-of-draws” workflow
described in <code><a href="../articles/tidybayes.html">vignette("tidybayes")</a></code> or
<code><a href="../articles/tidy-brms.html">vignette("tidy-brms")</a></code>. The <code>rvar</code> based approach
may be particularly useful on larger models, as it is more memory
efficient.</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://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://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://github.com/paul-buerkner/brms" class="external-link">brms</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://ggrepel.slowkow.com/" class="external-link">ggrepel</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 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/posterior/" class="external-link">posterior</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://pkg.mitchelloharawild.com/distributional/" class="external-link">distributional</a></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-posterior_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://paul-buerkner.github.io/brms/reference/brm.html" class="external-link">brm</a></span><span class="op">(</span></span>
<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>, </span>
<span>  data <span class="op">=</span> <span class="va">ABC</span>, </span>
<span>  prior <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>
<span>    <span class="fu"><a href="https://paul-buerkner.github.io/brms/reference/set_prior.html" class="external-link">prior</a></span><span class="op">(</span><span class="fu">normal</span><span class="op">(</span><span class="fl">0</span>, <span class="fl">1</span><span class="op">)</span>, class <span class="op">=</span> <span class="va">Intercept</span><span class="op">)</span>,</span>
<span>    <span class="fu"><a href="https://paul-buerkner.github.io/brms/reference/set_prior.html" class="external-link">prior</a></span><span class="op">(</span><span class="fu">student_t</span><span class="op">(</span><span class="fl">3</span>, <span class="fl">0</span>, <span class="fl">1</span><span class="op">)</span>, class <span class="op">=</span> <span class="va">sd</span><span class="op">)</span>,</span>
<span>    <span class="fu"><a href="https://paul-buerkner.github.io/brms/reference/set_prior.html" class="external-link">prior</a></span><span class="op">(</span><span class="fu">student_t</span><span class="op">(</span><span class="fl">3</span>, <span class="fl">0</span>, <span class="fl">1</span><span class="op">)</span>, class <span class="op">=</span> <span class="va">sigma</span><span class="op">)</span></span>
<span>  <span class="op">)</span>,</span>
<span>  control <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>adapt_delta <span class="op">=</span> <span class="fl">.99</span><span class="op">)</span>,</span>
<span>  </span>
<span>  file <span class="op">=</span> <span class="st">"models/tidy-brms_m.rds"</span> <span class="co"># cache model (can be removed)  </span></span>
<span><span class="op">)</span></span></code></pre></div>
<p>The format returned by <code><a href="../reference/tidy_draws.html">tidybayes::tidy_draws()</a></code> is
compatible with the <code><a href="https://mc-stan.org/posterior/reference/draws_df.html" class="external-link">posterior::draws_df()</a></code> format, so
<code><a href="https://mc-stan.org/posterior/reference/draws_summary.html" class="external-link">posterior::summarise_draws()</a></code> supports it. Thus, we can use
<code><a href="https://mc-stan.org/posterior/reference/draws_summary.html" class="external-link">posterior::summarise_draws()</a></code> to get a quick look at draws
from the model:</p>
<div class="sourceCode" id="cb8"><pre class="downlit sourceCode r">
<code class="sourceCode R"><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="fu"><a href="../reference/tidy_draws.html">tidy_draws</a></span><span class="op">(</span><span class="va">m</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 × 10</span></span></span>
<span><span class="co">##    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;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> b_Intercept            0.504    0.526   0.478  4.56<span style="color: #949494;">e</span><span style="color: #BB0000;">-1</span>  -<span style="color: #BB0000;">0.310</span>    1.26     1.00   904.      <span style="text-decoration: underline;">1</span>204.</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 2</span> sd_condition__Inter…   1.17     1.09    0.428  3.57<span style="color: #949494;">e</span><span style="color: #BB0000;">-1</span>   0.651    1.94     1.00   944.      <span style="text-decoration: underline;">1</span>542.</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 3</span> sigma                  0.561    0.557   0.060<span style="text-decoration: underline;">2</span> 5.89<span style="color: #949494;">e</span><span style="color: #BB0000;">-2</span>   0.470    0.668    1.00  <span style="text-decoration: underline;">2</span>057.      <span style="text-decoration: underline;">2</span>243.</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 4</span> r_condition[A,Inter…  -<span style="color: #BB0000;">0.312</span>   -<span style="color: #BB0000;">0.336</span>   0.498  4.80<span style="color: #949494;">e</span><span style="color: #BB0000;">-1</span>  -<span style="color: #BB0000;">1.08</span>     0.526    1.00   972.      <span style="text-decoration: underline;">1</span>267.</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 5</span> r_condition[B,Inter…   0.493    0.480   0.500  4.74<span style="color: #949494;">e</span><span style="color: #BB0000;">-1</span>  -<span style="color: #BB0000;">0.301</span>    1.34     1.00   983.      <span style="text-decoration: underline;">1</span>326.</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 6</span> r_condition[C,Inter…   1.33     1.32    0.501  4.78<span style="color: #949494;">e</span><span style="color: #BB0000;">-1</span>   0.557    2.19     1.00   994.      <span style="text-decoration: underline;">1</span>489.</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 7</span> r_condition[D,Inter…   0.512    0.498   0.502  4.77<span style="color: #949494;">e</span><span style="color: #BB0000;">-1</span>  -<span style="color: #BB0000;">0.277</span>    1.36     1.00  <span style="text-decoration: underline;">1</span>013.      <span style="text-decoration: underline;">1</span>396.</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 8</span> r_condition[E,Inter…  -<span style="color: #BB0000;">1.39</span>    -<span style="color: #BB0000;">1.40</span>    0.500  4.68<span style="color: #949494;">e</span><span style="color: #BB0000;">-1</span>  -<span style="color: #BB0000;">2.17</span>    -<span style="color: #BB0000;">0.535</span>    1.00   990.      <span style="text-decoration: underline;">1</span>392.</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 9</span> lp__                 -<span style="color: #BB0000;">51.9</span>    -<span style="color: #BB0000;">51.6</span>     2.41   2.33<span style="color: #949494;">e</span>+0 -<span style="color: #BB0000;">56.3</span>    -<span style="color: #BB0000;">48.7</span>      1.00   977.      <span style="text-decoration: underline;">1</span>634.</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">10</span> accept_stat__          0.988    0.995   0.021<span style="text-decoration: underline;">1</span> 6.66<span style="color: #949494;">e</span><span style="color: #BB0000;">-3</span>   0.952    1.00     1.05    59.5      192.</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">11</span> stepsize__             0.054<span style="text-decoration: underline;">4</span>   0.054<span style="text-decoration: underline;">9</span>  0.011<span style="text-decoration: underline;">9</span> 1.62<span style="color: #949494;">e</span><span style="color: #BB0000;">-2</span>   0.039<span style="text-decoration: underline;">3</span>   0.068<span style="text-decoration: underline;">4</span> <span style="color: #BB0000;">Inf</span>        4.03      <span style="color: #BB0000;">NA</span> </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">12</span> treedepth__            5.31     5       1.01   1.48<span style="color: #949494;">e</span>+0   4        7        1.07    40.9       <span style="color: #BB0000;">NA</span> </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">13</span> n_leapfrog__          65.9     63      42.8    4.74<span style="color: #949494;">e</span>+1  15      127        1.07    41.6     <span style="text-decoration: underline;">1</span>530.</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">14</span> divergent__            0        0       0      0   <span style="color: #949494;"> </span>     0        0       <span style="color: #BB0000;">NA</span>       <span style="color: #BB0000;">NA</span>         <span style="color: #BB0000;">NA</span> </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">15</span> energy__              55.9     55.6     3.14   3.03<span style="color: #949494;">e</span>+0  51.6     61.7      1.00  <span style="text-decoration: underline;">1</span>040.      <span style="text-decoration: underline;">1</span>772.</span></span></code></pre>
</div>
<div class="section level2">
<h2 id="extracting-draws-from-a-fit-in-tidy-format-using-spread_rvars">Extracting draws from a fit in tidy-format using
<code>spread_rvars</code><a class="anchor" aria-label="anchor" href="#extracting-draws-from-a-fit-in-tidy-format-using-spread_rvars"></a>
</h2>
<p>Now that we have our results, the fun begins: getting the variables
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 variable 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] "b_Intercept"              "sd_condition__Intercept"  "sigma"                   </span></span>
<span><span class="co">##  [4] "r_condition[A,Intercept]" "r_condition[B,Intercept]" "r_condition[C,Intercept]"</span></span>
<span><span class="co">##  [7] "r_condition[D,Intercept]" "r_condition[E,Intercept]" "lp__"                    </span></span>
<span><span class="co">## [10] "accept_stat__"            "stepsize__"               "treedepth__"             </span></span>
<span><span class="co">## [13] "n_leapfrog__"             "divergent__"              "energy__"</span></span></code></pre>
<p>Here, <code>b_Intercept</code> is the global mean, and the
<code>r_condition[]</code> variables are offsets from that mean for each
condition. Given these variables:</p>
<ul>
<li><code>r_condition[A,Intercept]</code></li>
<li><code>r_condition[B,Intercept]</code></li>
<li><code>r_condition[C,Intercept]</code></li>
<li><code>r_condition[D,Intercept]</code></li>
<li><code>r_condition[E,Intercept]</code></li>
</ul>
<p>We might want a data frame where each row is a random variable
(<code>rvar</code>) representing all draws from
<code>r_condition[A,Intercept]</code>,
<code>r_condition[B,Intercept]</code>, <code>...[C,...]</code>,
<code>...[D,...]</code>, or <code>...[E,...]</code>. 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 simultaneously.</p>
<p>We can do this using the <code><a href="../reference/spread_rvars.html">spread_rvars()</a></code> function. It
includes a simple specification format that we can use to extract
variables and their indices into tidy-format data frames. This function
is analogous to <code><a href="../reference/spread_draws.html">spread_draws()</a></code> from the
tidy-data-frames-of-draws workflow described in
<code><a href="../articles/tidy-brms.html">vignette("tidy-brms")</a></code>.</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 variable in the model like this:</p>
<p><code>r_condition[D,Intercept]</code></p>
<p>We can provide <code><a href="../reference/spread_rvars.html">spread_rvars()</a></code> with a column
specification like this:</p>
<p><code>r_condition[condition,term]</code></p>
<p>Where <code>condition</code> corresponds to <code>D</code> and
<code>term</code> corresponds to <code>Intercept</code>. There is
nothing too magical about what <code><a href="../reference/spread_rvars.html">spread_rvars()</a></code> does with
this specification: under the hood, it splits the variable 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>r_condition[D,Intercept]</code> has indices
<code>D</code> and <code>Intercept</code>, and
<code><a href="../reference/spread_rvars.html">spread_rvars()</a></code> lets us extract these indices as columns in
the resulting tidy data frame of draws from
<code>r_condition</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_rvars.html">spread_rvars</a></span><span class="op">(</span><span class="va">r_condition</span><span class="op">[</span><span class="va">condition</span>,<span class="va">term</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 × 3</span></span></span>
<span><span class="co">##   condition term       r_condition</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;rvar[1d]&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span> A         Intercept  -0.31 <span style="color: #949494;">± 0.5</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span> B         Intercept   0.49 <span style="color: #949494;">± 0.5</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">3</span> C         Intercept   1.33 <span style="color: #949494;">± 0.5</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">4</span> D         Intercept   0.51 <span style="color: #949494;">± 0.5</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">5</span> E         Intercept  -1.39 <span style="color: #949494;">± 0.5</span></span></span></code></pre>
<p>The <code>r_condition</code> column above is a
<code><a href="https://mc-stan.org/posterior/reference/rvar.html" class="external-link">posterior::rvar()</a></code> datatype, which is an array-like datatype
representing draws from a random variable:</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_rvars.html">spread_rvars</a></span><span class="op">(</span><span class="va">r_condition</span><span class="op">[</span><span class="va">condition</span>,<span class="va">term</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/pull.html" class="external-link">pull</a></span><span class="op">(</span><span class="va">r_condition</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## rvar&lt;1000,4&gt;[5] <span style="color: #949494;">mean ± sd:</span></span></span>
<span><span class="co">## [1] -0.31 ± 0.5   0.49 ± 0.5   1.33 ± 0.5   0.51 ± 0.5  -1.39 ± 0.5</span></span></code></pre>
<p>In this case, for each of the 5 elements of this <code>rvar</code>
vector, we have 1000 draws from each of 4 chains in the model. For more
on the <code>rvar</code> datatype, see
<code><a href="https://mc-stan.org/posterior/articles/rvar.html" class="external-link">vignette("rvar", package = "posterior")</a></code>.</p>
<p>We can choose whatever names we want for the index columns; e.g.:</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_rvars.html">spread_rvars</a></span><span class="op">(</span><span class="va">r_condition</span><span class="op">[</span><span class="va">c</span>,<span class="va">t</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 × 3</span></span></span>
<span><span class="co">##   c     t          r_condition</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;rvar[1d]&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span> A     Intercept  -0.31 <span style="color: #949494;">± 0.5</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span> B     Intercept   0.49 <span style="color: #949494;">± 0.5</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">3</span> C     Intercept   1.33 <span style="color: #949494;">± 0.5</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">4</span> D     Intercept   0.51 <span style="color: #949494;">± 0.5</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">5</span> E     Intercept  -1.39 <span style="color: #949494;">± 0.5</span></span></span></code></pre>
<p>But the more descriptive and less cryptic names from the previous
example are probably preferable.</p>
<p>If we leave off the name for an index, it is left “nested” in the
column. For example, we could nest the <code>term</code> since it only
has one value <code>"Intercept"</code> anyway:</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_rvars.html">spread_rvars</a></span><span class="op">(</span><span class="va">r_condition</span><span class="op">[</span><span class="va">condition</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 × 2</span></span></span>
<span><span class="co">##   condition r_condition[,1]</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;rvar[,1]&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span> A             -0.31 <span style="color: #949494;">± 0.5</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span> B              0.49 <span style="color: #949494;">± 0.5</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">3</span> C              1.33 <span style="color: #949494;">± 0.5</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">4</span> D              0.51 <span style="color: #949494;">± 0.5</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">5</span> E             -1.39 <span style="color: #949494;">± 0.5</span></span></span></code></pre>
<p>Or we could nest the <code>condition</code>, though this is probably
not that useful practically:</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_rvars.html">spread_rvars</a></span><span class="op">(</span><span class="va">r_condition</span><span class="op">[</span>,<span class="va">term</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: 1 × 2</span></span></span>
<span><span class="co">##   term      r_condition[,1]        [,2]       [,3]        [,4]        [,5]</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;rvar[,1]&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;rvar[,1]&gt;</span> <span style="color: #949494; font-style: italic;">&lt;rvar[,1]&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;rvar[,1]&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;rvar[,1]&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span> Intercept     -0.31 <span style="color: #949494;">± 0.5</span>  0.49 <span style="color: #949494;">± 0.5</span>  1.3 <span style="color: #949494;">± 0.5</span>  0.51 <span style="color: #949494;">± 0.5</span>  -1.4 <span style="color: #949494;">± 0.5</span></span></span></code></pre>
</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 summary 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 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_rvars.html">spread_rvars</a></span><span class="op">(</span><span class="va">b_Intercept</span>, <span class="va">sigma</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 1 × 2</span></span></span>
<span><span class="co">##   b_Intercept        sigma</span></span>
<span><span class="co">##    <span style="color: #949494; font-style: italic;">&lt;rvar[1d]&gt;</span>   <span style="color: #949494; font-style: italic;">&lt;rvar[1d]&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span>  0.5 <span style="color: #949494;">± 0.48</span>  0.56 <span style="color: #949494;">± 0.06</span></span></span></code></pre>
<p>Like with <code>r_condition[condition,term]</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_rvars.html">spread_rvars</a></span><span class="op">(</span><span class="va">b_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">b_Intercept</span>, <span class="va">sigma</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 1 × 9</span></span></span>
<span><span class="co">##   b_Intercept b_Intercept.lower b_Intercept.upper sigma sigma.lower sigma.upper .width .point</span></span>
<span><span class="co">##         <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;chr&gt;</span> </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span>       0.526            -<span style="color: #BB0000;">0.490</span>              1.41 0.557       0.456       0.696   0.95 median</span></span>
<span><span class="co">## <span style="color: #949494;"># ℹ 1 more variable: .interval &lt;chr&gt;</span></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. Thus in the above
example, <code>b_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_rvars.html">spread_rvars</a></span><span class="op">(</span><span class="va">b_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: 1 × 9</span></span></span>
<span><span class="co">##   b_Intercept b_Intercept.lower b_Intercept.upper sigma sigma.lower sigma.upper .width .point</span></span>
<span><span class="co">##         <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;chr&gt;</span> </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span>       0.526            -<span style="color: #BB0000;">0.490</span>              1.41 0.557       0.456       0.696   0.95 median</span></span>
<span><span class="co">## <span style="color: #949494;"># ℹ 1 more variable: .interval &lt;chr&gt;</span></span></span></code></pre>
<p>If you would rather have a long-format list, use
<code><a href="../reference/spread_rvars.html">gather_rvars()</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_rvars.html">gather_rvars</a></span><span class="op">(</span><span class="va">b_Intercept</span>, <span class="va">sigma</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 2 × 2</span></span></span>
<span><span class="co">##   .variable         .value</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;rvar[1d]&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span> b_Intercept  0.50 <span style="color: #949494;">± 0.48</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span> sigma        0.56 <span style="color: #949494;">± 0.06</span></span></span></code></pre>
<p>We could also use <code><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi()</a></code> here:</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_rvars.html">gather_rvars</a></span><span class="op">(</span><span class="va">b_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">.value</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> b_Intercept  0.526 -<span style="color: #BB0000;">0.490</span>  1.41    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span> sigma        0.557  0.456  0.696   0.95 median qi</span></span></code></pre>
</div>
<div class="section level3">
<h3 id="with-indexed-model-variables">With indexed model variables<a class="anchor" aria-label="anchor" href="#with-indexed-model-variables"></a>
</h3>
<p>When we have a model variable with one or more indices, such as
<code>r_condition</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="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_rvars.html">spread_rvars</a></span><span class="op">(</span><span class="va">r_condition</span><span class="op">[</span><span class="va">condition</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="va">r_condition</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">##   condition r_condition .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> A              -<span style="color: #BB0000;">0.336</span> -<span style="color: #BB0000;">1.23</span>   0.734   0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span> B               0.480 -<span style="color: #BB0000;">0.441</span>  1.54    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">3</span> C               1.32   0.406  2.38    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">4</span> D               0.498 -<span style="color: #BB0000;">0.443</span>  1.55    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">5</span> E              -<span style="color: #BB0000;">1.40</span>  -<span style="color: #BB0000;">2.36</span>  -<span style="color: #BB0000;">0.396</span>   0.95 median qi</span></span></code></pre>
<p><strong>Note for existing users of
<code><a href="../reference/spread_draws.html">spread_draws()</a></code></strong>: you may notice that
<code><a href="../reference/spread_rvars.html">spread_rvars()</a></code> requires us to be a bit more explicit in
passing column names to <code><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi()</a></code>. This is because
<code><a href="../reference/spread_rvars.html">spread_rvars()</a></code> does not return pre-grouped data frames,
unlike <code><a href="../reference/spread_draws.html">spread_draws()</a></code> — since every row would always be its
own group in the output from <code><a href="../reference/spread_rvars.html">spread_rvars()</a></code>, returning a
pre-grouped data frame would be redundant.</p>
<p>You can also use <code><a href="https://mc-stan.org/posterior/reference/draws_summary.html" class="external-link">posterior::summarise_draws()</a></code> on an
<code>rvar</code> column to generate summaries with convergence
diagnostics. That function returns a data frame, which can be passed
directly into the <code><a href="https://dplyr.tidyverse.org/reference/mutate.html" class="external-link">dplyr::mutate()</a></code> function:</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_rvars.html">spread_rvars</a></span><span class="op">(</span><span class="va">r_condition</span><span class="op">[</span><span class="va">condition</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><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="va">r_condition</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 × 12</span></span></span>
<span><span class="co">##   condition r_condition[,1] 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;rvar[,1]&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> A             -0.31 <span style="color: #949494;">± 0.5</span> r_condi… -<span style="color: #BB0000;">0.312</span> -<span style="color: #BB0000;">0.336</span> 0.498 0.480 -<span style="color: #BB0000;">1.08</span>   0.526  1.00     972.    <span style="text-decoration: underline;">1</span>267.</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span> B              0.49 <span style="color: #949494;">± 0.5</span> r_condi…  0.493  0.480 0.500 0.474 -<span style="color: #BB0000;">0.301</span>  1.34   1.00     983.    <span style="text-decoration: underline;">1</span>326.</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">3</span> C              1.33 <span style="color: #949494;">± 0.5</span> r_condi…  1.33   1.32  0.501 0.478  0.557  2.19   1.00     994.    <span style="text-decoration: underline;">1</span>489.</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">4</span> D              0.51 <span style="color: #949494;">± 0.5</span> r_condi…  0.512  0.498 0.502 0.477 -<span style="color: #BB0000;">0.277</span>  1.36   1.00    <span style="text-decoration: underline;">1</span>013.    <span style="text-decoration: underline;">1</span>396.</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">5</span> E             -1.39 <span style="color: #949494;">± 0.5</span> r_condi… -<span style="color: #BB0000;">1.39</span>  -<span style="color: #BB0000;">1.40</span>  0.500 0.468 -<span style="color: #BB0000;">2.17</span>  -<span style="color: #BB0000;">0.535</span>  1.00     990.    <span style="text-decoration: underline;">1</span>392.</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_rvars.html">spread_rvars()</a></code> and <code><a href="../reference/spread_rvars.html">gather_rvars()</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 for every
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>b_Intercept</code>) plus the effect for a given condition
(<code>r_condition</code>).</p>
<p>We can gather draws from <code>b_Intercept</code> and
<code>r_condition</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_rvars.html">spread_rvars</a></span><span class="op">(</span><span class="va">b_Intercept</span>, <span class="va">r_condition</span><span class="op">[</span><span class="va">condition</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 × 3</span></span></span>
<span><span class="co">##   b_Intercept condition r_condition[,1]</span></span>
<span><span class="co">##    <span style="color: #949494; font-style: italic;">&lt;rvar[1d]&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>          <span style="color: #949494; font-style: italic;">&lt;rvar[,1]&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span>  0.5 <span style="color: #949494;">± 0.48</span> A             -0.31 <span style="color: #949494;">± 0.5</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span>  0.5 <span style="color: #949494;">± 0.48</span> B              0.49 <span style="color: #949494;">± 0.5</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">3</span>  0.5 <span style="color: #949494;">± 0.48</span> C              1.33 <span style="color: #949494;">± 0.5</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">4</span>  0.5 <span style="color: #949494;">± 0.48</span> D              0.51 <span style="color: #949494;">± 0.5</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">5</span>  0.5 <span style="color: #949494;">± 0.48</span> E             -1.39 <span style="color: #949494;">± 0.5</span></span></span></code></pre>
<p>Within each draw, <code>b_Intercept</code> is repeated as necessary
to correspond to every index of <code>r_condition</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_rvars.html">spread_rvars</a></span><span class="op">(</span><span class="va">`b_Intercept`</span>, <span class="va">r_condition</span><span class="op">[</span><span class="va">condition</span>,<span class="va">Intercept</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">b_Intercept</span> <span class="op">+</span> <span class="va">r_condition</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 5 × 5</span></span></span>
<span><span class="co">##   b_Intercept condition Intercept  r_condition condition_mean</span></span>
<span><span class="co">##    <span style="color: #949494; font-style: italic;">&lt;rvar[1d]&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 style="color: #949494; font-style: italic;">&lt;rvar[1d]&gt;</span>     <span style="color: #949494; font-style: italic;">&lt;rvar[1d]&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span>  0.5 <span style="color: #949494;">± 0.48</span> A         Intercept  -0.31 <span style="color: #949494;">± 0.5</span>    0.19 <span style="color: #949494;">± 0.18</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span>  0.5 <span style="color: #949494;">± 0.48</span> B         Intercept   0.49 <span style="color: #949494;">± 0.5</span>    1.00 <span style="color: #949494;">± 0.18</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">3</span>  0.5 <span style="color: #949494;">± 0.48</span> C         Intercept   1.33 <span style="color: #949494;">± 0.5</span>    1.84 <span style="color: #949494;">± 0.17</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">4</span>  0.5 <span style="color: #949494;">± 0.48</span> D         Intercept   0.51 <span style="color: #949494;">± 0.5</span>    1.02 <span style="color: #949494;">± 0.18</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">5</span>  0.5 <span style="color: #949494;">± 0.48</span> E         Intercept  -1.39 <span style="color: #949494;">± 0.5</span>   -0.88 <span style="color: #949494;">± 0.18</span></span></span></code></pre>
</div>
<div class="section level2">
<h2 id="plotting-point-summaries-and-intervals">Plotting point summaries and intervals<a class="anchor" aria-label="anchor" href="#plotting-point-summaries-and-intervals"></a>
</h2>
<p>Plotting point summaries and intervals is straightforward using
<code><a href="http://mjskay.github.io/ggdist/reference/stat_pointinterval.html" class="external-link">ggdist::stat_pointinterval()</a></code>, which will produce
visualizations with 66% and 95% intervals by default (this can be
changed using the <code>.width</code> parameter, the default is
<code>.width = c(.66, .95)</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_rvars.html">spread_rvars</a></span><span class="op">(</span><span class="va">b_Intercept</span>, <span class="va">r_condition</span><span class="op">[</span><span class="va">condition</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">b_Intercept</span> <span class="op">+</span> <span class="va">r_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://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>, xdist <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_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-posterior_files/figure-html/unnamed-chunk-21-1.png" width="648"></p>
<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 also produce an
arbitrary number of probability intervals by setting the
<code>.width =</code> argument:</p>
<div class="sourceCode" id="cb41"><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_rvars.html">spread_rvars</a></span><span class="op">(</span><span class="va">b_Intercept</span>, <span class="va">r_condition</span><span class="op">[</span><span class="va">condition</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">b_Intercept</span> <span class="op">+</span> <span class="va">r_condition</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 × 9</span></span></span>
<span><span class="co">##    b_Intercept condition r_condition[,1] condition_mean  .lower .upper .width .point .interval</span></span>
<span><span class="co">##     <span style="color: #949494; font-style: italic;">&lt;rvar[1d]&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>          <span style="color: #949494; font-style: italic;">&lt;rvar[,1]&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>  0.5 <span style="color: #949494;">± 0.48</span> A             -0.31 <span style="color: #949494;">± 0.5</span>          0.190 -<span style="color: #BB0000;">0.146</span>   0.536   0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 2</span>  0.5 <span style="color: #949494;">± 0.48</span> B              0.49 <span style="color: #949494;">± 0.5</span>          0.998  0.640   1.33    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 3</span>  0.5 <span style="color: #949494;">± 0.48</span> C              1.33 <span style="color: #949494;">± 0.5</span>          1.84   1.49    2.18    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 4</span>  0.5 <span style="color: #949494;">± 0.48</span> D              0.51 <span style="color: #949494;">± 0.5</span>          1.02   0.670   1.36    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 5</span>  0.5 <span style="color: #949494;">± 0.48</span> E             -1.39 <span style="color: #949494;">± 0.5</span>         -<span style="color: #BB0000;">0.884</span> -<span style="color: #BB0000;">1.24</span>   -<span style="color: #BB0000;">0.528</span>   0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 6</span>  0.5 <span style="color: #949494;">± 0.48</span> A             -0.31 <span style="color: #949494;">± 0.5</span>          0.190 -<span style="color: #BB0000;">0.034</span><span style="color: #BB0000; text-decoration: underline;">0</span>  0.421   0.8  median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 7</span>  0.5 <span style="color: #949494;">± 0.48</span> B              0.49 <span style="color: #949494;">± 0.5</span>          0.998  0.766   1.22    0.8  median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 8</span>  0.5 <span style="color: #949494;">± 0.48</span> C              1.33 <span style="color: #949494;">± 0.5</span>          1.84   1.61    2.06    0.8  median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 9</span>  0.5 <span style="color: #949494;">± 0.48</span> D              0.51 <span style="color: #949494;">± 0.5</span>          1.02   0.786   1.24    0.8  median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">10</span>  0.5 <span style="color: #949494;">± 0.48</span> E             -1.39 <span style="color: #949494;">± 0.5</span>         -<span style="color: #BB0000;">0.884</span> -<span style="color: #BB0000;">1.11</span>   -<span style="color: #BB0000;">0.656</span>   0.8  median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">11</span>  0.5 <span style="color: #949494;">± 0.48</span> A             -0.31 <span style="color: #949494;">± 0.5</span>          0.190  0.077<span style="text-decoration: underline;">8</span>  0.307   0.5  median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">12</span>  0.5 <span style="color: #949494;">± 0.48</span> B              0.49 <span style="color: #949494;">± 0.5</span>          0.998  0.881   1.11    0.5  median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">13</span>  0.5 <span style="color: #949494;">± 0.48</span> C              1.33 <span style="color: #949494;">± 0.5</span>          1.84   1.72    1.95    0.5  median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">14</span>  0.5 <span style="color: #949494;">± 0.48</span> D              0.51 <span style="color: #949494;">± 0.5</span>          1.02   0.898   1.13    0.5  median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">15</span>  0.5 <span style="color: #949494;">± 0.48</span> E             -1.39 <span style="color: #949494;">± 0.5</span>         -<span style="color: #BB0000;">0.884</span> -<span style="color: #BB0000;">1.01</span>   -<span style="color: #BB0000;">0.767</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, and is
essentially what <code><a href="http://mjskay.github.io/ggdist/reference/stat_pointinterval.html" class="external-link">ggdist::stat_pointinterval()</a></code> is doing for
you under the hood above. For example, assigning <code>-.width</code> to
the <code>linewidth</code> aesthetic will show all intervals, making
thicker lines correspond to smaller intervals.</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="cb43"><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_rvars.html">spread_rvars</a></span><span class="op">(</span><span class="va">b_Intercept</span>, <span class="va">r_condition</span><span class="op">[</span><span class="va">condition</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">b_Intercept</span> <span class="op">+</span> <span class="va">r_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://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>, xdist <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-posterior_files/figure-html/unnamed-chunk-23-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="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_rvars.html">spread_rvars</a></span><span class="op">(</span><span class="va">b_Intercept</span>, <span class="va">r_condition</span><span class="op">[</span><span class="va">condition</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">b_Intercept</span> <span class="op">+</span> <span class="va">r_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://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>, xdist <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-posterior_files/figure-html/unnamed-chunk-24-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/stat_slabinterval.html" class="external-link">ggdist::stat_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. All geoms that start with <code>stat_...</code> support the
use of <code>rvar</code> columns in the <code>xdist</code> and
<code>ydist</code> aesthetics.</p>
</div>
<div class="section level2">
<h2 id="posterior-means">Posterior means<a class="anchor" aria-label="anchor" href="#posterior-means"></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">brms::posterior_epred()</a></code>, giving posterior draws from
posterior distributions of the mean of the response (i.e. the
distribution of the expected value of the posterior predictive). 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 fits we want, then populate that grid with
<code>rvar</code>s representing draws from the posterior:</p>
<div class="sourceCode" id="cb45"><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_rvars.html">add_epred_rvars</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">## <span style="color: #949494;"># A tibble: 5 × 2</span></span></span>
<span><span class="co">##   condition        .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;rvar[1d]&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span> A           0.19 <span style="color: #949494;">± 0.18</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span> B           1.00 <span style="color: #949494;">± 0.18</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">3</span> C           1.84 <span style="color: #949494;">± 0.17</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">4</span> D           1.02 <span style="color: #949494;">± 0.18</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">5</span> E          -0.88 <span style="color: #949494;">± 0.18</span></span></span></code></pre>
</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_dots.html" class="external-link">ggdist::stat_dots()</a></code> and
<code><a href="http://mjskay.github.io/ggdist/reference/stat_dotsinterval.html" class="external-link">ggdist::stat_dotsinterval()</a></code> are the variants designed for
use on <code>rvar</code>s:</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_rvars.html">add_epred_rvars</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>xdist <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-posterior_files/figure-html/unnamed-chunk-26-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_rvars.html">add_epred_rvars()</a></code> is analogous to
<code><a href="https://mc-stan.org/rstantools/reference/posterior_epred.html" class="external-link">brms::posterior_epred()</a></code>, <code><a href="../reference/add_predicted_rvars.html">add_predicted_rvars()</a></code>
is analogous to <code><a href="https://mc-stan.org/rstantools/reference/posterior_predict.html" class="external-link">brms::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">ggdist::stat_interval()</a></code> to plot predictive
bands alongside the data:</p>
<div class="sourceCode" id="cb48"><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_rvars.html">add_predicted_rvars</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>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_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>xdist <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">.50</span>, <span class="fl">.80</span>, <span class="fl">.95</span>, <span class="fl">.99</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="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">response</span><span class="op">)</span>, data <span class="op">=</span> <span class="va">ABC</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-posterior_files/figure-html/unnamed-chunk-27-1.png" width="648"></p>
<p>The <code>add_XXX_rvars()</code> functions can be chained together to
add posterior predictions (<code>predicted_rvars</code>) and the
distribution of the mean of the posterior predictive
(<code>epred_rvars</code>) to the same data frame. This makes it easy to
plot both together alongside the data:</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_rvars.html">add_epred_rvars</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="../reference/add_predicted_rvars.html">add_predicted_rvars</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>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_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>xdist <span class="op">=</span> <span class="va">.prediction</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><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>xdist <span class="op">=</span> <span class="va">.epred</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="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">response</span><span class="op">)</span>, data <span class="op">=</span> <span class="va">ABC</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-posterior_files/figure-html/unnamed-chunk-28-1.png" width="648"></p>
</div>
<div class="section level2">
<h2 id="posterior-predictions-kruschke-style">Posterior predictions, Kruschke-style<a class="anchor" aria-label="anchor" href="#posterior-predictions-kruschke-style"></a>
</h2>
<p>The above approach to posterior predictions integrates over the
parameter uncertainty to give a single posterior predictive
distribution. Another approach, often used by John Kruschke in his book
<a href="https://sites.google.com/site/doingbayesiandataanalysis/" class="external-link">Doing
Bayesian Data Analysis</a>, is to attempt to show both the predictive
uncertainty and the parameter uncertainty simultaneously by showing
several possible predictive distributions implied by the posterior.</p>
<p>We can do this pretty easily by asking for the distributional
parameters for a given prediction implied by the posterior. We’ll do it
explicitly here by setting <code>dpar = c("mu", "sigma")</code> in
<code><a href="../reference/add_predicted_draws.html">add_epred_draws()</a></code>. Rather than specifying the parameters
explicitly, you can also just set <code>dpar = TRUE</code> to get draws
from all distributional parameters in a model, and this will work for
any response distribution supported by <code><a href="https://paul-buerkner.github.io/brms/reference/brm.html" class="external-link">brms::brm()</a></code>:</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_rvars.html">add_epred_rvars</a></span><span class="op">(</span><span class="va">m</span>, dpar <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">"mu"</span>, <span class="st">"sigma"</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 × 4</span></span></span>
<span><span class="co">##   condition        .epred            mu        sigma</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;rvar[1d]&gt;</span>    <span style="color: #949494; font-style: italic;">&lt;rvar[1d]&gt;</span>   <span style="color: #949494; font-style: italic;">&lt;rvar[1d]&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span> A           0.19 <span style="color: #949494;">± 0.18</span>   0.19 <span style="color: #949494;">± 0.18</span>  0.56 <span style="color: #949494;">± 0.06</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span> B           1.00 <span style="color: #949494;">± 0.18</span>   1.00 <span style="color: #949494;">± 0.18</span>  0.56 <span style="color: #949494;">± 0.06</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">3</span> C           1.84 <span style="color: #949494;">± 0.17</span>   1.84 <span style="color: #949494;">± 0.17</span>  0.56 <span style="color: #949494;">± 0.06</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">4</span> D           1.02 <span style="color: #949494;">± 0.18</span>   1.02 <span style="color: #949494;">± 0.18</span>  0.56 <span style="color: #949494;">± 0.06</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">5</span> E          -0.88 <span style="color: #949494;">± 0.18</span>  -0.88 <span style="color: #949494;">± 0.18</span>  0.56 <span style="color: #949494;">± 0.06</span></span></span></code></pre>
<p>At this point, we will need to use the “long-data-frame-of-draws”
format more typical of the standard tidybayes workflow. What we want to
do is select a small number of draws from the joint distribution of
<code>mu</code> and <code>sigma</code> to plot predictive densities
from. We will use <code><a href="../reference/nest_rvars.html">unnest_rvars()</a></code> to “unnest” all the
<code>rvar</code>s in the above output into a long-format data frame,
sample 30 of the draws using <code><a href="../reference/sample_draws.html">sample_draws()</a></code>, and then use
<code><a href="http://mjskay.github.io/ggdist/reference/stat_slab.html" class="external-link">ggdist::stat_slab()</a></code> to visualize each predictive
distribution implied by the values of <code>mu</code> and
<code>sigma</code>:</p>
<div class="sourceCode" id="cb52"><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_rvars.html">add_epred_rvars</a></span><span class="op">(</span><span class="va">m</span>, dpar <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">"mu"</span>, <span class="st">"sigma"</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/nest_rvars.html">unnest_rvars</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="../reference/sample_draws.html">sample_draws</a></span><span class="op">(</span><span class="fl">30</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><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_slab.html" class="external-link">stat_slab</a></span><span class="op">(</span></span>
<span>    <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>xdist <span class="op">=</span> <span class="fu"><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_normal.html" class="external-link">dist_normal</a></span><span class="op">(</span><span class="va">mu</span>, <span class="va">sigma</span><span class="op">)</span><span class="op">)</span>, </span>
<span>    color <span class="op">=</span> <span class="st">"gray65"</span>, alpha <span class="op">=</span> <span class="fl">1</span><span class="op">/</span><span class="fl">10</span>, fill <span class="op">=</span> <span class="cn">NA</span></span>
<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="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">response</span><span class="op">)</span>, data <span class="op">=</span> <span class="va">ABC</span>, shape <span class="op">=</span> <span class="fl">21</span>, fill <span class="op">=</span> <span class="st">"#9ECAE1"</span>, size <span class="op">=</span> <span class="fl">2</span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-posterior_files/figure-html/unnamed-chunk-30-1.png" width="648"></p>
<p>The use of <code><a href="../reference/nest_rvars.html">unnest_rvars()</a></code> after
<code><a href="../reference/add_predicted_rvars.html">add_epred_rvars()</a></code> is essentially equivalent to just using
the <code>_draws()</code> instead of <code>_rvars()</code> form of the
prediction functions (e.g. <code><a href="../reference/add_predicted_draws.html">add_epred_draws()</a></code>), which may be
faster and/or more convenient depending on what other data manipulation
you need to do.</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="cb53"><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://paul-buerkner.github.io/brms/reference/brm.html" class="external-link">brm</a></span><span class="op">(</span></span>
<span>  <span class="va">mpg</span> <span class="op">~</span> <span class="va">hp</span> <span class="op">*</span> <span class="va">cyl</span>, </span>
<span>  data <span class="op">=</span> <span class="va">mtcars</span>, </span>
<span>  </span>
<span>  file <span class="op">=</span> <span class="st">"models/tidy-brms_m_mpg.rds"</span>  <span class="co"># cache model (can be removed)</span></span>
<span><span class="op">)</span></span></code></pre></div>
<p>We can draw fit curves (i.e., curves showing the uncertainty in the
conditional expectation, aka the expectation of the posterior
predictive) with probability bands:</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">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_rvars.html">add_epred_rvars</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>, 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>ydist <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><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">mpg</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-posterior_files/figure-html/unnamed-chunk-31-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_rvars.html">add_predicted_rvars</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>, 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>ydist <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><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">mpg</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-posterior_files/figure-html/unnamed-chunk-32-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 class="section level3">
<h3 id="extracting-distributional-regression-parameters">Extracting distributional regression parameters<a class="anchor" aria-label="anchor" href="#extracting-distributional-regression-parameters"></a>
</h3>
<p><code><a href="https://paul-buerkner.github.io/brms/reference/brm.html" class="external-link">brms::brm()</a></code> also allows us to set up submodels for
parameters of the response distribution <em>other than</em> the location
(e.g., mean). For example, we can allow a variance parameter, such as
the standard deviation, to also be some function of the predictors.</p>
<p>This approach can be helpful in cases of non-constant variance (also
called <em>heteroskedasticity</em> by folks who like obfuscation via
Latin). E.g., imagine two groups, each with different mean response
<em>and variance</em>:</p>
<div class="sourceCode" id="cb56"><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">1234</span><span class="op">)</span></span>
<span><span class="va">AB</span> <span class="op">=</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>  group <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="op">)</span>, each <span class="op">=</span> <span class="fl">20</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="fl">40</span>, mean <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="fl">1</span>, <span class="fl">5</span><span class="op">)</span>, each <span class="op">=</span> <span class="fl">20</span><span class="op">)</span>, sd <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="fl">1</span>, <span class="fl">3</span><span class="op">)</span>, each <span class="op">=</span> <span class="fl">20</span><span class="op">)</span><span class="op">)</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="va">AB</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">response</span>, y <span class="op">=</span> <span class="va">group</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-posterior_files/figure-html/unnamed-chunk-33-1.png" width="648"></p>
<p>Here is a model that lets the mean <em>and standard deviation</em> of
<code>response</code> be dependent on <code>group</code>:</p>
<div class="sourceCode" id="cb57"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m_ab</span> <span class="op">=</span> <span class="fu"><a href="https://paul-buerkner.github.io/brms/reference/brm.html" class="external-link">brm</a></span><span class="op">(</span></span>
<span>  <span class="fu"><a href="https://paul-buerkner.github.io/brms/reference/brmsformula.html" class="external-link">bf</a></span><span class="op">(</span></span>
<span>    <span class="va">response</span> <span class="op">~</span> <span class="va">group</span>,</span>
<span>    <span class="va">sigma</span> <span class="op">~</span> <span class="va">group</span></span>
<span>  <span class="op">)</span>,</span>
<span>  data <span class="op">=</span> <span class="va">AB</span>,</span>
<span>  </span>
<span>  file <span class="op">=</span> <span class="st">"models/tidy-brms_m_ab.rds"</span>  <span class="co"># cache model (can be removed)</span></span>
<span><span class="op">)</span></span></code></pre></div>
<p>We can plot the posterior distribution of the mean
<code>response</code> alongside posterior predictive intervals and the
data:</p>
<div class="sourceCode" id="cb58"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">AB</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">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>
<span>  <span class="fu"><a href="../reference/add_predicted_rvars.html">add_epred_rvars</a></span><span class="op">(</span><span class="va">m_ab</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_rvars.html">add_predicted_rvars</a></span><span class="op">(</span><span class="va">m_ab</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><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="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>xdist <span class="op">=</span> <span class="va">.epred</span><span class="op">)</span>, scale <span class="op">=</span> <span class="fl">0.6</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="fl">0.175</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>xdist <span class="op">=</span> <span class="va">.prediction</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="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">response</span><span class="op">)</span>, data <span class="op">=</span> <span class="va">AB</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-posterior_files/figure-html/unnamed-chunk-34-1.png" width="648"></p>
<p>This shows posteriors of the mean of each group (black intervals and
the density plots) and posterior predictive intervals (blue).</p>
<p>The predictive intervals in group <code>b</code> are larger than in
group <code>a</code> because the model fits a different standard
deviation for each group. We can see how the corresponding
distributional parameter, <code>sigma</code>, changes by extracting it
using the <code>dpar</code> argument to
<code><a href="../reference/add_predicted_rvars.html">add_epred_rvars()</a></code>:</p>
<div class="sourceCode" id="cb59"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">AB</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">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>
<span>  <span class="fu"><a href="../reference/add_predicted_rvars.html">add_epred_rvars</a></span><span class="op">(</span><span class="va">m_ab</span>, dpar <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://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>xdist <span class="op">=</span> <span class="va">sigma</span>, y <span class="op">=</span> <span class="va">group</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-posterior_files/figure-html/unnamed-chunk-35-1.png" width="648"></p>
<p>By setting <code>dpar = TRUE</code>, all distributional parameters
are added as additional columns in the result of
<code><a href="../reference/add_predicted_rvars.html">add_epred_rvars()</a></code>; if you only want a specific parameter,
you can specify it (or a list of just the parameters you want). In the
above model, <code>dpar = TRUE</code> is equivalent to
<code>dpar = list("mu", "sigma")</code>.</p>
</div>
</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="cb60"><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_rvars.html">spread_rvars</a></span><span class="op">(</span><span class="va">r_condition</span><span class="op">[</span><span class="va">condition</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/compare_levels.html">compare_levels</a></span><span class="op">(</span><span class="va">r_condition</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">r_condition</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>, xdist <span class="op">=</span> <span class="va">r_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_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-posterior_files/figure-html/unnamed-chunk-36-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>
<p>The <code><a href="https://mc-stan.org/rstantools/reference/posterior_epred.html" class="external-link">brms::posterior_epred()</a></code> function for ordinal and
multinomial regression models in brms returns multidimensional variables
for each draw, where an additional dimension to the result contains
outcome categories. 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 and multinomial <code>brms</code>
models, <code><a href="../reference/add_predicted_draws.html">add_epred_draws()</a></code> outputs a nested
<code>.epred</code> variable that has additional columns for each level
of the response variable.</p>
<div class="section level3">
<h3 id="ordinal-model-with-continuous-predictor">Ordinal model with continuous predictor<a class="anchor" aria-label="anchor" href="#ordinal-model-with-continuous-predictor"></a>
</h3>
<p>We’ll fit a model using the <code>mtcars</code> dataset that predicts
the number of cylinders in a car given the car’s mileage (in miles per
gallon). While this is a little backwards causality-wise (presumably the
number of cylinders causes the mileage, if anything), that does not mean
this is not a fine prediction task (I could probably tell someone who
knows something about cars the MPG of a car and they could do reasonably
well at guessing the number of cylinders in the engine).</p>
<p>Before we fit the model, let’s clean the dataset by making the
<code>cyl</code> column an ordered factor (by default it is just a
number):</p>
<div class="sourceCode" id="cb61"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">mtcars_clean</span> <span class="op">=</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/mutate.html" class="external-link">mutate</a></span><span class="op">(</span>cyl <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>
<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">mtcars_clean</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb</span></span>
<span><span class="co">## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4</span></span>
<span><span class="co">## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4</span></span>
<span><span class="co">## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1</span></span>
<span><span class="co">## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1</span></span>
<span><span class="co">## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2</span></span>
<span><span class="co">## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1</span></span></code></pre>
<p>Then we’ll fit an ordinal regression model:</p>
<div class="sourceCode" id="cb63"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m_cyl</span> <span class="op">=</span> <span class="fu"><a href="https://paul-buerkner.github.io/brms/reference/brm.html" class="external-link">brm</a></span><span class="op">(</span></span>
<span>  <span class="va">cyl</span> <span class="op">~</span> <span class="va">mpg</span>, </span>
<span>  data <span class="op">=</span> <span class="va">mtcars_clean</span>, </span>
<span>  family <span class="op">=</span> <span class="va">cumulative</span>,</span>
<span>  seed <span class="op">=</span> <span class="fl">58393</span>,</span>
<span>  </span>
<span>  file <span class="op">=</span> <span class="st">"models/tidy-brms_m_cyl.rds"</span>  <span class="co"># cache model (can be removed)</span></span>
<span><span class="op">)</span></span></code></pre></div>
<p><code><a href="../reference/add_predicted_rvars.html">add_epred_rvars()</a></code> will now return a matrix instead of a
vector for the <code>.epred</code> column, where the nested columns of
<code>.epred</code> are the probability that the response is in that
category. For example, here is the fit for two values of
<code>mpg</code> in the dataset:</p>
<div class="sourceCode" id="cb64"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://tibble.tidyverse.org/reference/tibble.html" class="external-link">tibble</a></span><span class="op">(</span>mpg <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">21</span>,<span class="fl">22</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_rvars.html">add_epred_rvars</a></span><span class="op">(</span><span class="va">m_cyl</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 2 × 2</span></span></span>
<span><span class="co">##     mpg .epred[,"4"]       [,"6"]         [,"8"]</span></span>
<span><span class="co">##   <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>   <span style="color: #949494; font-style: italic;">&lt;rvar[,1]&gt;</span>   <span style="color: #949494; font-style: italic;">&lt;rvar[,1]&gt;</span>     <span style="color: #949494; font-style: italic;">&lt;rvar[,1]&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span>    21  0.36 <span style="color: #949494;">± 0.16</span>  0.61 <span style="color: #949494;">± 0.16</span>  0.027 <span style="color: #949494;">± 0.035</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span>    22  0.72 <span style="color: #949494;">± 0.16</span>  0.27 <span style="color: #949494;">± 0.16</span>  0.008 <span style="color: #949494;">± 0.015</span></span></span></code></pre>
<p>This format can be useful in some cases, but for our immediate
purposes it would be better to have the prediction for each category be
on a separate row. We can use the <code>columns_to</code> parameter of
<code><a href="../reference/add_predicted_rvars.html">add_epred_rvars()</a></code> to move the nested column headers into
values of a column (here <code>"cyl"</code>). This will also add a
<code>.row</code> column indexing which row of the input data frame each
prediction came from:</p>
<div class="sourceCode" id="cb66"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://tibble.tidyverse.org/reference/tibble.html" class="external-link">tibble</a></span><span class="op">(</span>mpg <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">21</span>,<span class="fl">22</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_rvars.html">add_epred_rvars</a></span><span class="op">(</span><span class="va">m_cyl</span>, columns_to <span class="op">=</span> <span class="st">"cyl"</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 6 × 4</span></span></span>
<span><span class="co">##     mpg  .row cyl           .epred</span></span>
<span><span class="co">##   <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;chr&gt;</span>     <span style="color: #949494; font-style: italic;">&lt;rvar[1d]&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span>    21     1 4      0.360 <span style="color: #949494;">± 0.162</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span>    22     2 4      0.722 <span style="color: #949494;">± 0.164</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">3</span>    21     1 6      0.613 <span style="color: #949494;">± 0.164</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">4</span>    22     2 6      0.270 <span style="color: #949494;">± 0.159</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">5</span>    21     1 8      0.027 <span style="color: #949494;">± 0.035</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">6</span>    22     2 8      0.008 <span style="color: #949494;">± 0.015</span></span></span></code></pre>
<p>Note: for the <code>cyl</code> variable to retain its original factor
level names you must be using <code>brms</code> greater than or equal to
version 2.15.9.</p>
<p>We could plot fit lines for fitted probabilities against the
dataset:</p>
<div class="sourceCode" id="cb68"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">data_plot</span> <span class="op">=</span> <span class="va">mtcars_clean</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">mpg</span>, y <span class="op">=</span> <span class="va">cyl</span>, color <span class="op">=</span> <span class="va">cyl</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>palette <span class="op">=</span> <span class="st">"Dark2"</span>, name <span class="op">=</span> <span class="st">"cyl"</span><span class="op">)</span></span>
<span></span>
<span><span class="va">fit_plot</span> <span class="op">=</span> <span class="va">mtcars_clean</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>mpg <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">mpg</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_rvars.html">add_epred_rvars</a></span><span class="op">(</span><span class="va">m_cyl</span>, value <span class="op">=</span> <span class="st">"P(cyl | mpg)"</span>, columns_to <span class="op">=</span> <span class="st">"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://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">mpg</span>, color <span class="op">=</span> <span class="va">cyl</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>ydist <span class="op">=</span> <span class="va">`P(cyl | mpg)`</span>, fill <span class="op">=</span> <span class="va">cyl</span><span class="op">)</span>, alpha <span class="op">=</span> <span class="fl">1</span><span class="op">/</span><span class="fl">5</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 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">"Dark2"</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">labs</a></span><span class="op">(</span>y <span class="op">=</span> <span class="st">"P(cyl | mpg)"</span><span class="op">)</span></span>
<span></span>
<span><span class="fu"><a href="https://wilkelab.org/cowplot/reference/plot_grid.html" class="external-link">plot_grid</a></span><span class="op">(</span>ncol <span class="op">=</span> <span class="fl">1</span>, align <span class="op">=</span> <span class="st">"v"</span>,</span>
<span>  <span class="va">data_plot</span>,</span>
<span>  <span class="va">fit_plot</span></span>
<span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-posterior_files/figure-html/unnamed-chunk-40-1.png" width="648"></p>
<p>While talking about the mean for an ordinal distribution often does
not make sense, in this particular case one could argue that the
expected number of cylinders for a car given its miles per gallon is a
meaningful quantity. We could plot the posterior distribution for the
average number of cylinders for a car given a particular miles per
gallon as follows:</p>
<p><math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext mathvariant="normal">E</mtext><mrow><mo stretchy="true" form="prefix">[</mo><mtext mathvariant="normal">cyl</mtext><mo stretchy="false" form="prefix">|</mo><mtext mathvariant="normal">mpg</mtext><mo>=</mo><mi>m</mi><mo stretchy="true" form="postfix">]</mo></mrow><mo>=</mo><munder><mo>∑</mo><mrow><mi>c</mi><mo>∈</mo><mo stretchy="false" form="prefix">{</mo><mn>4</mn><mo>,</mo><mn>6</mn><mo>,</mo><mn>8</mn><mo stretchy="false" form="postfix">}</mo></mrow></munder><mi>c</mi><mo>⋅</mo><mtext mathvariant="normal">P</mtext><mrow><mo stretchy="true" form="prefix">(</mo><mtext mathvariant="normal">cyl</mtext><mo>=</mo><mi>c</mi><mo stretchy="false" form="prefix">|</mo><mtext mathvariant="normal">mpg</mtext><mo>=</mo><mi>m</mi><mo stretchy="true" form="postfix">)</mo></mrow></mrow><annotation encoding="application/x-tex">
\textrm{E}[\textrm{cyl}|\textrm{mpg}=m] = \sum_{c \in \{4,6,8\}} c\cdot \textrm{P}(\textrm{cyl}=c|\textrm{mpg}=m)
</annotation></semantics></math></p>
<p>Given the matrix form of the output of <code><a href="../reference/add_predicted_rvars.html">add_epred_rvars()</a></code>
(i.e. when we do not use <code>columns_to</code>), this quantity is just
the dot product of <code>P(cyl|mpg)</code> with <code>c(4,6,8)</code>.
Since the <code>rvar</code> format supports math operations, including
matrix multiplication (as the <code>%**%</code> operator), we can
transform the prediction column into an expectation easily. Here is an
example on two rows:</p>
<div class="sourceCode" id="cb69"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://tibble.tidyverse.org/reference/tibble.html" class="external-link">tibble</a></span><span class="op">(</span>mpg <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">21</span>,<span class="fl">22</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 we are *not* using `columns_to` anymore</span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_rvars.html">add_epred_rvars</a></span><span class="op">(</span><span class="va">m_cyl</span>, value <span class="op">=</span> <span class="st">"P(cyl | 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://dplyr.tidyverse.org/reference/mutate.html" class="external-link">mutate</a></span><span class="op">(</span>cyl <span class="op">=</span> <span class="va">`P(cyl | mpg)`</span> <span class="op"><a href="https://mc-stan.org/posterior/reference/rvar-matmult.html" class="external-link">%**%</a></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">4</span>,<span class="fl">6</span>,<span class="fl">8</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 × 3</span></span></span>
<span><span class="co">##     mpg `P(cyl | mpg)`[,"4"]       [,"6"]         [,"8"]     cyl[,1]</span></span>
<span><span class="co">##   <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>           <span style="color: #949494; font-style: italic;">&lt;rvar[,1]&gt;</span>   <span style="color: #949494; font-style: italic;">&lt;rvar[,1]&gt;</span>     <span style="color: #949494; font-style: italic;">&lt;rvar[,1]&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;rvar[,1]&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span>    21          0.36 <span style="color: #949494;">± 0.16</span>  0.61 <span style="color: #949494;">± 0.16</span>  0.027 <span style="color: #949494;">± 0.035</span>  5.3 <span style="color: #949494;">± 0.34</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span>    22          0.72 <span style="color: #949494;">± 0.16</span>  0.27 <span style="color: #949494;">± 0.16</span>  0.008 <span style="color: #949494;">± 0.015</span>  4.6 <span style="color: #949494;">± 0.34</span></span></span></code></pre>
<p>Altogether, followed by <code><a href="../reference/nest_rvars.html">unnest_rvars()</a></code> so we can create
spaghetti plots:</p>
<div class="sourceCode" id="cb71"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">label_data_function</span> <span class="op">=</span> <span class="va">.</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/filter.html" class="external-link">filter</a></span><span class="op">(</span><span class="va">mpg</span> <span class="op">==</span> <span class="fu"><a href="https://rdrr.io/r/stats/quantile.html" class="external-link">quantile</a></span><span class="op">(</span><span class="va">mpg</span>, <span class="fl">.47</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/summarise_all.html" class="external-link">summarise_if</a></span><span class="op">(</span><span class="va">is.numeric</span>, <span class="va">mean</span><span class="op">)</span></span>
<span></span>
<span><span class="va">data_plot_with_mean</span> <span class="op">=</span> <span class="va">mtcars_clean</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>mpg <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">mpg</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: use of ndraws = 100 here subsets draws for the creation of spaghetti plots;</span></span>
<span>  <span class="co"># DOT NOT do this if you are making other chart types like intervals or densities</span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_rvars.html">add_epred_rvars</a></span><span class="op">(</span><span class="va">m_cyl</span>, value <span class="op">=</span> <span class="st">"P(cyl | 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="co"># calculate expected cylinder value</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>cyl <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/drop.html" class="external-link">drop</a></span><span class="op">(</span><span class="va">`P(cyl | mpg)`</span> <span class="op"><a href="https://mc-stan.org/posterior/reference/rvar-matmult.html" class="external-link">%**%</a></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">4</span>,<span class="fl">6</span>,<span class="fl">8</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="co"># transform in long-data-frame-of-draws format for making spaghetti plots</span></span>
<span>  <span class="fu"><a href="../reference/nest_rvars.html">unnest_rvars</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://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">mpg</span>, y <span class="op">=</span> <span class="va">cyl</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>group <span class="op">=</span> <span class="va">.draw</span><span class="op">)</span>, alpha <span class="op">=</span> <span class="fl">5</span><span class="op">/</span><span class="fl">100</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="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="fu"><a href="https://rdrr.io/r/base/numeric.html" class="external-link">as.numeric</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/character.html" class="external-link">as.character</a></span><span class="op">(</span><span class="va">cyl</span><span class="op">)</span><span class="op">)</span>, fill <span class="op">=</span> <span class="va">cyl</span><span class="op">)</span>, data <span class="op">=</span> <span class="va">mtcars_clean</span>, shape <span class="op">=</span> <span class="fl">21</span>, size <span class="op">=</span> <span class="fl">2</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_text.html" class="external-link">geom_text</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">mpg</span> <span class="op">+</span> <span class="fl">4</span><span class="op">)</span>, label <span class="op">=</span> <span class="st">"E[cyl | mpg]"</span>, data <span class="op">=</span> <span class="va">label_data_function</span>, hjust <span class="op">=</span> <span class="fl">0</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_segment.html" class="external-link">geom_segment</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>yend <span class="op">=</span> <span class="va">cyl</span>, xend <span class="op">=</span> <span class="va">mpg</span> <span class="op">+</span> <span class="fl">3.9</span><span class="op">)</span>, data <span class="op">=</span> <span class="va">label_data_function</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>, name <span class="op">=</span> <span class="st">"cyl"</span><span class="op">)</span></span>
<span></span>
<span><span class="fu"><a href="https://wilkelab.org/cowplot/reference/plot_grid.html" class="external-link">plot_grid</a></span><span class="op">(</span>ncol <span class="op">=</span> <span class="fl">1</span>, align <span class="op">=</span> <span class="st">"v"</span>,</span>
<span>  <span class="va">data_plot_with_mean</span>,</span>
<span>  <span class="va">fit_plot</span></span>
<span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-posterior_files/figure-html/unnamed-chunk-42-1.png" width="648"></p>
<p>Now let’s add on a plot of the latent linear predictor against the
thresholds used to determine the probabilities for each category. We can
use <code><a href="https://mc-stan.org/posterior/reference/draws_rvars.html" class="external-link">posterior::as_draws_rvars()</a></code> to get parameters from the
model as <code>rvar</code> objects:</p>
<div class="sourceCode" id="cb72"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">draws_cyl</span> <span class="op">=</span> <span class="va">m_cyl</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/tidy_draws.html">tidy_draws</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://mc-stan.org/posterior/reference/draws_rvars.html" class="external-link">as_draws_rvars</a></span><span class="op">(</span><span class="op">)</span></span>
<span></span>
<span><span class="va">draws_cyl</span></span></code></pre></div>
<pre><code><span><span class="co">## # A draws_rvars: 1000 iterations, 4 chains, and 10 variables</span></span>
<span><span class="co">## $b_Intercept: rvar&lt;1000,4&gt;[2] <span style="color: #949494;">mean ± sd:</span></span></span>
<span><span class="co">## [1] -38 ± 12  -33 ± 11 </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## $b_mpg: rvar&lt;1000,4&gt;[1] <span style="color: #949494;">mean ± sd:</span></span></span>
<span><span class="co">## [1] -1.8 ± 0.57 </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## $disc: rvar&lt;1000,4&gt;[1] <span style="color: #949494;">mean ± sd:</span></span></span>
<span><span class="co">## [1] 1 ± 0 </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## $lp__: rvar&lt;1000,4&gt;[1] <span style="color: #949494;">mean ± sd:</span></span></span>
<span><span class="co">## [1] -13 ± 1.3 </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## $accept_stat__: rvar&lt;1000,4&gt;[1] <span style="color: #949494;">mean ± sd:</span></span></span>
<span><span class="co">## [1] 0.94 ± 0.081 </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## $stepsize__: rvar&lt;1000,4&gt;[1] <span style="color: #949494;">mean ± sd:</span></span></span>
<span><span class="co">## [1] 0.31 ± 0.043 </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## $treedepth__: rvar&lt;1000,4&gt;[1] <span style="color: #949494;">mean ± sd:</span></span></span>
<span><span class="co">## [1] 2.7 ± 0.66 </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## $n_leapfrog__: rvar&lt;1000,4&gt;[1] <span style="color: #949494;">mean ± sd:</span></span></span>
<span><span class="co">## [1] 9.1 ± 5.1 </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## # ... with 2 more variables</span></span></code></pre>
<p>We’re really interested in the <code>b_Intercept</code> parameter,
which represents thresholds on the latent linear predictor:</p>
<div class="sourceCode" id="cb74"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">beta</span> <span class="op">=</span> <span class="va">draws_cyl</span><span class="op">$</span><span class="va">b_Intercept</span></span>
<span><span class="va">beta</span></span></code></pre></div>
<pre><code><span><span class="co">## rvar&lt;1000,4&gt;[2] <span style="color: #949494;">mean ± sd:</span></span></span>
<span><span class="co">## [1] -38 ± 12  -33 ± 11</span></span></code></pre>
<p>We’re also going to want the positions where the linear predictor
intercepts those thresholds, which we can calculate using the thresholds
and the slope (<code>b_mpg</code>):</p>
<div class="sourceCode" id="cb76"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">x_intercept</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/with.html" class="external-link">with</a></span><span class="op">(</span><span class="va">draws_cyl</span>, <span class="va">b_Intercept</span> <span class="op">/</span> <span class="va">b_mpg</span><span class="op">)</span></span>
<span><span class="va">x_intercept</span></span></code></pre></div>
<pre><code><span><span class="co">## rvar&lt;1000,4&gt;[2] <span style="color: #949494;">mean ± sd:</span></span></span>
<span><span class="co">## [1] 21 ± 0.50  18 ± 0.52</span></span></code></pre>
<p>We can use <code><a href="../reference/add_predicted_rvars.html">add_linpred_rvars()</a></code> analogously to
<code><a href="../reference/add_predicted_rvars.html">add_epred_rvars()</a></code> to get the latent linear predictor. We’ll
combine this with the thresholds in <code>beta</code>, subtracting
<code>beta[1]</code> from the linear predictor and from the other
threshold, <code>beta[2]</code>, as these values are all highly
correlated (thus are hard to visualize with uncertainty in a meaningful
way without looking at their differences). We’ll also demonstrate the
use of <code>.width = ppoints(XXX)</code> with
<code><a href="http://mjskay.github.io/ggdist/reference/stat_lineribbon.html" class="external-link">stat_lineribbon()</a></code> where <code>XXX</code> is a number like
<code>30</code> or <code>50</code>, which combined with a low
<code>alpha</code> value produces gradient-like lineribbons:</p>
<div class="sourceCode" id="cb78"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">beta_2_color</span> <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>n <span class="op">=</span> <span class="fl">3</span>, name <span class="op">=</span> <span class="st">"Dark2"</span><span class="op">)</span><span class="op">[[</span><span class="fl">3</span><span class="op">]</span><span class="op">]</span></span>
<span><span class="va">beta_1_color</span> <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>n <span class="op">=</span> <span class="fl">3</span>, name <span class="op">=</span> <span class="st">"Dark2"</span><span class="op">)</span><span class="op">[[</span><span class="fl">1</span><span class="op">]</span><span class="op">]</span></span>
<span></span>
<span><span class="co"># vertical lines we will use to show the relationship between the linear </span></span>
<span><span class="co"># predictor and P(cyl | mpg)</span></span>
<span><span class="va">x_intercept_lines</span> <span class="op">=</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></span>
<span>  <span class="co"># this works because `rvar`s define median() to take the median of the </span></span>
<span>  <span class="co"># distribution of each element, see vignette("rvar", package = "posterior")</span></span>
<span>  xintercept <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/median.html" class="external-link">median</a></span><span class="op">(</span><span class="va">x_intercept</span><span class="op">)</span>,</span>
<span>  color <span class="op">=</span> <span class="st">"gray50"</span>,</span>
<span>  alpha <span class="op">=</span> <span class="fl">0.2</span>,</span>
<span>  linewidth <span class="op">=</span> <span class="fl">1</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="va">thresholds_plot</span> <span class="op">=</span> <span class="va">mtcars_clean</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>mpg <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">mpg</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_rvars.html">add_linpred_rvars</a></span><span class="op">(</span><span class="va">m_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://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">mpg</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>
<span>    <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>ydist <span class="op">=</span> <span class="va">beta</span><span class="op">[</span><span class="fl">2</span><span class="op">]</span> <span class="op">-</span> <span class="va">beta</span><span class="op">[</span><span class="fl">1</span><span class="op">]</span><span class="op">)</span>,</span>
<span>    color <span class="op">=</span> <span class="va">beta_2_color</span>, fill <span class="op">=</span> <span class="va">beta_2_color</span>, </span>
<span>    alpha <span class="op">=</span> <span class="fl">1</span><span class="op">/</span><span class="fl">30</span>, .width <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/ppoints.html" class="external-link">ppoints</a></span><span class="op">(</span><span class="fl">30</span><span class="op">)</span>,</span>
<span>    linewidth <span class="op">=</span> <span class="fl">1</span>, linetype <span class="op">=</span> <span class="st">"21"</span></span>
<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="fl">0</span><span class="op">)</span>, linewidth <span class="op">=</span> <span class="fl">1</span>, color <span class="op">=</span> <span class="va">beta_1_color</span>, linetype <span class="op">=</span> <span class="st">"21"</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>
<span>    <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>ydist <span class="op">=</span> <span class="va">.linpred</span> <span class="op">-</span> <span class="va">beta</span><span class="op">[</span><span class="fl">1</span><span class="op">]</span><span class="op">)</span>,</span>
<span>    fill <span class="op">=</span> <span class="st">"black"</span>, color <span class="op">=</span> <span class="st">"black"</span>,</span>
<span>    alpha <span class="op">=</span> <span class="fl">1</span><span class="op">/</span><span class="fl">30</span>, .width <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/ppoints.html" class="external-link">ppoints</a></span><span class="op">(</span><span class="fl">30</span><span class="op">)</span></span>
<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">labs</a></span><span class="op">(</span>y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/expression.html" class="external-link">expression</a></span><span class="op">(</span><span class="st">"linear predictor"</span> <span class="op">-</span> <span class="va">beta</span><span class="op">[</span><span class="fl">1</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/annotate.html" class="external-link">annotate</a></span><span class="op">(</span><span class="st">"label"</span>,</span>
<span>    label <span class="op">=</span> <span class="st">"beta[1]"</span>, parse <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span>    x <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/Extremes.html" class="external-link">max</a></span><span class="op">(</span><span class="va">mtcars_clean</span><span class="op">$</span><span class="va">mpg</span><span class="op">)</span>, y <span class="op">=</span> <span class="fl">0</span>, hjust <span class="op">=</span> <span class="fl">0.8</span>,</span>
<span>    color <span class="op">=</span> <span class="va">beta_1_color</span></span>
<span>  <span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/annotate.html" class="external-link">annotate</a></span><span class="op">(</span><span class="st">"label"</span>,</span>
<span>    label <span class="op">=</span> <span class="st">"beta[2] - beta[1]"</span>, parse <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span>    x <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/Extremes.html" class="external-link">max</a></span><span class="op">(</span><span class="va">mtcars_clean</span><span class="op">$</span><span class="va">mpg</span><span class="op">)</span>, y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/median.html" class="external-link">median</a></span><span class="op">(</span><span class="va">beta</span><span class="op">[</span><span class="fl">2</span><span class="op">]</span> <span class="op">-</span> <span class="va">beta</span><span class="op">[</span><span class="fl">1</span><span class="op">]</span><span class="op">)</span>, hjust <span class="op">=</span> <span class="fl">0.9</span>,</span>
<span>    color <span class="op">=</span> <span class="va">beta_2_color</span></span>
<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>ylim <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">10</span>, <span class="fl">10</span><span class="op">)</span><span class="op">)</span></span>
<span></span>
<span><span class="fu"><a href="https://wilkelab.org/cowplot/reference/plot_grid.html" class="external-link">plot_grid</a></span><span class="op">(</span>ncol <span class="op">=</span> <span class="fl">1</span>, align <span class="op">=</span> <span class="st">"v"</span>, axis <span class="op">=</span> <span class="st">"lr"</span>,</span>
<span>  <span class="va">data_plot_with_mean</span> <span class="op">+</span> <span class="va">x_intercept_lines</span>,</span>
<span>  <span class="va">fit_plot</span> <span class="op">+</span> <span class="va">x_intercept_lines</span>,</span>
<span>  <span class="va">thresholds_plot</span> <span class="op">+</span> <span class="va">x_intercept_lines</span></span>
<span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-posterior_files/figure-html/unnamed-chunk-46-1.png" width="648"></p>
<p>Note how when the linear predictor intersects the line for
<code>beta[1]</code> categories 1 and 2 are equally likely, and when it
intersects the line for <code>beta[2]</code> categories 2 and 3 are
equally likely.</p>
<p>For more examples with this model using the long-data-frame-of-draws
workflow (which can be easier for certain tasks), see the corresponding
section of <code><a href="../articles/tidy-brms.html">vignette("tidy-brms")</a></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>
