<!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>Incorporating Bayesian Statistics • specr</title>
<!-- jquery --><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script><!-- Bootstrap --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha256-bZLfwXAP04zRMK2BjiO8iu9pf4FbLqX6zitd+tIvLhE=" 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><meta property="og:title" content="Incorporating Bayesian Statistics">
<meta property="og:description" content="specr">
<!-- 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]-->
</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">specr</a>
        <span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="">1.0.1</span>
      </span>
    </div>

    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
<li>
  <a href="../articles/getting-started.html">Getting 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 class="dropdown-header">Basics</li>
    <li>
      <a href="../articles/different-specifications.html">Setting up different types of specifications</a>
    </li>
    <li>
      <a href="../articles/custom-plot.html">Visualizing specification curve analyses</a>
    </li>
    <li>
      <a href="../articles/invest-spec.html">Investigating specific specifications</a>
    </li>
    <li>
      <a href="../articles/inferences.html">Inferences with specification curve analysis</a>
    </li>
    <li>
      <a href="../articles/parallelization.html">Parallelization</a>
    </li>
    <li class="divider">
    </li>
<li class="dropdown-header">Incorporating specific models</li>
    <li>
      <a href="../articles/measurement-models.html">Structural equation models</a>
    </li>
    <li>
      <a href="../articles/multilevel-models.html">Multilevel models</a>
    </li>
    <li>
      <a href="../articles/parallel-bayesian-models.html">Bayesian models</a>
    </li>
  </ul>
</li>
<li>
  <a href="../news/index.html">News</a>
</li>
      </ul>
<ul class="nav navbar-nav navbar-right">
<li>
  <a href="https://github.com/masurp/specr" class="external-link">
    <span class="fa 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>Incorporating Bayesian Statistics</h1>
            
      
      <small class="dont-index">Source: <a href="https://github.com/masurp/specr/blob/HEAD/vignettes/parallel-bayesian-models.Rmd" class="external-link"><code>vignettes/parallel-bayesian-models.Rmd</code></a></small>
      <div class="hidden name"><code>parallel-bayesian-models.Rmd</code></div>

    </div>

    
    
