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

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


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

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

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

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



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

    </div>

    
    
<style type="text/css">
.kable-table table {
  margin-left: 0;
}
img {
  border: none;
}
</style>
<div class="section level2">
<h2 id="introduction">Introduction<a class="anchor" aria-label="anchor" href="#introduction"></a>
</h2>
<p>This vignette describes how to use the <code>tidybayes</code> and
<code>ggdist</code> packages to extract and visualize <a href="https://dx.doi.org/10.18637/jss.v059.i10" class="external-link">tidy</a> data frames of
draws from posterior distributions of model variables, means, and
predictions from <code><a href="https://paul-buerkner.github.io/brms/reference/brm.html" class="external-link">brms::brm</a></code>. For a more general introduction
to <code>tidybayes</code> and its use on general-purpose Bayesian
modeling languages (like Stan and JAGS), see
<code><a href="../articles/tidybayes.html">vignette("tidybayes")</a></code>.</p>
</div>
<div class="section level2">
<h2 id="setup">Setup<a class="anchor" aria-label="anchor" href="#setup"></a>
</h2>
<p>The following libraries are required to run this vignette:</p>
<div class="sourceCode" id="cb1"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://magrittr.tidyverse.org" class="external-link">magrittr</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://dplyr.tidyverse.org" class="external-link">dplyr</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://purrr.tidyverse.org/" class="external-link">purrr</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://forcats.tidyverse.org/" class="external-link">forcats</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://tidyr.tidyverse.org" class="external-link">tidyr</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://modelr.tidyverse.org" class="external-link">modelr</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://mjskay.github.io/ggdist/" class="external-link">ggdist</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://mjskay.github.io/tidybayes/">tidybayes</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://ggplot2.tidyverse.org" class="external-link">ggplot2</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://wilkelab.org/cowplot/" class="external-link">cowplot</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://mc-stan.org/rstan/" class="external-link">rstan</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://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://gganimate.com" class="external-link">gganimate</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/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-brms_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 results look like this:</p>
<div class="sourceCode" id="cb8"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span></span></code></pre></div>
<pre><code><span><span class="co">##  Family: gaussian </span></span>
<span><span class="co">##   Links: mu = identity; sigma = identity </span></span>
<span><span class="co">## Formula: response ~ (1 | condition) </span></span>
<span><span class="co">##    Data: ABC (Number of observations: 50) </span></span>
<span><span class="co">##   Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;</span></span>
<span><span class="co">##          total post-warmup draws = 4000</span></span>
<span><span class="co">## </span></span>
<span><span class="co">## Multilevel Hyperparameters:</span></span>
<span><span class="co">## ~condition (Number of levels: 5) </span></span>
<span><span class="co">##               Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS</span></span>
<span><span class="co">## sd(Intercept)     1.17      0.43     0.60     2.22 1.00      944     1542</span></span>
<span><span class="co">## </span></span>
<span><span class="co">## Regression Coefficients:</span></span>
<span><span class="co">##           Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS</span></span>
<span><span class="co">## Intercept     0.50      0.48    -0.49     1.41 1.00      904     1204</span></span>
<span><span class="co">## </span></span>
<span><span class="co">## Further Distributional Parameters:</span></span>
<span><span class="co">##       Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS</span></span>
<span><span class="co">## sigma     0.56      0.06     0.46     0.70 1.00     2057     2243</span></span>
<span><span class="co">## </span></span>
<span><span class="co">## Draws were sampled using sampling(NUTS). For each parameter, Bulk_ESS</span></span>
<span><span class="co">## and Tail_ESS are effective sample size measures, and Rhat is the potential</span></span>
<span><span class="co">## scale reduction factor on split chains (at convergence, Rhat = 1).</span></span></code></pre>
</div>
<div class="section level2">
<h2 id="extracting-draws-from-a-fit-in-tidy-format-using-spread_draws">Extracting draws from a fit in tidy-format using
<code>spread_draws</code><a class="anchor" aria-label="anchor" href="#extracting-draws-from-a-fit-in-tidy-format-using-spread_draws"></a>
</h2>
<p>Now that we have our results, the fun begins: getting the draws out
in a tidy format! First, we’ll use the <code><a href="../reference/get_variables.html">get_variables()</a></code>
function to get a list of raw model 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"                    "r_condition[A,Intercept]"</span></span>
<span><span class="co">##  [5] "r_condition[B,Intercept]" "r_condition[C,Intercept]" "r_condition[D,Intercept]" "r_condition[E,Intercept]"</span></span>
<span><span class="co">##  [9] "lp__"                     "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 draw from either
<code>r_condition[A,Intercept]</code>,
<code>r_condition[B,Intercept]</code>, <code>...[C,...]</code>,
<code>...[D,...]</code>, or <code>...[E,...]</code>, and where we have
columns indexing which chain/iteration/draw the row came from and which
condition (<code>A</code> to <code>E</code>) it is for. That would allow
us to easily compute quantities grouped by condition, or generate plots
by condition using ggplot, or even merge draws with the original data to
plot data and posteriors simultaneously.</p>
<p>The workhorse of <code>tidybayes</code> is the
<code><a href="../reference/spread_draws.html">spread_draws()</a></code> function, which does this extraction for us.
It includes a simple specification format that we can use to extract
variables and their indices into tidy-format data frames.</p>
<div class="section level3">
<h3 id="gathering-variable-indices-into-a-separate-column-in-a-tidy-format-data-frame">Gathering variable indices into a separate column in a tidy format
data frame<a class="anchor" aria-label="anchor" href="#gathering-variable-indices-into-a-separate-column-in-a-tidy-format-data-frame"></a>
</h3>
<p>Given a variable in the model like this:</p>
<p><code>r_condition[D,Intercept]</code></p>
<p>We can provide <code><a href="../reference/spread_draws.html">spread_draws()</a></code> with a column
specification like this:</p>
<p><code>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_draws.html">spread_draws()</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_draws.html">spread_draws()</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_draws.html">spread_draws</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://rdrr.io/r/utils/head.html" class="external-link">head</a></span><span class="op">(</span><span class="fl">10</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 10 × 6</span></span></span>
<span><span class="co">## <span style="color: #949494;"># Groups:   condition, term [1]</span></span></span>
<span><span class="co">##    condition term      r_condition .chain .iteration .draw</span></span>
<span><span class="co">##    <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>     <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>           <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;int&gt;</span>      <span style="color: #949494; font-style: italic;">&lt;int&gt;</span> <span style="color: #949494; font-style: italic;">&lt;int&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 1</span> A         Intercept      0.683       1          1     1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 2</span> A         Intercept     -<span style="color: #BB0000;">0.779</span>       1          2     2</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 3</span> A         Intercept     -<span style="color: #BB0000;">0.580</span>       1          3     3</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 4</span> A         Intercept     -<span style="color: #BB0000;">0.717</span>       1          4     4</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 5</span> A         Intercept     -<span style="color: #BB0000;">0.786</span>       1          5     5</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 6</span> A         Intercept     -<span style="color: #BB0000;">0.818</span>       1          6     6</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 7</span> A         Intercept     -<span style="color: #BB0000;">0.456</span>       1          7     7</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 8</span> A         Intercept     -<span style="color: #BB0000;">0.248</span>       1          8     8</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 9</span> A         Intercept      0.087<span style="text-decoration: underline;">0</span>      1          9     9</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">10</span> A         Intercept     -<span style="color: #BB0000;">0.098</span><span style="color: #BB0000; text-decoration: underline;">3</span>      1         10    10</span></span></code></pre>
<p>We can choose whatever names we want for the index columns; e.g.:</p>
<div class="sourceCode" id="cb14"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">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 class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://rdrr.io/r/utils/head.html" class="external-link">head</a></span><span class="op">(</span><span class="fl">10</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 10 × 6</span></span></span>
<span><span class="co">## <span style="color: #949494;"># Groups:   c, t [1]</span></span></span>
<span><span class="co">##    c     t         r_condition .chain .iteration .draw</span></span>
<span><span class="co">##    <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>           <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;int&gt;</span>      <span style="color: #949494; font-style: italic;">&lt;int&gt;</span> <span style="color: #949494; font-style: italic;">&lt;int&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 1</span> A     Intercept      0.683       1          1     1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 2</span> A     Intercept     -<span style="color: #BB0000;">0.779</span>       1          2     2</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 3</span> A     Intercept     -<span style="color: #BB0000;">0.580</span>       1          3     3</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 4</span> A     Intercept     -<span style="color: #BB0000;">0.717</span>       1          4     4</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 5</span> A     Intercept     -<span style="color: #BB0000;">0.786</span>       1          5     5</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 6</span> A     Intercept     -<span style="color: #BB0000;">0.818</span>       1          6     6</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 7</span> A     Intercept     -<span style="color: #BB0000;">0.456</span>       1          7     7</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 8</span> A     Intercept     -<span style="color: #BB0000;">0.248</span>       1          8     8</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 9</span> A     Intercept      0.087<span style="text-decoration: underline;">0</span>      1          9     9</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">10</span> A     Intercept     -<span style="color: #BB0000;">0.098</span><span style="color: #BB0000; text-decoration: underline;">3</span>      1         10    10</span></span></code></pre>
<p>But the more descriptive and less cryptic names from the previous
example are probably preferable.</p>
<p>In this particular model, there is only one term
(<code>Intercept</code>), thus we could omit that index altogether to
just get each <code>condition</code> and the value of
<code>r_condition</code> for that condition:</p>
<div class="sourceCode" id="cb16"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">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://rdrr.io/r/utils/head.html" class="external-link">head</a></span><span class="op">(</span><span class="fl">10</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 10 × 5</span></span></span>
<span><span class="co">## <span style="color: #949494;"># Groups:   condition [1]</span></span></span>
<span><span class="co">##    condition r_condition .chain .iteration .draw</span></span>
<span><span class="co">##    <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>           <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;int&gt;</span>      <span style="color: #949494; font-style: italic;">&lt;int&gt;</span> <span style="color: #949494; font-style: italic;">&lt;int&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 1</span> A              0.683       1          1     1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 2</span> A             -<span style="color: #BB0000;">0.779</span>       1          2     2</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 3</span> A             -<span style="color: #BB0000;">0.580</span>       1          3     3</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 4</span> A             -<span style="color: #BB0000;">0.717</span>       1          4     4</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 5</span> A             -<span style="color: #BB0000;">0.786</span>       1          5     5</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 6</span> A             -<span style="color: #BB0000;">0.818</span>       1          6     6</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 7</span> A             -<span style="color: #BB0000;">0.456</span>       1          7     7</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 8</span> A             -<span style="color: #BB0000;">0.248</span>       1          8     8</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 9</span> A              0.087<span style="text-decoration: underline;">0</span>      1          9     9</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">10</span> A             -<span style="color: #BB0000;">0.098</span><span style="color: #BB0000; text-decoration: underline;">3</span>      1         10    10</span></span></code></pre>
<p><strong>Note:</strong> If you have used <code><a href="../reference/spread_draws.html">spread_draws()</a></code>
with a raw sample from Stan or JAGS, you may be used to using
<code>recover_types</code> before <code><a href="../reference/spread_draws.html">spread_draws()</a></code> to get
index column values back (e.g. if the index was a factor). This is not
necessary when using <code><a href="../reference/spread_draws.html">spread_draws()</a></code> on
<code>rstanarm</code> models, because those models already contain that
information in their variable names. For more on
<code>recover_types</code>, see <code><a href="../articles/tidybayes.html">vignette("tidybayes")</a></code>.</p>
</div>
</div>
<div class="section level2">
<h2 id="point-summaries-and-intervals">Point summaries and intervals<a class="anchor" aria-label="anchor" href="#point-summaries-and-intervals"></a>
</h2>
<div class="section level3">
<h3 id="with-simple-model-variables">With simple model variables<a class="anchor" aria-label="anchor" href="#with-simple-model-variables"></a>
</h3>
<p><code>tidybayes</code> provides a family of functions for generating
point summaries and intervals from draws in a tidy format. These
functions follow the naming scheme
<code>[median|mean|mode]_[qi|hdi]</code>, for example,
<code><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi()</a></code>, <code><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">mean_qi()</a></code>,
<code><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">mode_hdi()</a></code>, and so on. The first name (before the
<code>_</code>) indicates the type of point summary, and the second name
indicates the type of interval. <code>qi</code> yields a quantile
interval (a.k.a. equi-tailed interval, central interval, or percentile
interval) and <code>hdi</code> yields a highest (posterior) density
interval. Custom point 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="cb18"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">b_Intercept</span>, <span class="va">sigma</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://rdrr.io/r/utils/head.html" class="external-link">head</a></span><span class="op">(</span><span class="fl">10</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 10 × 5</span></span></span>
<span><span class="co">##    .chain .iteration .draw b_Intercept sigma</span></span>
<span><span class="co">##     <span style="color: #949494; font-style: italic;">&lt;int&gt;</span>      <span style="color: #949494; font-style: italic;">&lt;int&gt;</span> <span style="color: #949494; font-style: italic;">&lt;int&gt;</span>       <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 1</span>      1          1     1      -<span style="color: #BB0000;">0.277</span> 0.567</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 2</span>      1          2     2       0.892 0.562</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 3</span>      1          3     3       0.941 0.554</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 4</span>      1          4     4       0.999 0.553</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 5</span>      1          5     5       1.04  0.544</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 6</span>      1          6     6       1.41  0.580</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 7</span>      1          7     7       0.333 0.551</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 8</span>      1          8     8       0.562 0.536</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 9</span>      1          9     9       0.177 0.544</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">10</span>      1         10    10       0.324 0.498</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="cb20"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">b_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">##   b_Intercept b_Intercept.lower b_Intercept.upper     sigma sigma.lower sigma.upper .width .point .interval</span></span>
<span><span class="co">## 1   0.5258874        -0.4902497           1.40569 0.5571528   0.4558006   0.6957939   0.95 median        qi</span></span></code></pre>
<p>We can specify the columns we want to get medians and intervals from,
as above, or if we omit the list of columns, <code><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi()</a></code>
will use every column that is not a grouping column or a special column
(like <code>.chain</code>, <code>.iteration</code>, or
<code>.draw</code>). Thus in the above example, <code>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="cb22"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">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">##   b_Intercept b_Intercept.lower b_Intercept.upper     sigma sigma.lower sigma.upper .width .point .interval</span></span>
<span><span class="co">## 1   0.5258874        -0.4902497           1.40569 0.5571528   0.4558006   0.6957939   0.95 median        qi</span></span></code></pre>
<p>If you would rather have a long-format list of intervals, use
<code><a href="../reference/spread_draws.html">gather_draws()</a></code> instead:</p>
<div class="sourceCode" id="cb24"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">gather_draws</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: 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>
<p>For more on <code><a href="../reference/spread_draws.html">gather_draws()</a></code>, see
<code><a href="../articles/tidybayes.html">vignette("tidybayes")</a></code>.</p>
</div>
<div class="section level3">
<h3 id="with-indexed-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="cb26"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">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="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>How did <code><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi()</a></code> know what to aggregate? Data frames
returned by <code><a href="../reference/spread_draws.html">spread_draws()</a></code> are automatically grouped by all
index variables you pass to it; in this case, that means
<code><a href="../reference/spread_draws.html">spread_draws()</a></code> groups its results by
<code>condition</code>. <code><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi()</a></code> respects those groups,
and calculates the point summaries and intervals within all groups.
Then, because no columns were passed to <code><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi()</a></code>, it
acts on the only non-special (<code>.</code>-prefixed) and non-group
column, <code>r_condition</code>. So the above shortened syntax is
equivalent to this more verbose call:</p>
<div class="sourceCode" id="cb28"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</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/group_by.html" class="external-link">group_by</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 class="co"># this line not necessary (done by spread_draws)</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi</a></span><span class="op">(</span><span class="va">r_condition</span><span class="op">)</span>      <span class="co"># b is not necessary (it is the only non-group column)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 5 × 7</span></span></span>
<span><span class="co">##   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><code>tidybayes</code> also provides an implementation of
<code><a href="https://mc-stan.org/posterior/reference/draws_summary.html" class="external-link">posterior::summarise_draws()</a></code> for grouped data frames
(<code>tidybayes::summaries_draws.grouped_df()</code>), which you can
use to quickly get convergence diagnostics:</p>
<div class="sourceCode" id="cb30"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">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://mc-stan.org/posterior/reference/draws_summary.html" class="external-link">summarise_draws</a></span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 5 × 11</span></span></span>
<span><span class="co">## <span style="color: #949494;"># Groups:   condition [5]</span></span></span>
<span><span class="co">##   condition variable      mean median    sd   mad     q5    q95  rhat ess_bulk ess_tail</span></span>
<span><span class="co">##   <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>     <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>        <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>    <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>    <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span> A         r_condition -<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         r_condition  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         r_condition  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         r_condition  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         r_condition -<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_draws.html">spread_draws()</a></code> and <code><a href="../reference/spread_draws.html">gather_draws()</a></code> support
extracting variables that have different indices into the same data
frame. Indices with the same name are automatically matched up, and
values are duplicated as necessary to produce one row per all
combination of levels of all indices. For example, we might want to
calculate the mean within each condition (call this
<code>condition_mean</code>). In this model, that mean is the intercept
(<code>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="cb32"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span> </span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">b_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://rdrr.io/r/utils/head.html" class="external-link">head</a></span><span class="op">(</span><span class="fl">10</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 10 × 6</span></span></span>
<span><span class="co">## <span style="color: #949494;"># Groups:   condition [5]</span></span></span>
<span><span class="co">##    .chain .iteration .draw b_Intercept condition r_condition</span></span>
<span><span class="co">##     <span style="color: #949494; font-style: italic;">&lt;int&gt;</span>      <span style="color: #949494; font-style: italic;">&lt;int&gt;</span> <span style="color: #949494; font-style: italic;">&lt;int&gt;</span>       <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>           <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 1</span>      1          1     1      -<span style="color: #BB0000;">0.277</span> A              0.683 </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 2</span>      1          1     1      -<span style="color: #BB0000;">0.277</span> B              1.16  </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 3</span>      1          1     1      -<span style="color: #BB0000;">0.277</span> C              2.18  </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 4</span>      1          1     1      -<span style="color: #BB0000;">0.277</span> D              1.06  </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 5</span>      1          1     1      -<span style="color: #BB0000;">0.277</span> E             -<span style="color: #BB0000;">0.396</span> </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 6</span>      1          2     2       0.892 A             -<span style="color: #BB0000;">0.779</span> </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 7</span>      1          2     2       0.892 B             -<span style="color: #BB0000;">0.014</span><span style="color: #BB0000; text-decoration: underline;">5</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 8</span>      1          2     2       0.892 C              0.803 </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 9</span>      1          2     2       0.892 D              0.221 </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">10</span>      1          2     2       0.892 E             -<span style="color: #BB0000;">1.58</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="cb34"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">`b_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="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi</a></span><span class="op">(</span><span class="va">condition_mean</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 5 × 7</span></span></span>
<span><span class="co">##   condition condition_mean .lower .upper .width .point .interval</span></span>
<span><span class="co">##   <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>              <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>    </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span> A                  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> B                  0.998  0.640  1.33    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">3</span> C                  1.84   1.49   2.18    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">4</span> D                  1.02   0.670  1.36    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">5</span> E                 -<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></code></pre>
<p><code><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi()</a></code> uses tidy evaluation (see
<code>vignette("tidy-evaluation", package = "rlang")</code>), so it can
take column expressions, not just column names. Thus, we can simplify
the above example by moving the calculation of
<code>condition_mean</code> from <code>mutate</code> into
<code><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi()</a></code>:</p>
<div class="sourceCode" id="cb36"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">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><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 condition_mean .lower .upper .width .point .interval</span></span>
<span><span class="co">##   <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>              <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>    </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span> A                  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> B                  0.998  0.640  1.33    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">3</span> C                  1.84   1.49   2.18    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">4</span> D                  1.02   0.670  1.36    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">5</span> E                 -<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></code></pre>
</div>
<div class="section level2">
<h2 id="plotting-intervals-with-multiple-probability-levels">Plotting intervals with multiple probability levels<a class="anchor" aria-label="anchor" href="#plotting-intervals-with-multiple-probability-levels"></a>
</h2>
<p><code><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi()</a></code> and its sister functions can produce an
arbitrary number of probability intervals by setting the
<code>.width =</code> argument:</p>
<div class="sourceCode" id="cb38"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">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 × 7</span></span></span>
<span><span class="co">##    condition condition_mean  .lower .upper .width .point .interval</span></span>
<span><span class="co">##    <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>              <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>   <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>    </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 1</span> A                  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> B                  0.998  0.640   1.33    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 3</span> C                  1.84   1.49    2.18    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 4</span> D                  1.02   0.670   1.36    0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 5</span> E                 -<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> A                  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> B                  0.998  0.766   1.22    0.8  median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 8</span> C                  1.84   1.61    2.06    0.8  median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 9</span> D                  1.02   0.786   1.24    0.8  median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">10</span> E                 -<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> A                  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> B                  0.998  0.881   1.11    0.5  median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">13</span> C                  1.84   1.72    1.95    0.5  median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">14</span> D                  1.02   0.898   1.13    0.5  median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">15</span> E                 -<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. For
example, assigning <code>-.width</code> to the <code>linewidth</code>
aesthetic will show all intervals, making thicker lines correspond to
smaller intervals. The <code><a href="http://mjskay.github.io/ggdist/reference/geom_pointinterval.html" class="external-link">ggdist::geom_pointinterval()</a></code> geom
automatically sets the <code>linewidth</code> aesthetic appropriately
based on the <code>.width</code> column in the data to produce plots of
points with multiple probability levels:</p>
<div class="sourceCode" id="cb40"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">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">.66</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">condition</span>, x <span class="op">=</span> <span class="va">condition_mean</span>, xmin <span class="op">=</span> <span class="va">.lower</span>, xmax <span class="op">=</span> <span class="va">.upper</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/geom_pointinterval.html" class="external-link">geom_pointinterval</a></span><span class="op">(</span><span class="op">)</span> </span></code></pre></div>
<p><img src="tidy-brms_files/figure-html/unnamed-chunk-21-1.png" width="648"></p>
</div>
<div class="section level2">
<h2 id="intervals-with-densities">Intervals with densities<a class="anchor" aria-label="anchor" href="#intervals-with-densities"></a>
</h2>
<p>To see the density along with the intervals, we can use
<code><a href="http://mjskay.github.io/ggdist/reference/stat_eye.html" class="external-link">ggdist::stat_eye()</a></code> (“eye plots”, which combine intervals
with violin plots), or <code><a href="http://mjskay.github.io/ggdist/reference/stat_halfeye.html" class="external-link">ggdist::stat_halfeye()</a></code> (interval +
density plots):</p>
<div class="sourceCode" id="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_draws.html">spread_draws</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>, x <span class="op">=</span> <span class="va">condition_mean</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/stat_halfeye.html" class="external-link">stat_halfeye</a></span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-brms_files/figure-html/unnamed-chunk-22-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="cb42"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">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>, x <span class="op">=</span> <span class="va">condition_mean</span>, fill <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes_eval.html" class="external-link">after_stat</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/MathFun.html" class="external-link">abs</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span> <span class="op">&lt;</span> <span class="fl">.8</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/stat_halfeye.html" class="external-link">stat_halfeye</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_abline.html" class="external-link">geom_vline</a></span><span class="op">(</span>xintercept <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="op">-</span><span class="fl">.8</span>, <span class="fl">.8</span><span class="op">)</span>, linetype <span class="op">=</span> <span class="st">"dashed"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_manual.html" class="external-link">scale_fill_manual</a></span><span class="op">(</span>values <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"gray80"</span>, <span class="st">"skyblue"</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-brms_files/figure-html/unnamed-chunk-23-1.png" width="648"></p>
</div>
<div class="section level2">
<h2 id="other-visualizations-of-distributions-stat_slabinterval">Other visualizations of distributions:
<code>stat_slabinterval</code><a class="anchor" aria-label="anchor" href="#other-visualizations-of-distributions-stat_slabinterval"></a>
</h2>
<p>There are a variety of additional stats for visualizing distributions
in the <code><a href="http://mjskay.github.io/ggdist/reference/geom_slabinterval.html" class="external-link">ggdist::geom_slabinterval()</a></code> family of stats and
geoms:</p>
<p><img src="slabinterval_family.png" width="700" alt="The slabinterval family of geoms and stats"></p>
<p>See <code><a href="http://mjskay.github.io/ggdist/articles/slabinterval.html" class="external-link">vignette("slabinterval", package = "ggdist")</a></code> for an
overview.</p>
</div>
<div class="section level2">
<h2 id="posterior-means-and-predictions">Posterior means and predictions<a class="anchor" aria-label="anchor" href="#posterior-means-and-predictions"></a>
</h2>
<p>Rather than calculating conditional means manually as in the previous
example, we could use <code><a href="../reference/add_predicted_draws.html">add_epred_draws()</a></code>, which is analogous
to <code><a href="https://mc-stan.org/rstantools/reference/posterior_epred.html" class="external-link">brms::posterior_epred()</a></code> (giving posterior draws from the
expectation of the posterior predictive; i.e. posterior distributions of
conditional means), but uses a tidy data format. We can combine it with
<code><a href="https://modelr.tidyverse.org/reference/data_grid.html" class="external-link">modelr::data_grid()</a></code> to first generate a grid describing the
predictions we want, then transform that grid into a long-format data
frame of draws from conditional means:</p>
<div class="sourceCode" id="cb43"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">ABC</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://modelr.tidyverse.org/reference/data_grid.html" class="external-link">data_grid</a></span><span class="op">(</span><span class="va">condition</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_epred_draws</a></span><span class="op">(</span><span class="va">m</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://rdrr.io/r/utils/head.html" class="external-link">head</a></span><span class="op">(</span><span class="fl">10</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 10 × 6</span></span></span>
<span><span class="co">## <span style="color: #949494;"># Groups:   condition, .row [1]</span></span></span>
<span><span class="co">##    condition  .row .chain .iteration .draw .epred</span></span>
<span><span class="co">##    <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>     <span style="color: #949494; font-style: italic;">&lt;int&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;int&gt;</span>      <span style="color: #949494; font-style: italic;">&lt;int&gt;</span> <span style="color: #949494; font-style: italic;">&lt;int&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 1</span> A             1     <span style="color: #BB0000;">NA</span>         <span style="color: #BB0000;">NA</span>     1  0.406</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 2</span> A             1     <span style="color: #BB0000;">NA</span>         <span style="color: #BB0000;">NA</span>     2  0.114</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 3</span> A             1     <span style="color: #BB0000;">NA</span>         <span style="color: #BB0000;">NA</span>     3  0.361</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 4</span> A             1     <span style="color: #BB0000;">NA</span>         <span style="color: #BB0000;">NA</span>     4  0.282</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 5</span> A             1     <span style="color: #BB0000;">NA</span>         <span style="color: #BB0000;">NA</span>     5  0.253</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 6</span> A             1     <span style="color: #BB0000;">NA</span>         <span style="color: #BB0000;">NA</span>     6  0.596</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 7</span> A             1     <span style="color: #BB0000;">NA</span>         <span style="color: #BB0000;">NA</span>     7 -<span style="color: #BB0000;">0.123</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 8</span> A             1     <span style="color: #BB0000;">NA</span>         <span style="color: #BB0000;">NA</span>     8  0.314</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 9</span> A             1     <span style="color: #BB0000;">NA</span>         <span style="color: #BB0000;">NA</span>     9  0.264</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">10</span> A             1     <span style="color: #BB0000;">NA</span>         <span style="color: #BB0000;">NA</span>    10  0.226</span></span></code></pre>
<p>To plot this example, we’ll also show the use of
<code><a href="http://mjskay.github.io/ggdist/reference/stat_pointinterval.html" class="external-link">ggdist::stat_pointinterval()</a></code> instead of
<code><a href="http://mjskay.github.io/ggdist/reference/geom_pointinterval.html" class="external-link">ggdist::geom_pointinterval()</a></code>, which summarizes draws into
points and intervals within ggplot:</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_draws.html">add_epred_draws</a></span><span class="op">(</span><span class="va">m</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">.epred</span>, y <span class="op">=</span> <span class="va">condition</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/stat_pointinterval.html" class="external-link">stat_pointinterval</a></span><span class="op">(</span>.width <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">.66</span>, <span class="fl">.95</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-brms_files/figure-html/unnamed-chunk-25-1.png" width="648"></p>
</div>
<div class="section level2">
<h2 id="quantile-dotplots">Quantile dotplots<a class="anchor" aria-label="anchor" href="#quantile-dotplots"></a>
</h2>
<p>Intervals are nice if the alpha level happens to line up with
whatever decision you are trying to make, but getting a shape of the
posterior is better (hence eye plots, above). On the other hand, making
inferences from density plots is imprecise (estimating the area of one
shape as a proportion of another is a hard perceptual task). Reasoning
about probability in frequency formats is easier, motivating <a href="https://github.com/mjskay/when-ish-is-my-bus/blob/master/quantile-dotplots.md" class="external-link">quantile
dotplots</a> (<a href="https://doi.org/10.1145/2858036.2858558" class="external-link">Kay et
al. 2016</a>, <a href="https://doi.org/10.1145/3173574.3173718" class="external-link">Fernandes et
al. 2018</a>), which also allow precise estimation of arbitrary
intervals (down to the dot resolution of the plot, 100 in the example
below).</p>
<p>Within the slabinterval family of geoms in tidybayes is the
<code>dots</code> and <code>dotsinterval</code> family, which
automatically determine appropriate bin sizes for dotplots and can
calculate quantiles from samples to construct quantile dotplots.
<code><a href="http://mjskay.github.io/ggdist/reference/stat_dotsinterval.html" class="external-link">ggdist::stat_dotsinterval()</a></code> is the variant designed for use
on samples:</p>
<div class="sourceCode" id="cb46"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">ABC</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://modelr.tidyverse.org/reference/data_grid.html" class="external-link">data_grid</a></span><span class="op">(</span><span class="va">condition</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_epred_draws</a></span><span class="op">(</span><span class="va">m</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">.epred</span>, y <span class="op">=</span> <span class="va">condition</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/stat_dotsinterval.html" class="external-link">stat_dotsinterval</a></span><span class="op">(</span>quantiles <span class="op">=</span> <span class="fl">100</span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-brms_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_draws.html">add_epred_draws()</a></code> is analogous to
<code><a href="https://mc-stan.org/rstantools/reference/posterior_epred.html" class="external-link">brms::posterior_epred()</a></code>, <code><a href="../reference/add_predicted_draws.html">add_predicted_draws()</a></code>
is analogous to <code><a href="https://mc-stan.org/rstantools/reference/posterior_predict.html" class="external-link">brms::posterior_predict()</a></code>, giving draws
from the posterior predictive distribution.</p>
<p>Here is an example of posterior predictive distributions plotted
using <code><a href="http://mjskay.github.io/ggdist/reference/stat_slab.html" class="external-link">ggdist::stat_slab()</a></code>:</p>
<div class="sourceCode" id="cb47"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">ABC</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://modelr.tidyverse.org/reference/data_grid.html" class="external-link">data_grid</a></span><span class="op">(</span><span class="va">condition</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_predicted_draws</a></span><span class="op">(</span><span class="va">m</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">.prediction</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 class="op">)</span></span></code></pre></div>
<p><img src="tidy-brms_files/figure-html/unnamed-chunk-27-1.png" width="648"></p>
<p>We could also 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_draws.html">add_predicted_draws</a></span><span class="op">(</span><span class="va">m</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">condition</span>, x <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_interval.html" class="external-link">stat_interval</a></span><span class="op">(</span>.width <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">.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-brms_files/figure-html/unnamed-chunk-28-1.png" width="648"></p>
<p>Altogether, data, posterior predictions, and posterior distributions
of the means:</p>
<div class="sourceCode" id="cb49"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">grid</span> <span class="op">=</span> <span class="va">ABC</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://modelr.tidyverse.org/reference/data_grid.html" class="external-link">data_grid</a></span><span class="op">(</span><span class="va">condition</span><span class="op">)</span></span>
<span></span>
<span><span class="va">means</span> <span class="op">=</span> <span class="va">grid</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_epred_draws</a></span><span class="op">(</span><span class="va">m</span><span class="op">)</span></span>
<span></span>
<span><span class="va">preds</span> <span class="op">=</span> <span class="va">grid</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_predicted_draws</a></span><span class="op">(</span><span class="va">m</span><span class="op">)</span></span>
<span></span>
<span><span class="va">ABC</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">condition</span>, x <span class="op">=</span> <span class="va">response</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/stat_interval.html" class="external-link">stat_interval</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">.prediction</span><span class="op">)</span>, data <span class="op">=</span> <span class="va">preds</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/stat_pointinterval.html" class="external-link">stat_pointinterval</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">.epred</span><span class="op">)</span>, data <span class="op">=</span> <span class="va">means</span>, .width <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">.66</span>, <span class="fl">.95</span><span class="op">)</span>, position <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/position_nudge.html" class="external-link">position_nudge</a></span><span class="op">(</span>y <span class="op">=</span> <span class="op">-</span><span class="fl">0.3</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html" class="external-link">geom_point</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_color_brewer</a></span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-brms_files/figure-html/unnamed-chunk-29-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 brms. Then, we can select a small
number of 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="cb50"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">ABC</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://modelr.tidyverse.org/reference/data_grid.html" class="external-link">data_grid</a></span><span class="op">(</span><span class="va">condition</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_epred_draws</a></span><span class="op">(</span><span class="va">m</span>, 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/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 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>    slab_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-brms_files/figure-html/unnamed-chunk-30-1.png" width="648"></p>
<p>We could even combine the Kruschke-style plots of predictive
distributions with half-eyes showing the posterior means:</p>
<div class="sourceCode" id="cb51"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">ABC</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://modelr.tidyverse.org/reference/data_grid.html" class="external-link">data_grid</a></span><span class="op">(</span><span class="va">condition</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_epred_draws</a></span><span class="op">(</span><span class="va">m</span>, 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="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">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 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="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>    slab_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>, data <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 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>, scale <span class="op">=</span> <span class="fl">.5</span></span>
<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>y <span class="op">=</span> <span class="va">.epred</span><span class="op">)</span>, side <span class="op">=</span> <span class="st">"bottom"</span>, scale <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/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">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>, 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>x <span class="op">=</span> <span class="op">-</span><span class="fl">.2</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-brms_files/figure-html/unnamed-chunk-31-1.png" width="648"></p>
</div>
<div class="section level2">
<h2 id="fitprediction-curves">Fit/prediction curves<a class="anchor" aria-label="anchor" href="#fitprediction-curves"></a>
</h2>
<p>To demonstrate drawing fit curves with uncertainty, let’s fit a
slightly naive model to part of the <code>mtcars</code> dataset:</p>
<div class="sourceCode" id="cb52"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m_mpg</span> <span class="op">=</span> <span class="fu"><a href="https://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 with probability bands:</p>
<div class="sourceCode" id="cb53"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">mtcars</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/group_by.html" class="external-link">group_by</a></span><span class="op">(</span><span class="va">cyl</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://modelr.tidyverse.org/reference/data_grid.html" class="external-link">data_grid</a></span><span class="op">(</span>hp <span class="op">=</span> <span class="fu"><a href="https://modelr.tidyverse.org/reference/seq_range.html" class="external-link">seq_range</a></span><span class="op">(</span><span class="va">hp</span>, n <span class="op">=</span> <span class="fl">51</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_epred_draws</a></span><span class="op">(</span><span class="va">m_mpg</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">hp</span>, y <span class="op">=</span> <span class="va">mpg</span>, color <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html" class="external-link">ordered</a></span><span class="op">(</span><span class="va">cyl</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/stat_lineribbon.html" class="external-link">stat_lineribbon</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">.epred</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html" class="external-link">geom_point</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_fill_brewer</a></span><span class="op">(</span>palette <span class="op">=</span> <span class="st">"Greys"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_color_brewer</a></span><span class="op">(</span>palette <span class="op">=</span> <span class="st">"Set2"</span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-brms_files/figure-html/unnamed-chunk-32-1.png" width="648"></p>
<p>Or we can sample a reasonable number of fit lines (say 100) and
overplot them:</p>
<div class="sourceCode" id="cb54"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">mtcars</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/group_by.html" class="external-link">group_by</a></span><span class="op">(</span><span class="va">cyl</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://modelr.tidyverse.org/reference/data_grid.html" class="external-link">data_grid</a></span><span class="op">(</span>hp <span class="op">=</span> <span class="fu"><a href="https://modelr.tidyverse.org/reference/seq_range.html" class="external-link">seq_range</a></span><span class="op">(</span><span class="va">hp</span>, n <span class="op">=</span> <span class="fl">101</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="co"># NOTE: this shows the use of ndraws to subsample within add_epred_draws()</span></span>
<span>  <span class="co"># ONLY do this IF you are planning to make spaghetti plots, etc.</span></span>
<span>  <span class="co"># NEVER subsample to a small sample to plot intervals, densities, etc.</span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_epred_draws</a></span><span class="op">(</span><span class="va">m_mpg</span>, ndraws <span class="op">=</span> <span class="fl">100</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">hp</span>, y <span class="op">=</span> <span class="va">mpg</span>, color <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html" class="external-link">ordered</a></span><span class="op">(</span><span class="va">cyl</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_path.html" class="external-link">geom_line</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">.epred</span>, group <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/paste.html" class="external-link">paste</a></span><span class="op">(</span><span class="va">cyl</span>, <span class="va">.draw</span><span class="op">)</span><span class="op">)</span>, alpha <span class="op">=</span> <span class="fl">.1</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html" class="external-link">geom_point</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_color_brewer</a></span><span class="op">(</span>palette <span class="op">=</span> <span class="st">"Dark2"</span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-brms_files/figure-html/unnamed-chunk-33-1.png" width="648"></p>
<p>Or we can create animated <a href="https://mucollective.northwestern.edu/project/hops-trends" class="external-link">hypothetical
outcome plots (HOPs)</a> of fit lines:</p>
<div class="sourceCode" id="cb55"><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">123456</span><span class="op">)</span></span>
<span><span class="co"># NOTE: using a small number of draws to keep this example</span></span>
<span><span class="co"># small, but in practice you probably want 50 or 100</span></span>
<span><span class="va">ndraws</span> <span class="op">=</span> <span class="fl">20</span></span>
<span></span>
<span><span class="va">p</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/group_by.html" class="external-link">group_by</a></span><span class="op">(</span><span class="va">cyl</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://modelr.tidyverse.org/reference/data_grid.html" class="external-link">data_grid</a></span><span class="op">(</span>hp <span class="op">=</span> <span class="fu"><a href="https://modelr.tidyverse.org/reference/seq_range.html" class="external-link">seq_range</a></span><span class="op">(</span><span class="va">hp</span>, n <span class="op">=</span> <span class="fl">101</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_epred_draws</a></span><span class="op">(</span><span class="va">m_mpg</span>, ndraws <span class="op">=</span> <span class="va">ndraws</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">hp</span>, y <span class="op">=</span> <span class="va">mpg</span>, color <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html" class="external-link">ordered</a></span><span class="op">(</span><span class="va">cyl</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_path.html" class="external-link">geom_line</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">.epred</span>, group <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/paste.html" class="external-link">paste</a></span><span class="op">(</span><span class="va">cyl</span>, <span class="va">.draw</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html" class="external-link">geom_point</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_color_brewer</a></span><span class="op">(</span>palette <span class="op">=</span> <span class="st">"Dark2"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://gganimate.com/reference/transition_states.html" class="external-link">transition_states</a></span><span class="op">(</span><span class="va">.draw</span>, <span class="fl">0</span>, <span class="fl">1</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://gganimate.com/reference/shadow_mark.html" class="external-link">shadow_mark</a></span><span class="op">(</span>future <span class="op">=</span> <span class="cn">TRUE</span>, color <span class="op">=</span> <span class="st">"gray50"</span>, alpha <span class="op">=</span> <span class="fl">1</span><span class="op">/</span><span class="fl">20</span><span class="op">)</span></span>
<span></span>
<span><span class="fu"><a href="https://gganimate.com/reference/animate.html" class="external-link">animate</a></span><span class="op">(</span><span class="va">p</span>, nframes <span class="op">=</span> <span class="va">ndraws</span>, fps <span class="op">=</span> <span class="fl">2.5</span>, width <span class="op">=</span> <span class="fl">432</span>, height <span class="op">=</span> <span class="fl">288</span>, units <span class="op">=</span> <span class="st">"px"</span>, res <span class="op">=</span> <span class="fl">96</span>, dev <span class="op">=</span> <span class="st">"ragg_png"</span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-brms_hops_lines.gif"></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="cb56"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">mtcars</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/group_by.html" class="external-link">group_by</a></span><span class="op">(</span><span class="va">cyl</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://modelr.tidyverse.org/reference/data_grid.html" class="external-link">data_grid</a></span><span class="op">(</span>hp <span class="op">=</span> <span class="fu"><a href="https://modelr.tidyverse.org/reference/seq_range.html" class="external-link">seq_range</a></span><span class="op">(</span><span class="va">hp</span>, n <span class="op">=</span> <span class="fl">101</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_predicted_draws</a></span><span class="op">(</span><span class="va">m_mpg</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">hp</span>, y <span class="op">=</span> <span class="va">mpg</span>, color <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html" class="external-link">ordered</a></span><span class="op">(</span><span class="va">cyl</span><span class="op">)</span>, fill <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html" class="external-link">ordered</a></span><span class="op">(</span><span class="va">cyl</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/stat_lineribbon.html" class="external-link">stat_lineribbon</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">.prediction</span><span class="op">)</span>, .width <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">.95</span>, <span class="fl">.80</span>, <span class="fl">.50</span><span class="op">)</span>, alpha <span class="op">=</span> <span class="fl">1</span><span class="op">/</span><span class="fl">4</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html" class="external-link">geom_point</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_fill_brewer</a></span><span class="op">(</span>palette <span class="op">=</span> <span class="st">"Set2"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_color_brewer</a></span><span class="op">(</span>palette <span class="op">=</span> <span class="st">"Dark2"</span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-brms_files/figure-html/unnamed-chunk-35-1.png" width="648"></p>
<p>This gets difficult to judge by group, so probably better to facet
into multiple plots. Fortunately, since we are using ggplot, that
functionality is built in:</p>
<div class="sourceCode" id="cb57"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">mtcars</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/group_by.html" class="external-link">group_by</a></span><span class="op">(</span><span class="va">cyl</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://modelr.tidyverse.org/reference/data_grid.html" class="external-link">data_grid</a></span><span class="op">(</span>hp <span class="op">=</span> <span class="fu"><a href="https://modelr.tidyverse.org/reference/seq_range.html" class="external-link">seq_range</a></span><span class="op">(</span><span class="va">hp</span>, n <span class="op">=</span> <span class="fl">101</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_predicted_draws</a></span><span class="op">(</span><span class="va">m_mpg</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">hp</span>, y <span class="op">=</span> <span class="va">mpg</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/stat_lineribbon.html" class="external-link">stat_lineribbon</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">.prediction</span><span class="op">)</span>, .width <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">.99</span>, <span class="fl">.95</span>, <span class="fl">.8</span>, <span class="fl">.5</span><span class="op">)</span>, color <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/RColorBrewer/man/ColorBrewer.html" class="external-link">brewer.pal</a></span><span class="op">(</span><span class="fl">5</span>, <span class="st">"Blues"</span><span class="op">)</span><span class="op">[[</span><span class="fl">5</span><span class="op">]</span><span class="op">]</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html" class="external-link">geom_point</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_fill_brewer</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/facet_grid.html" class="external-link">facet_grid</a></span><span class="op">(</span><span class="va">.</span> <span class="op">~</span> <span class="va">cyl</span>, space <span class="op">=</span> <span class="st">"free_x"</span>, scales <span class="op">=</span> <span class="st">"free_x"</span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-brms_files/figure-html/unnamed-chunk-36-1.png" width="648"></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="cb58"><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-brms_files/figure-html/unnamed-chunk-37-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="cb59"><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="cb60"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">grid</span> <span class="op">=</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>
<span></span>
<span><span class="va">means</span> <span class="op">=</span> <span class="va">grid</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_epred_draws</a></span><span class="op">(</span><span class="va">m_ab</span><span class="op">)</span></span>
<span></span>
<span><span class="va">preds</span> <span class="op">=</span> <span class="va">grid</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_predicted_draws</a></span><span class="op">(</span><span class="va">m_ab</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="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>x <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>, data <span class="op">=</span> <span class="va">means</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/stat_interval.html" class="external-link">stat_interval</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">.prediction</span><span class="op">)</span>, data <span class="op">=</span> <span class="va">preds</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html" class="external-link">geom_point</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">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-brms_files/figure-html/unnamed-chunk-38-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_draws.html">add_epred_draws()</a></code>:</p>
<div class="sourceCode" id="cb61"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">grid</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_epred_draws</a></span><span class="op">(</span><span class="va">m_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>x <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-brms_files/figure-html/unnamed-chunk-39-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_draws.html">add_epred_draws()</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="cb62"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">m</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/spread_draws.html">spread_draws</a></span><span class="op">(</span><span class="va">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>, x <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-brms_files/figure-html/unnamed-chunk-40-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">posterior_epred()</a></code> function for ordinal and
multinomial regression models in brms returns multiple variables for
each draw: one for each outcome category (in contrast to
<code><a href="https://mc-stan.org/rstanarm/reference/stan_polr.html" class="external-link">rstanarm::stan_polr()</a></code> models, which return draws from the
latent linear predictor). 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> adds an additional column called
<code>.category</code> and a separate row containing the variable for
each category is output for every draw and predictor.</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="cb63"><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="cb65"><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_draws.html">add_epred_draws()</a></code> will include a <code>.category</code>
column, and <code>.epred</code> will contain draws from the posterior
distribution for the probability that the response is in that category.
For example, here is the fit for the first row in the dataset:</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="fl">21</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_epred_draws</a></span><span class="op">(</span><span class="va">m_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="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi</a></span><span class="op">(</span><span class="va">.epred</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 3 × 9</span></span></span>
<span><span class="co">##     mpg  .row .category .epred   .lower .upper .width .point .interval</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;fct&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>    21     1 4         0.346  0.095<span style="text-decoration: underline;">2</span>    0.710   0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span>    21     1 6         0.620  0.263     0.892   0.95 median qi       </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">3</span>    21     1 8         0.013<span style="text-decoration: underline;">6</span> 0.000<span style="text-decoration: underline;">287</span>  0.124   0.95 median qi</span></span></code></pre>
<p>Note: for the <code>.category</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 predicted 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_draws.html">add_epred_draws</a></span><span class="op">(</span><span class="va">m_cyl</span>, value <span class="op">=</span> <span class="st">"P(cyl | mpg)"</span>, category <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>, y <span class="op">=</span> <span class="va">`P(cyl | 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>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>
<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-brms_files/figure-html/unnamed-chunk-43-1.png" width="648"></p>
<p>The above display does not let you see the correlation between
<code>P(cyl|mpg)</code> for different values of <code>cyl</code> at a
particular value of <code>mpg</code>. For example, in the portion of the
posterior where <code>P(cyl = 6|mpg = 20)</code> is high,
<code>P(cyl = 4|mpg = 20)</code> and <code>P(cyl = 8|mpg = 20)</code>
must be low (since these must add up to 1).</p>
<p>One way to see this correlation might be to employ <a href="https://doi.org/10.1371/journal.pone.0142444" class="external-link">hypothetical outcome
plots (HOPs)</a> just for the fit line, “detaching” it from the ribbon
(another alternative would be to use HOPs on top of line ensembles, as
demonstrated earlier in this document). By employing animation, you can
see how the lines move in tandem or opposition to each other, revealing
some patterns in how they are correlated:</p>
<div class="sourceCode" id="cb69"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># NOTE: using a small number of draws to keep this example</span></span>
<span><span class="co"># small, but in practice you probably want 50 or 100</span></span>
<span><span class="va">ndraws</span> <span class="op">=</span> <span class="fl">20</span></span>
<span></span>
<span><span class="va">p</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_draws.html">add_epred_draws</a></span><span class="op">(</span><span class="va">m_cyl</span>, value <span class="op">=</span> <span class="st">"P(cyl | mpg)"</span>, category <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>, y <span class="op">=</span> <span class="va">`P(cyl | 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="co"># we remove the `.draw` column from the data for stat_lineribbon so that the same ribbons</span></span>
<span>  <span class="co"># are drawn on every frame (since we use .draw to determine the transitions below)</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>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>, color <span class="op">=</span> <span class="cn">NA</span>, data <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 class="fu"><a href="https://dplyr.tidyverse.org/reference/select.html" class="external-link">select</a></span><span class="op">(</span><span class="op">-</span><span class="va">.draw</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="co"># we use sample_draws to subsample at the level of geom_line (rather than for the full dataset</span></span>
<span>  <span class="co"># as in previous HOPs examples) because we need the full set of draws for stat_lineribbon above</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="fu"><a href="https://rdrr.io/r/base/paste.html" class="external-link">paste</a></span><span class="op">(</span><span class="va">.draw</span>, <span class="va">cyl</span><span class="op">)</span><span class="op">)</span>, linewidth <span class="op">=</span> <span class="fl">1</span>, data <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 class="fu"><a href="../reference/sample_draws.html">sample_draws</a></span><span class="op">(</span><span class="va">ndraws</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><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://gganimate.com/reference/transition_manual.html" class="external-link">transition_manual</a></span><span class="op">(</span><span class="va">.draw</span><span class="op">)</span></span>
<span></span>
<span><span class="fu"><a href="https://gganimate.com/reference/animate.html" class="external-link">animate</a></span><span class="op">(</span><span class="va">p</span>, nframes <span class="op">=</span> <span class="va">ndraws</span>, fps <span class="op">=</span> <span class="fl">2.5</span>, width <span class="op">=</span> <span class="fl">576</span>, height <span class="op">=</span> <span class="fl">192</span>, units <span class="op">=</span> <span class="st">"px"</span>, res <span class="op">=</span> <span class="fl">96</span>, dev <span class="op">=</span> <span class="st">"ragg_png"</span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-brms_hops_ordinal_ribbon_lines.gif"></p>
<p>Notice how the lines move together, and how they move up or down
together or in opposition. We could take a slice through these lines at
an x position in the above chart (say, <code>mpg = 20</code>) and look
at the correlation between them using a scatterplot matrix:</p>
<div class="sourceCode" id="cb70"><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="fl">20</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_epred_draws</a></span><span class="op">(</span><span class="va">m_cyl</span>, value <span class="op">=</span> <span class="st">"P(cyl | mpg = 20)"</span>, category <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://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/select.html" class="external-link">select</a></span><span class="op">(</span><span class="va">.draw</span>, <span class="va">cyl</span>, <span class="va">`P(cyl | mpg = 20)`</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/gather_pairs.html">gather_pairs</a></span><span class="op">(</span><span class="va">cyl</span>, <span class="va">`P(cyl | mpg = 20)`</span>, triangle <span class="op">=</span> <span class="st">"both"</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">.row</span> <span class="op">!=</span> <span class="va">.col</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><span class="va">.x</span>, <span class="va">.y</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>alpha <span class="op">=</span> <span class="fl">1</span><span class="op">/</span><span class="fl">50</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/facet_grid.html" class="external-link">facet_grid</a></span><span class="op">(</span><span class="va">.row</span> <span class="op">~</span> <span class="va">.col</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">ylab</a></span><span class="op">(</span><span class="st">"P(cyl = row | mpg = 20)"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">xlab</a></span><span class="op">(</span><span class="st">"P(cyl = col | mpg = 20)"</span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-brms_files/figure-html/unnamed-chunk-45-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> We can use the above formula to derive
a posterior distribution for
<math display="inline" 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></mrow><annotation encoding="application/x-tex">\textrm{E}[\textrm{cyl}|\textrm{mpg}=m]</annotation></semantics></math>
from the model. The model gives us a posterior distribution for
<math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><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{P}(\textrm{cyl}=c|\textrm{mpg}=m)</annotation></semantics></math>:
when <code>mpg</code> =
<math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mi>m</mi><annotation encoding="application/x-tex">m</annotation></semantics></math>,
the response-scale linear predictor (the <code>.epred</code> column from
<code><a href="../reference/add_predicted_draws.html">add_epred_draws()</a></code>) for <code>cyl</code> (aka
<code>.category</code>) =
<math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mi>c</mi><annotation encoding="application/x-tex">c</annotation></semantics></math>
is
<math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><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{P}(\textrm{cyl}=c|\textrm{mpg}=m)</annotation></semantics></math>.
Thus, we can group within <code>.draw</code> and then use
<code>summarise</code> to calculate the expected value:</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: this shows the use of ndraws to subsample within add_epred_draws()</span></span>
<span>  <span class="co"># ONLY do this IF you are planning to make spaghetti plots, etc.</span></span>
<span>  <span class="co"># NEVER subsample to a small sample to plot intervals, densities, etc.</span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_epred_draws</a></span><span class="op">(</span><span class="va">m_cyl</span>, value <span class="op">=</span> <span class="st">"P(cyl | mpg)"</span>, category <span class="op">=</span> <span class="st">"cyl"</span>, ndraws <span class="op">=</span> <span class="fl">100</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/group_by.html" class="external-link">group_by</a></span><span class="op">(</span><span class="va">mpg</span>, <span class="va">.draw</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="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/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><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.html" class="external-link">summarise</a></span><span class="op">(</span>cyl <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/sum.html" class="external-link">sum</a></span><span class="op">(</span><span class="va">cyl</span> <span class="op">*</span> <span class="va">`P(cyl | mpg)`</span><span class="op">)</span>, .groups <span class="op">=</span> <span class="st">"drop"</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-brms_files/figure-html/unnamed-chunk-46-1.png" width="648"></p>
<p>Now let’s do some posterior predictive checking: do posterior
predictions look like the data? For this, we’ll make new predictions at
the same values of <code>mpg</code> as were present in the original
dataset (gray circles) and plot these with the observed data (colored
circles):</p>
<div class="sourceCode" id="cb72"><pre class="downlit sourceCode r">
<code class="sourceCode R"><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="co"># we use `select` instead of `data_grid` here because we want to make posterior predictions</span></span>
<span>  <span class="co"># for exactly the same set of observations we have in the original data</span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/select.html" class="external-link">select</a></span><span class="op">(</span><span class="va">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="../reference/add_predicted_draws.html">add_predicted_draws</a></span><span class="op">(</span><span class="va">m_cyl</span>, seed <span class="op">=</span> <span class="fl">1234</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"># recover original factor labels</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/levels.html" class="external-link">levels</a></span><span class="op">(</span><span class="va">mtcars_clean</span><span class="op">$</span><span class="va">cyl</span><span class="op">)</span><span class="op">[</span><span class="va">.prediction</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_count.html" class="external-link">geom_count</a></span><span class="op">(</span>color <span class="op">=</span> <span class="st">"gray75"</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>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/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://ggrepel.slowkow.com/reference/geom_text_repel.html" class="external-link">geom_label_repel</a></span><span class="op">(</span></span>
<span>    data <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 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 class="fu"><a href="https://dplyr.tidyverse.org/reference/filter.html" class="external-link">filter</a></span><span class="op">(</span><span class="va">cyl</span> <span class="op">==</span> <span class="st">"8"</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span> <span class="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/base/Extremes.html" class="external-link">max</a></span><span class="op">(</span><span class="va">mpg</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 class="fu">dplyr</span><span class="fu">::</span><span class="fu"><a href="https://dplyr.tidyverse.org/reference/slice.html" class="external-link">slice</a></span><span class="op">(</span><span class="fl">1</span><span class="op">)</span>,</span>
<span>    label <span class="op">=</span> <span class="st">"posterior predictions"</span>, xlim <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">26</span>, <span class="cn">NA</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="cn">NA</span>, <span class="fl">2.8</span><span class="op">)</span>, point.padding <span class="op">=</span> <span class="fl">0.3</span>,</span>
<span>    label.size <span class="op">=</span> <span class="cn">NA</span>, color <span class="op">=</span> <span class="st">"gray50"</span>, segment.color <span class="op">=</span> <span class="st">"gray75"</span></span>
<span>  <span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggrepel.slowkow.com/reference/geom_text_repel.html" class="external-link">geom_label_repel</a></span><span class="op">(</span></span>
<span>    data <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 class="fu"><a href="https://dplyr.tidyverse.org/reference/filter.html" class="external-link">filter</a></span><span class="op">(</span><span class="va">cyl</span> <span class="op">==</span> <span class="st">"6"</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span> <span class="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/base/Extremes.html" class="external-link">max</a></span><span class="op">(</span><span class="va">mpg</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 class="fu">dplyr</span><span class="fu">::</span><span class="fu"><a href="https://dplyr.tidyverse.org/reference/slice.html" class="external-link">slice</a></span><span class="op">(</span><span class="fl">1</span><span class="op">)</span>,</span>
<span>    label <span class="op">=</span> <span class="st">"observed data"</span>, xlim <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">26</span>, <span class="cn">NA</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="fl">2.2</span>, <span class="cn">NA</span><span class="op">)</span>, point.padding <span class="op">=</span> <span class="fl">0.2</span>,</span>
<span>    label.size <span class="op">=</span> <span class="cn">NA</span>, segment.color <span class="op">=</span> <span class="st">"gray35"</span></span>
<span>  <span class="op">)</span></span></code></pre></div>
<p><img src="tidy-brms_files/figure-html/unnamed-chunk-47-1.png" width="768"></p>
<p>This looks pretty good. Let’s check using another typical posterior
predictive checking plot: many simulated distributions of the response
(<code>cyl</code>) against the observed distribution of the response.
For a continuous response variable this is usually done with a density
plot; here, we’ll plot the number of posterior predictions in each bin
as a line plot, since the response variable is discrete:</p>
<div class="sourceCode" id="cb73"><pre class="downlit sourceCode r">
<code class="sourceCode R"><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://dplyr.tidyverse.org/reference/select.html" class="external-link">select</a></span><span class="op">(</span><span class="va">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="../reference/add_predicted_draws.html">add_predicted_draws</a></span><span class="op">(</span><span class="va">m_cyl</span>, ndraws <span class="op">=</span> <span class="fl">100</span>, seed <span class="op">=</span> <span class="fl">12345</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"># recover original factor labels</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/levels.html" class="external-link">levels</a></span><span class="op">(</span><span class="va">mtcars_clean</span><span class="op">$</span><span class="va">cyl</span><span class="op">)</span><span class="op">[</span><span class="va">.prediction</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">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_bar.html" class="external-link">stat_count</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="cn">NA</span><span class="op">)</span>, geom <span class="op">=</span> <span class="st">"line"</span>, data <span class="op">=</span> <span class="va">mtcars_clean</span>, color <span class="op">=</span> <span class="st">"red"</span>, linewidth <span class="op">=</span> <span class="fl">3</span>, alpha <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/geom_bar.html" class="external-link">stat_count</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>, geom <span class="op">=</span> <span class="st">"line"</span>, position <span class="op">=</span> <span class="st">"identity"</span>, alpha <span class="op">=</span> <span class="fl">.05</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_label</a></span><span class="op">(</span>data <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html" class="external-link">data.frame</a></span><span class="op">(</span>cyl <span class="op">=</span> <span class="st">"4"</span><span class="op">)</span>, y <span class="op">=</span> <span class="fl">9.5</span>, label <span class="op">=</span> <span class="st">"posterior\npredictions"</span>,</span>
<span>    hjust <span class="op">=</span> <span class="fl">1</span>, color <span class="op">=</span> <span class="st">"gray50"</span>, lineheight <span class="op">=</span> <span class="fl">1</span>, label.size <span class="op">=</span> <span class="cn">NA</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_label</a></span><span class="op">(</span>data <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html" class="external-link">data.frame</a></span><span class="op">(</span>cyl <span class="op">=</span> <span class="st">"8"</span><span class="op">)</span>, y <span class="op">=</span> <span class="fl">14</span>, label <span class="op">=</span> <span class="st">"observed\ndata"</span>,</span>
<span>    hjust <span class="op">=</span> <span class="fl">0</span>, color <span class="op">=</span> <span class="st">"red"</span>, lineheight <span class="op">=</span> <span class="fl">1</span>, label.size <span class="op">=</span> <span class="cn">NA</span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-brms_files/figure-html/unnamed-chunk-48-1.png" width="648"></p>
<p>This also looks good.</p>
<p>Another way to look at these posterior predictions might be as a
scatterplot matrix. <code><a href="../reference/gather_pairs.html">gather_pairs()</a></code> makes it easy to
generate long-format data frames suitable for creating custom
scatterplot matrices (or really, arbitrary matrix-style small multiples
plots) in ggplot using <code><a href="https://ggplot2.tidyverse.org/reference/facet_grid.html" class="external-link">ggplot2::facet_grid()</a></code>:</p>
<div class="sourceCode" id="cb74"><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">12345</span><span class="op">)</span></span>
<span></span>
<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://dplyr.tidyverse.org/reference/select.html" class="external-link">select</a></span><span class="op">(</span><span class="va">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="../reference/add_predicted_draws.html">add_predicted_draws</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="co"># recover original factor labels. Must ungroup first so that the</span></span>
<span>  <span class="co"># factor is created in the same way in all groups; this is a workaround</span></span>
<span>  <span class="co"># because brms no longer returns labelled predictions (hopefully that</span></span>
<span>  <span class="co"># is fixed then this will no longer be necessary)</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>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="fu"><a href="https://rdrr.io/r/base/levels.html" class="external-link">levels</a></span><span class="op">(</span><span class="va">mtcars_clean</span><span class="op">$</span><span class="va">cyl</span><span class="op">)</span><span class="op">[</span><span class="va">.prediction</span><span class="op">]</span>, <span class="fu"><a href="https://rdrr.io/r/base/levels.html" class="external-link">levels</a></span><span class="op">(</span><span class="va">mtcars_clean</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"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="co"># need .drop = FALSE to ensure 0 counts are not dropped</span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/group_by.html" class="external-link">group_by</a></span><span class="op">(</span><span class="va">.draw</span>, .drop <span class="op">=</span> <span class="cn">FALSE</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/count.html" class="external-link">count</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="../reference/gather_pairs.html">gather_pairs</a></span><span class="op">(</span><span class="va">cyl</span>, <span class="va">n</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><span class="va">.x</span>, <span class="va">.y</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_count.html" class="external-link">geom_count</a></span><span class="op">(</span>color <span class="op">=</span> <span class="st">"gray75"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html" class="external-link">geom_point</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">mtcars_clean</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span> <span class="fu"><a href="https://dplyr.tidyverse.org/reference/count.html" class="external-link">count</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 class="fu"><a href="../reference/gather_pairs.html">gather_pairs</a></span><span class="op">(</span><span class="va">cyl</span>, <span class="va">n</span><span class="op">)</span>, color <span class="op">=</span> <span class="st">"red"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/facet_grid.html" class="external-link">facet_grid</a></span><span class="op">(</span><span class="fu"><a href="https://dplyr.tidyverse.org/reference/vars.html" class="external-link">vars</a></span><span class="op">(</span><span class="va">.row</span><span class="op">)</span>, <span class="fu"><a href="https://dplyr.tidyverse.org/reference/vars.html" class="external-link">vars</a></span><span class="op">(</span><span class="va">.col</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/labs.html" class="external-link">xlab</a></span><span class="op">(</span><span class="st">"Number of observations with cyl = col"</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">ylab</a></span><span class="op">(</span><span class="st">"Number of observations with cyl = row"</span><span class="op">)</span> </span></code></pre></div>
<p><img src="tidy-brms_files/figure-html/unnamed-chunk-49-1.png" width="648"></p>
</div>
<div class="section level3">
<h3 id="ordinal-model-with-categorical-predictor">Ordinal model with categorical predictor<a class="anchor" aria-label="anchor" href="#ordinal-model-with-categorical-predictor"></a>
</h3>
<p>Here’s an ordinal model with a categorical predictor:</p>
<div class="sourceCode" id="cb75"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/utils/data.html" class="external-link">data</a></span><span class="op">(</span><span class="va">esoph</span><span class="op">)</span></span>
<span><span class="va">m_esoph_brm</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">tobgp</span> <span class="op">~</span> <span class="va">agegp</span>, </span>
<span>  data <span class="op">=</span> <span class="va">esoph</span>, </span>
<span>  family <span class="op">=</span> <span class="fu"><a href="https://paul-buerkner.github.io/brms/reference/brmsfamily.html" class="external-link">cumulative</a></span><span class="op">(</span><span class="op">)</span>,</span>
<span></span>
<span>  file <span class="op">=</span> <span class="st">"models/tidy-brms_m_esoph_brm.rds"</span>  </span>
<span><span class="op">)</span></span></code></pre></div>
<p>Then we can plot predicted probabilities for each outcome category
within each level of the predictor:</p>
<div class="sourceCode" id="cb76"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">esoph</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://modelr.tidyverse.org/reference/data_grid.html" class="external-link">data_grid</a></span><span class="op">(</span><span class="va">agegp</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_epred_draws</a></span><span class="op">(</span><span class="va">m_esoph_brm</span>, dpar <span class="op">=</span> <span class="cn">TRUE</span>, category <span class="op">=</span> <span class="st">"tobgp"</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">agegp</span>, y <span class="op">=</span> <span class="va">.epred</span>, color <span class="op">=</span> <span class="va">tobgp</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/stat_pointinterval.html" class="external-link">stat_pointinterval</a></span><span class="op">(</span>position <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/position_dodge.html" class="external-link">position_dodge</a></span><span class="op">(</span>width <span class="op">=</span> <span class="fl">.4</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_size.html" class="external-link">scale_size_continuous</a></span><span class="op">(</span>guide <span class="op">=</span> <span class="st">"none"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_manual.html" class="external-link">scale_color_manual</a></span><span class="op">(</span>values <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/RColorBrewer/man/ColorBrewer.html" class="external-link">brewer.pal</a></span><span class="op">(</span><span class="fl">6</span>, <span class="st">"Blues"</span><span class="op">)</span><span class="op">[</span><span class="op">-</span><span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">1</span>,<span class="fl">2</span><span class="op">)</span><span class="op">]</span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-brms_files/figure-html/unnamed-chunk-50-1.png" width="648"></p>
<p>It is hard to see the changes in categories in the above plot; let’s
try something that gives a better gist of the distribution within each
year:</p>
<div class="sourceCode" id="cb77"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">esoph_plot</span> <span class="op">=</span> <span class="va">esoph</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://modelr.tidyverse.org/reference/data_grid.html" class="external-link">data_grid</a></span><span class="op">(</span><span class="va">agegp</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="../reference/add_predicted_draws.html">add_epred_draws</a></span><span class="op">(</span><span class="va">m_esoph_brm</span>, category <span class="op">=</span> <span class="st">"tobgp"</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">.epred</span>, y <span class="op">=</span> <span class="va">tobgp</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/coord_cartesian.html" class="external-link">coord_cartesian</a></span><span class="op">(</span>expand <span class="op">=</span> <span class="cn">FALSE</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/facet_grid.html" class="external-link">facet_grid</a></span><span class="op">(</span><span class="va">.</span> <span class="op">~</span> <span class="va">agegp</span>, switch <span class="op">=</span> <span class="st">"x"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggtheme.html" class="external-link">theme_classic</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/theme.html" class="external-link">theme</a></span><span class="op">(</span>strip.background <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/element.html" class="external-link">element_blank</a></span><span class="op">(</span><span class="op">)</span>, strip.placement <span class="op">=</span> <span class="st">"outside"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span><span class="st">"P(tobacco consumption category | age group)"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">xlab</a></span><span class="op">(</span><span class="st">"age group"</span><span class="op">)</span></span>
<span></span>
<span><span class="va">esoph_plot</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/stat_summary.html" class="external-link">stat_summary</a></span><span class="op">(</span>fun <span class="op">=</span> <span class="va">median</span>, geom <span class="op">=</span> <span class="st">"bar"</span>, fill <span class="op">=</span> <span class="st">"gray65"</span>, width <span class="op">=</span> <span class="fl">1</span>, color <span class="op">=</span> <span class="st">"white"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/stat_pointinterval.html" class="external-link">stat_pointinterval</a></span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
<p><img src="tidy-brms_files/figure-html/unnamed-chunk-51-1.png" width="648"></p>
<p>The bars in this case might present a false sense of precision, so we
could also try CCDF barplots instead:</p>
<div class="sourceCode" id="cb78"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">esoph_plot</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/stat_ccdfinterval.html" class="external-link">stat_ccdfinterval</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/expand_limits.html" class="external-link">expand_limits</a></span><span class="op">(</span>x <span class="op">=</span> <span class="fl">0</span><span class="op">)</span> <span class="co">#ensure bars go to 0</span></span></code></pre></div>
<p><img src="tidy-brms_files/figure-html/unnamed-chunk-52-1.png" width="648"></p>
<p>This output should be very similar to the output from the
corresponding <code>m_esoph_rs</code> model in
<code><a href="../articles/tidy-rstanarm.html">vignette("tidy-rstanarm")</a></code> (modulo different priors), though
brms does more of the work for us to produce it than
<code>rstanarm</code> does.</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>