<p>This vignette shows how to incorporate Bayesian modeling using the
package <a href="https://paul-buerkner.github.io/brms/" class="external-link">brms</a> in
specr. When we fit a model with <code>brms</code>, the package actually
calls Rstan in the background, which, in turn, is an R interface to the
statistical programming language <code>Stan</code>. Stan is built in the
programming language C++ and models have to be compiled using C++ to be
run. This is all taken care of by brms, so you just need to run
<code>brm(…)</code> to fit any model. <code>brms</code> uses a syntax
for specifying model formulae that is based on the syntax of the
commonly known lme4 package (for multilevel modeling). The comparatively
easy syntax of <code>brms</code> is then converted into Stan code
automatically.</p>
<p>That said, since the models have to be compiled in C++, you need to
set up your computer so that it can actually use C++. This has to be
done only once, before installing brms. Unfortunately, as almost always,
how you install C++ depends on your operating system. Check <a href="https://github.com/masurp/workshop_bayes/blob/main/exercises/Exercise_02.md" class="external-link">this
vignette</a> to if you have problems installing brms.</p>
<p>We are also loading the library <code>furrr</code> as we want to
parallelize the computations.</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://tidyverse.tidyverse.org" class="external-link">tidyverse</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://masurp.github.io/specr/" class="external-link">specr</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://github.com/bbolker/broom.mixed" class="external-link">broom.mixed</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/ggridges/" class="external-link">ggridges</a></span><span class="op">)</span></span></code></pre></div>
<div class="section level2">
<h2 id="setting-up-custom-functions">Setting up custom functions<a class="anchor" aria-label="anchor" href="#setting-up-custom-functions"></a>
</h2>
<p>Although you can simply pass the function <code>brm</code> to
<code>setup</code> and run <code><a href="../reference/specr.html">specr()</a></code> with
<code>workers = 1</code> without any specific custom function, it
usually make sense to set up some custom model fitting and extraction
functions to make sure <code><a href="../reference/specr.html">specr()</a></code> does exactly what we
want.</p>
<p>Here, I am creating a custom model fitting function and specify
relevant parameters for the Bayesian models. Here, I am only switching
of the progress indication and messages, and reduce the iteration to
1000 to save time. Of course, we can bump this up to wherever we need it
to be.</p>
<p>Note that you can use specr to parallize the process (then you would
set <code>workers = 1</code> in the brm function) and use the
parallelization via <code>furrr</code> as outlined in <a href="https://masurp.github.io/specr/articles/parallelization.html" class="external-link">this
vignette</a>. Alternatively, we can let <code>brms</code> parallelize
the process (e.g., by setting e.g., <code>cores = 4</code>). Then, we
would not use the parallelization procedure by <code>specr</code>. Here,
I keep it simple and just do all computations sequentially. Thus, all
chains per specifications as well as the specifications themselves are
fitted one after the other.</p>
<p>If you want to be able to inspect each brms-object after the
computation, I suggest to also create a custom fit extract function.
Here, I added the full <code>brms.fit</code> object to the
<code>glance</code> output. This way, we can later access the entire
object in order to e.g., extract posterior distributions or other
aspects of the Bayesian models.</p>
<div class="sourceCode" id="cb2"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># Customized function, not necessary if standard values are to be used</span></span>
<span><span class="va">brm_new</span> <span class="op">&lt;-</span> <span class="kw">function</span><span class="op">(</span><span class="va">formula</span>, <span class="va">data</span><span class="op">)</span> <span class="op">{</span></span>
<span>  <span class="fu"><a href="https://rdrr.io/pkg/brms/man/brm.html" class="external-link">brm</a></span><span class="op">(</span><span class="va">formula</span>, <span class="va">data</span>,</span>
<span>      silent <span class="op">=</span> <span class="fl">2</span>,    <span class="co"># Don't print progress</span></span>
<span>      refresh <span class="op">=</span> <span class="fl">0</span>,   <span class="co"># Remove message (not necessary, but nicer in the output)</span></span>
<span>      iter <span class="op">=</span> <span class="fl">1000</span><span class="op">)</span>   <span class="co"># I just set this here to reduce computing time, can be higher, of course</span></span>
<span><span class="op">}</span></span>
<span></span>
<span><span class="co"># New fun2, fit extract function</span></span>
<span><span class="va">glance_brm</span> <span class="op">&lt;-</span> <span class="kw">function</span><span class="op">(</span><span class="va">x</span><span class="op">)</span> <span class="op">{</span></span>
<span>  <span class="va">fit2</span> <span class="op">&lt;-</span> <span class="fu">broom</span><span class="fu">::</span><span class="fu"><a href="https://generics.r-lib.org/reference/glance.html" class="external-link">glance</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span></span>
<span>  <span class="va">fit2</span><span class="op">$</span><span class="va">full_model</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/list.html" class="external-link">list</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span>  <span class="co"># add full model</span></span>
<span>  <span class="kw"><a href="https://rdrr.io/r/base/function.html" class="external-link">return</a></span><span class="op">(</span><span class="va">fit2</span><span class="op">)</span></span>
<span><span class="op">}</span></span>
<span></span>
<span><span class="co"># Setting up specifications</span></span>
<span><span class="va">specs</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/setup.html">setup</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">example_data</span>,</span>
<span>              x <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">"x1"</span>, <span class="st">"x2"</span>, <span class="st">"x3"</span><span class="op">)</span>,</span>
<span>              y <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">"y1"</span>, <span class="st">"y2"</span><span class="op">)</span>,</span>
<span>              model <span class="op">=</span> <span class="st">"brm_new"</span>,</span>
<span>              controls <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">"c1"</span>, <span class="st">"c2"</span><span class="op">)</span>,</span>
<span>              fun2 <span class="op">=</span> <span class="va">glance_brm</span><span class="op">)</span></span>
<span><span class="co"># Check specs</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/summary.html" class="external-link">summary</a></span><span class="op">(</span><span class="va">specs</span><span class="op">)</span></span>
<span><span class="co">#&gt; Setup for the Specification Curve Analysis</span></span>
<span><span class="co">#&gt; -------------------------------------------</span></span>
<span><span class="co">#&gt; Class:                      specr.setup -- version: 1.0.0 </span></span>
<span><span class="co">#&gt; Number of specifications:   24 </span></span>
<span><span class="co">#&gt; </span></span>
<span><span class="co">#&gt; Specifications:</span></span>
<span><span class="co">#&gt; </span></span>
<span><span class="co">#&gt;   Independent variable:     x1, x2, x3 </span></span>
<span><span class="co">#&gt;   Dependent variable:       y1, y2 </span></span>
<span><span class="co">#&gt;   Models:                   brm_new </span></span>
<span><span class="co">#&gt;   Covariates:               no covariates, c1, c2, c1 + c2 </span></span>
<span><span class="co">#&gt;   Subsets analyses:         all </span></span>
<span><span class="co">#&gt; </span></span>
<span><span class="co">#&gt; Function used to extract parameters:</span></span>
<span><span class="co">#&gt; </span></span>
<span><span class="co">#&gt;   function(x) broom::tidy(x, conf.int = TRUE)</span></span>
<span><span class="co">#&gt; &lt;environment: 0x7f9c7371bad0&gt;</span></span>
<span><span class="co">#&gt; </span></span>
<span><span class="co">#&gt; </span></span>
<span><span class="co">#&gt; Head of specifications table (first 6 rows):</span></span>
<span><span class="co">#&gt; # A tibble: 6 × 6</span></span>
<span><span class="co">#&gt;   x     y     model   controls      subsets formula          </span></span>
<span><span class="co">#&gt;   &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;   &lt;chr&gt;         &lt;chr&gt;   &lt;glue&gt;           </span></span>
<span><span class="co">#&gt; 1 x1    y1    brm_new no covariates all     y1 ~ x1 + 1      </span></span>
<span><span class="co">#&gt; 2 x1    y1    brm_new c1            all     y1 ~ x1 + c1     </span></span>
<span><span class="co">#&gt; 3 x1    y1    brm_new c2            all     y1 ~ x1 + c2     </span></span>
<span><span class="co">#&gt; 4 x1    y1    brm_new c1 + c2       all     y1 ~ x1 + c1 + c2</span></span>
<span><span class="co">#&gt; 5 x1    y2    brm_new no covariates all     y2 ~ x1 + 1      </span></span>
<span><span class="co">#&gt; 6 x1    y2    brm_new c1            all     y2 ~ x1 + c1</span></span></code></pre></div>
</div>
<div class="section level2">
<h2 id="estimating-the-models">Estimating the models<a class="anchor" aria-label="anchor" href="#estimating-the-models"></a>
</h2>
<p>Because we are estimating 24 Bayesian models, this may take a while,
but, again, brms automatically parallelizes the computations if we do
not</p>
<div class="sourceCode" id="cb3"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">results</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/specr.html">specr</a></span><span class="op">(</span><span class="va">specs</span><span class="op">)</span></span></code></pre></div>
<p>Now we can again summarize and plot our results.</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/base/summary.html" class="external-link">summary</a></span><span class="op">(</span><span class="va">results</span><span class="op">)</span></span>
<span><span class="co">#&gt; Results of the specification curve analysis</span></span>
<span><span class="co">#&gt; -------------------</span></span>
<span><span class="co">#&gt; Technical details:</span></span>
<span><span class="co">#&gt; </span></span>
<span><span class="co">#&gt;   Class:                          specr.object -- version: 1.0.0 </span></span>
<span><span class="co">#&gt;   Cores used:                     1 </span></span>
<span><span class="co">#&gt;   Duration of fitting process:    9.25 sec elapsed </span></span>
<span><span class="co">#&gt;   Number of specifications:       24 </span></span>
<span><span class="co">#&gt; </span></span>
<span><span class="co">#&gt; Descriptive summary of the specification curve:</span></span>
<span><span class="co">#&gt; </span></span>
<span><span class="co">#&gt;  median  mad   min  max   q25  q75</span></span>
<span><span class="co">#&gt;    0.16 0.25 -0.34 0.62 -0.07 0.25</span></span>
<span><span class="co">#&gt; </span></span>
<span><span class="co">#&gt; Descriptive summary of sample sizes: </span></span>
<span><span class="co">#&gt; </span></span>
<span><span class="co">#&gt;  median  min  max</span></span>
<span><span class="co">#&gt;    1000 1000 1000</span></span>
<span><span class="co">#&gt; </span></span>
<span><span class="co">#&gt; Head of the specification results (first 6 rows): </span></span>
<span><span class="co">#&gt; </span></span>
<span><span class="co">#&gt; # A tibble: 6 × 18</span></span>
<span><span class="co">#&gt;   x     y     model   controls      subsets formula      effect component group estimate std.error </span></span>
<span><span class="co">#&gt;   &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;   &lt;chr&gt;         &lt;chr&gt;   &lt;glue&gt;       &lt;chr&gt;  &lt;chr&gt;     &lt;chr&gt;    &lt;dbl&gt;     &lt;dbl&gt; </span></span>
<span><span class="co">#&gt; 1 x1    y1    brm_new no covariates all     y1 ~ x1 + 1  fixed  cond      &lt;NA&gt;      0.62      0.04 </span></span>
<span><span class="co">#&gt; 2 x1    y1    brm_new c1            all     y1 ~ x1 + c1 fixed  cond      &lt;NA&gt;      0.6       0.04 </span></span>
<span><span class="co">#&gt; 3 x1    y1    brm_new c2            all     y1 ~ x1 + c2 fixed  cond      &lt;NA&gt;      0.61      0.04 </span></span>
<span><span class="co">#&gt; 4 x1    y1    brm_new c1 + c2       all     y1 ~ x1 + c… fixed  cond      &lt;NA&gt;      0.59      0.04 </span></span>
<span><span class="co">#&gt; 5 x1    y2    brm_new no covariates all     y2 ~ x1 + 1  fixed  cond      &lt;NA&gt;     -0.33      0.04 </span></span>
<span><span class="co">#&gt; 6 x1    y2    brm_new c1            all     y2 ~ x1 + c1 fixed  cond      &lt;NA&gt;     -0.34      0.04 </span></span>
<span><span class="co">#&gt; # … with 7 more variable: conf.low &lt;dbl&gt;, conf.high &lt;dbl&gt;, fit_algorithm &lt;chr&gt;, fit_pss &lt;dbl&gt;</span></span>
<span><span class="co">#&gt; #   fit_nobs &lt;dbl&gt;, fit_sigma &lt;dbl&gt;, fit_full_model &lt;list&gt;</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/graphics/plot.default.html" class="external-link">plot</a></span><span class="op">(</span><span class="va">results</span>, choices <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">"x"</span>, <span class="st">"y"</span>, <span class="st">"controls"</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><img src="https://raw.githubusercontent.com/masurp/specr/master/docs/articles/parallel-bayesian-models_files/figure-html/unnamed-chunk-4-1.png" title="plot of chunk unnamed-chunk-4" alt="plot of chunk unnamed-chunk-4" width="100%"></p>
<p>Again, the coefficients are median estimates from the posterior and
their respective 95% HDIs.</p>
</div>
<div class="section level2">
<h2 id="inspecting-specific-models">Inspecting specific models<a class="anchor" aria-label="anchor" href="#inspecting-specific-models"></a>
</h2>
<p>Because we kept the entire brms models in our result data set, we can
explore specific models using the <code>pull</code> function.</p>
<div class="sourceCode" id="cb5"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># Pull entire models from the listed vector</span></span>
<span><span class="va">models</span> <span class="op">&lt;-</span> <span class="va">results</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="va">as_tibble</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/pull.html" class="external-link">pull</a></span><span class="op">(</span><span class="va">fit_full_model</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Summarize e.g., the first model</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/summary.html" class="external-link">summary</a></span><span class="op">(</span><span class="va">models</span><span class="op">[[</span><span class="fl">1</span><span class="op">]</span><span class="op">]</span><span class="op">)</span></span>
<span><span class="co">#&gt;  Family: gaussian </span></span>
<span><span class="co">#&gt;   Links: mu = identity; sigma = identity </span></span>
<span><span class="co">#&gt; Formula: y1 ~ x1 + 1 </span></span>
<span><span class="co">#&gt;    Data: data (Number of observations: 1000) </span></span>
<span><span class="co">#&gt;   Draws: 4 chains, each with iter = 1000; warmup = 500; thin = 1;</span></span>
<span><span class="co">#&gt;          total post-warmup draws = 2000</span></span>
<span><span class="co">#&gt; </span></span>
<span><span class="co">#&gt; Population-Level Effects: </span></span>
<span><span class="co">#&gt;           Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS</span></span>
<span><span class="co">#&gt; Intercept    -1.10      0.04    -1.17    -1.02 1.00     1915     1356</span></span>
<span><span class="co">#&gt; x1            0.62      0.04     0.55     0.69 1.00     1900     1631</span></span>
<span><span class="co">#&gt; </span></span>
<span><span class="co">#&gt; Family Specific Parameters: </span></span>
<span><span class="co">#&gt;       Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS</span></span>
<span><span class="co">#&gt; sigma     1.14      0.03     1.09     1.18 1.00     2903     1542</span></span>
<span><span class="co">#&gt; </span></span>
<span><span class="co">#&gt; Draws were sampled using sampling(NUTS). For each parameter, Bulk_ESS</span></span>
<span><span class="co">#&gt; and Tail_ESS are effective sample size measures, and Rhat is the potential</span></span>
<span><span class="co">#&gt; scale reduction factor on split chains (at convergence, Rhat = 1).</span></span></code></pre></div>
<p>From these model fit objects, we can also extract posterior
distributions for the parameters of interest and stort them in one data
frame.</p>
<div class="sourceCode" id="cb6"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">posteriors</span> <span class="op">&lt;-</span> <span class="va">results</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="va">as_tibble</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/pull.html" class="external-link">pull</a></span><span class="op">(</span><span class="va">fit_full_model</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://purrr.tidyverse.org/reference/map.html" class="external-link">map</a></span><span class="op">(</span><span class="kw">function</span><span class="op">(</span><span class="va">x</span><span class="op">)</span> <span class="fu"><a href="https://tibble.tidyverse.org/reference/as_tibble.html" class="external-link">as_tibble</a></span><span class="op">(</span><span class="fu"><a href="https://mc-stan.org/posterior/reference/draws_df.html" class="external-link">as_draws_df</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span><span class="op">)</span><span class="op">[</span>, <span class="fl">2</span><span class="op">]</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>        <span class="fu"><a href="https://tidyr.tidyverse.org/reference/gather.html" class="external-link">gather</a></span><span class="op">(</span><span class="va">key</span>, <span class="va">value</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/bind_rows.html" class="external-link">bind_rows</a></span><span class="op">(</span><span class="va">.</span>, .id <span class="op">=</span> <span class="st">"id"</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># First 10 draws from the first specification for predictor `x2`</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">posteriors</span>, n <span class="op">=</span> <span class="fl">10</span><span class="op">)</span></span>
<span><span class="co">#&gt; # A tibble: 10 × 3</span></span>
<span><span class="co">#&gt;    id    key   value</span></span>
<span><span class="co">#&gt;    &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt;</span></span>
<span><span class="co">#&gt;  1 1     b_x1  0.642</span></span>
<span><span class="co">#&gt;  2 1     b_x1  0.655</span></span>
<span><span class="co">#&gt;  3 1     b_x1  0.647</span></span>
<span><span class="co">#&gt;  4 1     b_x1  0.650</span></span>
<span><span class="co">#&gt;  5 1     b_x1  0.611</span></span>
<span><span class="co">#&gt;  6 1     b_x1  0.736</span></span>
<span><span class="co">#&gt;  7 1     b_x1  0.565</span></span>
<span><span class="co">#&gt;  8 1     b_x1  0.624</span></span>
<span><span class="co">#&gt;  9 1     b_x1  0.602</span></span>
<span><span class="co">#&gt; 10 1     b_x1  0.611</span></span></code></pre></div>
</div>
<div class="section level2">
<h2 id="plotting-posterior-distributions-for-all-specifications">Plotting posterior distributions for all specifications<a class="anchor" aria-label="anchor" href="#plotting-posterior-distributions-for-all-specifications"></a>
</h2>
<p>Using the <code>ggridges</code> package, we can plot the posterior
distributions of the different specifications.</p>
<div class="sourceCode" id="cb7"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">posteriors</span> <span 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>id <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html" class="external-link">factor</a></span><span class="op">(</span><span class="va">id</span>, levels <span class="op">=</span> <span class="fl">1</span><span class="op">:</span><span class="fl">24</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">value</span>, y <span class="op">=</span> <span class="va">id</span>, fill <span class="op">=</span> <span class="va">key</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://wilkelab.org/ggridges/reference/geom_density_ridges.html" class="external-link">geom_density_ridges</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_fill_brewer</a></span><span class="op">(</span>palette <span class="op">=</span> <span class="st">"Blues"</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_minimal</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/labs.html" class="external-link">labs</a></span><span class="op">(</span>x <span class="op">=</span> <span class="st">"Posterior"</span>,</span>
<span>       y <span class="op">=</span> <span class="st">"Specifications"</span>,</span>
<span>       fill <span class="op">=</span> <span class="st">""</span><span class="op">)</span></span></code></pre></div>
<p><img src="https://raw.githubusercontent.com/masurp/specr/master/docs/articles/parallel-bayesian-models_files/figure-html/unnamed-chunk-7-1.png" title="plot of chunk unnamed-chunk-7" alt="plot of chunk unnamed-chunk-7" width="100%"></p>
<p>Of course, the posterior distributions are not sorted in any way.
With a bit of data wrangling, we can create a specification curve
consisting of posterior distributions.</p>
<div class="sourceCode" id="cb8"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># First panel</span></span>
<span><span class="va">p1</span> <span class="op">&lt;-</span> <span class="va">results</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="va">as_tibble</span> <span 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>id <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="fl">1</span><span class="op">:</span><span class="fu"><a href="https://rdrr.io/r/base/nrow.html" class="external-link">nrow</a></span><span class="op">(</span><span class="va">.</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/arrange.html" class="external-link">arrange</a></span><span class="op">(</span><span class="va">estimate</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>specifications <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html" class="external-link">factor</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="fl">1</span><span class="op">:</span><span class="fu"><a href="https://rdrr.io/r/base/nrow.html" class="external-link">nrow</a></span><span class="op">(</span><span class="va">.</span><span class="op">)</span><span class="op">)</span>,</span>
<span>                                 levels <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="op">:</span><span class="fl">24</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/mutate-joins.html" class="external-link">left_join</a></span><span class="op">(</span><span class="va">posteriors</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">value</span>, y <span class="op">=</span> <span class="va">specifications</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://wilkelab.org/ggridges/reference/stat_density_ridges.html" class="external-link">stat_density_ridges</a></span><span class="op">(</span>quantile_lines <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span>                      quantiles <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">0.025</span>, <span class="fl">0.975</span><span class="op">)</span>,</span>
<span>                      alpha <span class="op">=</span> <span class="fl">.5</span>,</span>
<span>                      fill <span class="op">=</span> <span class="st">"lightblue"</span>,</span>
<span>                      color <span class="op">=</span> <span class="st">"black"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/coord_flip.html" class="external-link">coord_flip</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/ggtheme.html" class="external-link">theme_minimal</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/labs.html" class="external-link">labs</a></span><span class="op">(</span>x <span class="op">=</span> <span class="st">"Posterior"</span>, y <span class="op">=</span> <span class="st">""</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.text <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>,</span>
<span>        axis.line <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/element.html" class="external-link">element_line</a></span><span class="op">(</span><span class="st">"black"</span>, size <span class="op">=</span> <span class="fl">.5</span><span class="op">)</span>,</span>
<span>        legend.position <span class="op">=</span> <span class="st">"none"</span>,</span>
<span>        panel.spacing <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/grid/unit.html" class="external-link">unit</a></span><span class="op">(</span><span class="fl">.75</span>, <span class="st">"lines"</span><span class="op">)</span>,</span>
<span>        axis.text <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/element.html" class="external-link">element_text</a></span><span class="op">(</span>colour <span class="op">=</span> <span class="st">"black"</span><span class="op">)</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Second panel</span></span>
<span><span class="va">p2</span> <span class="op">&lt;-</span> <span class="va">results</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="va">as_tibble</span> <span 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/arrange.html" class="external-link">arrange</a></span><span class="op">(</span><span class="va">estimate</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>specifications <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html" class="external-link">factor</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="fl">1</span><span class="op">:</span><span class="fu"><a href="https://rdrr.io/r/base/nrow.html" class="external-link">nrow</a></span><span class="op">(</span><span class="va">.</span><span class="op">)</span><span class="op">)</span>,</span>
<span>                                 levels <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="op">:</span><span class="fl">24</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://tidyr.tidyverse.org/reference/gather.html" class="external-link">gather</a></span><span class="op">(</span><span class="va">key</span>, <span class="va">value</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">"x"</span>, <span class="st">"y"</span>, <span class="st">"controls"</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>key <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html" class="external-link">factor</a></span><span class="op">(</span><span class="va">key</span>, levels <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">"x"</span>, <span class="st">"y"</span>, <span class="st">"controls"</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://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</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_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">specifications</span>,</span>
<span>                 y <span class="op">=</span> <span class="va">value</span><span class="op">)</span>,</span>
<span>             shape <span class="op">=</span> <span class="fl">124</span>,</span>
<span>             size <span class="op">=</span> <span class="fl">3.35</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_minimal</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">.data</span><span class="op">$</span><span class="va">key</span><span class="op">~</span><span class="fl">1</span>, scales <span class="op">=</span> <span class="st">"free_y"</span>, space <span class="op">=</span> <span class="st">"free_y"</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></span>
<span>    axis.line <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/element.html" class="external-link">element_line</a></span><span class="op">(</span><span class="st">"black"</span>, size <span class="op">=</span> <span class="fl">.5</span><span class="op">)</span>,</span>
<span>    legend.position <span class="op">=</span> <span class="st">"none"</span>,</span>
<span>    panel.spacing <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/grid/unit.html" class="external-link">unit</a></span><span class="op">(</span><span class="fl">.75</span>, <span class="st">"lines"</span><span class="op">)</span>,</span>
<span>    axis.text <span class="op">=</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/element.html" class="external-link">element_text</a></span><span class="op">(</span>colour <span class="op">=</span> <span class="st">"black"</span><span class="op">)</span>,</span>
<span>    strip.text.x <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><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">labs</a></span><span class="op">(</span>x <span class="op">=</span> <span class="st">""</span>, y <span class="op">=</span> <span class="st">""</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Combine</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><span class="va">p1</span>, <span class="va">p2</span>,</span>
<span>          ncol <span class="op">=</span> <span class="fl">1</span>,</span>
<span>          align <span class="op">=</span> <span class="st">"hv"</span>,</span>
<span>          axis <span class="op">=</span> <span class="st">"tblr"</span>,</span>
<span>          rel_heights <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">3</span>, <span class="fl">2</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><img src="https://raw.githubusercontent.com/masurp/specr/master/docs/articles/parallel-bayesian-models_files/figure-html/unnamed-chunk-8-1.png" title="plot of chunk unnamed-chunk-8" alt="plot of chunk unnamed-chunk-8" width="100%"></p>
</div>
  </div>

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

        <nav id="toc" data-toggle="toc"><h2 data-toc-skip>Contents</h2>
    </nav>
</div>

</div>



      <footer><div class="copyright">
  <p></p>
<p>Developed by Philipp K. Masur, Michael Scharkow.</p>
</div>

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

      </footer>
</div>

  


  

  </body>
</html>
