<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Stat&#39;s What It&#39;s All About</title>
<link>https://blog.msbstats.info/</link>
<atom:link href="https://blog.msbstats.info/index.xml" rel="self" type="application/rss+xml"/>
<description></description>
<generator>quarto-1.7.33</generator>
<lastBuildDate>Sun, 14 Sep 2025 21:00:00 GMT</lastBuildDate>
<item>
  <title>Exploring {ggplot2}’s Geoms and Stats</title>
  <link>https://blog.msbstats.info/posts/2025-09-16-exploring-ggplot2/</link>
  <description><![CDATA[ 






<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(patchwork)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gt)</span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># sysfonts::font_add_google(name = "fira code")</span></span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># showtext::showtext_auto()</span></span>
<span id="cb1-7"></span>
<span id="cb1-8">knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>opts_chunk<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">collapse =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">comment =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#&gt;"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dev =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ragg_png"</span>)</span></code></pre></div>
</details>
</div>
<p>In celebration of the release of <code>{ggplot2}</code> 4.0.0 🥳, I wanted to explore the relationships between the geometric objects (“geoms”) and statistical transformations (“stats”) that are offered by the core <code>{ggplot2}</code> functions.</p>
<section id="geoms-and-what" class="level1">
<h1>Geoms and What?</h1>
<p>Within <a href="https://vita.had.co.nz/papers/layered-grammar.pdf"><em>The Layered Grammar of Graphics</em></a> framework, plots in <code>{ggplot2}</code> are built by adding <a href="https://ggplot2.tidyverse.org/reference/layer.html"><code>layer()</code></a>s. Each <code>layer()</code> consists of a geom (the actual thing being drawn), with different geoms having different <code>aes()</code>thetics that can (and should) be mapped to variables in the data.</p>
<p>However, all <code>layer()</code>s <em>also</em> have a stat - this can be thought of as a function applied to the data, transforming it in some way before it is passed to the geom and its aesthetics.</p>
<p>For example, <code>geom_histogram()</code> uses the <code>"bin"</code> stat by default:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>stat</span>
<span id="cb2-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; &lt;ggproto object: Class StatBin, Stat, gg&gt;</span></span>
<span id="cb2-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     aesthetics: function</span></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     compute_group: function</span></span>
<span id="cb2-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     compute_layer: function</span></span>
<span id="cb2-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     compute_panel: function</span></span>
<span id="cb2-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     default_aes: ggplot2::mapping, uneval, gg, S7_object</span></span>
<span id="cb2-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     dropped_aes: weight</span></span>
<span id="cb2-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     extra_params: na.rm orientation</span></span>
<span id="cb2-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     finish_layer: function</span></span>
<span id="cb2-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     non_missing_aes: </span></span>
<span id="cb2-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     optional_aes: </span></span>
<span id="cb2-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     parameters: function</span></span>
<span id="cb2-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     required_aes: x|y</span></span>
<span id="cb2-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     retransform: TRUE</span></span>
<span id="cb2-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     setup_data: function</span></span>
<span id="cb2-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     setup_params: function</span></span>
<span id="cb2-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     super:  &lt;ggproto object: Class Stat, gg&gt;</span></span></code></pre></div>
</div>
<p>This stat takes the raw data, counts the number of occurrences of each value of the <code>x</code> aesthetic within each x-bin.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">p1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(mpg, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(hwy)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bins =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>)</span>
<span id="cb3-3"></span>
<span id="cb3-4">p1</span></code></pre></div>
<div class="cell-output-display">
<div id="fig-histogram" class="quarto-float quarto-figure quarto-figure-center anchored" width="672">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-histogram-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://blog.msbstats.info/posts/2025-09-16-exploring-ggplot2/index_files/figure-html/fig-histogram-1.png" id="fig-histogram" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig quarto-uncaptioned" id="fig-histogram-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1
</figcaption>
</figure>
</div>
</div>
</div>
<p>We can see the product of this transformation by using the <code>layer_data()</code> function, which extracts the data after it has been transformed by the stat:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># access the data from the first (and only) layer</span></span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">layer_data</span>(p1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">i =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># see computed variables:</span></span>
<span id="cb4-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(x, count, density, ncount, ndensity, width)</span>
<span id="cb4-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;           x count     density     ncount   ndensity width</span></span>
<span id="cb4-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1  12.00000     5 0.006009615 0.07352941 0.07352941   0.9</span></span>
<span id="cb4-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 2  15.55556    50 0.060096154 0.73529412 0.73529412   0.9</span></span>
<span id="cb4-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 3  19.11111    34 0.040865385 0.50000000 0.50000000   0.9</span></span>
<span id="cb4-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 4  22.66667    29 0.034855769 0.42647059 0.42647059   0.9</span></span>
<span id="cb4-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 5  26.22222    68 0.081730769 1.00000000 1.00000000   0.9</span></span>
<span id="cb4-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 6  29.77778    33 0.039663462 0.48529412 0.48529412   0.9</span></span>
<span id="cb4-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 7  33.33333     9 0.010817308 0.13235294 0.13235294   0.9</span></span>
<span id="cb4-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 8  36.88889     3 0.003605769 0.04411765 0.04411765   0.9</span></span>
<span id="cb4-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 9  40.44444     1 0.001201923 0.01470588 0.01470588   0.9</span></span>
<span id="cb4-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 10 44.00000     2 0.002403846 0.02941176 0.02941176   0.9</span></span></code></pre></div>
</div>
<p>We can see that this data has been transformed, and reflects the underlying data represented in the final plot in Figure&nbsp;1: 10 columns, their x location, and their heights (<code>count</code>).<sup>1</sup></p>
<hr>
<p>Let’s take a deeper look at the geoms and stats in <code>{ggplot2}</code> and how they relate to each other.</p>
<div class="cell">
<details class="code-fold">
<summary>Data collection</summary>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># list all functions in ggplot2 that start with geom_ or stat_:</span></span>
<span id="cb5-2">get_statgeom <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(fname) {</span>
<span id="cb5-3">  layer <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">match.fun</span>(fname)()</span>
<span id="cb5-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb5-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">class</span>(layer<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>stat)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb5-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">geom =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">class</span>(layer<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>geom)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb5-7">  )</span>
<span id="cb5-8">}</span>
<span id="cb5-9"></span>
<span id="cb5-10">has_statgeom.args <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(fname) {</span>
<span id="cb5-11">  frmls <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">formals</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">match.fun</span>(fname))</span>
<span id="cb5-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb5-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">has_stat_arg =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"stat"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(frmls),</span>
<span id="cb5-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">has_geom_arg =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"geom"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(frmls)</span>
<span id="cb5-15">  )</span>
<span id="cb5-16">}</span>
<span id="cb5-17"></span>
<span id="cb5-18">ggdata <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb5-19">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ggfunction =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ls</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"package:ggplot2"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pattern =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^(geom|stat)_"</span>)</span>
<span id="cb5-20">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(</span>
<span id="cb5-22">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(ggfunction, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"(bin2d|density2d|binhex|summary2d)"</span>),</span>
<span id="cb5-23">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>ggfunction <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"geom_errobarh"</span>)</span>
<span id="cb5-24">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb5-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb5-26">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_when</span>(</span>
<span id="cb5-27">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_starts</span>(ggfunction, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"geom_"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"geom"</span>,</span>
<span id="cb5-28">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_starts</span>(ggfunction, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"stat_"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"stat"</span>,</span>
<span id="cb5-29">      <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_character_</span></span>
<span id="cb5-30">    ),</span>
<span id="cb5-31">    </span>
<span id="cb5-32">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">help_page =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(ggfunction, help) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-33">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(as.character) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-34">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_chr</span>(str_extract, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"(?&lt;=help/).*"</span>),</span>
<span id="cb5-35">    </span>
<span id="cb5-36">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(ggfunction, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">possibly</span>(get_statgeom, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-37">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_rows</span>(),</span>
<span id="cb5-38">    </span>
<span id="cb5-39">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(ggfunction, has_statgeom.args) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-40">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_rows</span>()</span>
<span id="cb5-41">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-42">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">NA.</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-43">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(stat) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(geom)), <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>help_page <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ggsf"</span>))</span></code></pre></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Plot geom-stat combinations</summary>
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">ggdata_tidy <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ggdata <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb6-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_remove</span>(stat, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Stat"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_to_lower</span>(),</span>
<span id="cb6-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">geom =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_remove</span>(geom, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Geom"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_to_lower</span>(),</span>
<span id="cb6-5">    </span>
<span id="cb6-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fct_reorder</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(stat), stat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.fun =</span> length),</span>
<span id="cb6-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">geom =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fct_reorder</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(geom), stat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.fun =</span> length)</span>
<span id="cb6-8">  )</span>
<span id="cb6-9"></span>
<span id="cb6-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(ggdata_tidy, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(geom, stat)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_discrete</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> rev, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">guide =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">guide_axis</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fira code"</span>))</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2025-09-16-exploring-ggplot2/index_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>We can see that the most common stat by far is the <em>identity stat</em>. What’s that about?</p>
</section>
<section id="the-identity-stat-and-the-basic-geom-builing-blocks" class="level1">
<h1>The Identity Stat and the Basic Geom Builing Blocks</h1>
<p>The <em>identity</em> stat is a stat that does nothing.<sup>2</sup> It takes the data as-is and passes it to the geom’s aesthetics.</p>
<p>Geoms that use the identity stat can be thought of a the most basic building blocks of <code>{ggplot2}</code> - many types of lines, bars/tiles, points, areas, etc. These are the geoms you’d probably use with <code>annotate()</code>.<sup>3</sup> We can see such geoms are often used with other stats as well:</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">ggdata <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"StatIdentity"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> stat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.by =</span> geom) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb7-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_remove</span>(stat, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Stat"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_to_lower</span>(),</span>
<span id="cb7-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">geom =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_remove</span>(geom, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Geom"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_to_lower</span>(),</span>
<span id="cb7-6">    </span>
<span id="cb7-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">is_identity =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(stat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"other"</span>),</span>
<span id="cb7-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">geom =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fct_reorder</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(geom), geom, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.fun =</span> length)</span>
<span id="cb7-9">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(stat, geom, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> geom)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(is_identity), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_x"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">space =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_x"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(</span>
<span id="cb7-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>,</span>
<span id="cb7-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>,</span>
<span id="cb7-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span></span>
<span id="cb7-16">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-17">  ggrepel<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_label_repel</span>(</span>
<span id="cb7-18">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> ggfunction),</span>
<span id="cb7-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fira code"</span>,</span>
<span id="cb7-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layout =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb7-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">force =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,</span>
<span id="cb7-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max.overlaps =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span></span>
<span id="cb7-23">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb7-25">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fira code"</span>),</span>
<span id="cb7-26">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fira code"</span>)</span>
<span id="cb7-27">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_viridis_d</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">option =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">end =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">begin =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_discrete</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">guide =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">guide_axis</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">guides</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2025-09-16-exploring-ggplot2/index_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>For example, the point geom is used by <code>{ggplot2}</code> together with 7 other non-identity stats.</p>
</section>
<section id="geom-stat-pairs" class="level1">
<h1>Geom-Stat Pairs</h1>
<p><code>{ggplot2}</code> provides many stats that basically can only be used with a specific geom (and vice versa). These geom-stat pairs are almost exclusive to one another - think of a boxplot for example, which is a unique geom that has little meaning without the boxplot stat.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">p2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(mpg, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(class, hwy)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>()</span>
<span id="cb8-3">p2</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2025-09-16-exploring-ggplot2/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"></span>
<span id="cb9-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">layer_data</span>(p2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">i =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb9-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># see computed variables:</span></span>
<span id="cb9-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(x, ymin<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>ymax, width, outliers)</span>
<span id="cb9-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   x ymin lower middle upper ymax width                       outliers</span></span>
<span id="cb9-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1 1   23  24.0   25.0  26.0   26   0.9                               </span></span>
<span id="cb9-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 2 2   23  26.0   27.0  29.0   33   0.9                 35, 37, 35, 44</span></span>
<span id="cb9-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 3 3   23  26.0   27.0  29.0   32   0.9                               </span></span>
<span id="cb9-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 4 4   21  22.0   23.0  24.0   24   0.9                             17</span></span>
<span id="cb9-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 5 5   15  16.0   17.0  18.0   20   0.9                 12, 12, 12, 22</span></span>
<span id="cb9-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 6 6   20  24.5   26.0  30.5   36   0.9                         44, 41</span></span>
<span id="cb9-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 7 7   14  17.0   17.5  19.0   22   0.9 12, 12, 25, 24, 27, 25, 26, 23</span></span></code></pre></div>
</div>
<p>The table below lists all the geom-stat pairs in <code>{ggplot2}</code>:</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">ggdata_pairs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ggdata <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(</span>
<span id="cb10-3">    stat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"StatIdentity"</span>,</span>
<span id="cb10-4">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>help_page <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span></span>
<span id="cb10-5">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb10-6">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"geom_abline"</span>,</span>
<span id="cb10-7">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"geom_linerange"</span>,</span>
<span id="cb10-8">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ggsf"</span>,</span>
<span id="cb10-9">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"stat_summary"</span>,</span>
<span id="cb10-10">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"stat_summary_2d"</span></span>
<span id="cb10-11">      ),</span>
<span id="cb10-12">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>ggfunction <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"geom_col"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"geom_freqpoly"</span>),</span>
<span id="cb10-13">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(ggfunction, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"(bin2d|density2d|binhex)"</span>)</span>
<span id="cb10-14">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.by =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(help_page)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(help_page) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb10-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb10-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_remove</span>(stat, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Stat"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_to_lower</span>(),</span>
<span id="cb10-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">geom =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_remove</span>(geom, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Geom"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_to_lower</span>()</span>
<span id="cb10-20">  )</span>
<span id="cb10-21"></span>
<span id="cb10-22">ggdata_pairs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_wider</span>(</span>
<span id="cb10-24">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_from =</span> type,</span>
<span id="cb10-25">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_from =</span> ggfunction,</span>
<span id="cb10-26">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id_cols =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(help_page, geom, stat),</span>
<span id="cb10-27">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_prefix =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type_"</span></span>
<span id="cb10-28">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(help_page) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fill</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">starts_with</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type_"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.direction =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"downup"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distinct</span>(help_page, type_geom, type_stat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.keep_all =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb10-34">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">help_page =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_when</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> help_page, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.default =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Other topics"</span>),</span>
<span id="cb10-35">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.by =</span> help_page</span>
<span id="cb10-36">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-37">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gt</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">groupname_col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"help_page"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-38">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cols_merge</span>(</span>
<span id="cb10-39">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">columns =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(type_geom, stat),</span>
<span id="cb10-40">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pattern =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'{1}(stat = "{2}")'</span></span>
<span id="cb10-41">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-42">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cols_merge</span>(</span>
<span id="cb10-43">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">columns =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(type_stat, geom),</span>
<span id="cb10-44">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pattern =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'{1}(geom = "{2}")'</span></span>
<span id="cb10-45">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-46">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">opt_table_font</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">font =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">google_font</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Fira Code"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-47">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tab_style</span>(</span>
<span id="cb10-48">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb10-49">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cell_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weight =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>)</span>
<span id="cb10-50">    ),</span>
<span id="cb10-51">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">locations =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cells_column_labels</span>()</span>
<span id="cb10-52">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-53">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tab_style</span>(</span>
<span id="cb10-54">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb10-55">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cell_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weight =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>)</span>
<span id="cb10-56">    ),</span>
<span id="cb10-57">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">locations =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cells_row_groups</span>()</span>
<span id="cb10-58">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-59">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cols_label</span>(</span>
<span id="cb10-60">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type_geom"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"geom_"</span>,</span>
<span id="cb10-61">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type_stat"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"stat_"</span>,</span>
<span id="cb10-62">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"help_page"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Topic"</span></span>
<span id="cb10-63">  )</span></code></pre></div>
</details>
<div class="cell-output-display">
<div id="idketcchyd" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>@import url("https://fonts.googleapis.com/css2?family=Fira+Code:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap");
#idketcchyd table {
  font-family: 'Fira Code', system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#idketcchyd thead, #idketcchyd tbody, #idketcchyd tfoot, #idketcchyd tr, #idketcchyd td, #idketcchyd th {
  border-style: none;
}

#idketcchyd p {
  margin: 0;
  padding: 0;
}

#idketcchyd .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#idketcchyd .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#idketcchyd .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#idketcchyd .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#idketcchyd .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#idketcchyd .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#idketcchyd .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#idketcchyd .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#idketcchyd .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#idketcchyd .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#idketcchyd .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#idketcchyd .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#idketcchyd .gt_spanner_row {
  border-bottom-style: hidden;
}

#idketcchyd .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#idketcchyd .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#idketcchyd .gt_from_md > :first-child {
  margin-top: 0;
}

#idketcchyd .gt_from_md > :last-child {
  margin-bottom: 0;
}

#idketcchyd .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#idketcchyd .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#idketcchyd .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#idketcchyd .gt_row_group_first td {
  border-top-width: 2px;
}

#idketcchyd .gt_row_group_first th {
  border-top-width: 2px;
}

#idketcchyd .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#idketcchyd .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#idketcchyd .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#idketcchyd .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#idketcchyd .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#idketcchyd .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#idketcchyd .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#idketcchyd .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#idketcchyd .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#idketcchyd .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#idketcchyd .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#idketcchyd .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#idketcchyd .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#idketcchyd .gt_left {
  text-align: left;
}

#idketcchyd .gt_center {
  text-align: center;
}

#idketcchyd .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#idketcchyd .gt_font_normal {
  font-weight: normal;
}

#idketcchyd .gt_font_bold {
  font-weight: bold;
}

#idketcchyd .gt_font_italic {
  font-style: italic;
}

#idketcchyd .gt_super {
  font-size: 65%;
}

#idketcchyd .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#idketcchyd .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#idketcchyd .gt_indent_1 {
  text-indent: 5px;
}

#idketcchyd .gt_indent_2 {
  text-indent: 10px;
}

#idketcchyd .gt_indent_3 {
  text-indent: 15px;
}

#idketcchyd .gt_indent_4 {
  text-indent: 20px;
}

#idketcchyd .gt_indent_5 {
  text-indent: 25px;
}

#idketcchyd .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#idketcchyd div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
<thead>
<tr class="gt_col_headings header">
<th id="type_geom" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" style="font-weight: bold" scope="col">geom_</th>
<th id="type_stat" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" style="font-weight: bold" scope="col">stat_</th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="gt_group_heading_row odd">
<td colspan="2" id="Other topics" class="gt_group_heading" data-quarto-table-cell-role="th" style="font-weight: bold" scope="colgroup">Other topics</td>
</tr>
<tr class="gt_row_group_first even">
<td class="gt_row gt_left" headers="Other topics  type_geom">geom_bar(stat = "count")</td>
<td class="gt_row gt_left" headers="Other topics  type_stat">stat_count(geom = "bar")</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="Other topics  type_geom">geom_bin_2d(stat = "bin2d")</td>
<td class="gt_row gt_left" headers="Other topics  type_stat">stat_bin_2d(geom = "bin2d")</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="Other topics  type_geom">geom_boxplot(stat = "boxplot")</td>
<td class="gt_row gt_left" headers="Other topics  type_stat">stat_boxplot(geom = "boxplot")</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="Other topics  type_geom">geom_count(stat = "sum")</td>
<td class="gt_row gt_left" headers="Other topics  type_stat">stat_sum(geom = "point")</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="Other topics  type_geom">geom_density(stat = "density")</td>
<td class="gt_row gt_left" headers="Other topics  type_stat">stat_density(geom = "density")</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="Other topics  type_geom">geom_hex(stat = "binhex")</td>
<td class="gt_row gt_left" headers="Other topics  type_stat">stat_bin_hex(geom = "hex")</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="Other topics  type_geom">geom_histogram(stat = "bin")</td>
<td class="gt_row gt_left" headers="Other topics  type_stat">stat_bin(geom = "bar")</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="Other topics  type_geom">geom_quantile(stat = "quantile")</td>
<td class="gt_row gt_left" headers="Other topics  type_stat">stat_quantile(geom = "quantile")</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="Other topics  type_geom">geom_area(stat = "align")</td>
<td class="gt_row gt_left" headers="Other topics  type_stat">stat_align(geom = "area")</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="Other topics  type_geom">geom_smooth(stat = "smooth")</td>
<td class="gt_row gt_left" headers="Other topics  type_stat">stat_smooth(geom = "smooth")</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="Other topics  type_geom">geom_violin(stat = "ydensity")</td>
<td class="gt_row gt_left" headers="Other topics  type_stat">stat_ydensity(geom = "violin")</td>
</tr>
<tr class="gt_group_heading_row odd">
<td colspan="2" id="geom_contour" class="gt_group_heading" data-quarto-table-cell-role="th" style="font-weight: bold" scope="colgroup">geom_contour</td>
</tr>
<tr class="gt_row_group_first even">
<td class="gt_row gt_left" headers="geom_contour  type_geom">geom_contour(stat = "contour")</td>
<td class="gt_row gt_left" headers="geom_contour  type_stat">stat_contour(geom = "contour")</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="geom_contour  type_geom">geom_contour_filled(stat = "contourfilled")</td>
<td class="gt_row gt_left" headers="geom_contour  type_stat">stat_contour_filled(geom = "contourfilled")</td>
</tr>
<tr class="gt_group_heading_row even">
<td colspan="2" id="geom_density_2d" class="gt_group_heading" data-quarto-table-cell-role="th" style="font-weight: bold" scope="colgroup">geom_density_2d</td>
</tr>
<tr class="gt_row_group_first odd">
<td class="gt_row gt_left" headers="geom_density_2d  type_geom">geom_density_2d(stat = "density2d")</td>
<td class="gt_row gt_left" headers="geom_density_2d  type_stat">stat_density_2d(geom = "density2d")</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="geom_density_2d  type_geom">geom_density_2d_filled(stat = "density2dfilled")</td>
<td class="gt_row gt_left" headers="geom_density_2d  type_stat">stat_density_2d_filled(geom = "density2dfilled")</td>
</tr>
<tr class="gt_group_heading_row odd">
<td colspan="2" id="geom_qq" class="gt_group_heading" data-quarto-table-cell-role="th" style="font-weight: bold" scope="colgroup">geom_qq</td>
</tr>
<tr class="gt_row_group_first even">
<td class="gt_row gt_left" headers="geom_qq  type_geom">geom_qq(stat = "qq")</td>
<td class="gt_row gt_left" headers="geom_qq  type_stat">stat_qq(geom = "point")</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="geom_qq  type_geom">geom_qq_line(stat = "qqline")</td>
<td class="gt_row gt_left" headers="geom_qq  type_stat">stat_qq_line(geom = "abline")</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<p>When using the default <code>geom=</code> and <code>stat=</code> arguments, these pairs are interchangeable - meaning you can swap the <code>stat_</code> function for the <code>geom_</code> function from the table above. For example:</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># g1 &lt;- ggplot(mpg, aes(class))</span></span>
<span id="cb11-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># g1 + geom_bar() + g1 + stat_count()</span></span>
<span id="cb11-3"></span>
<span id="cb11-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># g2 &lt;- ggplot(diamonds, aes(x, y)) + xlim(4, 10) + ylim(4, 10)</span></span>
<span id="cb11-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># g2 + geom_bin_2d() + g2 + stat_bin_2d()</span></span>
<span id="cb11-6"></span>
<span id="cb11-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># g3 &lt;- ggplot(mpg, aes(class, hwy))</span></span>
<span id="cb11-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># g3 + geom_boxplot() + g3 + stat_boxplot()</span></span>
<span id="cb11-9"></span>
<span id="cb11-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># g4 &lt;- ggplot(faithfuld, aes(waiting, eruptions, z = density))</span></span>
<span id="cb11-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># g4 + geom_contour() + g4 + stat_contour()</span></span>
<span id="cb11-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># g4 + geom_contour_filled() + g4 + stat_contour_filled()</span></span>
<span id="cb11-13"></span>
<span id="cb11-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># g5 &lt;- ggplot(mpg, aes(cty, hwy))</span></span>
<span id="cb11-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># g5 + geom_count() + g5 + stat_sum()</span></span>
<span id="cb11-16"></span>
<span id="cb11-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># g7 &lt;- ggplot(faithful, aes(x = eruptions, y = waiting))</span></span>
<span id="cb11-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># g7 + geom_density_2d() + g7 + stat_density_2d()</span></span>
<span id="cb11-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># g7 + geom_density_2d_filled() + g7 + stat_density_2d_filled()</span></span>
<span id="cb11-20"></span>
<span id="cb11-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># g8 &lt;- ggplot(diamonds, aes(carat, price))</span></span>
<span id="cb11-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># g8 + geom_hex() + g8 + stat_bin_hex()</span></span>
<span id="cb11-23"></span>
<span id="cb11-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># g9 &lt;- ggplot(diamonds, aes(carat))</span></span>
<span id="cb11-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># g9 + geom_histogram() + g9 + stat_bin()</span></span>
<span id="cb11-26"></span>
<span id="cb11-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># df &lt;- data.frame(y = rt(200, df = 5))</span></span>
<span id="cb11-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># g10 &lt;- ggplot(df, aes(sample = y))</span></span>
<span id="cb11-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># g10 + stat_qq() + stat_qq_line() + g10 + geom_qq() + geom_qq_line()</span></span>
<span id="cb11-30"></span>
<span id="cb11-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># g11 &lt;- ggplot(mpg, aes(displ, 1 / hwy))</span></span>
<span id="cb11-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># g11 + geom_quantile() + g11 + stat_quantile()</span></span>
<span id="cb11-33"></span>
<span id="cb11-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># huron &lt;- data.frame(year = 1875:1972, level = as.vector(LakeHuron))</span></span>
<span id="cb11-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># g12 &lt;- ggplot(huron, aes(year, level))</span></span>
<span id="cb11-36"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># g12 + geom_area() + g12 + stat_align()</span></span>
<span id="cb11-37"></span>
<span id="cb11-38"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># g13 &lt;- ggplot(mpg, aes(displ, hwy))</span></span>
<span id="cb11-39"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># g13 + geom_smooth() + g13 + stat_smooth()</span></span>
<span id="cb11-40"></span>
<span id="cb11-41">g14 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(mtcars, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(cyl), mpg))</span>
<span id="cb11-42">(g14 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_violin</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggtitle</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"geom_violin()"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-43">  (g14 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_ydensity</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggtitle</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"stat_ydensity()"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span></span>
<span id="cb11-44">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fira code"</span>))</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2025-09-16-exploring-ggplot2/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The only exception is <code>geom/stat_density()</code> for some reason?</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">g6 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(diamonds, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(carat))</span>
<span id="cb12-2">(g6 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb12-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"geom_density()"</span>,</span>
<span id="cb12-4">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'stat = "density", geom = "area"'</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-5">  (g6 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_density</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb12-6">     <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"stat_density()"</span>,</span>
<span id="cb12-7">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'stat = "density", geom = "ribbon"'</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span></span>
<span id="cb12-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fira code"</span>),</span>
<span id="cb12-9">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fira code"</span>))</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2025-09-16-exploring-ggplot2/index_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<hr>
<p>Understanding the relationship between geoms and stats is a key part of mastering <code>{ggplot2}</code>. Building a mental model of how <code>{ggplot2}</code> processes your data – really understanding its underlying <em>grammar</em> – allows you to move away from simply using defaults to confidently customizing your plots. When you understand that a function like <code>geom_bar()</code> is just a shortcut for <code>layer(stat = "count", geom = "bar")</code>, you can start to think about how you might pair different geoms with existing stats, or how to use the different computed variables provided by a stat.</p>
<p>Personally I’m looking forward to experimenting with the new <a href="https://ggplot2.tidyverse.org/reference/stat_manual.html"><code>stat_manual()</code></a>!</p>
<p>So the next time you create a plot, I encourage you to use <code>layer_data()</code> and ask not what the data can do for you, but what <em>you</em> can do to your data!</p>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>And some other computed variables not used by default, but that can be used with <code>aes(... = after_stat(...))</code>.↩︎</p></li>
<li id="fn2"><p>There is also a “does-nothing” geom - <code>geom_blank()</code>, that takes the data and then proceeds to <em>not</em> plot it.↩︎</p></li>
<li id="fn3"><p>I mean… is anyone using <code>annotate(geom = "boxplot")</code>?↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>R</category>
  <category>ggplot2</category>
  <category>code</category>
  <guid>https://blog.msbstats.info/posts/2025-09-16-exploring-ggplot2/</guid>
  <pubDate>Sun, 14 Sep 2025 21:00:00 GMT</pubDate>
</item>
<item>
  <title>Working with Ordinal Ranks in {marginaleffects}</title>
  <link>https://blog.msbstats.info/posts/2025-05-01-ordinal-ranks-with-marginaleffects/</link>
  <description><![CDATA[ 





<p>Given an ordinal regression model, it is relatively easy to get class-wise predictions - the conditional predicted probability of each level of the outcome. However, <a href="https://bsky.app/profile/happonen.bsky.social/post/3lo4fl3nlz22w">often</a>, one might be interested in summarizing effects not on the class-wise probability scale (nor on the latent scale), but instead on the <em>rank</em> scale - the expected ordinal level, expressed as a single number.</p>
<p>The <code>{emmeans}</code> package has the <a href="https://rvlenth.github.io/emmeans/articles/models.html#O"><code>mode = "mean.class"</code></a> that does just this.</p>
<p>Let’s see how we can do this in <code>{marginaleffects}</code> by utilizing the powerful <code>hypothesis=</code> argument.</p>
<section id="fit-a-model" class="level1">
<h1>Fit a model</h1>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(marginaleffects)</span>
<span id="cb1-4"></span>
<span id="cb1-5">bfi <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> psych<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>bfi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb1-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gender =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(gender),</span>
<span id="cb1-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">A1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ordered</span>(A1)</span>
<span id="cb1-9">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>(A1, age, gender)</span>
<span id="cb1-11"></span>
<span id="cb1-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(bfi)</span>
<span id="cb1-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] 2784</span></span>
<span id="cb1-14"></span>
<span id="cb1-15">fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> MASS<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">polr</span>(A1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> gender, </span>
<span id="cb1-16">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> bfi,</span>
<span id="cb1-17">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Hess =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
</div>
</section>
<section id="class-wise-predictions" class="level1">
<h1>Class-Wise Predictions</h1>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">pr1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predictions</span>(fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variables =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gender"</span>)</span>
<span id="cb2-2">pr1</span>
<span id="cb2-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Group Estimate Std. Error     z Pr(&gt;|z|)     S   2.5 % 97.5 %</span></span>
<span id="cb2-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      1   0.1800    0.01136 15.85   &lt;0.001 185.5 0.15777 0.2023</span></span>
<span id="cb2-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      1   0.1886    0.01128 16.71   &lt;0.001 205.9 0.16649 0.2107</span></span>
<span id="cb2-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      1   0.1843    0.01132 16.28   &lt;0.001 195.6 0.16210 0.2065</span></span>
<span id="cb2-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      1   0.1843    0.01132 16.28   &lt;0.001 195.6 0.16210 0.2065</span></span>
<span id="cb2-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      1   0.1843    0.01132 16.28   &lt;0.001 195.6 0.16210 0.2065</span></span>
<span id="cb2-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; --- 33398 rows omitted. See ?print.marginaleffects --- </span></span>
<span id="cb2-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      6   0.0289    0.00335  8.64   &lt;0.001 57.3 0.02234 0.0355</span></span>
<span id="cb2-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      6   0.0231    0.00264  8.78   &lt;0.001 59.1 0.01798 0.0283</span></span>
<span id="cb2-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      6   0.0219    0.00250  8.76   &lt;0.001 58.8 0.01699 0.0268</span></span>
<span id="cb2-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      6   0.0207    0.00238  8.71   &lt;0.001 58.1 0.01604 0.0254</span></span>
<span id="cb2-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      6   0.0121    0.00165  7.35   &lt;0.001 42.2 0.00891 0.0154</span></span>
<span id="cb2-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Type:  probs</span></span>
<span id="cb2-17"></span>
<span id="cb2-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(pr1) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># original data * 2 (counterfactual gender) * 6 (levels of A1)</span></span>
<span id="cb2-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] 33408</span></span></code></pre></div>
</div>
<p>For each observation in the original data frame we now have 12 predictions:<br>
- 2 counterfactual predictions for the two levels of gender, times<br>
- 6 (arguably also counterfactual) predictions for each of the possible outcome levels.</p>
</section>
<section id="sum-scores-mean-rank" class="level1">
<h1>Sum-scores (mean rank)</h1>
<p>We can average the class <em>ranks</em> by their predicted probability to obtain “sum scores”:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ascore_i%20=%20%5Csum_%7Bk=1%7D%5E%7BK%7D%7Bp_%7Bik%7D%5Ctimes%20k%7D%0A"> And we can do this for each row in the counterfactual data frame (marked by the <code>rowidcf</code> variable):</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">sum_score <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) {</span>
<span id="cb3-2">  x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb3-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(rowidcf, group, gender) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb3-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for each counterfactual row and level of gender</span></span>
<span id="cb3-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(</span>
<span id="cb3-6">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">term =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sum score"</span>,</span>
<span id="cb3-7">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(estimate <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>)), </span>
<span id="cb3-8">      </span>
<span id="cb3-9">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.by =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(rowidcf, gender)</span>
<span id="cb3-10">    )</span>
<span id="cb3-11">}</span>
<span id="cb3-12"></span>
<span id="cb3-13">pr2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predictions</span>(fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variables =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gender"</span>, </span>
<span id="cb3-14">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hypothesis =</span> sum_score)</span>
<span id="cb3-15">pr2</span>
<span id="cb3-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb3-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Estimate Std. Error    z Pr(&gt;|z|)   S 2.5 % 97.5 %</span></span>
<span id="cb3-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      3.02     0.0583 51.8   &lt;0.001 Inf  2.90   3.13</span></span>
<span id="cb3-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      2.51     0.0451 55.6   &lt;0.001 Inf  2.42   2.60</span></span>
<span id="cb3-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      2.97     0.0553 53.7   &lt;0.001 Inf  2.86   3.08</span></span>
<span id="cb3-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      2.46     0.0415 59.4   &lt;0.001 Inf  2.38   2.55</span></span>
<span id="cb3-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      3.00     0.0568 52.8   &lt;0.001 Inf  2.88   3.11</span></span>
<span id="cb3-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; --- 5558 rows omitted. See ?print.marginaleffects --- </span></span>
<span id="cb3-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      2.24     0.0305 73.4   &lt;0.001   Inf  2.18   2.30</span></span>
<span id="cb3-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      2.67     0.0473 56.4   &lt;0.001   Inf  2.57   2.76</span></span>
<span id="cb3-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      2.20     0.0304 72.2   &lt;0.001   Inf  2.14   2.26</span></span>
<span id="cb3-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      2.26     0.0645 35.0   &lt;0.001 890.8  2.13   2.38</span></span>
<span id="cb3-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      1.85     0.0457 40.6   &lt;0.001   Inf  1.77   1.94</span></span>
<span id="cb3-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Term: sum score</span></span>
<span id="cb3-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Type:  probs</span></span>
<span id="cb3-31"></span>
<span id="cb3-32"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(pr2) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># original data * 2 (counterfactual gender)</span></span>
<span id="cb3-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] 5568</span></span></code></pre></div>
</div>
<p>As expected, we now have 2 predictions for each observation in the original data frame: - 2 counterfactual predictions for the two levels of gender</p>
<p>Note that we could have also computed the mean rank for the two levels of gender, or literally anything else. But here I am trying to mimic the basic behavior of the <code>avg_/comparisons()</code> functions by keeping with the workflow:</p>
<ol type="1">
<li>Compute observation wise counterfactual predictions</li>
<li>Contrast them for each observation</li>
<li>(Possibly) average them</li>
</ol>
<p>So let’s get those contrasts!</p>
<section id="contrast-the-ranks" class="level2">
<h2 class="anchored" data-anchor-id="contrast-the-ranks">Contrast the ranks</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">sum_score.contr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) {</span>
<span id="cb4-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># same as before, and...</span></span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum_score</span>(x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb4-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># compute a single contrast for each counterfactual row</span></span>
<span id="cb4-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(</span>
<span id="cb4-6">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">term =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gender (2-1)"</span>,</span>
<span id="cb4-7">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> estimate[gender <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> estimate[gender <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>], </span>
<span id="cb4-8">      </span>
<span id="cb4-9">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.by =</span> rowidcf</span>
<span id="cb4-10">    )</span>
<span id="cb4-11">}</span></code></pre></div>
</div>
<p>This function will compute a difference between the rank for each gender <em>for each</em> observation.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">pr3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predictions</span>(fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variables =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gender"</span>, </span>
<span id="cb5-2">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hypothesis =</span> sum_score.contr)</span>
<span id="cb5-3">pr3</span>
<span id="cb5-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb5-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Estimate Std. Error     z Pr(&gt;|z|)    S  2.5 % 97.5 %</span></span>
<span id="cb5-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    -0.511     0.0588 -8.68   &lt;0.001 57.8 -0.626 -0.395</span></span>
<span id="cb5-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    -0.506     0.0584 -8.67   &lt;0.001 57.7 -0.621 -0.392</span></span>
<span id="cb5-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    -0.509     0.0586 -8.68   &lt;0.001 57.8 -0.623 -0.394</span></span>
<span id="cb5-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    -0.509     0.0586 -8.68   &lt;0.001 57.8 -0.623 -0.394</span></span>
<span id="cb5-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    -0.509     0.0586 -8.68   &lt;0.001 57.8 -0.623 -0.394</span></span>
<span id="cb5-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; --- 2774 rows omitted. See ?print.marginaleffects --- </span></span>
<span id="cb5-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    -0.504     0.0582 -8.67   &lt;0.001 57.6 -0.618 -0.390</span></span>
<span id="cb5-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    -0.483     0.0560 -8.62   &lt;0.001 57.1 -0.593 -0.373</span></span>
<span id="cb5-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    -0.477     0.0554 -8.61   &lt;0.001 56.9 -0.586 -0.368</span></span>
<span id="cb5-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    -0.471     0.0548 -8.59   &lt;0.001 56.7 -0.578 -0.363</span></span>
<span id="cb5-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    -0.403     0.0488 -8.26   &lt;0.001 52.6 -0.499 -0.307</span></span>
<span id="cb5-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Term: gender (2-1)</span></span>
<span id="cb5-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Type:  probs</span></span>
<span id="cb5-19"></span>
<span id="cb5-20"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(pr3) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># original data</span></span>
<span id="cb5-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] 2784</span></span></code></pre></div>
</div>
</section>
<section id="average-contrast" class="level2">
<h2 class="anchored" data-anchor-id="average-contrast">Average Contrast</h2>
<p>Finally, we can get the <em>average</em> difference:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># average contrast </span></span>
<span id="cb6-2">avg_sum_score.contr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) {</span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum_score.contr</span>(x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb6-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(</span>
<span id="cb6-5">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">term =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"avg. gender (2-1)"</span>,</span>
<span id="cb6-6">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(estimate)</span>
<span id="cb6-7">    )</span>
<span id="cb6-8">}</span>
<span id="cb6-9"></span>
<span id="cb6-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predictions</span>(fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variables =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gender"</span>, </span>
<span id="cb6-11">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hypothesis =</span> avg_sum_score.contr)</span>
<span id="cb6-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb6-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Estimate Std. Error     z Pr(&gt;|z|)    S  2.5 % 97.5 %</span></span>
<span id="cb6-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    -0.474     0.0551 -8.61   &lt;0.001 56.9 -0.582 -0.366</span></span>
<span id="cb6-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb6-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Term: avg. gender (2-1)</span></span>
<span id="cb6-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Type:  probs</span></span></code></pre></div>
</div>
</section>
</section>
<section id="pomp" class="level1">
<h1>POMP</h1>
<p>POMP (percent of maximum possible) are a for of standardized units for Likert-type items (see <a href="https://solomonkurz.netlify.app/blog/2022-07-18-sum-score-effect-sizes-for-multilevel-bayesian-cumulative-probit-models/#pomp-differences">Solomon Kurz’s excellent blog post</a> for more – better – info).</p>
<p>These are linear transformations of the ranks described above:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0APOMP_i%20=%20100%5Ctimes%20%5Cfrac%7Bscore_i%20-%20%5Ctext%7Bmin%7D%7D%7B%5Ctext%7Bmax%7D%20-%20%5Ctext%7Bmin%7D%7D%0A"> We can obtain POMPs for each row in the counterfactual data frame by further processing the sum-scores:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">POMP <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) {</span>
<span id="cb7-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum_score</span>(x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb7-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb7-4">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (estimate <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb7-5">    )</span>
<span id="cb7-6">}</span>
<span id="cb7-7"></span>
<span id="cb7-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predictions</span>(fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variables =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gender"</span>, </span>
<span id="cb7-9">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hypothesis =</span> POMP)</span>
<span id="cb7-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb7-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Estimate Std. Error    z Pr(&gt;|z|)     S 2.5 % 97.5 %</span></span>
<span id="cb7-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      40.4      1.166 34.6   &lt;0.001 870.6  38.1   42.7</span></span>
<span id="cb7-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      30.2      0.902 33.5   &lt;0.001 812.8  28.4   31.9</span></span>
<span id="cb7-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      39.4      1.107 35.6   &lt;0.001 920.9  37.3   41.6</span></span>
<span id="cb7-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      29.3      0.829 35.3   &lt;0.001 905.5  27.7   30.9</span></span>
<span id="cb7-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      39.9      1.135 35.1   &lt;0.001 896.3  37.7   42.1</span></span>
<span id="cb7-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; --- 5558 rows omitted. See ?print.marginaleffects --- </span></span>
<span id="cb7-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      24.7      0.609 40.6   &lt;0.001   Inf  23.5   25.9</span></span>
<span id="cb7-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      33.3      0.945 35.3   &lt;0.001 902.9  31.5   35.2</span></span>
<span id="cb7-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      23.9      0.608 39.3   &lt;0.001   Inf  22.7   25.1</span></span>
<span id="cb7-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      25.2      1.289 19.5   &lt;0.001 279.4  22.6   27.7</span></span>
<span id="cb7-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      17.1      0.914 18.7   &lt;0.001 256.9  15.3   18.9</span></span>
<span id="cb7-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Term: sum score</span></span>
<span id="cb7-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Type:  probs</span></span></code></pre></div>
</div>
<p>And likewise get contrasts -</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">POMP.contr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) {</span>
<span id="cb8-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">POMP</span>(x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb8-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(</span>
<span id="cb8-4">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">term =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gender (2-1)"</span>,</span>
<span id="cb8-5">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> estimate[gender <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> estimate[gender <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>], </span>
<span id="cb8-6">      </span>
<span id="cb8-7">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.by =</span> rowidcf</span>
<span id="cb8-8">    )</span>
<span id="cb8-9">}</span>
<span id="cb8-10"></span>
<span id="cb8-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predictions</span>(fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variables =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gender"</span>, </span>
<span id="cb8-12">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hypothesis =</span> sum_score.contr)</span>
<span id="cb8-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb8-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Estimate Std. Error     z Pr(&gt;|z|)    S  2.5 % 97.5 %</span></span>
<span id="cb8-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    -0.511     0.0588 -8.68   &lt;0.001 57.8 -0.626 -0.395</span></span>
<span id="cb8-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    -0.506     0.0584 -8.67   &lt;0.001 57.7 -0.621 -0.392</span></span>
<span id="cb8-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    -0.509     0.0586 -8.68   &lt;0.001 57.8 -0.623 -0.394</span></span>
<span id="cb8-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    -0.509     0.0586 -8.68   &lt;0.001 57.8 -0.623 -0.394</span></span>
<span id="cb8-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    -0.509     0.0586 -8.68   &lt;0.001 57.8 -0.623 -0.394</span></span>
<span id="cb8-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; --- 2774 rows omitted. See ?print.marginaleffects --- </span></span>
<span id="cb8-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    -0.504     0.0582 -8.67   &lt;0.001 57.6 -0.618 -0.390</span></span>
<span id="cb8-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    -0.483     0.0560 -8.62   &lt;0.001 57.1 -0.593 -0.373</span></span>
<span id="cb8-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    -0.477     0.0554 -8.61   &lt;0.001 56.9 -0.586 -0.368</span></span>
<span id="cb8-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    -0.471     0.0548 -8.59   &lt;0.001 56.7 -0.578 -0.363</span></span>
<span id="cb8-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    -0.403     0.0488 -8.26   &lt;0.001 52.6 -0.499 -0.307</span></span>
<span id="cb8-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Term: gender (2-1)</span></span>
<span id="cb8-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Type:  probs</span></span></code></pre></div>
</div>
<p>And average contrasts -</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># average contrast </span></span>
<span id="cb9-2">avg_POMP.contr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) {</span>
<span id="cb9-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">POMP.contr</span>(x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb9-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(</span>
<span id="cb9-5">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">term =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"avg. gender (2-1)"</span>,</span>
<span id="cb9-6">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(estimate)</span>
<span id="cb9-7">    )</span>
<span id="cb9-8">}</span>
<span id="cb9-9"></span>
<span id="cb9-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predictions</span>(fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variables =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gender"</span>, </span>
<span id="cb9-11">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hypothesis =</span> avg_POMP.contr)</span>
<span id="cb9-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb9-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Estimate Std. Error     z Pr(&gt;|z|)    S 2.5 % 97.5 %</span></span>
<span id="cb9-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     -9.49        1.1 -8.61   &lt;0.001 56.9 -11.6  -7.33</span></span>
<span id="cb9-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb9-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Term: avg. gender (2-1)</span></span>
<span id="cb9-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Type:  probs</span></span></code></pre></div>
</div>
</section>
<section id="a-note-for-bayesians" class="level1">
<h1>A note for Bayesians</h1>
<p>Hey, Bayesians, how are you doing?</p>
<p>I have some bad news: <code>{marginaleffects}</code> does not support <code>hypothesis=&lt;function&gt;</code>.</p>
<p>But I also have some good news! You can basically do all of this by getting the posterior draws in an <a href="https://mc-stan.org/posterior/articles/rvar.html"><code>rvar</code></a> format, and then directly manipulating the posterior(s) - so the examples above should basically work out of the box.</p>
<p>For example:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(brms)</span>
<span id="cb10-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(posterior)</span>
<span id="cb10-3"></span>
<span id="cb10-4">fit_b <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brm</span>(A1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> gender, </span>
<span id="cb10-5">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> bfi,</span>
<span id="cb10-6">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cumulative</span>(),</span>
<span id="cb10-7">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prior =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># obviously this is a bad prior</span></span>
<span id="cb10-8"></span>
<span id="cb10-9">pr1_b <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predictions</span>(fit_b, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variables =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gender"</span>)</span></code></pre></div>
</div>
<p>We just need to adapt the function(s) written above to work properly with <code>rvar</code>s. I’ve marked</p>
<div class="cell">
<div class="sourceCode cell-code" id="annotated-cell-11" style="background: #f1f3f5;"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><span id="annotated-cell-11-1">avg_POMP.contr_b <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) {</span>
<span id="annotated-cell-11-2">  x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="annotated-cell-11-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(rowidcf, group, gender) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="annotated-cell-11-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for each counterfactual row and level of gender</span></span>
<span id="annotated-cell-11-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(</span>
<span id="annotated-cell-11-6">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">term =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sum score"</span>,</span>
<span id="annotated-cell-11-7">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># estimate = sum(estimate * (1:6)), </span></span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-11" data-target-annotation="1" onclick="event.preventDefault();">1</a><span id="annotated-cell-11-8" class="code-annotation-target">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rvar_sum</span>(rvar <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>)),</span>
<span id="annotated-cell-11-9">      </span>
<span id="annotated-cell-11-10">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.by =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(rowidcf, gender)</span>
<span id="annotated-cell-11-11">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="annotated-cell-11-12">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># POMP</span></span>
<span id="annotated-cell-11-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="annotated-cell-11-14">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (estimate <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="annotated-cell-11-15">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="annotated-cell-11-16">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># contrasts</span></span>
<span id="annotated-cell-11-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(</span>
<span id="annotated-cell-11-18">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">term =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gender (2-1)"</span>,</span>
<span id="annotated-cell-11-19">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> estimate[gender <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> estimate[gender <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>], </span>
<span id="annotated-cell-11-20">      </span>
<span id="annotated-cell-11-21">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.by =</span> rowidcf</span>
<span id="annotated-cell-11-22">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="annotated-cell-11-23">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># average</span></span>
<span id="annotated-cell-11-24">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(</span>
<span id="annotated-cell-11-25">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">term =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"avg. gender (2-1)"</span>,</span>
<span id="annotated-cell-11-26">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># estimate = mean(estimate)</span></span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-11" data-target-annotation="2" onclick="event.preventDefault();">2</a><span id="annotated-cell-11-27" class="code-annotation-target">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rvar_mean</span>(estimate)</span>
<span id="annotated-cell-11-28">    )</span>
<span id="annotated-cell-11-29">}</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div>
<div class="cell-annotation">
<dl class="code-annotation-container-grid">
<dt data-target-cell="annotated-cell-11" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-11" data-code-lines="8" data-code-annotation="1">Use the <code>rvar</code> column and the <code>rvar_sum()</code> function (instead of <code>sum()</code>)</span>
</dd>
<dt data-target-cell="annotated-cell-11" data-target-annotation="2">2</dt>
<dd>
<span data-code-cell="annotated-cell-11" data-code-lines="27" data-code-annotation="2">Use the <code>rvar_mean()</code> function (instead of <code>mean()</code>)</span>
</dd>
</dl>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_draws</span>(pr1_b, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rvar"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">avg_POMP.contr_b</span>()</span>
<span id="cb11-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                term   estimate</span></span>
<span id="cb11-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1 avg. gender (2-1) -9.4 ± 1.1</span></span></code></pre></div>
</div>
<p>Compare this to the frequentists estimate of -9.49, <em>SE</em> = 1.10 we got above.</p>


</section>

 ]]></description>
  <category>R</category>
  <category>statistics</category>
  <category>code</category>
  <category>marginaleffects</category>
  <guid>https://blog.msbstats.info/posts/2025-05-01-ordinal-ranks-with-marginaleffects/</guid>
  <pubDate>Wed, 30 Apr 2025 21:00:00 GMT</pubDate>
</item>
<item>
  <title>Centering in Moderation Analysis: A Guide</title>
  <link>https://blog.msbstats.info/posts/2024-01-21-centering-101/</link>
  <description><![CDATA[ 





<p>This blog post will cover what centering is, what a sum-to-zero contrast is, why and when you should use them, and how you can do them in <strong><code>R</code></strong>. Specifically, we will focus on their use and how they affect coefficient interpretation in regression tables and affect interpretation in ANOVA tables.</p>
<p>I will be using the following packages and versions:</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(datawizard) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0.9.1</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(parameters) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0.21.3 </span></span></code></pre></div>
<p>We will be using the following toy data set:</p>
<details>
<summary>
Generate Toy Dataset
</summary>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb2-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">22.8</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">21.4</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">18.7</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">18.1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">14.3</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">24.4</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">22.8</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">19.2</span>), </span>
<span id="cb2-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">G =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"g2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"g2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"g1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"g2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"g3"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"g2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"g3"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"g1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"g1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"g2"</span>)), </span>
<span id="cb2-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">X =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">160</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">160</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">108</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">258</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">360</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">225</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">360</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">146.7</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">140.8</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">167.6</span>),</span>
<span id="cb2-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Q =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.9</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.9</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.85</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.08</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.15</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.76</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.21</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.69</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.92</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.92</span>), </span>
<span id="cb2-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">A =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a2"</span>))</span>
<span id="cb2-7">)</span></code></pre></div>
</div>
<p>(This is really a re-labeled version of <code>mtcars[1:10, c(1:3, 5, 8)]</code>.)</p>
</details>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str</span>(data)</span>
<span id="cb3-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 'data.frame':    10 obs. of  5 variables:</span></span>
<span id="cb3-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  $ Y: num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2</span></span>
<span id="cb3-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  $ G: Factor w/ 3 levels "g1","g2","g3": 2 2 1 2 3 2 3 1 1 2</span></span>
<span id="cb3-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  $ X: num  160 160 108 258 360 ...</span></span>
<span id="cb3-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  $ Q: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92</span></span>
<span id="cb3-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  $ A: Factor w/ 2 levels "a1","a2": 1 1 2 2 1 2 1 2 2 2</span></span></code></pre></div>
</div>
<section id="simple-regression" class="level1">
<h1>Simple Regression</h1>
<p>We will start with not centering anything.</p>
<section id="continuous-predictor" class="level2">
<h2 class="anchored" data-anchor-id="continuous-predictor">Continuous Predictor</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">model1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X, </span>
<span id="cb4-2">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data)</span></code></pre></div>
</div>
<p>Something that is <em>always</em> true - the <code>(Intercept)</code> represents the predicted values when all the predictors are set to 0.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model_parameters</span>(model1)</span>
<span id="cb5-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Parameter   | Coefficient |       SE |         95% CI |  t(8) |      p</span></span>
<span id="cb5-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ----------------------------------------------------------------------</span></span>
<span id="cb5-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; (Intercept) |       25.56 |     1.62 | [21.82, 29.31] | 15.74 | &lt; .001</span></span>
<span id="cb5-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; X           |       -0.02 | 7.20e-03 | [-0.04, -0.01] | -3.46 | 0.009</span></span>
<span id="cb5-6"></span>
<span id="cb5-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predict for X=0</span></span>
<span id="cb5-8">pred_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">X =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb5-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(model1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> pred_grid)</span>
<span id="cb5-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;       1 </span></span>
<span id="cb5-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 25.5638</span></span></code></pre></div>
</div>
<p>But say we wanted the intercept to represent the predicted value when <img src="https://latex.codecogs.com/png.latex?x_i=%5Cbar%7BX%7D"> - how would we do that? <strong>By centering!</strong></p>
<p>So what is centering?</p>
<blockquote class="blockquote">
<p>Don’t just be a zero…</p>
</blockquote>
<p>Centering is the processes of assigning <em>meaning</em> to the value of 0 of some variable. We do this by:</p>
<ol type="1">
<li>defining a meaningful value</li>
<li><em>Subtracting</em> that value from all values of the variable.</li>
</ol>
<p>For example, I can center <em>X</em> so that 0 is 258.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(</span>
<span id="cb6-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">X =</span> data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>X,</span>
<span id="cb6-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X - 258"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">258</span></span>
<span id="cb6-4">)</span>
<span id="cb6-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;         [,1] [,2] [,3] [,4] [,5] [,6] [,7]   [,8]   [,9] [,10]</span></span>
<span id="cb6-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; X        160  160  108  258  360  225  360  146.7  140.8 167.6</span></span>
<span id="cb6-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; X - 258  -98  -98 -150    0  102  -33  102 -111.3 -117.2 -90.4</span></span></code></pre></div>
</div>
<p>We can see that by subtracting 258 from <em>X</em>, values that were above 258 are positive, values that were below 258 are negative, and the values that were 258 are now <em>0</em>. Thus, for this new variable, when we speak of <em>0</em>, we actually mean “258”.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">model2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">I</span>(X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">258</span>),</span>
<span id="cb7-2">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data)</span>
<span id="cb7-3"></span>
<span id="cb7-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model_parameters</span>(model2)</span>
<span id="cb7-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Parameter   | Coefficient |       SE |         95% CI |  t(8) |      p</span></span>
<span id="cb7-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ----------------------------------------------------------------------</span></span>
<span id="cb7-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; (Intercept) |       19.14 |     0.71 | [17.50, 20.78] | 26.86 | &lt; .001</span></span>
<span id="cb7-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; X - 258     |       -0.02 | 7.20e-03 | [-0.04, -0.01] | -3.46 | 0.009</span></span>
<span id="cb7-9"></span>
<span id="cb7-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predict for X=258</span></span>
<span id="cb7-11">pred_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">X =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">258</span>)</span>
<span id="cb7-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(model1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> pred_grid)</span>
<span id="cb7-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;        1 </span></span>
<span id="cb7-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 19.14033</span></span></code></pre></div>
</div>
<p>Typically, centering is done around the mean (mean-centering) - thus giving <em>0</em> the meaning of “mean”.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">model3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">I</span>(X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(X)),</span>
<span id="cb8-2">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data)</span>
<span id="cb8-3"></span>
<span id="cb8-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model_parameters</span>(model3)</span>
<span id="cb8-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Parameter   | Coefficient |       SE |         95% CI |  t(8) |      p</span></span>
<span id="cb8-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ----------------------------------------------------------------------</span></span>
<span id="cb8-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; (Intercept) |       20.37 |     0.62 | [18.95, 21.79] | 32.99 | &lt; .001</span></span>
<span id="cb8-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; X - mean(X) |       -0.02 | 7.20e-03 | [-0.04, -0.01] | -3.46 | 0.009</span></span>
<span id="cb8-9"></span>
<span id="cb8-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predict for X=mean(X)</span></span>
<span id="cb8-11">pred_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">X =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>X))</span>
<span id="cb8-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(model1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> pred_grid)</span>
<span id="cb8-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     1 </span></span>
<span id="cb8-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 20.37</span></span></code></pre></div>
</div>
<p>We can also do this with the <code>scale()</code> function, but since this function also standardizes (re-scales to and <em>sd</em> of 1), we need to set <code>scale(scale = FALSE)</code>:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">model4 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale</span>(X, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>),</span>
<span id="cb9-2">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data)</span>
<span id="cb9-3"></span>
<span id="cb9-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model_parameters</span>(model4)</span>
<span id="cb9-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Parameter   | Coefficient |       SE |         95% CI |  t(8) |      p</span></span>
<span id="cb9-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ----------------------------------------------------------------------</span></span>
<span id="cb9-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; (Intercept) |       20.37 |     0.62 | [18.95, 21.79] | 32.99 | &lt; .001</span></span>
<span id="cb9-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; X           |       -0.02 | 7.20e-03 | [-0.04, -0.01] | -3.46 | 0.009</span></span>
<span id="cb9-9"></span>
<span id="cb9-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predict for X=mean(X)</span></span>
<span id="cb9-11">pred_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">X =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>X))</span>
<span id="cb9-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(model1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> pred_grid)</span>
<span id="cb9-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     1 </span></span>
<span id="cb9-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 20.37</span></span></code></pre></div>
</div>
<p>Alternatively, we can just use <a href="https://easystats.github.io/datawizard/reference/center.html"><code>datawizard::center()</code></a>:<sup>1</sup></p>
<div class="cell">
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">model5 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">center</span>(X),</span>
<span id="cb10-2">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data)</span>
<span id="cb10-3"></span>
<span id="cb10-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model_parameters</span>(model5)</span>
<span id="cb10-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Parameter   | Coefficient |       SE |         95% CI |  t(8) |      p</span></span>
<span id="cb10-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ----------------------------------------------------------------------</span></span>
<span id="cb10-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; (Intercept) |       20.37 |     0.62 | [18.95, 21.79] | 32.99 | &lt; .001</span></span>
<span id="cb10-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; center(X)   |       -0.02 | 7.20e-03 | [-0.04, -0.01] | -3.46 | 0.009</span></span>
<span id="cb10-9"></span>
<span id="cb10-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predict for X=mean(X)</span></span>
<span id="cb10-11">pred_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">X =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>X))</span>
<span id="cb10-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(model1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> pred_grid)</span>
<span id="cb10-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     1 </span></span>
<span id="cb10-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 20.37</span></span></code></pre></div>
</div>
<p>Note that no matter how we centered <em>X</em>, its coefficient did not change - only the intercept changed!</p>
</section>
<section id="categorical-predictor" class="level2">
<h2 class="anchored" data-anchor-id="categorical-predictor">Categorical Predictor</h2>
<p>When adding categorical predictors (in R we call these “factors”), R converts them to a set of <img src="https://latex.codecogs.com/png.latex?k-1"> variables (<img src="https://latex.codecogs.com/png.latex?k"> being the number of levels in the factor) that represent a set of contrasts between the levels of the variable. For our <code>G</code> factor that has 3 levels, we get two such variables: <code>G[g2]</code> and <code>G[g3]</code>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">model6 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> G,</span>
<span id="cb11-2">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data)</span>
<span id="cb11-3"></span>
<span id="cb11-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model_parameters</span>(model6)</span>
<span id="cb11-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Parameter   | Coefficient |   SE |          95% CI |  t(7) |      p</span></span>
<span id="cb11-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; -------------------------------------------------------------------</span></span>
<span id="cb11-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; (Intercept) |       23.33 | 0.96 | [ 21.05, 25.61] | 24.21 | &lt; .001</span></span>
<span id="cb11-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; G [g2]      |       -3.19 | 1.22 | [ -6.08, -0.31] | -2.62 | 0.034 </span></span>
<span id="cb11-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; G [g3]      |       -6.83 | 1.52 | [-10.44, -3.23] | -4.49 | 0.003</span></span></code></pre></div>
</div>
<p>By default, R uses treatment contrasts (<code>stats::contr.treatment()</code>). These are built up by constructing each variable to be 0 for all but 1 level of the factor, with the 1st level being left out. We can see this by playing with <code>stats::contr.treatment()</code>:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contr.treatment</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>G))</span>
<span id="cb12-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    g2 g3</span></span>
<span id="cb12-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; g1  0  0</span></span>
<span id="cb12-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; g2  1  0</span></span>
<span id="cb12-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; g3  0  1</span></span></code></pre></div>
</div>
<p>The “variable” (called a <em>dummy variable</em>) of <code>g2</code> is only 1 for the level of <code>G=g2</code>, the <em>dummy variable</em> of <code>g3</code> is only 1 for the level of <code>G=g3</code>, while <code>g1</code> is left out - called the “reference level”.</p>
<p>Recall that the <code>(Intercept)</code> always represents the predicted values when all the predictors are set to 0. In this case, setting both <code>g2</code> and <code>g3</code> to 0 is the same as representing <code>G=g1</code>. And indeed, that is what the intercept represents:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model_parameters</span>(model6)</span>
<span id="cb13-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Parameter   | Coefficient |   SE |          95% CI |  t(7) |      p</span></span>
<span id="cb13-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; -------------------------------------------------------------------</span></span>
<span id="cb13-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; (Intercept) |       23.33 | 0.96 | [ 21.05, 25.61] | 24.21 | &lt; .001</span></span>
<span id="cb13-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; G [g2]      |       -3.19 | 1.22 | [ -6.08, -0.31] | -2.62 | 0.034 </span></span>
<span id="cb13-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; G [g3]      |       -6.83 | 1.52 | [-10.44, -3.23] | -4.49 | 0.003</span></span>
<span id="cb13-7"></span>
<span id="cb13-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predict for G=g1</span></span>
<span id="cb13-9">pred_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">G =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"g1"</span>)</span>
<span id="cb13-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(model6, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> pred_grid)</span>
<span id="cb13-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;        1 </span></span>
<span id="cb13-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 23.33333</span></span></code></pre></div>
</div>
<section id="interpreting-dummy-variables" class="level3">
<h3 class="anchored" data-anchor-id="interpreting-dummy-variables">Interpreting Dummy Variables</h3>
<p>The other variables now represent the differences between each level and the reference level. It is hard to see, but we can use some linear algebra to help us here:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">(mm1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(</span>
<span id="cb14-2">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">(Intercept)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb14-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contr.treatment</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>G))</span>
<span id="cb14-4">))</span>
<span id="cb14-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    (Intercept) g2 g3</span></span>
<span id="cb14-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; g1           1  0  0</span></span>
<span id="cb14-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; g2           1  1  0</span></span>
<span id="cb14-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; g3           1  0  1</span></span>
<span id="cb14-9"></span>
<span id="cb14-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">solve</span>(mm1))</span>
<span id="cb14-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    (Intercept) g2 g3</span></span>
<span id="cb14-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; g1           1 -1 -1</span></span>
<span id="cb14-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; g2           0  1  0</span></span>
<span id="cb14-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; g3           0  0  1</span></span></code></pre></div>
</div>
<p>Each column now holds the linear contrast weights represented by that coefficient. We can confirm this:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model_parameters</span>(model6)</span>
<span id="cb15-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Parameter   | Coefficient |   SE |          95% CI |  t(7) |      p</span></span>
<span id="cb15-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; -------------------------------------------------------------------</span></span>
<span id="cb15-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; (Intercept) |       23.33 | 0.96 | [ 21.05, 25.61] | 24.21 | &lt; .001</span></span>
<span id="cb15-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; G [g2]      |       -3.19 | 1.22 | [ -6.08, -0.31] | -2.62 | 0.034 </span></span>
<span id="cb15-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; G [g3]      |       -6.83 | 1.52 | [-10.44, -3.23] | -4.49 | 0.003</span></span>
<span id="cb15-7"></span>
<span id="cb15-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predict for G = g1, g2, g3</span></span>
<span id="cb15-9">pred_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">G =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"g1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"g2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"g3"</span>))</span>
<span id="cb15-10">(predicted_means <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(model6, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> pred_grid))</span>
<span id="cb15-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;        1        2        3 </span></span>
<span id="cb15-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 23.33333 20.14000 16.50000</span></span>
<span id="cb15-13"></span>
<span id="cb15-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Treatment contrasts</span></span>
<span id="cb15-15">predicted_means[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> predicted_means[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb15-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;         2 </span></span>
<span id="cb15-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; -3.193333</span></span>
<span id="cb15-18">predicted_means[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> predicted_means[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb15-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;         3 </span></span>
<span id="cb15-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; -6.833333</span></span></code></pre></div>
</div>
</section>
<section id="sum-to-zero-contrasts" class="level3">
<h3 class="anchored" data-anchor-id="sum-to-zero-contrasts">Sum-to-Zero Contrasts</h3>
<p>But say we wanted the intercept to represent the predicted value that is the grand mean - averaged across all levels of <code>G</code> (<img src="https://latex.codecogs.com/png.latex?%5Cfrac%7B1%7D%7Bk%7D%5Csum%7By%7CG=g_i%7D">) - how would we do that? <strong>By using sum-to-zero contrasts!</strong></p>
<p>Sum-to-zero contrasts, like treatment coding (a.k.a dummy coding) convert a factor to a set of contrasts between the levels of the variable. Unlike dummy coding, the weights of each contrast sum to 0.</p>
<p>There are many coding schemes that achieve this (Helmert contrasts, polynomial contrasts, …). The most popular one is effect coding (in R, confusingly built with <code>stats::contr.sum()</code>).</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contr.sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>G))</span>
<span id="cb16-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    [,1] [,2]</span></span>
<span id="cb16-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; g1    1    0</span></span>
<span id="cb16-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; g2    0    1</span></span>
<span id="cb16-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; g3   -1   -1</span></span></code></pre></div>
</div>
<p>We can see that each contrast (column) sums to 0.</p>
<div class="callout callout-style-simple callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Side note about changing factor coding
</div>
</div>
<div class="callout-body-container callout-body">
<p>There are 3 ways of changing the coding scheme (contrast matrix) of a factor in R:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. assign to the contrasts() function</span></span>
<span id="cb17-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrasts</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>G) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> contr.sum</span>
<span id="cb17-3"></span>
<span id="cb17-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. The C() function (BIG C!)</span></span>
<span id="cb17-5">data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>G <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">C</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>G, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">contr =</span> contr.sum)</span>
<span id="cb17-6"></span>
<span id="cb17-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3. In the model</span></span>
<span id="cb17-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> G,</span>
<span id="cb17-9">   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data,</span>
<span id="cb17-10">   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">contrasts =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">G =</span> contr.sum))</span></code></pre></div>
</div>
<p>These should all produce the same results.</p>
<p>I will be using the 3rd option here for verbosity.</p>
</div>
</div>
<p>Let’s see how this affects our coefficients:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">model7 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> G,</span>
<span id="cb18-2">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data,</span>
<span id="cb18-3">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">contrasts =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">G =</span> contr.sum))</span>
<span id="cb18-4"></span>
<span id="cb18-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model_parameters</span>(model7)</span>
<span id="cb18-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Parameter   | Coefficient |   SE |         95% CI |  t(7) |      p</span></span>
<span id="cb18-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ------------------------------------------------------------------</span></span>
<span id="cb18-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; (Intercept) |       19.99 | 0.57 | [18.65, 21.33] | 35.35 | &lt; .001</span></span>
<span id="cb18-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; G [1]       |        3.34 | 0.79 | [ 1.47,  5.22] |  4.21 | 0.004 </span></span>
<span id="cb18-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; G [2]       |        0.15 | 0.71 | [-1.53,  1.83] |  0.21 | 0.840</span></span>
<span id="cb18-11"></span>
<span id="cb18-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predict for G = g1, g2, g3</span></span>
<span id="cb18-13">pred_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">G =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"g1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"g2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"g3"</span>))</span>
<span id="cb18-14">(predicted_means <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(model6, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> pred_grid))</span>
<span id="cb18-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;        1        2        3 </span></span>
<span id="cb18-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 23.33333 20.14000 16.50000</span></span>
<span id="cb18-17"></span>
<span id="cb18-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Mean prediction</span></span>
<span id="cb18-19"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(predicted_means)</span>
<span id="cb18-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] 19.99111</span></span></code></pre></div>
</div>
<p>Success - the intercept is equal to the grand mean!</p>
<p>What about the other variables? It is hard to see, but we can again use some linear algebra to help us here:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1">(mm2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(</span>
<span id="cb19-2">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">(Intercept)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb19-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contr.sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>G))</span>
<span id="cb19-4">))</span>
<span id="cb19-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    (Intercept)      </span></span>
<span id="cb19-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; g1           1  1  0</span></span>
<span id="cb19-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; g2           1  0  1</span></span>
<span id="cb19-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; g3           1 -1 -1</span></span>
<span id="cb19-9"></span>
<span id="cb19-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">solve</span>(mm2))</span>
<span id="cb19-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    (Intercept)                      </span></span>
<span id="cb19-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; g1   0.3333333  0.6666667 -0.3333333</span></span>
<span id="cb19-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; g2   0.3333333 -0.3333333  0.6666667</span></span>
<span id="cb19-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; g3   0.3333333 -0.3333333 -0.3333333</span></span></code></pre></div>
</div>
<p>Again, each column holds the linear contrast weights represented by that coefficient. It might not be obvious, but these are the deviations of each mean from the grand mean.</p>
<p>Again, we can confirm this:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model_parameters</span>(model7)</span>
<span id="cb20-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Parameter   | Coefficient |   SE |         95% CI |  t(7) |      p</span></span>
<span id="cb20-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ------------------------------------------------------------------</span></span>
<span id="cb20-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; (Intercept) |       19.99 | 0.57 | [18.65, 21.33] | 35.35 | &lt; .001</span></span>
<span id="cb20-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; G [1]       |        3.34 | 0.79 | [ 1.47,  5.22] |  4.21 | 0.004 </span></span>
<span id="cb20-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; G [2]       |        0.15 | 0.71 | [-1.53,  1.83] |  0.21 | 0.840</span></span>
<span id="cb20-7"></span>
<span id="cb20-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predict for G = g1, g2, g3</span></span>
<span id="cb20-9">pred_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">G =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"g1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"g2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"g3"</span>))</span>
<span id="cb20-10">(predicted_means <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(model6, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> pred_grid))</span>
<span id="cb20-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;        1        2        3 </span></span>
<span id="cb20-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 23.33333 20.14000 16.50000</span></span>
<span id="cb20-13"></span>
<span id="cb20-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Effect contrasts</span></span>
<span id="cb20-15">predicted_means[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(predicted_means)</span>
<span id="cb20-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;        1 </span></span>
<span id="cb20-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 3.342222</span></span>
<span id="cb20-18">predicted_means[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(predicted_means)</span>
<span id="cb20-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;         2 </span></span>
<span id="cb20-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 0.1488889</span></span></code></pre></div>
</div>
<p>I personally like the interpretability of dummy-coding better - as differences from the reference level. Thankfully, we can preserve that interpretability while also having the intercept represent the grand mean, using <em>deviation contrasts</em> - another type of sum-to-zero contrasts, available with <a href="https://easystats.github.io/datawizard/reference/contr.deviation.html"><code>datawizard::contr.deviation()</code></a>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contr.deviation</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>G))</span>
<span id="cb21-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;            g2         g3</span></span>
<span id="cb21-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; g1 -0.3333333 -0.3333333</span></span>
<span id="cb21-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; g2  0.6666667 -0.3333333</span></span>
<span id="cb21-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; g3 -0.3333333  0.6666667</span></span></code></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1">model8 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> G,</span>
<span id="cb22-2">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data,</span>
<span id="cb22-3">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">contrasts =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">G =</span> contr.deviation))</span>
<span id="cb22-4"></span>
<span id="cb22-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model_parameters</span>(model8)</span>
<span id="cb22-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Parameter   | Coefficient |   SE |          95% CI |  t(7) |      p</span></span>
<span id="cb22-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; -------------------------------------------------------------------</span></span>
<span id="cb22-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; (Intercept) |       19.99 | 0.57 | [ 18.65, 21.33] | 35.35 | &lt; .001</span></span>
<span id="cb22-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; G [g2]      |       -3.19 | 1.22 | [ -6.08, -0.31] | -2.62 | 0.034 </span></span>
<span id="cb22-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; G [g3]      |       -6.83 | 1.52 | [-10.44, -3.23] | -4.49 | 0.003</span></span>
<span id="cb22-11"></span>
<span id="cb22-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predict for G = g1, g2, g3</span></span>
<span id="cb22-13">pred_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">G =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"g1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"g2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"g3"</span>))</span>
<span id="cb22-14">(predicted_means <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(model6, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> pred_grid))</span>
<span id="cb22-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;        1        2        3 </span></span>
<span id="cb22-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 23.33333 20.14000 16.50000</span></span>
<span id="cb22-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(predicted_means)</span>
<span id="cb22-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] 19.99111</span></span>
<span id="cb22-19"></span>
<span id="cb22-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Deviation / treatment contrasts</span></span>
<span id="cb22-21">predicted_means[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> predicted_means[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb22-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;         2 </span></span>
<span id="cb22-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; -3.193333</span></span>
<span id="cb22-24">predicted_means[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> predicted_means[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb22-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;         3 </span></span>
<span id="cb22-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; -6.833333</span></span></code></pre></div>
</div>
</section>
</section>
</section>
<section id="in-moderation-analysis" class="level1">
<h1>In Moderation Analysis</h1>
<p>All this is great, but who cares about the intercept?</p>
<p>Well, this all comes into play much more substantially when moderators are involved!</p>
<p>When we have a moderator, the coefficient of the focal predictor represents a conditional effect. Specifically the effect of that predictor when the moderator is 0!</p>
<p>For two continuous predictors (<img src="https://latex.codecogs.com/png.latex?Z"> the focal predictor and <img src="https://latex.codecogs.com/png.latex?W"> the moderator), we can write out the regression equation as:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Chat%7BY%7D%20=%20b_0%20+%20b_1%20Z%20+%20b_2%20W%20+%20b_3%20W%20Z%0A"> We can take out <img src="https://latex.codecogs.com/png.latex?Z"> as a common factor:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Chat%7BY%7D%20=%20b_0%20+%20(b_1%20+%20b_3%20W)%20Z%20+%20b_2%20W%0A"> In other words</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BSlope%20of%20%7D%20Z%20=%20b_1%20+%20b_3%20W%0A"> Within this regression equation, <img src="https://latex.codecogs.com/png.latex?b_1"> is the “intercept” - the predicted slope of <img src="https://latex.codecogs.com/png.latex?Z"> (the conditional slope) when <img src="https://latex.codecogs.com/png.latex?W=0">!</p>
<p>If we condition on the <em>mean</em> of the moderator, this is often called the main effect, or the average effect.<sup>2</sup></p>
<p>Let’s see this in action.</p>
<section id="continuous-predictor-continuous-moderator" class="level2">
<h2 class="anchored" data-anchor-id="continuous-predictor-continuous-moderator">Continuous Predictor, Continuous Moderator</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1">model9 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Q,</span>
<span id="cb23-2">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data)</span>
<span id="cb23-3"></span>
<span id="cb23-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model_parameters</span>(model9)</span>
<span id="cb23-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Parameter   | Coefficient |    SE |            95% CI |  t(6) |     p</span></span>
<span id="cb23-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ---------------------------------------------------------------------</span></span>
<span id="cb23-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; (Intercept) |        3.13 | 54.24 | [-129.58, 135.85] |  0.06 | 0.956</span></span>
<span id="cb23-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; X           |        0.10 |  0.27 | [  -0.57,   0.76] |  0.35 | 0.737</span></span>
<span id="cb23-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Q           |        6.57 | 15.60 | [ -31.59,  44.73] |  0.42 | 0.688</span></span>
<span id="cb23-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; X × Q       |       -0.04 |  0.08 | [  -0.24,   0.16] | -0.45 | 0.670</span></span></code></pre></div>
</div>
<p><em>X</em>’s coefficient is the conditional slope (a.k.a., the <em>simple slope</em>) of <em>X</em> when <em>Q</em> is 0:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predict for X = [0, 1] and Q = 0</span></span>
<span id="cb24-2">pred_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">X =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Q =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb24-3">(predicted_means <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(model9, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> pred_grid))</span>
<span id="cb24-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;        1        2 </span></span>
<span id="cb24-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 3.133346 3.228691</span></span>
<span id="cb24-6"></span>
<span id="cb24-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Conditional slope of X</span></span>
<span id="cb24-8">predicted_means[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> predicted_means[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb24-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;          2 </span></span>
<span id="cb24-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 0.09534522</span></span></code></pre></div>
</div>
<p>When we center <em>Q</em>, it is the conditional slope (a.k.a., the <em>simple slope</em>) of <em>X</em> at the mean of <em>Q</em> - also called the “main effect”:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1">model10 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">center</span>(Q),</span>
<span id="cb25-2">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data)</span>
<span id="cb25-3"></span>
<span id="cb25-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model_parameters</span>(model10)</span>
<span id="cb25-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Parameter     | Coefficient |    SE |          95% CI |  t(6) |      p</span></span>
<span id="cb25-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ----------------------------------------------------------------------</span></span>
<span id="cb25-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; (Intercept)   |       26.39 |  2.90 | [ 19.30, 33.48] |  9.11 | &lt; .001</span></span>
<span id="cb25-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; X             |       -0.03 |  0.02 | [ -0.08,  0.02] | -1.64 | 0.151 </span></span>
<span id="cb25-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; center(Q)     |        6.57 | 15.60 | [-31.59, 44.73] |  0.42 | 0.688 </span></span>
<span id="cb25-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; X × center(Q) |       -0.04 |  0.08 | [ -0.24,  0.16] | -0.45 | 0.670</span></span>
<span id="cb25-11"></span>
<span id="cb25-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predict for X = [0, 1] and Q = mean(Q)</span></span>
<span id="cb25-13">pred_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">X =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Q =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Q))</span>
<span id="cb25-14">(predicted_means <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(model9, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> pred_grid))</span>
<span id="cb25-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;        1        2 </span></span>
<span id="cb25-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 26.38931 26.35590</span></span>
<span id="cb25-17"></span>
<span id="cb25-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Conditional slope of X</span></span>
<span id="cb25-19">predicted_means[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> predicted_means[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb25-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;           2 </span></span>
<span id="cb25-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; -0.03341344</span></span></code></pre></div>
</div>
<div class="callout callout-style-simple callout-note">
<div class="callout-body d-flex">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-body-container">
<p>Note that the coefficient of <code>Q</code> is not affected - it is the conditional slope of <em>Q</em> when <em>X</em> is 0. If we also wanted the main effect of <em>Q</em> we would need to also center <em>X</em>.</p>
</div>
</div>
</div>
</section>
<section id="continuous-predictor-categorical-moderator" class="level2">
<h2 class="anchored" data-anchor-id="continuous-predictor-categorical-moderator">Continuous Predictor, Categorical Moderator</h2>
<p>We can observe this same idea when the moderator is a factor.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># default dummy coding</span></span>
<span id="cb26-2">model11 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Q <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> A,</span>
<span id="cb26-3">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data)</span>
<span id="cb26-4"></span>
<span id="cb26-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model_parameters</span>(model11)</span>
<span id="cb26-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Parameter   | Coefficient |    SE |          95% CI |  t(6) |     p</span></span>
<span id="cb26-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; -------------------------------------------------------------------</span></span>
<span id="cb26-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; (Intercept) |       -2.40 | 11.37 | [-30.23, 25.43] | -0.21 | 0.840</span></span>
<span id="cb26-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Q           |        5.97 |  3.20 | [ -1.85, 13.80] |  1.87 | 0.111</span></span>
<span id="cb26-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; A [a2]      |       14.76 | 13.58 | [-18.47, 47.99] |  1.09 | 0.319</span></span>
<span id="cb26-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Q × A [a2]  |       -3.40 |  3.81 | [-12.74,  5.93] | -0.89 | 0.406</span></span></code></pre></div>
</div>
<p><em>Q</em>’s coefficient is the conditional slope (a.k.a., the <em>simple slope</em>) of <em>Q</em> when the dummy variables (in this case just one) of <em>A</em> are set to 0 - when <em>A</em> is <code>a1</code>:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predict for Q = [0, 1] and A = a1</span></span>
<span id="cb27-2">pred_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Q =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">A =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a1"</span>)</span>
<span id="cb27-3">(predicted_means <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(model11, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> pred_grid))</span>
<span id="cb27-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;         1         2 </span></span>
<span id="cb27-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; -2.400173  3.574452</span></span>
<span id="cb27-6"></span>
<span id="cb27-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Conditional slope of Q</span></span>
<span id="cb27-8">predicted_means[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> predicted_means[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb27-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;        2 </span></span>
<span id="cb27-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 5.974625</span></span></code></pre></div>
</div>
<p>We can again use sum-to-zero contrasts to get the grand-average slope of <em>X</em> - the “main effect”:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1">model12 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Q <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> A,</span>
<span id="cb28-2">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data,</span>
<span id="cb28-3">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">contrasts =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">A =</span> contr.sum))</span>
<span id="cb28-4"></span>
<span id="cb28-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model_parameters</span>(model12)</span>
<span id="cb28-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Parameter   | Coefficient |   SE |          95% CI |  t(6) |     p</span></span>
<span id="cb28-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ------------------------------------------------------------------</span></span>
<span id="cb28-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; (Intercept) |        4.98 | 6.79 | [-11.63, 21.59] |  0.73 | 0.491</span></span>
<span id="cb28-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Q           |        4.27 | 1.91 | [ -0.39,  8.94] |  2.24 | 0.066</span></span>
<span id="cb28-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; A [1]       |       -7.38 | 6.79 | [-23.99,  9.23] | -1.09 | 0.319</span></span>
<span id="cb28-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Q × A [1]   |        1.70 | 1.91 | [ -2.96,  6.37] |  0.89 | 0.406</span></span>
<span id="cb28-12"></span>
<span id="cb28-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predict for all combinations of Q = [0, 1] and A = [a1, a2]</span></span>
<span id="cb28-14">pred_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand.grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Q =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), </span>
<span id="cb28-15">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">A =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a2"</span>))</span>
<span id="cb28-16">(predicted_means <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(model11, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> pred_grid))</span>
<span id="cb28-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;         1         2         3         4 </span></span>
<span id="cb28-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; -2.400173  3.574452 12.358596 14.929210</span></span>
<span id="cb28-19"></span>
<span id="cb28-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Average slope of X</span></span>
<span id="cb28-21"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(predicted_means[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> predicted_means[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], </span>
<span id="cb28-22">       predicted_means[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> predicted_means[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]))</span>
<span id="cb28-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] 4.272619</span></span></code></pre></div>
</div>
</section>
<section id="categorical-predictor-continuous-moderator" class="level2">
<h2 class="anchored" data-anchor-id="categorical-predictor-continuous-moderator">Categorical Predictor, Continuous Moderator</h2>
<p>This holds true when the focal predictor is a factor as well.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># default dummy coding</span></span>
<span id="cb29-2">model13 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> A <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Q,</span>
<span id="cb29-3">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data)</span>
<span id="cb29-4"></span>
<span id="cb29-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model_parameters</span>(model13)</span>
<span id="cb29-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Parameter   | Coefficient |    SE |          95% CI |  t(6) |     p</span></span>
<span id="cb29-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; -------------------------------------------------------------------</span></span>
<span id="cb29-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; (Intercept) |       -2.40 | 11.37 | [-30.23, 25.43] | -0.21 | 0.840</span></span>
<span id="cb29-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; A [a2]      |       14.76 | 13.58 | [-18.47, 47.99] |  1.09 | 0.319</span></span>
<span id="cb29-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Q           |        5.97 |  3.20 | [ -1.85, 13.80] |  1.87 | 0.111</span></span>
<span id="cb29-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; A [a2] × Q  |       -3.40 |  3.81 | [-12.74,  5.93] | -0.89 | 0.406</span></span></code></pre></div>
</div>
<p><em>A</em>’s coefficient (<code>A [a2]</code>) is the conditional difference (a.k.a., the <em>simple effect</em>) between the levels of <em>A</em> when <em>Q</em> is 0:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predict for A = [a1, a2] and Q = 0</span></span>
<span id="cb30-2">pred_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">A =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a2"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Q =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb30-3">(predicted_means <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(model13, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> pred_grid))</span>
<span id="cb30-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;         1         2 </span></span>
<span id="cb30-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; -2.400173 12.358596</span></span>
<span id="cb30-6"></span>
<span id="cb30-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Conditional treatment contrast</span></span>
<span id="cb30-8">predicted_means[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> predicted_means[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb30-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;        2 </span></span>
<span id="cb30-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 14.75877</span></span></code></pre></div>
</div>
<p>When we center <em>Q</em>, it is the conditional difference at the mean of <em>Q</em> - also called the “main effect”:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1">model14 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> A <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">center</span>(Q),</span>
<span id="cb31-2">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data)</span>
<span id="cb31-3"></span>
<span id="cb31-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model_parameters</span>(model14)</span>
<span id="cb31-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Parameter          | Coefficient |   SE |          95% CI |  t(6) |      p</span></span>
<span id="cb31-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; --------------------------------------------------------------------------</span></span>
<span id="cb31-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; (Intercept)        |       18.74 | 1.15 | [ 15.92, 21.56] | 16.26 | &lt; .001</span></span>
<span id="cb31-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; A [a2]             |        2.72 | 1.49 | [ -0.93,  6.36] |  1.82 | 0.118 </span></span>
<span id="cb31-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; center(Q)          |        5.97 | 3.20 | [ -1.85, 13.80] |  1.87 | 0.111 </span></span>
<span id="cb31-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; A [a2] × center(Q) |       -3.40 | 3.81 | [-12.74,  5.93] | -0.89 | 0.406</span></span>
<span id="cb31-11"></span>
<span id="cb31-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predict for A = [a1, a2] and Q = mean(Q)</span></span>
<span id="cb31-13">pred_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">A =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a2"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Q =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Q))</span>
<span id="cb31-14">(predicted_means <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(model13, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> pred_grid))</span>
<span id="cb31-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;        1        2 </span></span>
<span id="cb31-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 18.73805 21.45343</span></span>
<span id="cb31-17"></span>
<span id="cb31-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Conditional treatment contrast</span></span>
<span id="cb31-19">predicted_means[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> predicted_means[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb31-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;        2 </span></span>
<span id="cb31-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 2.715377</span></span></code></pre></div>
</div>
<div class="callout callout-style-simple callout-note">
<div class="callout-body d-flex">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-body-container">
<p>Note that in all cases, regardless of centering, the interaction coefficient was unchanged. This will always be true for the highest level interaction.</p>
</div>
</div>
</div>
</section>
</section>
<section id="in-anova" class="level1">
<h1>In ANOVA</h1>
<p>Finally, we have Categorical-by-Categorical moderation - more often seen in ANOVA tables. As you might expect, when looking at coefficient tables, centering affects the values and meaning of the coefficient. When looking at ANOVA tables things get more complicated, as it depends on the type of sum-of-squares computed (1, 2, or 3).</p>
<p>You can read more about those differences in my blog post <a href="https://blog.msbstats.info/posts/2021-05-25-everything-about-anova/"><em>Everything You Always Wanted to Know About ANOVA</em></a>.</p>
<p>The TL;DR is - yes, you should center your predictors / use sum-to-zero contrasts.</p>
</section>
<section id="summary" class="level1">
<h1>Summary</h1>
<p>Centering makes coefficient tables (and ANOVA tables) more interpretable (and in Bayesian analysis can make prior elicitation easier) - <strong>a very important goal to itself</strong>, easing the interaction between the <em>numbers</em> and our squishy-human-flesh-brains - but that’s really all it does: it does not affect the model as a whole (doesn’t change <img src="https://latex.codecogs.com/png.latex?R%5E2"> or <a href="https://doi.org/10.1177/0013164418817801">global multicollinearity</a>).</p>
<p>It is quite common to see conditional slopes interpreted as main effects even when moderators have not been properly centered. Now at least <em>you</em> know better!</p>
<div class="cell">
<details class="code-fold">
<summary>Session Info</summary>
<div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sessionInfo</span>()</span>
<span id="cb32-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; R version 4.3.1 (2023-06-16 ucrt)</span></span>
<span id="cb32-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Platform: x86_64-w64-mingw32/x64 (64-bit)</span></span>
<span id="cb32-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Running under: Windows 11 x64 (build 22621)</span></span>
<span id="cb32-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb32-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Matrix products: default</span></span>
<span id="cb32-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb32-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb32-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; locale:</span></span>
<span id="cb32-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] LC_COLLATE=English_Israel.utf8  LC_CTYPE=English_Israel.utf8   </span></span>
<span id="cb32-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [3] LC_MONETARY=English_Israel.utf8 LC_NUMERIC=C                   </span></span>
<span id="cb32-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [5] LC_TIME=English_Israel.utf8    </span></span>
<span id="cb32-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb32-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; time zone: Asia/Jerusalem</span></span>
<span id="cb32-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; tzcode source: internal</span></span>
<span id="cb32-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb32-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; attached base packages:</span></span>
<span id="cb32-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] stats     graphics  grDevices utils     datasets  methods   base     </span></span>
<span id="cb32-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb32-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; other attached packages:</span></span>
<span id="cb32-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] parameters_0.21.3 datawizard_0.9.1 </span></span>
<span id="cb32-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb32-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; loaded via a namespace (and not attached):</span></span>
<span id="cb32-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [1] cli_3.6.2           knitr_1.45          TH.data_1.1-2      </span></span>
<span id="cb32-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [4] rlang_1.1.2         xfun_0.41           estimability_1.4.1 </span></span>
<span id="cb32-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [7] xtable_1.8-4        jsonlite_1.8.8      zoo_1.8-12         </span></span>
<span id="cb32-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [10] MSBMisc_0.0.1.14    htmltools_0.5.7     rmarkdown_2.25     </span></span>
<span id="cb32-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [13] grid_4.3.1          evaluate_0.23       MASS_7.3-60        </span></span>
<span id="cb32-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [16] fastmap_1.1.1       yaml_2.3.8          mvtnorm_1.2-4      </span></span>
<span id="cb32-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [19] insight_0.19.7.4    compiler_4.3.1      multcomp_1.4-25    </span></span>
<span id="cb32-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [22] codetools_0.2-19    sandwich_3.1-0      emmeans_1.9.0      </span></span>
<span id="cb32-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [25] coda_0.19-4         htmlwidgets_1.6.4   rstudioapi_0.15.0  </span></span>
<span id="cb32-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [28] lattice_0.21-8      digest_0.6.33       splines_4.3.1      </span></span>
<span id="cb32-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [31] Matrix_1.6-4        tools_4.3.1         survival_3.5-5     </span></span>
<span id="cb32-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [34] bayestestR_0.13.1.7</span></span></code></pre></div>
</details>
</div>
</section>
<section id="references" class="level1">
<h1>References</h1>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>That also has the added benefit of returning a vector, and not a column matrix like <code>scale()</code>.↩︎</p></li>
<li id="fn2"><p>This is often <strong>not</strong> the <em>average</em> effect - for that you might want to use the great <a href="https://marginaleffects.com/"><code>{marginaleffects}</code></a> package.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>R</category>
  <category>statistics</category>
  <category>code</category>
  <category>datawaizard</category>
  <category>moderation</category>
  <category>interactions</category>
  <guid>https://blog.msbstats.info/posts/2024-01-21-centering-101/</guid>
  <pubDate>Sat, 20 Jan 2024 22:00:00 GMT</pubDate>
</item>
<item>
  <title>Order Constraints in Bayes Models (with brms)</title>
  <link>https://blog.msbstats.info/posts/2023-06-26-order-constraints-in-brms/</link>
  <description><![CDATA[ 





<p>Over a year ago, while listing to a very not-at-all-statistical podcast, I discovered that Bayesian modeling is widely used in archaeology since the mid 90s to calibrate carbon dating.<sup>1</sup></p>
<p>Carbon dating is a scientific method used to determine the age of ancient artifacts and archaeological remains. It relies on the natural decay of a radioactive isotope called carbon-14, which is present in all living organisms. By measuring the amount of carbon-14 present in a sample, scientists can estimate its age with remarkable precision, over spans of thousands of years.</p>
<p>How do archaeologists calibrate carbon dating with Bayesian models? One way is by applying <em>order constraints</em> to their models.</p>
<p>In this post I will present what order constraints are, and how we may assess and apply them in Bayesian models using the <code>{brms}</code> package <span class="citation" data-cites="burkner2017brms">(Bürkner, 2017)</span>. I assume the reader is already somewhat familiar with the <code>{brms}</code> package, Bayesian modeling and core concepts in MCMC sampling.</p>
<p>I will be using the following packages and versions:</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2.0.0</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(scales)     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1.2.1</span></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(insight)    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0.19.2</span></span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(brms)       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2.19.0</span></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(posterior)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1.4.1.9000</span></span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggdist)     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3.3.0</span></span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidybayes)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3.0.4</span></span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(bayestestR) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0.13.1 </span></span></code></pre></div>
<p>You will need the most recent development version of <code>{posterior}</code> for some of the code presented in this post. You can install it from GitHub by running the following:</p>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">remotes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install_github</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"stan-dev/posterior"</span>)</span></code></pre></div>
<div class="cell">
<details class="code-fold">
<summary>Options and Constants</summary>
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">options</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">brms.backend =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cmdstanr"</span>,</span>
<span id="cb3-2">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mc.cores =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_set</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"top"</span>))</span>
<span id="cb3-5">slab_aes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">slab_color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">slab_linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb3-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">update_geom_defaults</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"slabinterval"</span>, slab_aes)</span>
<span id="cb3-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">update_geom_defaults</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"slab"</span>, slab_aes)</span>
<span id="cb3-8"></span>
<span id="cb3-9">big_number <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">number_format</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">big.mark =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>)</span>
<span id="cb3-10">ci_levels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</span>)</span></code></pre></div>
</details>
</div>
<p>Let’s <em>dig</em> in!</p>
<section id="the-data" class="level1">
<h1>The Data</h1>
<p>Data is based on the data presented in Buck <span class="citation" data-cites="buck2017bayesian">(2017, table 1)</span>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">table1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> </span>
<span id="cb4-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tribble</span>(</span>
<span id="cb4-3">  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>Layer,  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>C14, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>error,</span>
<span id="cb4-4">     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5773</span>,     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>,</span>
<span id="cb4-5">     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5654</span>,     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>,</span>
<span id="cb4-6">     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5585</span>,     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>,</span>
<span id="cb4-7">     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5861</span>,     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>,</span>
<span id="cb4-8">     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5755</span>,     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>,</span>
<span id="cb4-9">     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"E"</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5850</span>,     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>,</span>
<span id="cb4-10">     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"E"</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5928</span>,     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>,</span>
<span id="cb4-11">     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"E"</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5905</span>,     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>,</span>
<span id="cb4-12">     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"G"</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6034</span>,     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>,</span>
<span id="cb4-13">     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"G"</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6184</span>,     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>,</span>
<span id="cb4-14">     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"I"</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6248</span>,     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>,</span>
<span id="cb4-15">     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"I"</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6350</span>,     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span></span>
<span id="cb4-16">  )</span></code></pre></div>
</div>
<p>The <code>C14</code> column is the estimated carbon dating age (years before present, relative to today), and the <code>error</code> column provides the (estimated) measurement error in the C-14 assessment.</p>
<p>Our goal: to estimate the year from which each layer originates.</p>
</section>
<section id="the-model" class="level1 page-columns page-full">
<h1>The Model</h1>
<p>We will begin with an unassuming Gaussian model.</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BCarbon-14%20Age%7D_i%20%5Csim%20N(%5Cmu_i,%20%5Csigma%5E2%20+%20%5Cgamma%5E2_%7Bij%7D)%20%5C%5C%0A"> That is, each sample (with it’s own given measurement error <img src="https://latex.codecogs.com/png.latex?%5Cgamma%5E2_%7Bij%7D">) comes from a <em>layer</em> with its own mean <img src="https://latex.codecogs.com/png.latex?%5Cmu_i">.</p>

<div class="no-row-height column-margin column-container"><div class="">
<p>If this model doesn’t make sense to you, that’s because it’s only a partial model. Under the <a href="https://bcal.shef.ac.uk/">full model</a>, each <img src="https://latex.codecogs.com/png.latex?%5Cmu"> is uniformly distributed:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmu_i%20%5Csim%20U(%5Calpha_i,%5Cbeta_i)%0A"></p>
<p>With <img src="https://latex.codecogs.com/png.latex?%5Calpha"> and <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> being the parameters of interest: marking the beginning and end of each layer.</p>
</div></div><p>We’ll set the following super-wide-and-very-weak priors:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmu_i%20%5Csim%20N(-5975,%201000)%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Csigma%20%5Csim%20Exponential(0.01)%0A"></p>
<p>In <code>{brms}</code>, this looks like this:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">priors <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> </span>
<span id="cb5-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_prior</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"normal(-5975, 1000)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">class =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"b"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb5-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_prior</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"exponential(0.01)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">class =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sigma"</span>)</span>
<span id="cb5-4"></span>
<span id="cb5-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">validate_prior</span>(priors,</span>
<span id="cb5-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bf</span>(C14 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">se</span>(error, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Layer),</span>
<span id="cb5-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> table1</span>
<span id="cb5-8">)</span>
<span id="cb5-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                prior class   coef group resp dpar nlpar lb ub       source</span></span>
<span id="cb5-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  normal(-5975, 1000)     b                                            user</span></span>
<span id="cb5-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  normal(-5975, 1000)     b LayerB                             (vectorized)</span></span>
<span id="cb5-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  normal(-5975, 1000)     b LayerC                             (vectorized)</span></span>
<span id="cb5-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  normal(-5975, 1000)     b LayerE                             (vectorized)</span></span>
<span id="cb5-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  normal(-5975, 1000)     b LayerG                             (vectorized)</span></span>
<span id="cb5-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  normal(-5975, 1000)     b LayerI                             (vectorized)</span></span>
<span id="cb5-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    exponential(0.01) sigma                               0            user</span></span></code></pre></div>
</div>
<p>Fitting the model:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">mod1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brm</span>(</span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bf</span>(C14 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">se</span>(error, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Layer),</span>
<span id="cb6-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gaussian</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>),</span>
<span id="cb6-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prior =</span> priors,</span>
<span id="cb6-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> table1,</span>
<span id="cb6-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1111</span></span>
<span id="cb6-7">)</span></code></pre></div>
</div>
<!-- This is a catch-all, no-output-from-Stan "hack" -->
<p>We can extract the posterior distribution of the <img src="https://latex.codecogs.com/png.latex?%5Cmu"> parameter from each layer using the <code>posterior_linpred()</code> function, or <code>{tidybayes}</code>’s <code>add_linpred_*()</code> function.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb7-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Layer =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"E"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"G"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"I"</span>),</span>
<span id="cb7-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb7-4">)</span>
<span id="cb7-5"></span>
<span id="cb7-6">grid_with_mu <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_linpred_rvars</span>(grid, mod1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".mu"</span>)</span>
<span id="cb7-7"></span>
<span id="cb7-8">grid_with_mu</span>
<span id="cb7-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; # A tibble: 5 × 3</span></span>
<span id="cb7-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   Layer error         .mu</span></span>
<span id="cb7-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   &lt;chr&gt; &lt;dbl&gt;  &lt;rvar[1d]&gt;</span></span>
<span id="cb7-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1 B         0  -5671 ± 53</span></span>
<span id="cb7-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 2 C         0  -5809 ± 63</span></span>
<span id="cb7-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 3 E         0  -5894 ± 57</span></span>
<span id="cb7-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 4 G         0  -6111 ± 64</span></span>
<span id="cb7-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 5 I         0  -6295 ± 70</span></span></code></pre></div>
</div>
<p>The <code>.mu</code> column contains a vector of class <code>rvar</code> – it is a convenient and tidy way to store samples of random variables, such as MCMC posterior draws.</p>
<p>Compare this to the “long” format, where each sample get’s its own row:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_linpred_draws</span>(grid, mod1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".mu"</span>)</span>
<span id="cb8-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; # A tibble: 20,000 × 7</span></span>
<span id="cb8-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; # Groups:   Layer, error, .row [5]</span></span>
<span id="cb8-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    Layer error  .row .chain .iteration .draw    .mu</span></span>
<span id="cb8-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    &lt;chr&gt; &lt;dbl&gt; &lt;int&gt;  &lt;int&gt;      &lt;int&gt; &lt;int&gt;  &lt;dbl&gt;</span></span>
<span id="cb8-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  1 B         0     1     NA         NA     1 -5737.</span></span>
<span id="cb8-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  2 B         0     1     NA         NA     2 -5552.</span></span>
<span id="cb8-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  3 B         0     1     NA         NA     3 -5578.</span></span>
<span id="cb8-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  4 B         0     1     NA         NA     4 -5576.</span></span>
<span id="cb8-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  5 B         0     1     NA         NA     5 -5710.</span></span>
<span id="cb8-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  6 B         0     1     NA         NA     6 -5635.</span></span>
<span id="cb8-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  7 B         0     1     NA         NA     7 -5675.</span></span>
<span id="cb8-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  8 B         0     1     NA         NA     8 -5646.</span></span>
<span id="cb8-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  9 B         0     1     NA         NA     9 -5647.</span></span>
<span id="cb8-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 10 B         0     1     NA         NA    10 -5662.</span></span>
<span id="cb8-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; # ℹ 19,990 more rows</span></span></code></pre></div>
</div>
<p>That’s too much for me to work with…</p>
<p>The <code>rvar</code> class also works nicely with <code>{ggdist}</code>!</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(grid_with_mu, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Layer)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-2">  </span>
<span id="cb9-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_slabinterval</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xdist =</span> .mu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill_ramp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">after_stat</span>(level)),</span>
<span id="cb9-4">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">point_interval =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"median_hdci"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.width =</span> ci_levels) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-5">  </span>
<span id="cb9-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Year"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> big_number) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb9-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_discrete</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> rev) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb9-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_ramp_discrete</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.translate =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">guide =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2023-06-26-order-constraints-in-brms/index_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>As well as with <code>{bayestestR}</code>:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">CIs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hdi</span>(grid_with_mu<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>.mu)</span>
<span id="cb10-2">CIs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Parameter <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Layer <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add the layer names back</span></span>
<span id="cb10-3">CIs</span>
<span id="cb10-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Highest Density Interval</span></span>
<span id="cb10-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb10-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Parameter |              95% HDI</span></span>
<span id="cb10-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; --------------------------------</span></span>
<span id="cb10-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; B         | [-5771.10, -5563.86]</span></span>
<span id="cb10-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; C         | [-5945.06, -5687.60]</span></span>
<span id="cb10-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; E         | [-6008.23, -5780.24]</span></span>
<span id="cb10-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; G         | [-6235.39, -5978.06]</span></span>
<span id="cb10-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; I         | [-6427.16, -6152.16]</span></span></code></pre></div>
</div>
<p>This model treats <code>Layer</code> as a categorical predictor: estimating the <img src="https://latex.codecogs.com/png.latex?%5Cmu"> parameter for each layer independently of the other layers. Of course we are happy to see that our basic knowledge of archaeology and time – specifically that layers are stratified: lower layers are older than the layers above them – is represented in the posterior.</p>
<p>But is it truly?</p>
<p>We can compute the posterior probability that the layers are in fact ordered. Once again, using <code>rvars</code> makes this relatively easy:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">is_stratified <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(grid_with_mu, {</span>
<span id="cb11-2">  .mu[Layer <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> .mu[Layer <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span></span>
<span id="cb11-3">    .mu[Layer <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> .mu[Layer <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"E"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span></span>
<span id="cb11-4">    .mu[Layer <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"E"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> .mu[Layer <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"G"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span></span>
<span id="cb11-5">    .mu[Layer <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"G"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> .mu[Layer <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"I"</span>]</span>
<span id="cb11-6">})</span></code></pre></div>
</div>
<p><code>is_stratified</code> is a logical <code>rvar</code> – <code>TRUE</code> for MCMC draws that conform to the ordering constraint, and <code>FALSE</code> for MCMC draws that do not.</p>
<p>The mean of these is the posterior probability of the constraint. Since this is a logical <code>rvar</code>, we can be fancy and use the <code>Pr()</code> function:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Pr</span>(is_stratified)</span>
<span id="cb12-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] 0.78725</span></span></code></pre></div>
</div>
<p>That is, our posterior model strongly suggests that the layers are ordered, with a 0.79 posterior probability!</p>
<p>But we <em>know</em> a-priori that the layers are ordered! That is, the prior probability is 1, and therefore the posterior probability should also be 1! Can we incorporate this a-priori knowledge into our model / estimates?</p>
<section id="what-are-order-constraints" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="what-are-order-constraints">What are Order Constraints?</h2>
<p>In Bayesian modeling, order constraints refer to restrictions imposed on the relationships between a model’s parameters. These constraints specify the order in which the parameters should appear or the direction of their relationships. For example, if we have the parameters <img src="https://latex.codecogs.com/png.latex?%5Ctheta">, <img src="https://latex.codecogs.com/png.latex?%5Cgamma"> and <img src="https://latex.codecogs.com/png.latex?%5Czeta">, the following are valid definitions of order constraints:</p>
<ol type="1">
<li><img src="https://latex.codecogs.com/png.latex?(%5Ctheta%20%3E%203)%20%5Cland%20(%5Cgamma%20%5Cgeq%20%5Czeta)"> (<img src="https://latex.codecogs.com/png.latex?%5Ctheta"> is greater than 3, AND <img src="https://latex.codecogs.com/png.latex?%5Cgamma"> is greater or equal to <img src="https://latex.codecogs.com/png.latex?%5Czeta">)</li>
<li><img src="https://latex.codecogs.com/png.latex?(%5Ctheta%20%3E%20%5Cgamma)%20%5Clor%20%5Clnot%20(%5Ctheta%20%3E%20%5Czeta)"> (<img src="https://latex.codecogs.com/png.latex?%5Ctheta"> is greater <img src="https://latex.codecogs.com/png.latex?%5Cgamma"> 3, OR <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> is NOT greater than <img src="https://latex.codecogs.com/png.latex?%5Czeta">)</li>
<li><img src="https://latex.codecogs.com/png.latex?(%5Ctheta%20-%20%5Czeta%20%3C%202)%20%5Cland%20(%5Ctheta%20-%20%5Czeta%20%3E%200)"> (The difference between <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> and <img src="https://latex.codecogs.com/png.latex?%5Czeta"> is less than 2 AND greater than 0.)</li>
<li>Etc…</li>
</ol>
<p>These constraints are applied <em>on top of</em> any prior specification. This means that if we have the the following prior or posterior distribution defined by the following marginal distributions:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctheta%20%5Csim%20Normal(3,%202)%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Czeta%20%5Csim%20%5Cchi%5E2(5)%0A"></p>
<p>AND the constraint that <img src="https://latex.codecogs.com/png.latex?%5Ctheta%20%3E%20%5Czeta%20-%202">, then the constrained distribution looks like this (on the right):</p>
<div class="cell">
<details class="code-fold">
<summary>Plot Code</summary>
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">dens_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand_grid</span>(</span>
<span id="cb13-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">len =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>),</span>
<span id="cb13-3">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dx =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dnorm</span>(x, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">log =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)),</span>
<span id="cb13-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">len =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>),</span>
<span id="cb13-5">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dy =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dchisq</span>(y, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">log =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))  </span>
<span id="cb13-6">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb13-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb13-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">d =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(dx <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> dy),</span>
<span id="cb13-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">d =</span> d <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(d),</span>
<span id="cb13-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">d_c =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(d, x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>),</span>
<span id="cb13-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">d_c =</span> d_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(d_c)</span>
<span id="cb13-12">  )</span>
<span id="cb13-13"></span>
<span id="cb13-14">p1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(dens_grid, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z =</span> d)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb13-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_contour_filled</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(dens_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>d_c), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">len =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb13-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(theta), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb13-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(zeta), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb13-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">guides</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb13-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Un-Constrained"</span>)</span>
<span id="cb13-20"></span>
<span id="cb13-21">p2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> p1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z =</span> d_c) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb13-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">guides</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">guide_bins</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Density"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb13-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb13-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Constrained"</span>)</span>
<span id="cb13-25"></span>
<span id="cb13-26">patchwork<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wrap_plots</span>(p1, p2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">guides =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"collect"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> </span>
<span id="cb13-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2023-06-26-order-constraints-in-brms/index_files/figure-html/unnamed-chunk-13-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>

<div class="no-row-height column-margin column-container"><div class="">
<p>Note that after applying the constraint, <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> and <img src="https://latex.codecogs.com/png.latex?%5Czeta">’s marginal distributions are different than those defined originally.</p>
</div></div><p>The application of order constraints in Bayesian modeling helps to incorporate prior knowledge and structural assumptions into the model. As we will see, by constraining the parameters, we can reduce the uncertainty in the model estimates and improve the interpretability of the results.</p>
<p>Back to our example - how can we apply the order constraints to our Layer data?</p>
</section>
<section id="constraining-post-sampling" class="level2">
<h2 class="anchored" data-anchor-id="constraining-post-sampling">Constraining Post-Sampling</h2>
<p>One solution for integrating a-priori model constraints into our posterior is to constrain our posterior model <strong>post-sampling</strong> simply by discarding any posterior draw that does not conform to our constraint.</p>
<p>Why does this work?</p>
<p>According to Bayes rule:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ap(%5Ctheta%20%7C%20%5Ctext%7BData%7D)%20=%20%5Cfrac%7Bp(%5Ctext%7BData%7D%20%7C%20%5Ctheta)%20p(%5Ctheta)%7D%7Bp(%5Ctext%7BData%7D)%7D%20=%20%5Cfrac%7Bp(%5Ctext%7BData%7D%20%7C%20%5Ctheta)%20p(%5Ctheta)%7D%7B%5Cint%20p(%5Ctext%7BData%7D%20%7C%20%5Ctheta)%20p(%5Ctheta)%20d%5Ctheta%7D%0A"></p>
<p>Or</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ap(%5Ctheta%20%7C%20%5Ctext%7BData%7D)%20%5Cpropto%20p(%5Ctext%7BData%7D%20%7C%20%5Ctheta)%20p(%5Ctheta)%0A"></p>
<p>Let’s denote the parameter space that is within our specified constrain as <img src="https://latex.codecogs.com/png.latex?S_Y%5Ek"> <span class="citation" data-cites="gelfand1992bayesian">(borrowing the notation from the seminal work of Gelfand, Smith, &amp; Lee, 1992)</span>. Had we constrained our model a-priori to <img src="https://latex.codecogs.com/png.latex?S_Y%5Ek">, what would be the effect on parameter values <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> that are outside the constrained <img src="https://latex.codecogs.com/png.latex?S_Y%5Ek"> space (<img src="https://latex.codecogs.com/png.latex?%5Ctheta%20%5Cnotin%20S_Y%5Ek">)? Their prior probability is 0. Therefore their posterior probability would also be 0.</p>
<p>What would be the effect of constraining our model a-priori on the probability of values within <img src="https://latex.codecogs.com/png.latex?S_Y%5Ek"> (<img src="https://latex.codecogs.com/png.latex?%5Ctheta%20%5Cin%20S_Y%5Ek">)? Their prior probability distribution would have to be proportionally adjusted by <img src="https://latex.codecogs.com/png.latex?1/p(S_Y%5Ek)"> (the ratio of the unconstrained space to the constrained space). Since this value is a constant, the posterior probability is simply scaled by this amount.</p>
<p>For any MCMC sampler that only needs a function that is proportional to the posterior density function:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ap(%5Ctheta%20%7C%20%5Ctext%7BData%7D)%20%5Cpropto%0A%5Cbegin%7Bcases%7D%0A%20%20%20%20%20%200,%20&amp;%20%5Ctext%7Bif%7D%5C%20%5Ctheta%20%5Cnotin%20S_Y%5Ek%20%5C%5C%0A%20%20%20%20%20%20p(%5Ctext%7BData%7D%20%7C%20%5Ctheta)%20p(%5Ctheta),%20&amp;%20%5Ctext%7Bif%7D%5C%20%5Ctheta%20%5Cin%20S_Y%5Ek%0A%5Cend%7Bcases%7D%0A"></p>
<p>In other words, proportionally, applying the constraint is equivalent to simply not sampling outside of <img src="https://latex.codecogs.com/png.latex?S_Y%5Ek"> – or discarding any such samples.</p>
<p>We can keep only the samples within the constrained space by subsetting our posterior <code>rvar</code> with the <code>is_stratified</code> <code>rvar</code>! (This requires the <code>{posterior}</code> package, version 1.4.1.9000 at least.)</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># subset with logical rvar</span></span>
<span id="cb14-2">grid_with_mu<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>.mu_constrained <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> grid_with_mu<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>.mu[is_stratified]</span></code></pre></div>
</div>
<p>If we now plot our posteriors, we can see that the constrained posteriors are ever-so-slightly more narrow than the unconstrained ones, as if the edges of the samples are afraid to touch the adjacent layers’ posteriors:</p>
<div class="cell">
<details class="code-fold">
<summary>Plot Code</summary>
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(grid_with_mu, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Layer)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-2">  </span>
<span id="cb15-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_slabinterval</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xdist =</span> .mu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill_ramp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">after_stat</span>(level),</span>
<span id="cb15-4">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Un-Constrained"</span>),</span>
<span id="cb15-5">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">point_interval =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"median_hdci"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.width =</span> ci_levels) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_slabinterval</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xdist =</span> .mu_constrained, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill_ramp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">after_stat</span>(level),</span>
<span id="cb15-7">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Constrained"</span>),</span>
<span id="cb15-8">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">point_interval =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"median_hdci"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.width =</span> ci_levels,</span>
<span id="cb15-9">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">side =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-10">  </span>
<span id="cb15-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Un-Constrained"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Constrained"</span>),</span>
<span id="cb15-12">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"royalblue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"firebrick2"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb15-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Year"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> big_number) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb15-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_discrete</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> rev) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb15-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_ramp_discrete</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.translate =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">guide =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_thickness_shared</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2023-06-26-order-constraints-in-brms/index_files/figure-html/unnamed-chunk-15-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Looking at the 95% HDI, we can see that we were able to shave a couple of decades off the CI boundaries (note especially how Layers C and E have shifted slightly away from each other).</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1">CIs</span>
<span id="cb16-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Highest Density Interval</span></span>
<span id="cb16-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb16-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Parameter |              95% HDI</span></span>
<span id="cb16-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; --------------------------------</span></span>
<span id="cb16-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; B         | [-5771.10, -5563.86]</span></span>
<span id="cb16-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; C         | [-5945.06, -5687.60]</span></span>
<span id="cb16-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; E         | [-6008.23, -5780.24]</span></span>
<span id="cb16-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; G         | [-6235.39, -5978.06]</span></span>
<span id="cb16-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; I         | [-6427.16, -6152.16]</span></span>
<span id="cb16-11"></span>
<span id="cb16-12">CIs_constrained <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hdi</span>(grid_with_mu<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>.mu_constrained)</span>
<span id="cb16-13">CIs_constrained<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Parameter <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Layer</span>
<span id="cb16-14">CIs_constrained</span>
<span id="cb16-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Highest Density Interval</span></span>
<span id="cb16-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb16-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Parameter |              95% HDI</span></span>
<span id="cb16-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; --------------------------------</span></span>
<span id="cb16-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; B         | [-5760.49, -5576.41]</span></span>
<span id="cb16-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; C         | [-5902.04, -5711.58]</span></span>
<span id="cb16-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; E         | [-6002.56, -5809.96]</span></span>
<span id="cb16-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; G         | [-6222.13, -5994.23]</span></span>
<span id="cb16-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; I         | [-6421.89, -6169.61]</span></span></code></pre></div>
</div>
<p>This might not seem like much, especially considering the time scale we’re on. But imagine how crucial a decade in archaeological research can be! For example, one of the layers might contain a slab with the writing “… glory to king <a href="https://en.wikipedia.org/wiki/Jeroboam">Jeroboam</a>”, which unfortunately for you (my dear archaeologist) was a popular regal name in ancient Israel – there were two kings by the name of “Jeroboam” who lived within 130 years from each other; Narrowing a layer’s estimated C-14 year span can be crucial to dating the layer and its artifacts.</p>
<p>These post-sampling constraints can be applied to all <code>brms()</code> outputs (the various <code>posterior_*(&lt;brmsfit&gt;)</code> or <code>{tidybayes}</code>’s <code>add_*()</code> functions) by providing a vector of draw indices that conform to the constraint via the <code>draw_ids</code> argument.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1">i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">draws_of</span>(is_stratified))</span></code></pre></div>
</div>
<p>For example, we can obtain the PPD (posterior predictive distribution) for Layer C:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">layerC <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Layer =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb18-2">layerC<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>PPD <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">posterior_predict</span>(mod1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> layerC) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rvar</span>()</span>
<span id="cb18-3">layerC<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>PPD_constrained <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">posterior_predict</span>(mod1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> layerC, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">draw_ids =</span> i) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rvar</span>()</span>
<span id="cb18-4"></span>
<span id="cb18-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># # Or with {tidybayes}:</span></span>
<span id="cb18-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># layerC &lt;- add_predicted_rvars(layerC, mod1, value = "PPD")</span></span>
<span id="cb18-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># layerC &lt;- add_predicted_rvars(layerC, mod1, value = "PPD_constrained", draw_ids = i)</span></span></code></pre></div>
</div>
<div class="cell">
<details class="code-fold">
<summary>Plot Code</summary>
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(layerC) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-2">  </span>
<span id="cb19-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_slab</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xdist =</span> PPD, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Un-Constrained"</span>, </span>
<span id="cb19-4">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill_ramp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">after_stat</span>(level)),</span>
<span id="cb19-5">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">point_interval =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"median_hdci"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.width =</span> ci_levels) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb19-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_slab</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xdist =</span> PPD_constrained, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Constrained"</span>, </span>
<span id="cb19-7">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill_ramp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">after_stat</span>(level)),</span>
<span id="cb19-8">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">point_interval =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"median_hdci"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.width =</span> ci_levels) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb19-9">  </span>
<span id="cb19-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Year (Layer C)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> big_number) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb19-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_ramp_discrete</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.translate =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">guide =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_thickness_shared</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2023-06-26-order-constraints-in-brms/index_files/figure-html/unnamed-chunk-19-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>We can even compute WAIC / LOO indices with post-sampling constraint:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1">loo1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loo</span>(mod1)</span>
<span id="cb20-2">loo2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loo</span>(mod1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">draw_ids =</span> i)</span>
<span id="cb20-3"></span>
<span id="cb20-4">loo_comp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loo_compare</span>(loo1, loo2) </span>
<span id="cb20-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(loo_comp) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Constrained"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Un-Constrained"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add model "names" ad-hoc</span></span>
<span id="cb20-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(loo_comp, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">simplify  =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb20-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                elpd_diff se_diff elpd_loo se_elpd_loo p_loo se_p_loo looic</span></span>
<span id="cb20-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Constrained      0.0       0.0   -73.2      1.6         4.6   1.0    146.3</span></span>
<span id="cb20-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Un-Constrained  -0.9       0.3   -74.0      1.4         5.0   0.9    148.1</span></span>
<span id="cb20-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                se_looic</span></span>
<span id="cb20-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Constrained      3.2   </span></span>
<span id="cb20-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Un-Constrained   2.9</span></span></code></pre></div>
</div>
<p>The constrained model is slightly preferred <img src="https://latex.codecogs.com/png.latex?%5CDelta%20LOOIC/SE_%7B%5CDelta%7D%20=%202.8">!<sup>2</sup> We can also <em>test</em> such constraints post-sampling with Bayes factors using <code>{bayestestR}</code>’s <a href="https://easystats.github.io/bayestestR/reference/bayesfactor_restricted.html"><code>bayesfactor_restricted()</code></a> (see the accompanying <a href="https://easystats.github.io/bayestestR/articles/bayes_factors.html#bayesfactor_restricted">vignette</a>).</p>
<p>These constraints can also be applied to estimates generated with <code>{emmeans}</code> or <code>{marginaleffects}</code>, again via the <code>draw_ids</code> argument (which can themselves be passed to the various <code>{bayestestR}</code> functions).</p>
<div class="sourceCode" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># e.g., </span></span>
<span id="cb21-2">emmeans<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emmeans</span>(mod1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">specs =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Layer"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">draw_ids =</span> i)</span>
<span id="cb21-3"></span>
<span id="cb21-4">marginaleffects<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">avg_comparisons</span>(mod1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variables =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Layer"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">draw_ids =</span> i)</span></code></pre></div>
<div class="callout callout-style-simple callout-tip">
<div class="callout-body d-flex">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-body-container">
<p>The constraints we’re using in this blog post involve all of the parameters which we are interested in estimating. But this need not be the case. For example, the constraint can be defined based one only a few parameters, and still apply it to all the parameters.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define constraint on mu_B, mu_C, mu_G</span></span>
<span id="cb22-2">new_constraint <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(grid_with_mu, {</span>
<span id="cb22-3">  .mu[Layer <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> .mu[Layer <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span></span>
<span id="cb22-4">    .mu[Layer <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> .mu[Layer <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"G"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span></span>
<span id="cb22-5">})</span>
<span id="cb22-6"></span>
<span id="cb22-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Pr</span>(new_constraint)</span>
<span id="cb22-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] 0.90725</span></span>
<span id="cb22-9"></span>
<span id="cb22-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Apply constraint when estimating all parameters</span></span>
<span id="cb22-11">CIs_constrained2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hdi</span>(grid_with_mu<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>.mu[new_constraint])</span>
<span id="cb22-12">CIs_constrained2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Parameter <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> grid_with_mu<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Layer</span>
<span id="cb22-13">CIs_constrained2</span>
<span id="cb22-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Highest Density Interval</span></span>
<span id="cb22-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb22-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Parameter |              95% HDI</span></span>
<span id="cb22-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; --------------------------------</span></span>
<span id="cb22-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; B         | [-5765.22, -5573.01]</span></span>
<span id="cb22-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; C         | [-5909.03, -5701.18]</span></span>
<span id="cb22-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; E         | [-6001.42, -5780.24]</span></span>
<span id="cb22-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; G         | [-6232.84, -5999.40]</span></span>
<span id="cb22-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; I         | [-6429.44, -6164.50]</span></span></code></pre></div>
</div>
</div>
</div>
</div>
</section>
</section>
<section id="constraining-the-model-a-priori" class="level1 page-columns page-full">
<h1>Constraining the Model A-Priori</h1>
<p>Constraining post-sampling is wasteful: not only are we now estimating based on less samples, but our poor MCMC sampler worked so hard getting those 800~ samples we discarded. It sure would be great if we could constrain our model from the start.</p>
<p>Let’s reparameterize our model:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BCarbon%2014%20Age%7D_i%20%5Csim%20N(%5Cmu,%20%5Csigma%5E2%20+%20%5Cgamma%5E2_%7Bij%7D)%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmu_%7Bij%7D%20=%20b_%7B%5Ctext%7BLayer%7D%20=%20B%7D%20+%20b_%7B%5Ctext%7BLayer%7D%20%5Cneq%20B%7D%20X_%7B%5Ctext%7BLayer%7D%20%5Cneq%20B%7D%5C%5C%0A"></p>
<p>In <code>brms</code> the model looks like this:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1">priors <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> </span>
<span id="cb23-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_prior</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"normal(-5975, 1000)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">coef =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Intercept"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb23-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_prior</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"normal(0, 1000)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">class =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"b"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb23-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_prior</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"exponential(0.01)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">class =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sigma"</span>)</span>
<span id="cb23-5"></span>
<span id="cb23-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">validate_prior</span>(priors, </span>
<span id="cb23-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bf</span>(C14 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">se</span>(error, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Intercept <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Layer),</span>
<span id="cb23-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> table1</span>
<span id="cb23-9">)</span>
<span id="cb23-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                prior class      coef group resp dpar nlpar lb ub       source</span></span>
<span id="cb23-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      normal(0, 1000)     b                                               user</span></span>
<span id="cb23-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  normal(-5975, 1000)     b Intercept                                     user</span></span>
<span id="cb23-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      normal(0, 1000)     b    LayerC                             (vectorized)</span></span>
<span id="cb23-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      normal(0, 1000)     b    LayerE                             (vectorized)</span></span>
<span id="cb23-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      normal(0, 1000)     b    LayerG                             (vectorized)</span></span>
<span id="cb23-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      normal(0, 1000)     b    LayerI                             (vectorized)</span></span>
<span id="cb23-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    exponential(0.01) sigma                                  0            user</span></span></code></pre></div>
</div>
<p>However, we would also like to add the following constraints:</p>
<ul>
<li>Each of the Layer coefficients <strong>can only be negative</strong>.</li>
<li><em>and</em> each coefficient is <strong>more negative than the coefficient of the layer above it</strong>.</li>
</ul>
<p>Unfortunately, <code>brms</code> does not readily support such constraints. For example, if we try to constrain the <code>LayerC</code> coefficient to be negative by specifying the upper bound (<code>ub</code>) to be 0 we get the following error:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_prior</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"normal(0, 1000)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">class =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"b"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">coef =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LayerC"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ub =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb24-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Error: Argument 'coef' may not be specified when using boundaries.</span></span></code></pre></div>
</div>
<p>Alas, we need to write some Stan…. But only a little!</p>
<p>In Stan, we can define <a href="https://mc-stan.org/docs/reference-manual/reject-statements.html">reject statements</a>, which reject candidate values from being sampled. This is typically done conditionally. For example, we can test if <code>b[1]</code> (corresponding to <code>b_LayerC</code>) is positive, and reject the sample if it is.</p>
<div class="sourceCode" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb25-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (!(b[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] &lt; <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)) {</span>
<span id="cb25-2">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hey! This value of b_LayerC is a-priori impossible!"</span>);</span>
<span id="cb25-3">}</span></code></pre></div>
<p>Simple enough!</p>
<p>Luckily for us, <code>brms</code> is actually 100% powered by Stan – it converts our <strong><code>R</code></strong>-flavored model specifications, and translates them automagically into Stan. We can see the raw Stan code using the <code>make_stancode()</code> function:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_stancode</span>(</span>
<span id="cb26-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bf</span>(C14 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">se</span>(error, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Intercept <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Layer),</span>
<span id="cb26-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> table1, </span>
<span id="cb26-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prior =</span> priors</span>
<span id="cb26-5">)</span>
<span id="cb26-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; // generated with brms 2.19.0</span></span>
<span id="cb26-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; functions {</span></span>
<span id="cb26-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   </span></span>
<span id="cb26-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; }</span></span>
<span id="cb26-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; data {</span></span>
<span id="cb26-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   int&lt;lower=1&gt; N; // total number of observations</span></span>
<span id="cb26-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   vector[N] Y; // response variable</span></span>
<span id="cb26-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   vector&lt;lower=0&gt;[N] se; // known sampling error</span></span>
<span id="cb26-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   int&lt;lower=1&gt; K; // number of population-level effects</span></span>
<span id="cb26-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   matrix[N, K] X; // population-level design matrix</span></span>
<span id="cb26-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   int prior_only; // should the likelihood be ignored?</span></span>
<span id="cb26-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; }</span></span>
<span id="cb26-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; transformed data {</span></span>
<span id="cb26-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   vector&lt;lower=0&gt;[N] se2 = square(se);</span></span>
<span id="cb26-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; }</span></span>
<span id="cb26-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; parameters {</span></span>
<span id="cb26-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   vector[K] b; // population-level effects</span></span>
<span id="cb26-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   real&lt;lower=0&gt; sigma; // dispersion parameter</span></span>
<span id="cb26-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; }</span></span>
<span id="cb26-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; transformed parameters {</span></span>
<span id="cb26-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   real lprior = 0; // prior contributions to the log posterior</span></span>
<span id="cb26-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   lprior += normal_lpdf(b[1] | -5975, 1000);</span></span>
<span id="cb26-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   lprior += normal_lpdf(b[2] | 0, 1000);</span></span>
<span id="cb26-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   lprior += normal_lpdf(b[3] | 0, 1000);</span></span>
<span id="cb26-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   lprior += normal_lpdf(b[4] | 0, 1000);</span></span>
<span id="cb26-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   lprior += normal_lpdf(b[5] | 0, 1000);</span></span>
<span id="cb26-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   lprior += exponential_lpdf(sigma | 0.01);</span></span>
<span id="cb26-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; }</span></span>
<span id="cb26-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; model {</span></span>
<span id="cb26-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   // likelihood including constants</span></span>
<span id="cb26-36"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   if (!prior_only) {</span></span>
<span id="cb26-37"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     // initialize linear predictor term</span></span>
<span id="cb26-38"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     vector[N] mu = rep_vector(0.0, N);</span></span>
<span id="cb26-39"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     mu += X * b;</span></span>
<span id="cb26-40"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     target += normal_lpdf(Y | mu, sqrt(square(sigma) + se2));</span></span>
<span id="cb26-41"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   }</span></span>
<span id="cb26-42"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   // priors including constants</span></span>
<span id="cb26-43"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   target += lprior;</span></span>
<span id="cb26-44"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; }</span></span>
<span id="cb26-45"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; generated quantities {</span></span>
<span id="cb26-46"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   </span></span>
<span id="cb26-47"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; }</span></span>
<span id="cb26-48"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span></code></pre></div>
</div>
<p>But <code>brms</code> <em>also</em> allows us to <em>inject</em> custom Stan code into the model before execution! And so, we can add such conditional reject statements, which are equivalent to setting the prior density of such values to 0 (=impossible).</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1">svar <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stanvar</span>(</span>
<span id="cb27-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scode =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span></span>
<span id="cb27-3"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  if (!(b[2] &lt; 0)) {</span></span>
<span id="cb27-4"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    reject("Hey! This value of b_LayerC is a-priori impossible!");</span></span>
<span id="cb27-5"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  }</span></span>
<span id="cb27-6"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  '</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">block =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tparameters"</span>)</span></code></pre></div>
</div>
<p>We can see it injected into the Stan model, under the <code>transformed parameters</code> block:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_stancode</span>(</span>
<span id="cb28-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bf</span>(C14 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">se</span>(error, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Intercept <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Layer),</span>
<span id="cb28-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> table1, </span>
<span id="cb28-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prior =</span> priors, </span>
<span id="cb28-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stanvars =</span> svar</span>
<span id="cb28-6">)</span>
<span id="cb28-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; // generated with brms 2.19.0</span></span>
<span id="cb28-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; functions {</span></span>
<span id="cb28-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   </span></span>
<span id="cb28-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; }</span></span>
<span id="cb28-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; data {</span></span>
<span id="cb28-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   int&lt;lower=1&gt; N; // total number of observations</span></span>
<span id="cb28-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   vector[N] Y; // response variable</span></span>
<span id="cb28-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   vector&lt;lower=0&gt;[N] se; // known sampling error</span></span>
<span id="cb28-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   int&lt;lower=1&gt; K; // number of population-level effects</span></span>
<span id="cb28-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   matrix[N, K] X; // population-level design matrix</span></span>
<span id="cb28-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   int prior_only; // should the likelihood be ignored?</span></span>
<span id="cb28-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; }</span></span>
<span id="cb28-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; transformed data {</span></span>
<span id="cb28-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   vector&lt;lower=0&gt;[N] se2 = square(se);</span></span>
<span id="cb28-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; }</span></span>
<span id="cb28-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; parameters {</span></span>
<span id="cb28-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   vector[K] b; // population-level effects</span></span>
<span id="cb28-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   real&lt;lower=0&gt; sigma; // dispersion parameter</span></span>
<span id="cb28-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; }</span></span>
<span id="cb28-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; transformed parameters {</span></span>
<span id="cb28-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   real lprior = 0; // prior contributions to the log posterior</span></span>
<span id="cb28-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   </span></span>
<span id="cb28-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   if (!(b[2] &lt; 0)) {</span></span>
<span id="cb28-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     reject("Hey! This value of b_LayerC is a-priori impossible!");</span></span>
<span id="cb28-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   }</span></span>
<span id="cb28-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   </span></span>
<span id="cb28-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   lprior += normal_lpdf(b[1] | -5975, 1000);</span></span>
<span id="cb28-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   lprior += normal_lpdf(b[2] | 0, 1000);</span></span>
<span id="cb28-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   lprior += normal_lpdf(b[3] | 0, 1000);</span></span>
<span id="cb28-36"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   lprior += normal_lpdf(b[4] | 0, 1000);</span></span>
<span id="cb28-37"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   lprior += normal_lpdf(b[5] | 0, 1000);</span></span>
<span id="cb28-38"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   lprior += exponential_lpdf(sigma | 0.01);</span></span>
<span id="cb28-39"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; }</span></span>
<span id="cb28-40"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; model {</span></span>
<span id="cb28-41"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   // likelihood including constants</span></span>
<span id="cb28-42"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   if (!prior_only) {</span></span>
<span id="cb28-43"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     // initialize linear predictor term</span></span>
<span id="cb28-44"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     vector[N] mu = rep_vector(0.0, N);</span></span>
<span id="cb28-45"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     mu += X * b;</span></span>
<span id="cb28-46"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     target += normal_lpdf(Y | mu, sqrt(square(sigma) + se2));</span></span>
<span id="cb28-47"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   }</span></span>
<span id="cb28-48"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   // priors including constants</span></span>
<span id="cb28-49"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   target += lprior;</span></span>
<span id="cb28-50"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; }</span></span>
<span id="cb28-51"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; generated quantities {</span></span>
<span id="cb28-52"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   </span></span>
<span id="cb28-53"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; }</span></span>
<span id="cb28-54"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span></code></pre></div>
</div>
<p>Let’s see if it works by sampling from the prior distribution:</p>
<div class="cell">
<div class="sourceCode cell-code" id="annotated-cell-28" style="background: #f1f3f5;"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><span id="annotated-cell-28-1">mod2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brm</span>(</span>
<span id="annotated-cell-28-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bf</span>(C14 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">se</span>(error, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Intercept <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Layer),</span>
<span id="annotated-cell-28-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gaussian</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>),</span>
<span id="annotated-cell-28-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prior =</span> priors,</span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-28" data-target-annotation="1" onclick="event.preventDefault();">1</a><span id="annotated-cell-28-5" class="code-annotation-target">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sample_prior =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"only"</span>,</span>
<span id="annotated-cell-28-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> table1,</span>
<span id="annotated-cell-28-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">222</span></span>
<span id="annotated-cell-28-8">)</span>
<span id="annotated-cell-28-9"></span>
<span id="annotated-cell-28-10">mod2_with_constrain <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brm</span>(</span>
<span id="annotated-cell-28-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bf</span>(C14 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">se</span>(error, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Intercept <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Layer),</span>
<span id="annotated-cell-28-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gaussian</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>),</span>
<span id="annotated-cell-28-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prior =</span> priors,</span>
<span id="annotated-cell-28-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sample_prior =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"only"</span>,</span>
<span id="annotated-cell-28-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stanvars =</span> svar,</span>
<span id="annotated-cell-28-16">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> table1,</span>
<span id="annotated-cell-28-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">333</span></span>
<span id="annotated-cell-28-18">)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div>
<div class="cell-annotation">
<dl class="code-annotation-container-grid">
<dt data-target-cell="annotated-cell-28" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-28" data-code-lines="5,14" data-code-annotation="1">Note we are sampling from the prior.</span>
</dd>
</dl>
</div>
</div>
<p>(You will get a warning about divergent transitions; This is because we are sampling the prior at its boundary.)</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1">b_LayerC <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(</span>
<span id="cb29-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gather_rvars</span>(mod2, b_LayerC) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb29-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Model =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Un-constrained"</span>),</span>
<span id="cb29-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gather_rvars</span>(mod2_with_constrain, b_LayerC) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb29-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Model =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Constrained"</span>)  </span>
<span id="cb29-6">)</span></code></pre></div>
</div>
<div class="cell page-columns page-full">
<details class="code-fold">
<summary>Plot Code</summary>
<div class="sourceCode cell-code" id="cb30" data-cap-location="margin" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(b_LayerC, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Model)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-2">  </span>
<span id="cb30-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_slabinterval</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xdist =</span> .value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill_ramp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">after_stat</span>(level)),</span>
<span id="cb30-4">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">point_interval =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"median_hdci"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.width =</span> ci_levels) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-5">  </span>
<span id="cb30-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"b_LayerC"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> big_number) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb30-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_ramp_discrete</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.translate =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">guide =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display page-columns page-full">
<div class="quarto-figure quarto-figure-center page-columns page-full">
<figure class="figure page-columns page-full">
<p><img src="https://blog.msbstats.info/posts/2023-06-26-order-constraints-in-brms/index_files/figure-html/unnamed-chunk-29-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption class="margin-caption">Who doens’t like a bumpy prior?</figcaption>
</figure>
</div>
</div>
</div>
<p>It worked!</p>
<p>Let’s apply all of the constraints we want: we want the first “slope” to be negative, and then each consecutive slope to be more negative than the previous one. If one of these does not apply, we will reject the sample.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1">svar <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stanvar</span>(</span>
<span id="cb31-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scode =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span></span>
<span id="cb31-3"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  if (!(b[2] &lt; 0 &amp;&amp; b[3] &lt; b[2] &amp;&amp; b[4] &lt; b[3] &amp;&amp; b[5] &lt; b[4])) {</span></span>
<span id="cb31-4"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    reject("Order constraint violated!");</span></span>
<span id="cb31-5"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  }</span></span>
<span id="cb31-6"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  '</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">block =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tparameters"</span>)</span></code></pre></div>
</div>
<p>Let’s sample from the posterior:</p>
<p>(We will use some <code>init</code>s here to make sure that our starting point for MCMC sampling is within our constrained space. Read more about those in Solomon Kurz’s excellent <a href="https://solomonkurz.netlify.app/blog/2021-06-05-don-t-forget-your-inits/">blog post</a>.)</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1">init <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb32-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Give broad initial values that satisfy the order constraints</span></span>
<span id="cb32-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">250</span>)</span>
<span id="cb32-4">)</span>
<span id="cb32-5"></span>
<span id="cb32-6">mod2_with_full_constrain <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brm</span>(</span>
<span id="cb32-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bf</span>(C14 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">se</span>(error, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Intercept <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Layer),</span>
<span id="cb32-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gaussian</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>),</span>
<span id="cb32-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prior =</span> priors,</span>
<span id="cb32-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stanvars =</span> svar,</span>
<span id="cb32-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> table1,</span>
<span id="cb32-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">init =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(init, init, init, init),</span>
<span id="cb32-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1234</span></span>
<span id="cb32-14">)</span></code></pre></div>
</div>
<p>We can test that the constraint was applied:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1">grid_with_mu <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> grid_with_mu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb33-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_linpred_rvars</span>(mod2_with_full_constrain, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".mu_con_prior"</span>)</span>
<span id="cb33-3"></span>
<span id="cb33-4">is_stratified <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(grid_with_mu, {</span>
<span id="cb33-5">  .mu_con_prior[Layer <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> .mu_con_prior[Layer <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span></span>
<span id="cb33-6">    .mu_con_prior[Layer <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> .mu_con_prior[Layer <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"E"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span></span>
<span id="cb33-7">    .mu_con_prior[Layer <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"E"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> .mu_con_prior[Layer <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"G"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span></span>
<span id="cb33-8">    .mu_con_prior[Layer <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"G"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> .mu_con_prior[Layer <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"I"</span>]</span>
<span id="cb33-9">})</span>
<span id="cb33-10"></span>
<span id="cb33-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Pr</span>(is_stratified) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Should be 1!</span></span>
<span id="cb33-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] 1</span></span></code></pre></div>
</div>
<p>Both constrained posteriors sit snugly on top of each other.</p>
<div class="cell">
<details class="code-fold">
<summary>Plot Code</summary>
<div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(grid_with_mu, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Layer)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb34-2">  </span>
<span id="cb34-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_slabinterval</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xdist =</span> .mu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill_ramp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">after_stat</span>(level),</span>
<span id="cb34-4">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"None"</span>),</span>
<span id="cb34-5">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">point_interval =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"median_hdci"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.width =</span> ci_levels) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb34-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_slabinterval</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xdist =</span> .mu_constrained, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill_ramp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">after_stat</span>(level),</span>
<span id="cb34-7">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Post Sampling"</span>),</span>
<span id="cb34-8">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">point_interval =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"median_hdci"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.width =</span> ci_levels,</span>
<span id="cb34-9">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">side =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb34-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_slabinterval</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xdist =</span> .mu_con_prior, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill_ramp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">after_stat</span>(level),</span>
<span id="cb34-11">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A-Priori"</span>),</span>
<span id="cb34-12">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">point_interval =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"median_hdci"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.width =</span> ci_levels,</span>
<span id="cb34-13">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">side =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb34-14">  </span>
<span id="cb34-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Constraint:"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"None"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Post Sampling"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A-Priori"</span>),</span>
<span id="cb34-16">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"royalblue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"firebrick2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tomato"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb34-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Year"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> big_number) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb34-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_discrete</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> rev) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb34-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_ramp_discrete</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.translate =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">guide =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb34-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_thickness_shared</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2023-06-26-order-constraints-in-brms/index_files/figure-html/unnamed-chunk-33-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="callout callout-style-simple callout-important">
<div class="callout-body d-flex">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-body-container">
<p>Note that the <a href="https://mc-stan.org/docs/reference-manual/reject-statements.html"><em>Stan reference manual</em></a> actually recommends against using reject statements in this way. Instead it advocates for writing out model constraints properly in parameter definitions and model parameterization. But hey, if we knew how to do that…</p>
<p>Additionally, the <a href="https://mc-stan.org/docs/stan-users-guide/label-switching-problematic.html#parameter-ordering-constraints"><em>Stan user’s guide</em></a> notes that applying such constraints on the prior-model does not allow us to assess the validity of the constraint (the posterior probability of the constraint in the unconstrained posterior model).</p>
</div>
</div>
</div>
</section>
<section id="monotonic-effects" class="level1">
<h1>Monotonic Effects</h1>
<p>What we’ve seen can be applied to <em>any</em> order constraints. However, the specific constraint we’re looking at here is a <em>monotonic</em> constraint, where the outcome exclusively increases or decreases from each consecutive level to the next <span class="citation" data-cites="burkner2020modelling">(see Bürkner &amp; Charpentier, 2020)</span>.</p>
<p><code>brms</code> actually comes with a built-in function to do this: <a href="https://paul-buerkner.github.io/brms/articles/brms_monotonic.html"><code>mo()</code></a>. By using this in the model’s formula, <code>brms</code> parameterizes the model such that the monotonic effect is defined by two parameters:</p>
<ul>
<li>A slope (<code>b_moLayer</code>) representing the direction and strength of the change.</li>
<li>A simplex (<code>simo</code>) defining the change increments from level to level.</li>
</ul>
<div class="cell">
<div class="sourceCode cell-code" id="cb35" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb35-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_prior</span>(</span>
<span id="cb35-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bf</span>(C14 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">se</span>(error, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mo</span>(Layer)),</span>
<span id="cb35-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gaussian</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>),</span>
<span id="cb35-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> table1</span>
<span id="cb35-5">)</span>
<span id="cb35-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                       prior     class     coef group resp dpar nlpar lb ub</span></span>
<span id="cb35-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                      (flat)         b                                     </span></span>
<span id="cb35-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                      (flat)         b  moLayer                            </span></span>
<span id="cb35-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  student_t(3, -5883, 206.8) Intercept                                     </span></span>
<span id="cb35-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      student_t(3, 0, 206.8)     sigma                                 0   </span></span>
<span id="cb35-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                dirichlet(1)      simo moLayer1                            </span></span>
<span id="cb35-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;        source</span></span>
<span id="cb35-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;       default</span></span>
<span id="cb35-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  (vectorized)</span></span>
<span id="cb35-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;       default</span></span>
<span id="cb35-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;       default</span></span>
<span id="cb35-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;       default</span></span></code></pre></div>
</div>
<p>To apply this method we first need to recode the Layer predictor as an ordinal factor:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1">table1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Layer <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.ordered</span>(table1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Layer)</span></code></pre></div>
</div>
<p>Using the same priors on the intercept and the variance, let’s fit the monotonic model.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb37-1">priors <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> </span>
<span id="cb37-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_prior</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"normal(-5975, 1000)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">class =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Intercept"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb37-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_prior</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"exponential(0.01)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">class =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sigma"</span>)</span>
<span id="cb37-4"></span>
<span id="cb37-5">mod3_mo <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brm</span>(</span>
<span id="cb37-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bf</span>(C14 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">se</span>(error, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mo</span>(Layer)),</span>
<span id="cb37-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gaussian</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>),</span>
<span id="cb37-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prior =</span> priors,</span>
<span id="cb37-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> table1,</span>
<span id="cb37-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4321</span></span>
<span id="cb37-11">)</span></code></pre></div>
</div>
<p>Let’s see the monotonic constraint in action:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1">grid_with_mu <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> grid_with_mu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb38-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_linpred_rvars</span>(mod3_mo, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".mu_mo"</span>)</span>
<span id="cb38-3"></span>
<span id="cb38-4">is_stratified <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(grid_with_mu, {</span>
<span id="cb38-5">  .mu_mo[Layer <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> .mu_mo[Layer <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span></span>
<span id="cb38-6">    .mu_mo[Layer <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> .mu_mo[Layer <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"E"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span></span>
<span id="cb38-7">    .mu_mo[Layer <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"E"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> .mu_mo[Layer <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"G"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span></span>
<span id="cb38-8">    .mu_mo[Layer <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"G"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> .mu_mo[Layer <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"I"</span>]</span>
<span id="cb38-9">})</span>
<span id="cb38-10"></span>
<span id="cb38-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Pr</span>(is_stratified) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Should be 1!</span></span>
<span id="cb38-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] 1</span></span></code></pre></div>
</div>
<p>Once again, all <em>three</em> constrained posteriors sit snugly on top of each other.</p>
<div class="cell">
<details class="code-fold">
<summary>Plot Code</summary>
<div class="sourceCode cell-code" id="cb39" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb39-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(grid_with_mu, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Layer)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb39-2">  </span>
<span id="cb39-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_slabinterval</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xdist =</span> .mu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill_ramp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">after_stat</span>(level), </span>
<span id="cb39-4">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"None"</span>),</span>
<span id="cb39-5">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">point_interval =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"median_hdci"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.width =</span> ci_levels) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb39-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_slabinterval</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xdist =</span> .mu_constrained, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill_ramp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">after_stat</span>(level),</span>
<span id="cb39-7">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Post Sampling"</span>),</span>
<span id="cb39-8">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">point_interval =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"median_hdci"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.width =</span> ci_levels,</span>
<span id="cb39-9">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">side =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb39-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_slabinterval</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xdist =</span> .mu_con_prior, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill_ramp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">after_stat</span>(level),</span>
<span id="cb39-11">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A-Priori"</span>),</span>
<span id="cb39-12">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">point_interval =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"median_hdci"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.width =</span> ci_levels,</span>
<span id="cb39-13">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">side =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb39-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_slabinterval</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xdist =</span> .mu_mo, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill_ramp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">after_stat</span>(level),</span>
<span id="cb39-15">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Monotonic"</span>),</span>
<span id="cb39-16">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">point_interval =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"median_hdci"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.width =</span> ci_levels,</span>
<span id="cb39-17">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">side =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb39-18">  </span>
<span id="cb39-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Constraint:"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"None"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Post Sampling"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A-Priori"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Monotonic"</span>),</span>
<span id="cb39-20">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"royalblue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"firebrick2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tomato"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"violetred"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb39-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Year"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> big_number) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb39-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_discrete</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> rev) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb39-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_ramp_discrete</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.translate =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">guide =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb39-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_thickness_shared</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2023-06-26-order-constraints-in-brms/index_files/figure-html/unnamed-chunk-38-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Although the <code>mo()</code> method does constrain the differences between levels to be monotonic, it does not constrain (a-priori) the direction of the differences. However, in this model, that is of no consequence – the posterior direction is completely negative:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb40" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1">bsp_moLayer <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gather_rvars</span>(mod3_mo, bsp_moLayer)</span>
<span id="cb40-2"></span>
<span id="cb40-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Pr</span>(bsp_moLayer<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>.value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb40-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] 1</span></span></code></pre></div>
</div>
<p>I highly recommend reading the <a href="https://paul-buerkner.github.io/brms/articles/brms_monotonic.html"><code>{brms}</code> vignette on monotonic effects</a>, which goes into much more details than I could here.</p>
</section>
<section id="concluding-remarks" class="level1">
<h1>Concluding Remarks</h1>
<p>Order constraints can be applied in a variety of models. For example, to examine if the direction of psychological effects is homogeneous <span class="citation" data-cites="haaf2017developing">(see Haaf &amp; Rouder, 2017)</span>.</p>
<p>Such constraints can also be tested with <a href="https://easystats.github.io/bayestestR/articles/bayes_factors.html#bayesfactor_restricted">Bayes factors</a> – comparing the unconstrained model to the constrained model (and constrained model to <em>any</em> other model). Unlike other Bayes factors, these Order Bayes factors are not (as) sensitive to the width of (0-centered) priors, and often it is relatively straightforward to elicit order constraints from theoretical models <span class="citation" data-cites="morey2014simple heck2015testing">(Heck, Wagenmakers, &amp; Morey, 2015; Morey &amp; Wagenmakers, 2014)</span>.</p>
<p>In this post we’ve seen how to define and apply such constraints <em>a-priori</em> and <em>post-sampling</em>. The consensus seems to be that post-sampling constraints are preferable – and here we’ve seen how easy it is to work with post-sampling constraints with post-sampling packages such as <code>{posterior}</code>, <code>{ggdist}</code>, <code>{bayestestR}</code>, <code>{emmeans}</code> and <code>{marginaleffects}</code>, as well as with post-sampling functions within <code>{brms}</code> itself.</p>
<p>Overall, order constraints are amazingly powerful tools to add to your Bayesian utility belt: they are easy – almost natural – to think about, and easy to implement (especially post-sampling). I hope you use them to <em>unearth</em> some new truths!</p>
<div class="callout callout-style-simple callout-note no-icon callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Hey, Thanks!
</div>
</div>
<div class="callout-body-container callout-body">
<p>I’d like to thank <a href="https://solomonkurz.netlify.app/">Solomon Kurz</a> (<a href="https://twitter.com/SolomonKurz">@SolomonKurz</a>) for taking the time to review this post and for offering useful feedback.</p>
</div>
</div>
<div class="cell">
<details class="code-fold">
<summary>Session Info</summary>
<div class="sourceCode cell-code" id="cb41" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb41-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sessionInfo</span>()</span>
<span id="cb41-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; R version 4.2.2 (2022-10-31 ucrt)</span></span>
<span id="cb41-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Platform: x86_64-w64-mingw32/x64 (64-bit)</span></span>
<span id="cb41-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Running under: Windows 10 x64 (build 22621)</span></span>
<span id="cb41-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb41-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Matrix products: default</span></span>
<span id="cb41-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb41-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; locale:</span></span>
<span id="cb41-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] LC_COLLATE=English_Israel.utf8  LC_CTYPE=English_Israel.utf8   </span></span>
<span id="cb41-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [3] LC_MONETARY=English_Israel.utf8 LC_NUMERIC=C                   </span></span>
<span id="cb41-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [5] LC_TIME=English_Israel.utf8    </span></span>
<span id="cb41-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb41-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; attached base packages:</span></span>
<span id="cb41-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] stats     graphics  grDevices utils     datasets  methods   base     </span></span>
<span id="cb41-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb41-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; other attached packages:</span></span>
<span id="cb41-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [1] bayestestR_0.13.1    tidybayes_3.0.4      ggdist_3.3.0        </span></span>
<span id="cb41-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [4] posterior_1.4.1.9000 brms_2.19.0          Rcpp_1.0.10         </span></span>
<span id="cb41-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [7] insight_0.19.2       scales_1.2.1         lubridate_1.9.2     </span></span>
<span id="cb41-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [10] forcats_1.0.0        stringr_1.5.0        dplyr_1.1.2         </span></span>
<span id="cb41-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [13] purrr_1.0.1          readr_2.1.4          tidyr_1.3.0         </span></span>
<span id="cb41-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [16] tibble_3.2.1         ggplot2_3.4.2        tidyverse_2.0.0     </span></span>
<span id="cb41-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb41-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; loaded via a namespace (and not attached):</span></span>
<span id="cb41-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   [1] TH.data_1.1-2        colorspace_2.1-0     ellipsis_0.3.2      </span></span>
<span id="cb41-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   [4] estimability_1.4.1   markdown_1.7         base64enc_0.1-3     </span></span>
<span id="cb41-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   [7] rstudioapi_0.14      farver_2.1.1         rstan_2.26.13       </span></span>
<span id="cb41-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [10] svUnit_1.0.6         DT_0.28              fansi_1.0.4         </span></span>
<span id="cb41-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [13] mvtnorm_1.2-2        splines_4.2.2        bridgesampling_1.1-2</span></span>
<span id="cb41-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [16] codetools_0.2-18     knitr_1.43           shinythemes_1.2.0   </span></span>
<span id="cb41-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [19] MSBMisc_0.0.1.14     bayesplot_1.10.0     jsonlite_1.8.5      </span></span>
<span id="cb41-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [22] shiny_1.7.4          compiler_4.2.2       emmeans_1.8.6       </span></span>
<span id="cb41-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [25] backports_1.4.1      Matrix_1.5-4.1       fastmap_1.1.1       </span></span>
<span id="cb41-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [28] cli_3.6.1            later_1.3.1          htmltools_0.5.5     </span></span>
<span id="cb41-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [31] prettyunits_1.1.1    tools_4.2.2          igraph_1.4.3        </span></span>
<span id="cb41-36"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [34] coda_0.19-4          gtable_0.3.3         glue_1.6.2          </span></span>
<span id="cb41-37"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [37] reshape2_1.4.4       V8_4.3.0             vctrs_0.6.2         </span></span>
<span id="cb41-38"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [40] nlme_3.1-160         crosstalk_1.2.0      tensorA_0.36.2      </span></span>
<span id="cb41-39"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [43] xfun_0.39            ps_1.7.5             timechange_0.2.0    </span></span>
<span id="cb41-40"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [46] mime_0.12            miniUI_0.1.1.1       lifecycle_1.0.3     </span></span>
<span id="cb41-41"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [49] gtools_3.9.4         MASS_7.3-58.1        zoo_1.8-12          </span></span>
<span id="cb41-42"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [52] colourpicker_1.2.0   hms_1.1.3            promises_1.2.0.1    </span></span>
<span id="cb41-43"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [55] Brobdingnag_1.2-9    sandwich_3.0-2       parallel_4.2.2      </span></span>
<span id="cb41-44"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [58] inline_0.3.19        shinystan_2.6.0      yaml_2.3.7          </span></span>
<span id="cb41-45"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [61] curl_5.0.1           gridExtra_2.3        loo_2.6.0           </span></span>
<span id="cb41-46"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [64] StanHeaders_2.26.26  stringi_1.7.12       dygraphs_1.1.1.6    </span></span>
<span id="cb41-47"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [67] checkmate_2.2.0      pkgbuild_1.4.0       cmdstanr_0.5.3      </span></span>
<span id="cb41-48"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [70] rlang_1.1.1          pkgconfig_2.0.3      matrixStats_1.0.0   </span></span>
<span id="cb41-49"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [73] distributional_0.3.2 evaluate_0.21        lattice_0.20-45     </span></span>
<span id="cb41-50"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [76] patchwork_1.1.2      labeling_0.4.2       rstantools_2.3.1    </span></span>
<span id="cb41-51"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [79] htmlwidgets_1.6.2    tidyselect_1.2.0     processx_3.8.1      </span></span>
<span id="cb41-52"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [82] plyr_1.8.8           magrittr_2.0.3       R6_2.5.1            </span></span>
<span id="cb41-53"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [85] generics_0.1.3       multcomp_1.4-24      pillar_1.9.0        </span></span>
<span id="cb41-54"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [88] withr_2.5.0          xts_0.13.1           datawizard_0.7.1    </span></span>
<span id="cb41-55"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [91] survival_3.4-0       abind_1.4-5          crayon_1.5.2        </span></span>
<span id="cb41-56"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [94] arrayhelpers_1.1-0   utf8_1.2.3           tzdb_0.4.0          </span></span>
<span id="cb41-57"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  [97] rmarkdown_2.22       isoband_0.2.7        grid_4.2.2          </span></span>
<span id="cb41-58"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [100] callr_3.7.3          threejs_0.3.3        digest_0.6.31       </span></span>
<span id="cb41-59"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [103] xtable_1.8-4         httpuv_1.6.11        RcppParallel_5.1.7  </span></span>
<span id="cb41-60"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [106] stats4_4.2.2         munsell_0.5.0        viridisLite_0.4.2   </span></span>
<span id="cb41-61"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [109] shinyjs_2.1.0</span></span></code></pre></div>
</details>
</div>



</section>


<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0" data-line-spacing="2">
<div id="ref-buck2017bayesian" class="csl-entry">
Buck, C. E., &amp; Juarez, M. (2017). Bayesian radiocarbon modelling for beginners. <em>arXiv Preprint arXiv:1704.07141</em>. <a href="https://doi.org/10.48550/arXiv.1704.07141">https://doi.org/10.48550/arXiv.1704.07141</a>
</div>
<div id="ref-burkner2017brms" class="csl-entry">
Bürkner, P.-C. (2017). Brms: An r package for bayesian multilevel models using stan. <em>Journal of Statistical Software</em>, <em>80</em>, 1–28. <a href="https://doi.org/10.18637/jss.v080.i01">https://doi.org/10.18637/jss.v080.i01</a>
</div>
<div id="ref-burkner2020modelling" class="csl-entry">
Bürkner, P.-C., &amp; Charpentier, E. (2020). Modelling monotonic effects of ordinal predictors in bayesian regression models. <em>British Journal of Mathematical and Statistical Psychology</em>, <em>73</em>(3), 420–451. <a href="https://doi.org/10.1111/bmsp.12195">https://doi.org/10.1111/bmsp.12195</a>
</div>
<div id="ref-gelfand1992bayesian" class="csl-entry">
Gelfand, A. E., Smith, A. F., &amp; Lee, T.-M. (1992). Bayesian analysis of constrained parameter and truncated data problems using gibbs sampling. <em>Journal of the American Statistical Association</em>, <em>87</em>(418), 523–532. <a href="https://doi.org/10.1080/01621459.1992.10475235">https://doi.org/10.1080/01621459.1992.10475235</a>
</div>
<div id="ref-haaf2017developing" class="csl-entry">
Haaf, J. M., &amp; Rouder, J. N. (2017). Developing constraint in bayesian mixed models. <em>Psychological Methods</em>, <em>22</em>(4), 779–798. <a href="https://doi.org/10.1037/met0000156">https://doi.org/10.1037/met0000156</a>
</div>
<div id="ref-heck2015testing" class="csl-entry">
Heck, D. W., Wagenmakers, E.-J., &amp; Morey, R. D. (2015). Testing order constraints: Qualitative differences between bayes factors and normalized maximum likelihood. <em>Statistics &amp; Probability Letters</em>, <em>105</em>, 157–162. <a href="https://doi.org/10.1016/j.spl.2015.06.014">https://doi.org/10.1016/j.spl.2015.06.014</a>
</div>
<div id="ref-morey2014simple" class="csl-entry">
Morey, R. D., &amp; Wagenmakers, E.-J. (2014). Simple relation between bayesian order-restricted and point-null hypothesis tests. <em>Statistics &amp; Probability Letters</em>, <em>92</em>, 121–124. <a href="https://doi.org/10.1016/j.spl.2014.05.010">https://doi.org/10.1016/j.spl.2014.05.010</a>
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>It was a podcast about the ancient near east, <a href="https://www.osimhistoria.com/osim-tanach">in Hebrew</a>, featuring the archaeologist Israel Finkelstein. Among the many interesting things Finkelstein said, he also blurted:</p>
<p>“… which we modeled with Bayesian methods, and found that…”</p>
<p>And that’s was it. No further details were provided.</p>
<p>This was enough to send me down a rabit hole – the end product of which is this blog post and the final lesson in my <em>Bayesian Modeling</em> course for undergrad statisticians.</p>
<p>What a ride.↩︎</p></li>
<li id="fn2"><p>The keen-eyed reader will also notice that the effective number of parameters (<code>p_loo</code>) is smaller – the constrained model, naturally, has <em>fewer</em> free parameters.)↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>R</category>
  <category>statistics</category>
  <category>code</category>
  <category>Bayes</category>
  <category>Stan</category>
  <guid>https://blog.msbstats.info/posts/2023-06-26-order-constraints-in-brms/</guid>
  <pubDate>Sun, 25 Jun 2023 21:00:00 GMT</pubDate>
</item>
<item>
  <title>Ordinal Regression as a Model for Signal Detection</title>
  <link>https://blog.msbstats.info/posts/2022-10-06-SDT-ordinal-regression/</link>
  <description><![CDATA[ 





<section id="preface" class="level1">
<h1>Preface</h1>
<p>I was basically done with this blog post when I came across <a href="https://mvuorre.github.io/posts/2017-10-09-bayesian-estimation-of-signal-detection-theory-models/">Matti Vuorre’s post on the same exact topic</a>. Matti goes into <em>all</em> the details, and really the present post can be seen as a brief account of all the cool things the probit-approach-to-SDT can do. I’m only posting this here because I really like my plots 🤷</p>
<hr>
<p><a href="https://blog.msbstats.info/posts/2019-07-29-sdt-logistic-regression/">Previously</a>, we’ve seen that for data from a binary decision signal detection task, we can use a <em>probit binomial</em> regression model (like a logistic regression, but with a probit link function) to estimate the two main parameters of <em>signal detection theory</em> (SDT): the sensitivity and the bias.</p>
<p>In this post I would like to show how this idea can be extended to multiple response SDT tasks by using an <em>ordinal probit</em> regression model.</p>
</section>
<section id="the-data" class="level1">
<h1>The Data</h1>
<p>Imagine the following task: after being presented with 20 images of dogs, you are presented with 300 new images of dogs, and you have to decide for each dog if it appeared in the training set (“Old”) or not (“New”).</p>
<!-- ![]() -->
<!-- image of dog -->
<p>In a binary decision task, you would simply indicate “New” or “Old”, but in this task you have multiple response options - from 1 to 6, with 1 = “Feels New” and 6 = “Feels Old”. We can call this scale a <a href="https://twitter.com/metallicbranch/status/1577170590053543936">“feelings numbers” scale</a>.</p>
<p>After going over all 30 photos, you have</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">STD_data</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 12 × 3
   Truth Response        N
   &lt;fct&gt; &lt;ord&gt;       &lt;dbl&gt;
 1 New   Confidence1    35
 2 New   Confidence2    31
 3 New   Confidence3    26
 4 New   Confidence4    22
 5 New   Confidence5    19
 6 New   Confidence6    17
 7 Old   Confidence1    14
 8 Old   Confidence2    20
 9 Old   Confidence3    22
10 Old   Confidence4    27
11 Old   Confidence5    32
12 Old   Confidence6    35</code></pre>
</div>
</div>
<p>Where <em>N</em> is the number of responses in each condition and response level.</p>
</section>
<section id="modeling-with-classic-sdt" class="level1">
<h1>Modeling with Classic SDT</h1>
<p>We can use <a href="https://www.unifr.ch/psycho/fr/assets/public/Forschungseinheiten/sdt/SDT.pdf">Siegfried Macho</a>’s R code to extract the SDT parameters. In this case, they are:</p>
<ol type="1">
<li><em>Sensitivity</em> - The distance between the two (latent) normal distributions. The further they are, the more “distinguishable” the Old and New images are from each other.<br>
</li>
<li><em>5 Threshold</em> - One between each pair of consecutive possible responses. Perceived “stimulation” above each threshold leads to a decision in that category.</li>
</ol>
<p>(These will probably make sense when we present them visually below.)</p>
<p>First, we’ll model this with classical SDT:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">SDT_equal <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">SDT.Estimate</span>(</span>
<span id="cb3-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> STD_data[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N"</span>]],</span>
<span id="cb3-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">test =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb3-4">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># We have 2 option: Old / New; We'll assume equal variance</span></span>
<span id="cb3-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n.sdt =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">restriction =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"equalvar"</span>)</span>
<span id="cb3-6">) </span>
<span id="cb3-7"></span>
<span id="cb3-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">SDT.Statistics</span>(SDT_equal)[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Free.parameters"</span>]]</span></code></pre></div>
</div>
<div class="cell">
<div class="cell-output cell-output-stdout">
<pre><code>         Value    SE CFI-95(Lower) CFI-95(Upper)
Mean[2]  0.564 0.040         0.486         0.642
t-1     -0.744 0.034        -0.810        -0.678
t-2     -0.165 0.031        -0.226        -0.104
t-3      0.267 0.031         0.206         0.329
t-4      0.707 0.033         0.643         0.772
t-5      1.260 0.036         1.189         1.331</code></pre>
</div>
</div>
</section>
<section id="modeling-as-a-probit-cumulative-ordinal" class="level1">
<h1>Modeling as a Probit Cumulative Ordinal</h1>
<div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1.1.4</span></span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyr)      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1.3.0</span></span>
<span id="cb5-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ordinal)    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2022.11.16</span></span>
<span id="cb5-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(parameters) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0.21.3</span></span>
<span id="cb5-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3.4.4</span></span>
<span id="cb5-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(patchwork)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1.1.3 </span></span></code></pre></div>
<p>We can also model this data with a Probit Cumulative Ordinal model, predicting the <em>Response</em> from the <em>Truth</em>: - The slope of <em>Truth</em> indicates the effect of the true image identity had on the response pattern - this is <strong><em>sensitivity</em></strong>.<br>
- In an ordinal model, we get <em>k</em>-1 “intercepts” (<em>k</em> being the number of unique responses). Each intercept represents the value above which a predicted value will be binned into the next class. There represent our <strong><em>shreshold</em></strong>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">m_equal <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">clm</span>(Response <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Truth, </span>
<span id="cb6-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> STD_data,</span>
<span id="cb6-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> N,</span>
<span id="cb6-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">link =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"probit"</span></span>
<span id="cb6-5">)</span>
<span id="cb6-6"></span>
<span id="cb6-7">parameters<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model_parameters</span>(m_equal) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb6-8">  insight<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print_html</span>()</span></code></pre></div>
<div class="cell-output-display">
<div id="ikewgnbcpe" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#ikewgnbcpe table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#ikewgnbcpe thead, #ikewgnbcpe tbody, #ikewgnbcpe tfoot, #ikewgnbcpe tr, #ikewgnbcpe td, #ikewgnbcpe th {
  border-style: none;
}

#ikewgnbcpe p {
  margin: 0;
  padding: 0;
}

#ikewgnbcpe .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 100%;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#ikewgnbcpe .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#ikewgnbcpe .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#ikewgnbcpe .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#ikewgnbcpe .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#ikewgnbcpe .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#ikewgnbcpe .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#ikewgnbcpe .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#ikewgnbcpe .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#ikewgnbcpe .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#ikewgnbcpe .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#ikewgnbcpe .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#ikewgnbcpe .gt_spanner_row {
  border-bottom-style: hidden;
}

#ikewgnbcpe .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#ikewgnbcpe .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#ikewgnbcpe .gt_from_md > :first-child {
  margin-top: 0;
}

#ikewgnbcpe .gt_from_md > :last-child {
  margin-bottom: 0;
}

#ikewgnbcpe .gt_row {
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#ikewgnbcpe .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#ikewgnbcpe .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#ikewgnbcpe .gt_row_group_first td {
  border-top-width: 2px;
}

#ikewgnbcpe .gt_row_group_first th {
  border-top-width: 2px;
}

#ikewgnbcpe .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#ikewgnbcpe .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#ikewgnbcpe .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#ikewgnbcpe .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#ikewgnbcpe .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#ikewgnbcpe .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#ikewgnbcpe .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#ikewgnbcpe .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#ikewgnbcpe .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#ikewgnbcpe .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#ikewgnbcpe .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#ikewgnbcpe .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#ikewgnbcpe .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#ikewgnbcpe .gt_left {
  text-align: left;
}

#ikewgnbcpe .gt_center {
  text-align: center;
}

#ikewgnbcpe .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#ikewgnbcpe .gt_font_normal {
  font-weight: normal;
}

#ikewgnbcpe .gt_font_bold {
  font-weight: bold;
}

#ikewgnbcpe .gt_font_italic {
  font-style: italic;
}

#ikewgnbcpe .gt_super {
  font-size: 65%;
}

#ikewgnbcpe .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#ikewgnbcpe .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#ikewgnbcpe .gt_indent_1 {
  text-indent: 5px;
}

#ikewgnbcpe .gt_indent_2 {
  text-indent: 10px;
}

#ikewgnbcpe .gt_indent_3 {
  text-indent: 15px;
}

#ikewgnbcpe .gt_indent_4 {
  text-indent: 20px;
}

#ikewgnbcpe .gt_indent_5 {
  text-indent: 25px;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
<thead>
<tr class="header gt_heading">
<th colspan="6" class="gt_heading gt_title gt_font_normal gt_bottom_border">Model Summary</th>
</tr>
<tr class="odd gt_col_headings">
<th id="Parameter" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col">Parameter</th>
<th id="Coefficient" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col">Coefficient</th>
<th id="SE" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col">SE</th>
<th id="95% CI" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col">95% CI</th>
<th id="z" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col">z</th>
<th id="p" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col">p</th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd gt_group_heading_row">
<td colspan="6" id="Intercept " class="gt_group_heading" data-quarto-table-cell-role="th" scope="colgroup">Intercept</td>
</tr>
<tr class="even gt_row_group_first">
<td class="gt_row gt_left" headers="Intercept   Parameter" style="border-right-width: 1px; border-right-style: solid; border-right-color: #d3d3d3">Confidence1|Confidence2</td>
<td class="gt_row gt_center" headers="Intercept   Coefficient">-0.74</td>
<td class="gt_row gt_center" headers="Intercept   SE">0.10</td>
<td class="gt_row gt_center" headers="Intercept   95% CI">(-0.94, -0.54)</td>
<td class="gt_row gt_center" headers="Intercept   z">-7.21</td>
<td class="gt_row gt_center" headers="Intercept   p">&lt; .001</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="Intercept   Parameter" style="border-right-width: 1px; border-right-style: solid; border-right-color: #d3d3d3">Confidence2|Confidence3</td>
<td class="gt_row gt_center" headers="Intercept   Coefficient">-0.16</td>
<td class="gt_row gt_center" headers="Intercept   SE">0.10</td>
<td class="gt_row gt_center" headers="Intercept   95% CI">(-0.35, 0.02)</td>
<td class="gt_row gt_center" headers="Intercept   z">-1.72</td>
<td class="gt_row gt_center" headers="Intercept   p">0.085</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="Intercept   Parameter" style="border-right-width: 1px; border-right-style: solid; border-right-color: #d3d3d3">Confidence3|Confidence4</td>
<td class="gt_row gt_center" headers="Intercept   Coefficient">0.27</td>
<td class="gt_row gt_center" headers="Intercept   SE">0.10</td>
<td class="gt_row gt_center" headers="Intercept   95% CI">(0.08, 0.46)</td>
<td class="gt_row gt_center" headers="Intercept   z">2.81</td>
<td class="gt_row gt_center" headers="Intercept   p">0.005</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="Intercept   Parameter" style="border-right-width: 1px; border-right-style: solid; border-right-color: #d3d3d3">Confidence4|Confidence5</td>
<td class="gt_row gt_center" headers="Intercept   Coefficient">0.71</td>
<td class="gt_row gt_center" headers="Intercept   SE">0.10</td>
<td class="gt_row gt_center" headers="Intercept   95% CI">(0.51, 0.90)</td>
<td class="gt_row gt_center" headers="Intercept   z">7.08</td>
<td class="gt_row gt_center" headers="Intercept   p">&lt; .001</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="Intercept   Parameter" style="border-right-width: 1px; border-right-style: solid; border-right-color: #d3d3d3">Confidence5|Confidence6</td>
<td class="gt_row gt_center" headers="Intercept   Coefficient">1.26</td>
<td class="gt_row gt_center" headers="Intercept   SE">0.11</td>
<td class="gt_row gt_center" headers="Intercept   95% CI">(1.05, 1.48)</td>
<td class="gt_row gt_center" headers="Intercept   z">11.38</td>
<td class="gt_row gt_center" headers="Intercept   p">&lt; .001</td>
</tr>
<tr class="odd gt_group_heading_row">
<td colspan="6" id="Location Parameters " class="gt_group_heading" data-quarto-table-cell-role="th" scope="colgroup">Location Parameters</td>
</tr>
<tr class="even gt_row_group_first">
<td class="gt_row gt_left" headers="Location Parameters   Parameter" style="border-right-width: 1px; border-right-style: solid; border-right-color: #d3d3d3">Truth (Old)</td>
<td class="gt_row gt_center" headers="Location Parameters   Coefficient">0.57</td>
<td class="gt_row gt_center" headers="Location Parameters   SE">0.12</td>
<td class="gt_row gt_center" headers="Location Parameters   95% CI">(0.33, 0.81)</td>
<td class="gt_row gt_center" headers="Location Parameters   z">4.65</td>
<td class="gt_row gt_center" headers="Location Parameters   p">&lt; .001</td>
</tr>
</tbody><tfoot class="gt_sourcenotes">
<tr class="even">
<td colspan="6" class="gt_sourcenote"></td>
</tr>
</tfoot>

</table>

</div>
</div>
</div>
<p>As we can see, the estimated values are identical!<sup>1</sup></p>
<p>The advantage of the probit ordinal model is that it is easy(er) to build this model up:</p>
<ul>
<li>Add predictors of sensitivity (interactions with <code>Truth</code>)<br>
</li>
<li>Add predictors of bias (main effects / intercept effects)<br>
</li>
<li>Add random effects (with <code>ordinal::clmm()</code>)</li>
</ul>
<p>(<a href="https://mvuorre.github.io/posts/2017-10-09-bayesian-estimation-of-signal-detection-theory-models/#unequal-variance-gaussian-sdt-model"><em>See Matti’s post for actual examples!</em></a>)</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r fold-hide code-with-copy"><code class="sourceCode r"><span id="cb7-1">mean2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(m_equal)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>]</span>
<span id="cb7-2"></span>
<span id="cb7-3">Thresholds <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(m_equal)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>]</span>
<span id="cb7-4"></span>
<span id="cb7-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb7-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Noise</span></span>
<span id="cb7-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_function</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Noise"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> dnorm,</span>
<span id="cb7-8">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb7-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Noise + Signal</span></span>
<span id="cb7-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_function</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Noise + Signal"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> dnorm, </span>
<span id="cb7-11">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">args =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> mean2),</span>
<span id="cb7-12">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb7-13">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Thresholds</span></span>
<span id="cb7-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> Thresholds, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(Thresholds)),</span>
<span id="cb7-15">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb7-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Threshold"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"div"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb7-17">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" | "</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb7-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Obs. signal"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb7-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand_limits</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.45</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb7-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2022-10-06-SDT-ordinal-regression/index_files/figure-html/CLM-eq-plot-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="unequal-variance" class="level1">
<h1>Unequal Variance</h1>
<p>The standard model of SDT assumes that the <em>Noise</em> and the <em>Noise + Signal</em> distribution differ only in their mean; that is, <em>N+S</em> is a shifted <em>N</em> distribution. This is almost always not true, with <img src="https://latex.codecogs.com/png.latex?%5Csigma_%7B%5Ctext%7BN+S%7D%7D%3E%5Csigma_%7B%5Ctext%7BN%7D%7D">.</p>
<p>To deal with this, we can also estimate the variance of the <em>N+S</em> distribution.</p>
<p>First, with the classic SDT model:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">SDT_unequal <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">SDT.Estimate</span>(</span>
<span id="cb8-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> STD_data[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N"</span>]],</span>
<span id="cb8-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">test =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb8-4">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># We have 2 option: Old / New; Not assuming equal variance</span></span>
<span id="cb8-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n.sdt =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">restriction =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"no"</span>)</span>
<span id="cb8-6">) </span>
<span id="cb8-7"></span>
<span id="cb8-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">SDT.Statistics</span>(SDT_unequal)[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Free.parameters"</span>]]</span></code></pre></div>
</div>
<div class="cell">
<div class="cell-output cell-output-stdout">
<pre><code>           Value    SE CFI-95(Lower) CFI-95(Upper)
Mean[2]    0.552 0.041         0.473         0.632
Stddev[2]  0.960 0.035         0.891         1.029
t-1       -0.728 0.036        -0.799        -0.658
t-2       -0.159 0.032        -0.221        -0.097
t-3        0.266 0.031         0.205         0.327
t-4        0.696 0.034         0.629         0.763
t-5        1.235 0.043         1.151         1.318</code></pre>
</div>
</div>
<p>And with a probit ordinal regression, but allow the latent scale to vary:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">m_unequal <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">clm</span>(Response <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Truth, </span>
<span id="cb10-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Truth, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># We indicate that the scale is a function of the underlying dist</span></span>
<span id="cb10-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> STD_data, </span>
<span id="cb10-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> N,</span>
<span id="cb10-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">link =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"probit"</span></span>
<span id="cb10-6">)</span>
<span id="cb10-7"></span>
<span id="cb10-8">parameters<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model_parameters</span>(m_unequal) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb10-9">  insight<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print_html</span>()</span></code></pre></div>
<div class="cell-output-display">
<div id="hnisrnndxp" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#hnisrnndxp table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#hnisrnndxp thead, #hnisrnndxp tbody, #hnisrnndxp tfoot, #hnisrnndxp tr, #hnisrnndxp td, #hnisrnndxp th {
  border-style: none;
}

#hnisrnndxp p {
  margin: 0;
  padding: 0;
}

#hnisrnndxp .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 100%;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#hnisrnndxp .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#hnisrnndxp .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#hnisrnndxp .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#hnisrnndxp .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#hnisrnndxp .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#hnisrnndxp .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#hnisrnndxp .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#hnisrnndxp .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#hnisrnndxp .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#hnisrnndxp .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#hnisrnndxp .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#hnisrnndxp .gt_spanner_row {
  border-bottom-style: hidden;
}

#hnisrnndxp .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#hnisrnndxp .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#hnisrnndxp .gt_from_md > :first-child {
  margin-top: 0;
}

#hnisrnndxp .gt_from_md > :last-child {
  margin-bottom: 0;
}

#hnisrnndxp .gt_row {
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#hnisrnndxp .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#hnisrnndxp .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#hnisrnndxp .gt_row_group_first td {
  border-top-width: 2px;
}

#hnisrnndxp .gt_row_group_first th {
  border-top-width: 2px;
}

#hnisrnndxp .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#hnisrnndxp .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#hnisrnndxp .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#hnisrnndxp .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#hnisrnndxp .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#hnisrnndxp .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#hnisrnndxp .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#hnisrnndxp .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#hnisrnndxp .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#hnisrnndxp .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#hnisrnndxp .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#hnisrnndxp .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#hnisrnndxp .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#hnisrnndxp .gt_left {
  text-align: left;
}

#hnisrnndxp .gt_center {
  text-align: center;
}

#hnisrnndxp .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#hnisrnndxp .gt_font_normal {
  font-weight: normal;
}

#hnisrnndxp .gt_font_bold {
  font-weight: bold;
}

#hnisrnndxp .gt_font_italic {
  font-style: italic;
}

#hnisrnndxp .gt_super {
  font-size: 65%;
}

#hnisrnndxp .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#hnisrnndxp .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#hnisrnndxp .gt_indent_1 {
  text-indent: 5px;
}

#hnisrnndxp .gt_indent_2 {
  text-indent: 10px;
}

#hnisrnndxp .gt_indent_3 {
  text-indent: 15px;
}

#hnisrnndxp .gt_indent_4 {
  text-indent: 20px;
}

#hnisrnndxp .gt_indent_5 {
  text-indent: 25px;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
<thead>
<tr class="header gt_heading">
<th colspan="6" class="gt_heading gt_title gt_font_normal gt_bottom_border">Model Summary</th>
</tr>
<tr class="odd gt_col_headings">
<th id="Parameter" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col">Parameter</th>
<th id="Coefficient" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col">Coefficient</th>
<th id="SE" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col">SE</th>
<th id="95% CI" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col">95% CI</th>
<th id="z" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col">z</th>
<th id="p" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col">p</th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd gt_group_heading_row">
<td colspan="6" id="Intercept " class="gt_group_heading" data-quarto-table-cell-role="th" scope="colgroup">Intercept</td>
</tr>
<tr class="even gt_row_group_first">
<td class="gt_row gt_left" headers="Intercept   Parameter" style="border-right-width: 1px; border-right-style: solid; border-right-color: #d3d3d3">Confidence1|Confidence2</td>
<td class="gt_row gt_center" headers="Intercept   Coefficient">-0.72</td>
<td class="gt_row gt_center" headers="Intercept   SE">0.11</td>
<td class="gt_row gt_center" headers="Intercept   95% CI">(-0.93, -0.50)</td>
<td class="gt_row gt_center" headers="Intercept   z">-6.51</td>
<td class="gt_row gt_center" headers="Intercept   p">&lt; .001</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="Intercept   Parameter" style="border-right-width: 1px; border-right-style: solid; border-right-color: #d3d3d3">Confidence2|Confidence3</td>
<td class="gt_row gt_center" headers="Intercept   Coefficient">-0.16</td>
<td class="gt_row gt_center" headers="Intercept   SE">0.10</td>
<td class="gt_row gt_center" headers="Intercept   95% CI">(-0.34, 0.03)</td>
<td class="gt_row gt_center" headers="Intercept   z">-1.61</td>
<td class="gt_row gt_center" headers="Intercept   p">0.107</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="Intercept   Parameter" style="border-right-width: 1px; border-right-style: solid; border-right-color: #d3d3d3">Confidence3|Confidence4</td>
<td class="gt_row gt_center" headers="Intercept   Coefficient">0.27</td>
<td class="gt_row gt_center" headers="Intercept   SE">0.10</td>
<td class="gt_row gt_center" headers="Intercept   95% CI">(0.08, 0.45)</td>
<td class="gt_row gt_center" headers="Intercept   z">2.80</td>
<td class="gt_row gt_center" headers="Intercept   p">0.005</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="Intercept   Parameter" style="border-right-width: 1px; border-right-style: solid; border-right-color: #d3d3d3">Confidence4|Confidence5</td>
<td class="gt_row gt_center" headers="Intercept   Coefficient">0.69</td>
<td class="gt_row gt_center" headers="Intercept   SE">0.10</td>
<td class="gt_row gt_center" headers="Intercept   95% CI">(0.49, 0.90)</td>
<td class="gt_row gt_center" headers="Intercept   z">6.66</td>
<td class="gt_row gt_center" headers="Intercept   p">&lt; .001</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="Intercept   Parameter" style="border-right-width: 1px; border-right-style: solid; border-right-color: #d3d3d3">Confidence5|Confidence6</td>
<td class="gt_row gt_center" headers="Intercept   Coefficient">1.23</td>
<td class="gt_row gt_center" headers="Intercept   SE">0.13</td>
<td class="gt_row gt_center" headers="Intercept   95% CI">(0.98, 1.49)</td>
<td class="gt_row gt_center" headers="Intercept   z">9.50</td>
<td class="gt_row gt_center" headers="Intercept   p">&lt; .001</td>
</tr>
<tr class="odd gt_group_heading_row">
<td colspan="6" id="Location Parameters " class="gt_group_heading" data-quarto-table-cell-role="th" scope="colgroup">Location Parameters</td>
</tr>
<tr class="even gt_row_group_first">
<td class="gt_row gt_left" headers="Location Parameters   Parameter" style="border-right-width: 1px; border-right-style: solid; border-right-color: #d3d3d3">Truth (Old)</td>
<td class="gt_row gt_center" headers="Location Parameters   Coefficient">0.55</td>
<td class="gt_row gt_center" headers="Location Parameters   SE">0.12</td>
<td class="gt_row gt_center" headers="Location Parameters   95% CI">(0.31, 0.80)</td>
<td class="gt_row gt_center" headers="Location Parameters   z">4.49</td>
<td class="gt_row gt_center" headers="Location Parameters   p">&lt; .001</td>
</tr>
<tr class="odd gt_group_heading_row">
<td colspan="6" id="Scale Parameters " class="gt_group_heading" data-quarto-table-cell-role="th" scope="colgroup">Scale Parameters</td>
</tr>
<tr class="even gt_row_group_first">
<td class="gt_row gt_left" headers="Scale Parameters   Parameter" style="border-right-width: 1px; border-right-style: solid; border-right-color: #d3d3d3">Truth (Old)</td>
<td class="gt_row gt_center" headers="Scale Parameters   Coefficient">-0.05</td>
<td class="gt_row gt_center" headers="Scale Parameters   SE">0.12</td>
<td class="gt_row gt_center" headers="Scale Parameters   95% CI">(0.31, 0.80)</td>
<td class="gt_row gt_center" headers="Scale Parameters   z">4.49</td>
<td class="gt_row gt_center" headers="Scale Parameters   p">&lt; .001</td>
</tr>
</tbody><tfoot class="gt_sourcenotes">
<tr class="even">
<td colspan="6" class="gt_sourcenote"></td>
</tr>
</tfoot>

</table>

</div>
</div>
</div>
<p>The scale parameter needs to be back transformed to get the sd of the <em>N+S</em> distribution: <img src="https://latex.codecogs.com/png.latex?e%5E%7B-0.05%7D=0.95">, and so one again the estimated values are identical!</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r fold-hide code-with-copy"><code class="sourceCode r"><span id="cb11-1">mean2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(m_unequal)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>]</span>
<span id="cb11-2">sd2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(m_unequal)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>])</span>
<span id="cb11-3"></span>
<span id="cb11-4">Thresholds <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(m_unequal)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>]</span>
<span id="cb11-5"></span>
<span id="cb11-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb11-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Noise</span></span>
<span id="cb11-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_function</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Noise"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> dnorm,</span>
<span id="cb11-9">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb11-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Noise + Signal</span></span>
<span id="cb11-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_function</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Noise + Signal"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> dnorm, </span>
<span id="cb11-12">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">args =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> mean2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> sd2),</span>
<span id="cb11-13">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb11-14">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Thresholds</span></span>
<span id="cb11-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> Thresholds, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(Thresholds)),</span>
<span id="cb11-16">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb11-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Threshold"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"div"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb11-18">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" | "</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb11-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Obs. signal"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb11-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand_limits</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.45</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb11-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2022-10-06-SDT-ordinal-regression/index_files/figure-html/CLM-uneq-plot-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>SDT with N and N+S distributions, and the 5 thresholds.</figcaption>
</figure>
</div>
</div>
</div>
<section id="roc-curve-or-roc-curves" class="level2">
<h2 class="anchored" data-anchor-id="roc-curve-or-roc-curves">ROC Curve or ROC Curve<em>s</em>?</h2>
<p>An additional check we can preform is whether the various responses are indeed the product of single ROC curve. We do this by plotting the ROC curve on a inv-normal transformation (that is, converting probabilities into normal quantiles). Quantiles that fall on a straight line indicate they are part of the same curve.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r fold-hide code-with-copy"><code class="sourceCode r"><span id="cb12-1">pred_table <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Truth =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Old"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"New"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb12-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(m_unequal, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pick</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">everything</span>()), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"prob"</span>)[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb12-3">  tidyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">starts_with</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Confidence"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Response"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb12-4">  tidyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_wider</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_from =</span> Truth)</span>
<span id="cb12-5"></span>
<span id="cb12-6">ROC_table <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> pred_table <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb12-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb12-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Sensitivity =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lag</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cumsum</span>(New), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">default =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>),</span>
<span id="cb12-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Specificity =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rev</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cumsum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rev</span>(Old))),</span>
<span id="cb12-10">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb12-11">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Some clean up...</span></span>
<span id="cb12-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rows_append</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Sensitivity =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Specificity =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb12-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb12-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Threshold =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"|"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>), <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>)</span>
<span id="cb12-15">  )</span>
<span id="cb12-16"></span>
<span id="cb12-17">p_roc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(ROC_table, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(Sensitivity, Specificity)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb12-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb12-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">slope =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">intercept =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb12-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ordered</span>(Threshold)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb12-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand_limits</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trans =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"reverse"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Threshold"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"div"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb12-24">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" | "</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>),</span>
<span id="cb12-25">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.translate =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb12-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb12-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span>
<span id="cb12-28"></span>
<span id="cb12-29">p_zroc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ROC_table <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb12-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qnorm</span>(Sensitivity), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qnorm</span>(Specificity))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb12-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb12-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ordered</span>(Threshold)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb12-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand_limits</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trans =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"reverse"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Threshold"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"div"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb12-36">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" | "</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>),</span>
<span id="cb12-37">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.translate =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb12-38">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Z(Sensitivity)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Z(Specificity)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb12-39">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span>
<span id="cb12-40"></span>
<span id="cb12-41">p_roc <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p_zroc <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">guides =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"collect"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing missing values (`geom_point()`).
Removed 2 rows containing missing values (`geom_point()`).</code></pre>
</div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2022-10-06-SDT-ordinal-regression/index_files/figure-html/ROC-plot-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Because the point (on the Z-scale) fall on a straight line, we can confidently say they represent the same ROC curve.</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="going-full-bayesian" class="level2">
<h2 class="anchored" data-anchor-id="going-full-bayesian">Going Full Bayesian</h2>
<p>Although the <code>clmm()</code> function allows for multilevel probit regression, it does not<sup>2</sup> support varying scale parameter.</p>
<p>Alas, we <em>must</em> use <code>brms</code>.</p>
<div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(brms)      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2.20.4</span></span>
<span id="cb14-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggdist)    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3.3.1.9000</span></span>
<span id="cb14-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidybayes) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3.0.6 </span></span></code></pre></div>
<p>In <code>brms</code> we will use the <code>cumulative()</code> family, which has a family-parameter called <code>disc</code> which gives the standard deviation of the latent distributions. I will set some weak priors on the mean and standard deviation of the <em>N+S</em> distribution, and I will also set the standard deviation of the <em>N</em> distribution to 1 (on a log scale, to 0) using the <code>constant(0)</code> prior.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">b_formula <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bf</span>(Response <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">weights</span>(N) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Truth,</span>
<span id="cb15-2">                disc <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Intercept <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Truth)</span>
<span id="cb15-3">b_priors <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> </span>
<span id="cb15-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_prior</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"normal(0, 3)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">coef =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TruthOld"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb15-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_prior</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"normal(0, 1.5)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">coef =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TruthOld"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dpar =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"disc"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb15-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_prior</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"constant(0)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">coef =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Intercept"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dpar =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"disc"</span>)</span>
<span id="cb15-7"></span>
<span id="cb15-8"></span>
<span id="cb15-9">Bayes_mod <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brm</span>(b_formula,</span>
<span id="cb15-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prior =</span> b_priors,</span>
<span id="cb15-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cumulative</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">link =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"probit"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">link_disc =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"log"</span>),</span>
<span id="cb15-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> STD_data,</span>
<span id="cb15-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">backend =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cmdstanr"</span>,</span>
<span id="cb15-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">refresh =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb15-15">)</span></code></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model_parameters</span>(Bayes_mod, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">test =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb16-2">  insight<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print_html</span>()</span></code></pre></div>
<div class="cell-output-display">
<div id="sctijbkghu" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#sctijbkghu table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#sctijbkghu thead, #sctijbkghu tbody, #sctijbkghu tfoot, #sctijbkghu tr, #sctijbkghu td, #sctijbkghu th {
  border-style: none;
}

#sctijbkghu p {
  margin: 0;
  padding: 0;
}

#sctijbkghu .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 100%;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#sctijbkghu .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#sctijbkghu .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#sctijbkghu .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#sctijbkghu .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#sctijbkghu .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#sctijbkghu .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#sctijbkghu .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#sctijbkghu .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#sctijbkghu .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#sctijbkghu .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#sctijbkghu .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#sctijbkghu .gt_spanner_row {
  border-bottom-style: hidden;
}

#sctijbkghu .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#sctijbkghu .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#sctijbkghu .gt_from_md > :first-child {
  margin-top: 0;
}

#sctijbkghu .gt_from_md > :last-child {
  margin-bottom: 0;
}

#sctijbkghu .gt_row {
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#sctijbkghu .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#sctijbkghu .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#sctijbkghu .gt_row_group_first td {
  border-top-width: 2px;
}

#sctijbkghu .gt_row_group_first th {
  border-top-width: 2px;
}

#sctijbkghu .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#sctijbkghu .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#sctijbkghu .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#sctijbkghu .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#sctijbkghu .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#sctijbkghu .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#sctijbkghu .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#sctijbkghu .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#sctijbkghu .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#sctijbkghu .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#sctijbkghu .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#sctijbkghu .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#sctijbkghu .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#sctijbkghu .gt_left {
  text-align: left;
}

#sctijbkghu .gt_center {
  text-align: center;
}

#sctijbkghu .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#sctijbkghu .gt_font_normal {
  font-weight: normal;
}

#sctijbkghu .gt_font_bold {
  font-weight: bold;
}

#sctijbkghu .gt_font_italic {
  font-style: italic;
}

#sctijbkghu .gt_super {
  font-size: 65%;
}

#sctijbkghu .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#sctijbkghu .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#sctijbkghu .gt_indent_1 {
  text-indent: 5px;
}

#sctijbkghu .gt_indent_2 {
  text-indent: 10px;
}

#sctijbkghu .gt_indent_3 {
  text-indent: 15px;
}

#sctijbkghu .gt_indent_4 {
  text-indent: 20px;
}

#sctijbkghu .gt_indent_5 {
  text-indent: 25px;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
<thead>
<tr class="header gt_heading">
<th colspan="5" class="gt_heading gt_title gt_font_normal gt_bottom_border">Model Summary</th>
</tr>
<tr class="odd gt_col_headings">
<th id="Parameter" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col">Parameter</th>
<th id="Median" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col">Median</th>
<th id="95% CI" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col">95% CI</th>
<th id="Rhat" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col">Rhat</th>
<th id="ESS" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col">ESS</th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<td class="gt_row gt_left" headers="Parameter" style="border-right-width: 1px; border-right-style: solid; border-right-color: #d3d3d3">Intercept(1)</td>
<td class="gt_row gt_center" headers="Median">-0.74</td>
<td class="gt_row gt_center" headers="95% CI">(-0.95, -0.52)</td>
<td class="gt_row gt_center" headers="Rhat">1.000</td>
<td class="gt_row gt_center" headers="ESS">2799.00</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="Parameter" style="border-right-width: 1px; border-right-style: solid; border-right-color: #d3d3d3">Intercept(2)</td>
<td class="gt_row gt_center" headers="Median">-0.16</td>
<td class="gt_row gt_center" headers="95% CI">(-0.36, 0.03)</td>
<td class="gt_row gt_center" headers="Rhat">1.000</td>
<td class="gt_row gt_center" headers="ESS">4556.00</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="Parameter" style="border-right-width: 1px; border-right-style: solid; border-right-color: #d3d3d3">Intercept(3)</td>
<td class="gt_row gt_center" headers="Median">0.27</td>
<td class="gt_row gt_center" headers="95% CI">(0.08, 0.46)</td>
<td class="gt_row gt_center" headers="Rhat">1.000</td>
<td class="gt_row gt_center" headers="ESS">5326.00</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="Parameter" style="border-right-width: 1px; border-right-style: solid; border-right-color: #d3d3d3">Intercept(4)</td>
<td class="gt_row gt_center" headers="Median">0.70</td>
<td class="gt_row gt_center" headers="95% CI">(0.50, 0.92)</td>
<td class="gt_row gt_center" headers="Rhat">1.000</td>
<td class="gt_row gt_center" headers="ESS">4285.00</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="Parameter" style="border-right-width: 1px; border-right-style: solid; border-right-color: #d3d3d3">Intercept(5)</td>
<td class="gt_row gt_center" headers="Median">1.26</td>
<td class="gt_row gt_center" headers="95% CI">(1.01, 1.52)</td>
<td class="gt_row gt_center" headers="Rhat">1.000</td>
<td class="gt_row gt_center" headers="ESS">2820.00</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="Parameter" style="border-right-width: 1px; border-right-style: solid; border-right-color: #d3d3d3">TruthOld</td>
<td class="gt_row gt_center" headers="Median">0.57</td>
<td class="gt_row gt_center" headers="95% CI">(0.32, 0.82)</td>
<td class="gt_row gt_center" headers="Rhat">1.000</td>
<td class="gt_row gt_center" headers="ESS">3627.00</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="Parameter" style="border-right-width: 1px; border-right-style: solid; border-right-color: #d3d3d3">disc_Intercept</td>
<td class="gt_row gt_center" headers="Median">0.00</td>
<td class="gt_row gt_center" headers="95% CI">(0.00, 0.00)</td>
<td class="gt_row gt_center" headers="Rhat"></td>
<td class="gt_row gt_center" headers="ESS"></td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="Parameter" style="border-right-width: 1px; border-right-style: solid; border-right-color: #d3d3d3">disc_TruthOld</td>
<td class="gt_row gt_center" headers="Median">0.02</td>
<td class="gt_row gt_center" headers="95% CI">(-0.20, 0.24)</td>
<td class="gt_row gt_center" headers="Rhat">1.000</td>
<td class="gt_row gt_center" headers="ESS">2068.00</td>
</tr>
</tbody><tfoot class="gt_sourcenotes">
<tr class="even">
<td colspan="5" class="gt_sourcenote"></td>
</tr>
</tfoot>

</table>

</div>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1">criteria <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gather_rvars</span>(Bayes_mod, b_Intercept[Response])</span>
<span id="cb17-2"></span>
<span id="cb17-3">signal_dist <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">spread_draws</span>(Bayes_mod, b_TruthOld, b_disc_TruthOld) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb17-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b_disc_TruthOld =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(b_disc_TruthOld)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb17-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(.draw) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb17-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reframe</span>(</span>
<span id="cb17-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>),</span>
<span id="cb17-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">d =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dnorm</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> b_TruthOld, b_disc_TruthOld)</span>
<span id="cb17-9">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb17-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb17-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">curve_interval</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.along =</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>)</span>
<span id="cb17-12"></span>
<span id="cb17-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-14">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Noise</span></span>
<span id="cb17-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_function</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Noise"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> dnorm) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-16">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Noise + Signal</span></span>
<span id="cb17-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_ribbon</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymin =</span> .lower, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> .upper),</span>
<span id="cb17-18">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> signal_dist,</span>
<span id="cb17-19">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, d, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Noise + Signal"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> signal_dist) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-21">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Thresholds</span></span>
<span id="cb17-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_slab</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xdist =</span> .value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ordered</span>(Response)),</span>
<span id="cb17-23">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">key_glyph =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"polygon"</span>,</span>
<span id="cb17-24">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> criteria) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-25">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Theme and scales</span></span>
<span id="cb17-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_brewer</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Threshold"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"div"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb17-27">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" | "</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>),</span>
<span id="cb17-28">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.translate =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb17-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Obs. signal"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2022-10-06-SDT-ordinal-regression/index_files/figure-html/DST-Bayes-plot-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>This gives roughly the same results as <code>clm()</code>, but would allow for multilevel modeling of both the location and scale of the latent variable.</p>


</section>
</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>Though the CIs are somewhat wider.↩︎</p></li>
<li id="fn2"><p>as of 2022-10-06↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>R</category>
  <category>statistics</category>
  <category>code</category>
  <category>ordinal regression</category>
  <category>signal detection theory</category>
  <guid>https://blog.msbstats.info/posts/2022-10-06-SDT-ordinal-regression/</guid>
  <pubDate>Wed, 05 Oct 2022 21:00:00 GMT</pubDate>
</item>
<item>
  <title>ggplot: Easy as pie (charts)</title>
  <link>https://blog.msbstats.info/posts/2021-08-12-easy-as-pie/</link>
  <description><![CDATA[ 





<p><sub><em>This post by no means endorses the use of pie charts. But, if you must, here’s how…</em></sub></p>
<p>For some reason, the top Google results for <em>“ggplot2 pie chart”</em> show some very convoluted code to accomplish what should be easy:</p>
<ol type="1">
<li>Make slices</li>
<li>Add labels to the middle of those slices</li>
</ol>
<p>Instead, let’s look at the easy way - with <a href="https://ggplot2.tidyverse.org/articles/articles/faq-bars.html"><code>position_stack()</code></a>!</p>
<section id="make-some-data" class="level3">
<h3 class="anchored" data-anchor-id="make-some-data">Make some data</h3>
<p>We first need some data fit for a pie chart - a column for slice label, and a column for their (preferably relative) size.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">d <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb1-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Slices =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Writing code"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Staring at plot"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Fixing code"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Enjoying plot"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb1-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Time =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb1-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">When =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Before reading this post"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"After reading this post"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb1-5">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transform</span>(</span>
<span id="cb1-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Make time relative</span></span>
<span id="cb1-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Time_relative =</span> Time <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ave</span>(Time, When, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FUN =</span> sum),</span>
<span id="cb1-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Slices =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(Slices, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(Slices)),</span>
<span id="cb1-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">When =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(When, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(When))</span>
<span id="cb1-11">  )</span>
<span id="cb1-12"></span>
<span id="cb1-13">d</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt;            Slices Time                     When Time_relative
#&gt; 1    Writing code    1 Before reading this post    0.08333333
#&gt; 2 Staring at plot    5 Before reading this post    0.41666667
#&gt; 3     Fixing code    4 Before reading this post    0.33333333
#&gt; 4   Enjoying plot    2 Before reading this post    0.16666667
#&gt; 5    Writing code    1  After reading this post    0.20000000
#&gt; 6 Staring at plot    1  After reading this post    0.20000000
#&gt; 7     Fixing code    1  After reading this post    0.20000000
#&gt; 8   Enjoying plot    2  After reading this post    0.40000000</code></pre>
</div>
</div>
</section>
<section id="now-lets-build-the-pie-chart" class="level3">
<h3 class="anchored" data-anchor-id="now-lets-build-the-pie-chart">Now let’s build the pie-chart!</h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb3-2"></span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(d, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Time_relative, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> Slices)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(When)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb3-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Make pie</span></span>
<span id="cb3-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_polar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add the *stacked* columns</span></span>
<span id="cb3-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">position_stack</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reverse =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>), </span>
<span id="cb3-9">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tan3"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb3-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add labels to the *stacked* position,</span></span>
<span id="cb3-11">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># in the middle of the column (vjust = 0.5)</span></span>
<span id="cb3-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> Slices), </span>
<span id="cb3-13">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">position_stack</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reverse =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb3-14">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Make it a pizza pie!</span></span>
<span id="cb3-15">  see<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_pizza_d</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb3-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_void</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb3-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Relative time spent building piecharts with ggplot2"</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2021-08-12-easy-as-pie/index_files/figure-html/make_plot-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><em><em>Best served HOT!</em></em></p>


</section>

 ]]></description>
  <category>R</category>
  <category>ggplot2</category>
  <guid>https://blog.msbstats.info/posts/2021-08-12-easy-as-pie/</guid>
  <pubDate>Wed, 11 Aug 2021 21:00:00 GMT</pubDate>
</item>
<item>
  <title>ggplot: plot only some of the data</title>
  <link>https://blog.msbstats.info/posts/2021-07-12-filter-data-on-a-ggplot/</link>
  <description><![CDATA[ 





<p>Often (especially when working with large and/or rich datasets) our (gg)plots can feel cluttered with information. But they don’t have to be!</p>
<p>Let’s look at the following plot:</p>
<div class="cell">
<details class="code-fold">
<summary><em>Generate some data</em></summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb1-2"></span>
<span id="cb1-3">bfi <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> psychTools<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>bfi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb1-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">O =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">starts_with</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"O"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowMeans</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb1-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">C =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">starts_with</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowMeans</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb1-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">E =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">starts_with</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"E"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowMeans</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb1-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">A =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">starts_with</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowMeans</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb1-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">starts_with</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowMeans</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb1-10">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb1-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gender =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(gender, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Man"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Woman"</span>)),</span>
<span id="cb1-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">education =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(education, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"HS"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"finished HS"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"some college"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"college graduate"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"graduate degree"</span>))</span>
<span id="cb1-14">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(gender, education, age, O<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-16">  tidyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>(education) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-17">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># multiply the data set</span></span>
<span id="cb1-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample_n</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-19">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># and add some noise</span></span>
<span id="cb1-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</span>(O<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N, \(x) x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(x, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(x))))</span></code></pre></div>
</details>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb2-2"></span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_set</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>())</span>
<span id="cb2-4"></span>
<span id="cb2-5">base_plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(bfi, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(age, O, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> education)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb2-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">facets =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(gender)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb2-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb2-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_viridis_d</span>()</span>
<span id="cb2-9"></span>
<span id="cb2-10">base_plot <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb2-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb2-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2021-07-12-filter-data-on-a-ggplot/index_files/figure-html/base_plot-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>This is a busy plot. It’s hard to see what the each prediction line is doing because there are so many of them, and it’s hard to make out the scatter plot behind the lines due to there being so many dots.</p>
<p>We might be tempted to pre-process some data in some way and pass it to each layer via the <code>data=</code> argument in <code>geom_point/smooth</code>. However, that gets ugly fast (did I clean the outliers in all the datasets for this plot?).</p>
<p>Instead, we can take advantage of the fact that the <code>data=</code> argument can take a <strong>function</strong> to pre-process that plot data before plotting:</p>
<blockquote class="blockquote">
<p>A <code>function</code> will be called with a single argument, the plot data. The return value must be a <code>data.frame</code>, and will be used as the layer data. A <code>function</code> can be created from a formula (e.g.&nbsp;<code>~ head(.x, 10)</code>).</p>
</blockquote>
<p>Let’s look at two examples.</p>
<section id="only-some-of-the-smooths" class="level2">
<h2 class="anchored" data-anchor-id="only-some-of-the-smooths">1. Only Some of the Smooths</h2>
<p>For example, we can pass a filtering function (here as a formula) to only get the prediction lines of two categories.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr) </span>
<span id="cb3-2"></span>
<span id="cb3-3">base_plot <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(</span>
<span id="cb3-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb3-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># looking only at HS vs collage graduates</span></span>
<span id="cb3-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(.x, education <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"finished HS"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"college graduate"</span>))</span>
<span id="cb3-9">  )</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2021-07-12-filter-data-on-a-ggplot/index_files/figure-html/some_smooths-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="only-some-of-the-points" class="level2">
<h2 class="anchored" data-anchor-id="only-some-of-the-points">2. Only Some of the Points</h2>
<p>We can also randomly select some points (note that the prediction lines are still based on the <em>full</em> dataset).</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">base_plot <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(</span>
<span id="cb4-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>,</span>
<span id="cb4-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># sample 10% from each group of `education`+`gender`</span></span>
<span id="cb4-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(.x, education, gender) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample_frac</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>)</span>
<span id="cb4-6">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Let the reader know what you've done</span></span>
<span id="cb4-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Scatter plot contains only 10% of the data for ease of viewing.</span></span>
<span id="cb4-10"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">       Prediction lines are based on the full dataset."</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2021-07-12-filter-data-on-a-ggplot/index_files/figure-html/some_points-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<hr>
<p>You might also be interested in <a href="https://yutannihilation.github.io/gghighlight/"><code>gghighlight</code></a> for plotting all of the data, but highlighting only some of it.</p>


</section>

 ]]></description>
  <category>R</category>
  <category>ggplot2</category>
  <guid>https://blog.msbstats.info/posts/2021-07-12-filter-data-on-a-ggplot/</guid>
  <pubDate>Sun, 11 Jul 2021 21:00:00 GMT</pubDate>
</item>
<item>
  <title>Everything You Always Wanted to Know About ANOVA*</title>
  <link>https://blog.msbstats.info/posts/2021-05-25-everything-about-ANOVA/</link>
  <description><![CDATA[ 





<p>Analysis of variance (ANOVA) is a statistical procedure, developed by R. A. Fisher, used to analyze the relationship between a continuous outcome (dependent variable) and categorical predictors (independent variables). This procedure produces a linear model, which can be used to estimate the conditional and marginal means of the outcome; In the presence of multiple factorial predictors, the model will include all interaction terms between the factors.</p>
<p>The ANOVA is part of a wider family of statistical procedures that include ANCOVA (which incorporates continuous predictors) and Analysis of Deviance (which allow for non-continuous outcomes<sup>1</sup>). This family of procedures all produce an ANOVA table (or ANOVA-like table) which summarizes the relationship between the underlying model and the outcome by partitioning the variation in the outcome into components which can be uniquely attributable to different sources according to the <em>law of total variance</em>. Essentially, each of the model’s terms is represented in a line in the ANOVA table which answers the question <em>how much of the variation in <img src="https://latex.codecogs.com/png.latex?Y"> can be attributed to the variation in <img src="https://latex.codecogs.com/png.latex?X">?</em>?<sup>2</sup> Where applicable, each source of variance has an accompanying test statistic (often <em>F</em>), sometimes called the omnibus test, which indicates the significance of the variance attributable to that term, often accompanied by some measure of effect size.</p>
<p>In this post I will demonstrate the various types of ANOVA tables, how R does ANOVA (what the defaults are, and how to produce alternatives).</p>
<p>Along the way, I hope to illustrate the applicability of ANOVA tables to other types of models - besides the classical case of a maximal model (all main effects and interactions) with strictly categorical predictors and a continuous outcome.</p>
<hr>
<p>Here are some assumptions I make about you, the reader, in this post:</p>
<ol type="1">
<li>You’re familiar with the ideas of multi-factor ANOVAs (what a main effect is, what interactions are…).</li>
<li>You know some <code>R</code> - how to fit a linear model, how to wrangle some data.</li>
<li><del>You are IID and normally distributed.</del></li>
</ol>
<p>Let’s dive right in!</p>
<section id="anovas-in-r" class="level1">
<h1>ANOVAs in R</h1>
<details>
<summary>
<i>Toy Data</i>
</summary>
<div class="cell">
<div class="cell-output cell-output-stdout">
<pre><code>d &lt;- tibble::tribble(
  ~id, ~group,    ~X, ~Z,       ~Rx, ~condition,       ~Y,
   1L,   "Gb", "102", 1L, "Placebo",       "Ca", "584.07",
   2L,   "Ga",  "52", 1L, "Placebo",       "Cb", "790.29",
   3L,   "Gb", "134", 2L, "Dose100",       "Ca", "875.76",
   4L,   "Gb", "128", 3L, "Dose100",       "Cb", "848.37",
   5L,   "Ga",  "78", 1L, "Dose250",       "Ca", "270.42",
   6L,   "Gb", "150", 2L, "Dose250",       "Cb", "999.87",
   7L,   "Ga",  "73", 1L, "Placebo",       "Ca",  "364.1",
   8L,   "Ga",  "87", 7L, "Placebo",       "Cb", "420.84",
   9L,   "Gb", "115", 6L, "Dose100",       "Ca", "335.78",
  10L,   "Gb", "113", 4L, "Dose100",       "Cb",    "627",
  11L,   "Gc", "148", 3L, "Dose250",       "Ca", "607.79",
  12L,   "Gc",  "82", 3L, "Dose250",       "Cb", "329.32",
  13L,   "Ga", "139", 1L, "Placebo",       "Ca", "335.56",
  14L,   "Ga",  "65", 2L, "Placebo",       "Cb", "669.04",
  15L,   "Gb", "139", 1L, "Dose100",       "Ca", "405.04",
  16L,   "Gc",  "96", 1L, "Dose100",       "Cb", "367.15",
  17L,   "Gb",  "50", 5L, "Dose250",       "Ca",  "27.37",
  18L,   "Gc",  "90", 2L, "Dose250",       "Cb", "468.69",
  19L,   "Ga",  "90", 2L, "Placebo",       "Ca", "584.67",
  20L,   "Ga", "116", 2L, "Placebo",       "Cb", "277.71",
  21L,   "Gb",  "78", 2L, "Dose100",       "Ca", "266.01",
  22L,   "Gb",  "60", 1L, "Dose100",       "Cb",   "0.04",
  23L,   "Gc", "112", 4L, "Dose250",       "Ca", "593.25",
  24L,   "Ga",  "63", 4L, "Dose250",       "Cb", "512.26",
  25L,   "Ga",  "89", 1L, "Placebo",       "Ca", "635.57",
  26L,   "Ga",  "97", 2L, "Placebo",       "Cb", "468.69",
  27L,   "Gc",  "76", 3L, "Dose100",       "Ca", "514.66",
  28L,   "Gb",  "83", 1L, "Dose100",       "Cb", "264.87",
  29L,   "Gc",  "84", 4L, "Dose250",       "Ca", "220.34",
  30L,   "Gb",  "88", 1L, "Dose250",       "Cb", "216.54"
  )</code></pre>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>id <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>id)</span>
<span id="cb2-2">d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>group <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>group)</span>
<span id="cb2-3">d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Rx <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Rx, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Placebo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dose100"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dose250"</span>))</span>
<span id="cb2-4">d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>condition <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>condition)</span></code></pre></div>
</div>
</details>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>(d)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Rows: 30
#&gt; Columns: 7
#&gt; $ id        &lt;fct&gt; 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 1…
#&gt; $ group     &lt;fct&gt; Gb, Ga, Gb, Gb, Ga, Gb, Ga, Ga, Gb, Gb, Gc, Gc, Ga, Ga, Gb, …
#&gt; $ X         &lt;dw_trnsf&gt; 102, 52, 134, 128, 78, 150, 73, 87, 115, 113, 148, 82, …
#&gt; $ Z         &lt;int&gt; 1, 1, 2, 3, 1, 2, 1, 7, 6, 4, 3, 3, 1, 2, 1, 1, 5, 2, 2, 2, …
#&gt; $ Rx        &lt;fct&gt; Placebo, Placebo, Dose100, Dose100, Dose250, Dose250, Placeb…
#&gt; $ condition &lt;fct&gt; Ca, Cb, Ca, Cb, Ca, Cb, Ca, Cb, Ca, Cb, Ca, Cb, Ca, Cb, Ca, …
#&gt; $ Y         &lt;dw_trnsf&gt; 584.07, 790.29, 875.76, 848.37, 270.42, 999.87, 364.10,…</code></pre>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">m <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> group <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> X, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> d)</span></code></pre></div>
</div>
<p>This is a multiple regression model with a covariable <code>X</code> and a 3-level factor <code>group</code>. We can summarize the results in a coefficient table (aka a “regression” table):</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(m)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; 
#&gt; Call:
#&gt; lm(formula = Y ~ group + X, data = d)
#&gt; 
#&gt; Residuals:
#&gt;     Min      1Q  Median      3Q     Max 
#&gt; -372.51 -166.47  -53.67  134.57  451.16 
#&gt; 
#&gt; Coefficients:
#&gt;             Estimate Std. Error t value Pr(&gt;|t|)   
#&gt; (Intercept)  118.608    145.557   0.815  0.42256   
#&gt; groupGb     -102.591     94.853  -1.082  0.28937   
#&gt; groupGc      -92.384    107.302  -0.861  0.39712   
#&gt; X              4.241      1.504   2.820  0.00908 **
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#&gt; 
#&gt; Residual standard error: 218.8 on 26 degrees of freedom
#&gt; Multiple R-squared:  0.2383, Adjusted R-squared:  0.1504 
#&gt; F-statistic: 2.711 on 3 and 26 DF,  p-value: 0.06556</code></pre>
</div>
</div>
<p>Or we can produce an ANOVA table:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(m)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Analysis of Variance Table
#&gt; 
#&gt; Response: Y
#&gt;           Df  Sum Sq Mean Sq F value   Pr(&gt;F)   
#&gt; group      2    8783    4391  0.0918 0.912617   
#&gt; X          1  380471  380471  7.9503 0.009077 **
#&gt; Residuals 26 1244265   47856                    
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
</div>
<p>While the <code>group</code> term had 2 parameters in the coefficient table, it now has a single test with a <code>Df</code> of 2. This <strong>omnibus</strong> test can be thought of as representing the <em>total</em> significance of the two parameters combined!</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p><strong>Note</strong> that the model being summarized here is neither completely factorial (<code>X</code> is a continuous covariable), nor maximal (the <code>group:X</code> interaction is missing)!</p>
</div>
</div>
<p>By default <code>R</code> calculates <em>type 1</em> sums of squares (SS) - these are also called <em>sequential SS</em>, because each term is attributed with a portion of the variation (represented by its SS) in <img src="https://latex.codecogs.com/png.latex?Y"> that has not yet been attributed to any of the PREVIOUS terms! Thus, in our example, the effect of <code>X</code> represents only what <code>X</code> explains on top of what <code>group</code> has already explained - the variance attributed to <code>X</code> is strictly the variance that can be <em>uniquely</em> attributed to <code>X</code>, controlling for <code>group</code>; the effect of <code>group</code> however does <em>not</em> represent its unique contribution to <img src="https://latex.codecogs.com/png.latex?Y"> ’s variance, but instead its <em>total</em> contribution.</p>
<p>This means that although the following models have the same terms, they will produce different <em>type 1</em> ANOVA tables because those terms are <em>in a different order</em>:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> group <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> X, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> d))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Analysis of Variance Table
#&gt; 
#&gt; Response: Y
#&gt;           Df  Sum Sq Mean Sq F value   Pr(&gt;F)   
#&gt; group      2    8783    4391  0.0918 0.912617   
#&gt; X          1  380471  380471  7.9503 0.009077 **
#&gt; Residuals 26 1244265   47856                    
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> d))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Analysis of Variance Table
#&gt; 
#&gt; Response: Y
#&gt;           Df  Sum Sq Mean Sq F value  Pr(&gt;F)  
#&gt; X          1  325745  325745  6.8067 0.01486 *
#&gt; group      2   63509   31754  0.6635 0.52353  
#&gt; Residuals 26 1244265   47856                  
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
</div>
<p>We can recreate the ANOVA table above by building a sequence of models, and comparing them (see (Judd, McClelland, &amp; Ryan, 2017)[https://doi.org/10.4324/9781315744131]):</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">m0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> d) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Intercept-only model</span></span>
<span id="cb14-2">m1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> d)</span>
<span id="cb14-3"></span>
<span id="cb14-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(m0, m1, m)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Analysis of Variance Table
#&gt; 
#&gt; Model 1: Y ~ 1
#&gt; Model 2: Y ~ group
#&gt; Model 3: Y ~ group + X
#&gt;   Res.Df     RSS Df Sum of Sq      F   Pr(&gt;F)   
#&gt; 1     29 1633519                                
#&gt; 2     27 1624737  2      8783 0.0918 0.912617   
#&gt; 3     26 1244265  1    380471 7.9503 0.009077 **
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(m) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Same SS values</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Analysis of Variance Table
#&gt; 
#&gt; Response: Y
#&gt;           Df  Sum Sq Mean Sq F value   Pr(&gt;F)   
#&gt; group      2    8783    4391  0.0918 0.912617   
#&gt; X          1  380471  380471  7.9503 0.009077 **
#&gt; Residuals 26 1244265   47856                    
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
</div>
<section id="simultaneous-sum-of-squares" class="level2">
<h2 class="anchored" data-anchor-id="simultaneous-sum-of-squares">Simultaneous Sum of Squares</h2>
<p>There is also <em>type 2 SS</em> - also called <em>simultaneous SS</em>, because each term is attributed with a portion of the variation in <img src="https://latex.codecogs.com/png.latex?Y"> that is not attributable to any of the other terms in the model - its unique contribution while controlling for the other terms. Type 2 SS can be obtained with the <code>Anova()</code> function from the {<code>car</code>} package:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">car<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Anova</span>(m, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Anova Table (Type II tests)
#&gt; 
#&gt; Response: Y
#&gt;            Sum Sq Df F value   Pr(&gt;F)   
#&gt; group       63509  2  0.6635 0.523533   
#&gt; X          380471  1  7.9503 0.009077 **
#&gt; Residuals 1244265 26                    
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
</div>
<p>We can recreate the above ANOVA table by building two sequences of models, and comparing them:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1">m_sans_X <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> d)</span>
<span id="cb20-2">m_sans_group <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> d)</span>
<span id="cb20-3"></span>
<span id="cb20-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(m_sans_group, m) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Same SS as the type 2 test for group</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Analysis of Variance Table
#&gt; 
#&gt; Model 1: Y ~ X
#&gt; Model 2: Y ~ group + X
#&gt;   Res.Df     RSS Df Sum of Sq      F Pr(&gt;F)
#&gt; 1     28 1307774                           
#&gt; 2     26 1244265  2     63509 0.6635 0.5235</code></pre>
</div>
<div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(m_sans_X,     m) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Same SS as the type 2 test for X</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Analysis of Variance Table
#&gt; 
#&gt; Model 1: Y ~ group
#&gt; Model 2: Y ~ group + X
#&gt;   Res.Df     RSS Df Sum of Sq      F   Pr(&gt;F)   
#&gt; 1     27 1624737                                
#&gt; 2     26 1244265  1    380471 7.9503 0.009077 **
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
</div>
<p>Because the order of terms is usually of little importance, type 1 tests are rarely used in practice…</p>
<p>Unfortunately, they are R’s default…</p>
</section>
<section id="adding-interactions" class="level2">
<h2 class="anchored" data-anchor-id="adding-interactions">Adding Interactions</h2>
<p>Things get a bit more complicated when interactions are involved, as type 2 SS treat interactions differently than main effects:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1">m_int <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> group <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> X, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> d)</span></code></pre></div>
</div>
<p>Each main effect term is attributed with variance (its SS) that is unique to it and that is not attributable to any of the other main effects (simultaneously, as we’ve already seen) but <em>without</em> accounting for the variance attributable to interactions, while the SS of the interaction term represents its unique variance after accounting for the underlying main effects (sequentially). So we get the unique contribution of each main effect when controlling only for the other main effects, and the unique contribution of the interactions controlling for the already-included combined contribution of the main effects.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1">car<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Anova</span>(m_int, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Anova Table (Type II tests)
#&gt; 
#&gt; Response: Y
#&gt;           Sum Sq Df F value    Pr(&gt;F)    
#&gt; group      63509  2  1.3555 0.2768607    
#&gt; X         380471  1 16.2410 0.0004884 ***
#&gt; group:X   682026  2 14.5566 7.246e-05 ***
#&gt; Residuals 562240 24                      
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
</div>
<p>We can again recreate this type 2 ANOVA table with model comparisons<sup>3</sup>:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(m_sans_group, m)     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Same SS as the type 2 test for group</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Analysis of Variance Table
#&gt; 
#&gt; Model 1: Y ~ X
#&gt; Model 2: Y ~ group + X
#&gt;   Res.Df     RSS Df Sum of Sq      F Pr(&gt;F)
#&gt; 1     28 1307774                           
#&gt; 2     26 1244265  2     63509 0.6635 0.5235</code></pre>
</div>
<div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(m_sans_X,     m)     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Same SS as the type 2 test for X</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Analysis of Variance Table
#&gt; 
#&gt; Model 1: Y ~ group
#&gt; Model 2: Y ~ group + X
#&gt;   Res.Df     RSS Df Sum of Sq      F   Pr(&gt;F)   
#&gt; 1     27 1624737                                
#&gt; 2     26 1244265  1    380471 7.9503 0.009077 **
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
<div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(m,            m_int) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Same SS as the type 2 test for group:X  </span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Analysis of Variance Table
#&gt; 
#&gt; Model 1: Y ~ group + X
#&gt; Model 2: Y ~ group * X
#&gt;   Res.Df     RSS Df Sum of Sq      F    Pr(&gt;F)    
#&gt; 1     26 1244265                                  
#&gt; 2     24  562240  2    682026 14.557 7.246e-05 ***
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
</div>
<p>In designs of higher order, each “order” is tested in a similar <em>simultaneous-sequential</em> manner. E.g., in a 3-way design, all main effects (1st order) are tested <em>simultaneously</em> (accounting for one another), <strong>then</strong> all 2-way interactions (2nd order) are tested <em>simultaneously</em> (accounting for the main effects <em>and</em> one another), and <strong>then</strong> the 3-way interaction is tested (accounting for all main effects and 2-way interactions).</p>
<section id="type-3" class="level3">
<h3 class="anchored" data-anchor-id="type-3">Type 3</h3>
<p>There is another type of simultaneous SS - the type 3 test, which treats interactions and main effects equally: the SS for each main effect or interaction is calculated as its unique contribution that is not attributable to any of the other effects in the model - main effects or interactions. So the effect of <code>X</code> is its unique contribution while controlling both for <code>group</code> <em>and</em> for <code>group:X</code>!</p>
<p>However, remember how we previously saw that these methods in R actually produce omnibus tests for the combined effect of the <em>parameters of each term</em>. But in the <code>m_int</code> model the parameters labeled <code>X</code>, <code>groupGb</code>, and <code>groupGc</code> no longer represent parameters of the main effects - instead they are parameters of simple (i.e., conditional) effects!</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(m_int)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; 
#&gt; Call:
#&gt; lm(formula = Y ~ group * X, data = d)
#&gt; 
#&gt; Residuals:
#&gt;     Min      1Q  Median      3Q     Max 
#&gt; -370.18  -67.15   33.68  111.35  172.14 
#&gt; 
#&gt; Coefficients:
#&gt;              Estimate Std. Error t value Pr(&gt;|t|)    
#&gt; (Intercept)   833.130    173.749   4.795 6.99e-05 ***
#&gt; groupGb     -1308.898    233.218  -5.612 8.90e-06 ***
#&gt; groupGc      -752.718    307.747  -2.446   0.0222 *  
#&gt; X              -4.041      1.942  -2.081   0.0482 *  
#&gt; groupGb:X      13.041      2.419   5.390 1.55e-05 ***
#&gt; groupGc:X       7.731      3.178   2.432   0.0228 *  
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#&gt; 
#&gt; Residual standard error: 153.1 on 24 degrees of freedom
#&gt; Multiple R-squared:  0.6558, Adjusted R-squared:  0.5841 
#&gt; F-statistic: 9.146 on 5 and 24 DF,  p-value: 5.652e-05</code></pre>
</div>
</div>
<ul>
<li><code>X</code> is the slope of <code>X</code> <strong><em>when <code>group=Ga</code></em></strong> (When both dummy variables are fixed at 0, as <code>Ga</code> is the reference level)<br>
</li>
<li><code>groupGb</code> is the difference between <code>group=Ga</code> and <code>group=Gb</code> <strong><em>when <code>X=0</code></em></strong><br>
</li>
<li><code>groupGc</code> is the difference between <code>group=Ga</code> and <code>group=Gc</code> <strong><em>when <code>X=0</code></em></strong></li>
</ul>
<p>(Pay attention to the “when” - this is what makes them conditional.)</p>
<p>We can see that changing the reference group changes the test for <code>X</code>:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb35" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb35-1">d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>group <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">relevel</span>(d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ref =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gb"</span>)</span>
<span id="cb35-2">m_int2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> group <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> X, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> d)</span>
<span id="cb35-3"></span>
<span id="cb35-4">car<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Anova</span>(m_int, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Anova Table (Type III tests)
#&gt; 
#&gt; Response: Y
#&gt;             Sum Sq Df F value    Pr(&gt;F)    
#&gt; (Intercept) 538630  1 22.9922 6.994e-05 ***
#&gt; group       738108  2 15.7536 4.269e-05 ***
#&gt; X           101495  1  4.3325   0.04823 *  
#&gt; group:X     682026  2 14.5566 7.246e-05 ***
#&gt; Residuals   562240 24                      
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
<div class="sourceCode cell-code" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb37-1">car<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Anova</span>(m_int2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Anova Table (Type III tests)
#&gt; 
#&gt; Response: Y
#&gt;             Sum Sq Df F value    Pr(&gt;F)    
#&gt; (Intercept) 219106  1  9.3528  0.005402 ** 
#&gt; group       738108  2 15.7536 4.269e-05 ***
#&gt; X           910646  1 38.8722 1.918e-06 ***
#&gt; group:X     682026  2 14.5566 7.246e-05 ***
#&gt; Residuals   562240 24                      
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
</div>
<p>How can we resolve this?</p>
</section>
<section id="centering" class="level3">
<h3 class="anchored" data-anchor-id="centering">Centering</h3>
<p>By centering our predictors! Centering is transforming our data in such a way that 0 represents the overall mean. When this is done, conditioning on 0 is the same as conditioning on the overall mean = looking at the main effect!</p>
<p>For covariables this is easy enough:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb39" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb39-1">d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>X_c <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>X) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># or scale(d$X, center = TRUE, scale = FALSE)</span></span></code></pre></div>
</div>
<p>But how do we center a factor??</p>
<p>The answer is - use some type of orthogonal coding, for example <code>contr.sum()</code> (effects coding). This makes the coefficients harder to interpret <sup>4</sup>, but we’re not looking at those anyway!</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb40" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrasts</span>(d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>group) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> contr.sum</span></code></pre></div>
</div>
<p>Now when looking at type 3 tests, the main effects terms actually are main effects!</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb41" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb41-1">m_int3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> group<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>X_c, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> d)</span>
<span id="cb41-2">car<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Anova</span>(m_int3, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Anova Table (Type III tests)
#&gt; 
#&gt; Response: Y
#&gt;              Sum Sq Df  F value    Pr(&gt;F)    
#&gt; (Intercept) 4743668  1 202.4902 3.401e-13 ***
#&gt; group         19640  2   0.4192   0.66231    
#&gt; X_c          143772  1   6.1371   0.02067 *  
#&gt; group:X_c    682026  2  14.5566 7.246e-05 ***
#&gt; Residuals    562240 24                       
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
</div>
<p>Remember: <strong><em>ABC - Always Be Centering</em></strong> (your predictors) - type 3 ANOVA tables make little sense without centering<sup>5</sup>!</p>
<details>
<summary>
<i>Recreate the Type 3 ANOVA</i>
</summary>
<p>Unfortunately, <a href="../..\posts/2019-10-30-ghost-interactions/">we can’t just build a model without an interaction term</a> and use it to recreate the type 3 ANOVA. Instead, we need to actually build the model matrix (i.e., the design matrix), and drop the columns of each term in turn:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb43" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb43-1">mm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model.matrix</span>(m_int2)</span>
<span id="cb43-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(mm)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt;   (Intercept) groupGa groupGc   X groupGa:X groupGc:X
#&gt; 1           1       0       0 102         0         0
#&gt; 2           1       1       0  52        52         0
#&gt; 3           1       0       0 134         0         0
#&gt; 4           1       0       0 128         0         0
#&gt; 5           1       1       0  78        78         0
#&gt; 6           1       0       0 150         0         0</code></pre>
</div>
</div>
<p>A type 3 test for a term, is equal to the comparison between a model without the parameters associated with that term and the <strong>full model</strong>:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb45" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb45-1">m_sans_group <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> mm[,<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> d)</span>
<span id="cb45-2">m_sans_X <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span>     <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> mm[,<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>],     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> d)</span>
<span id="cb45-3">m_sans_int <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span>   <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> mm[,<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>)], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> d)</span>
<span id="cb45-4"></span>
<span id="cb45-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(m_sans_group, m_int2) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Same SS as type 3 for group</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Analysis of Variance Table
#&gt; 
#&gt; Model 1: Y ~ mm[, -(2:3)]
#&gt; Model 2: Y ~ group * X
#&gt;   Res.Df     RSS Df Sum of Sq      F    Pr(&gt;F)    
#&gt; 1     26 1300348                                  
#&gt; 2     24  562240  2    738108 15.754 4.269e-05 ***
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
<div class="sourceCode cell-code" id="cb47" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb47-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(m_sans_X,     m_int2) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Same SS as type 3 for X</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Analysis of Variance Table
#&gt; 
#&gt; Model 1: Y ~ mm[, -4]
#&gt; Model 2: Y ~ group * X
#&gt;   Res.Df     RSS Df Sum of Sq      F    Pr(&gt;F)    
#&gt; 1     25 1472886                                  
#&gt; 2     24  562240  1    910646 38.872 1.918e-06 ***
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
<div class="sourceCode cell-code" id="cb49" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb49-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(m_sans_int,   m_int2) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Same SS as type 3 for group:X</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Analysis of Variance Table
#&gt; 
#&gt; Model 1: Y ~ mm[, -(5:6)]
#&gt; Model 2: Y ~ group * X
#&gt;   Res.Df     RSS Df Sum of Sq      F    Pr(&gt;F)    
#&gt; 1     26 1244265                                  
#&gt; 2     24  562240  2    682026 14.557 7.246e-05 ***
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
</div>
<p>Compare to:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb51" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb51-1">car<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Anova</span>(m_int2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Anova Table (Type III tests)
#&gt; 
#&gt; Response: Y
#&gt;             Sum Sq Df F value    Pr(&gt;F)    
#&gt; (Intercept) 219106  1  9.3528  0.005402 ** 
#&gt; group       738108  2 15.7536 4.269e-05 ***
#&gt; X           910646  1 38.8722 1.918e-06 ***
#&gt; group:X     682026  2 14.5566 7.246e-05 ***
#&gt; Residuals   562240 24                      
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
</div>
Okay, that’s some ugly stuff. Let us never look at that again.
</details>
</section>
<section id="type-2-vs-type-3" class="level3">
<h3 class="anchored" data-anchor-id="type-2-vs-type-3">Type 2 <em>vs</em> Type 3</h3>
<p>As mentioned above, the distinction between types 2 and 3 comes from how they estimate main effects in the presence of interactions.</p>
<p>Let’s look at the following factorial design:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb53" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb53-1">m_factorial <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> condition <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> d,</span>
<span id="cb53-2">                   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Another way to specify effects coding:</span></span>
<span id="cb53-3">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">contrasts =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">condition =</span> contr.sum,</span>
<span id="cb53-4">                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> contr.sum))</span>
<span id="cb53-5"></span>
<span id="cb53-6">car<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Anova</span>(m_factorial, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Anova Table (Type II tests)
#&gt; 
#&gt; Response: Y
#&gt;                  Sum Sq Df F value Pr(&gt;F)
#&gt; condition         12048  1  0.1840 0.6718
#&gt; group              7165  2  0.0547 0.9469
#&gt; condition:group   41204  2  0.3146 0.7330
#&gt; Residuals       1571485 24</code></pre>
</div>
<div class="sourceCode cell-code" id="cb55" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb55-1">car<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Anova</span>(m_factorial, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Anova Table (Type III tests)
#&gt; 
#&gt; Response: Y
#&gt;                  Sum Sq Df F value    Pr(&gt;F)    
#&gt; (Intercept)     5858845  1 89.4773 1.439e-09 ***
#&gt; condition          3452  1  0.0527    0.8203    
#&gt; group              8913  2  0.0681    0.9344    
#&gt; condition:group   41204  2  0.3146    0.7330    
#&gt; Residuals       1571485 24                      
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
</div>
<p>But where do these differences between type 2 and 3 come from?</p>
<p>Type 2 SS looks at the SS between the means of <code>A</code>, <em>across</em> the levels of <code>B</code>. So the marginal mean of the first group is estimated as:</p>
<center>
<big> <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BY%7D_%7B1.%7D%20=%20%5Cfrac%7B%5Csum%7BY_%7B1.%7D%7D%7D%7BN_%7B1.%7D%7D%20=%20%5Cfrac%7B%5Csum%7BY_%7B11%7D%7D+%5Csum%7BY_%7B12%7D%7D%7D%7BN_%7B11%7D+N_%7B12%7D%7D"> </big>
</center>
<p>Type 3 SS however looks at the SS between the means of <code>group</code>, weighted by <code>condition</code>. So the marginal mean of group <code>a</code> is estimated as:</p>
<center>
<big> <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BY%7D_%7B1.%7D%20=%20%5Cfrac%7B%5Cfrac%7B%5Csum%7BY_%7B11%7D%7D%7D%7BN_%7B11%7D%7D%20+%20%5Cfrac%7B%5Csum%7BY_%7B12%7D%7D%7D%7BN_%7B12%7D%7D%7D%7B2%7D"> </big>
</center>
<p>This makes type 3 SS invariant to the cell frequencies!</p>
<p>But as we will soon see, this need not always be the case…</p>
<hr>
<p>A lot has been said about type 2 vs type 3. I will not go into the weeds here, but it is important to note that</p>
<ol type="1">
<li>Most statistical softwares (SAS, Stata, SPSS, …) default to type 3 SS with orthogonal factor coding (but covariables are <em>not</em> mean-centered in most cases by default) (see <a href="https://doi.org/10.1023/A:1023260610025">Langsrud, 2003</a>). This makes <code>R</code> inconsistent as we’ve seen it defaults to type 1 ANOVA and treatment coding.<br>
</li>
<li>Often in factorial designs, any imbalance in the design is incidental, so it is often beneficial to have a method that is invariant to such imbalances. (Though this may not be true if the data is observational.)<br>
</li>
<li>Coefficient tables give results that are analogous to type 3 SS when all terms are covariables:</li>
</ol>
<div class="cell">
<div class="sourceCode cell-code" id="cb57" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb57-1">m_covs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Z, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> d)</span>
<span id="cb57-2"></span>
<span id="cb57-3">car<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Anova</span>(m_covs, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Anova Table (Type II tests)
#&gt; 
#&gt; Response: Y
#&gt;            Sum Sq Df F value  Pr(&gt;F)  
#&gt; X          324676  1  6.7657 0.01513 *
#&gt; Z            2430  1  0.0506 0.82373  
#&gt; X:Z         57640  1  1.2011 0.28315  
#&gt; Residuals 1247704 26                  
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
<div class="sourceCode cell-code" id="cb59" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb59-1">car<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Anova</span>(m_covs, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Anova Table (Type III tests)
#&gt; 
#&gt; Response: Y
#&gt;              Sum Sq Df F value Pr(&gt;F)
#&gt; (Intercept)   83130  1  1.7323 0.1996
#&gt; X              5866  1  0.1222 0.7294
#&gt; Z             59917  1  1.2486 0.2740
#&gt; X:Z           57640  1  1.2011 0.2831
#&gt; Residuals   1247704 26</code></pre>
</div>
<div class="sourceCode cell-code" id="cb61" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb61-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(m_covs) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># same p-values as type 3</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; 
#&gt; Call:
#&gt; lm(formula = Y ~ X * Z, data = d)
#&gt; 
#&gt; Residuals:
#&gt;     Min      1Q  Median      3Q     Max 
#&gt; -384.97 -153.72  -23.98  141.28  422.79 
#&gt; 
#&gt; Coefficients:
#&gt;             Estimate Std. Error t value Pr(&gt;|t|)
#&gt; (Intercept)  366.352    278.348   1.316    0.200
#&gt; X              1.014      2.900   0.350    0.729
#&gt; Z           -112.681    100.843  -1.117    0.274
#&gt; X:Z            1.175      1.072   1.096    0.283
#&gt; 
#&gt; Residual standard error: 219.1 on 26 degrees of freedom
#&gt; Multiple R-squared:  0.2362, Adjusted R-squared:  0.1481 
#&gt; F-statistic:  2.68 on 3 and 26 DF,  p-value: 0.06772</code></pre>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p><strong>Note</strong> once again that the model being summarized here is not factorial at all - both <code>Z</code> and <code>X</code> are continuous covariables!</p>
</div>
</div>
</section>
</section>
<section id="balanced-vs.-unbalanced-data" class="level2">
<h2 class="anchored" data-anchor-id="balanced-vs.-unbalanced-data">Balanced vs.&nbsp;Unbalanced Data</h2>
<p>The distinction between types 1, 2 and 3 SS is only relevant when there is some dependency between predictors (aka some collinearity). In our example, we can see that <code>group</code> and <code>X</code> are somewhat co-linear (VIF / tolerance are not strictly 1):</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb63" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb63-1">performance<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">check_collinearity</span>(m)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; # Check for Multicollinearity
#&gt; 
#&gt; Low Correlation
#&gt; 
#&gt;   Term  VIF   VIF 95% CI Increased SE Tolerance Tolerance 95% CI
#&gt;  group 1.08 [1.00, 5.73]         1.04      0.92     [0.17, 1.00]
#&gt;      X 1.08 [1.00, 5.73]         1.04      0.92     [0.17, 1.00]</code></pre>
</div>
</div>
<p>In a factorial design, we might call this dependence / collinearity among our predictors an “unbalanced design” (the number of observations differs between cells), and when the predictors are completely independent we would call this a “balanced design” (equal number of observations in all cells).</p>
<p>Let’s look at two examples:</p>
<section id="balanced-data" class="level3">
<h3 class="anchored" data-anchor-id="balanced-data">Balanced data</h3>
<p>We can see that <code>Rx</code> and <code>condition</code> are balanced:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb65" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb65-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Rx, d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>condition)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt;          
#&gt;           Ca Cb
#&gt;   Placebo  5  5
#&gt;   Dose100  5  5
#&gt;   Dose250  5  5</code></pre>
</div>
<div class="sourceCode cell-code" id="cb67" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb67-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">chisq.test</span>(d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Rx, d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>condition)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>statistic <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Chisq is exactly 0</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; X-squared 
#&gt;         0</code></pre>
</div>
</div>
<p>And so type 1, 2 and 3 ANOVA tables are identical:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb69" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb69-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrasts</span>(d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Rx) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> contr.sum</span>
<span id="cb69-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrasts</span>(d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>condition) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> contr.sum</span>
<span id="cb69-3"></span>
<span id="cb69-4">m_balanced <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> condition <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Rx, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> d)</span>
<span id="cb69-5"></span>
<span id="cb69-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(m_balanced)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Analysis of Variance Table
#&gt; 
#&gt; Response: Y
#&gt;              Df  Sum Sq Mean Sq F value Pr(&gt;F)
#&gt; condition     1   13666   13666  0.2162 0.6461
#&gt; Rx            2   41379   20690  0.3273 0.7240
#&gt; condition:Rx  2   61444   30722  0.4860 0.6210
#&gt; Residuals    24 1517030   63210</code></pre>
</div>
<div class="sourceCode cell-code" id="cb71" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb71-1">car<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Anova</span>(m_balanced, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Anova Table (Type II tests)
#&gt; 
#&gt; Response: Y
#&gt;               Sum Sq Df F value Pr(&gt;F)
#&gt; condition      13666  1  0.2162 0.6461
#&gt; Rx             41379  2  0.3273 0.7240
#&gt; condition:Rx   61444  2  0.4860 0.6210
#&gt; Residuals    1517030 24</code></pre>
</div>
<div class="sourceCode cell-code" id="cb73" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb73-1">car<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Anova</span>(m_balanced, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Anova Table (Type III tests)
#&gt; 
#&gt; Response: Y
#&gt;               Sum Sq Df  F value    Pr(&gt;F)    
#&gt; (Intercept)  6422803  1 101.6112 4.204e-10 ***
#&gt; condition      13666  1   0.2162    0.6461    
#&gt; Rx             41379  2   0.3273    0.7240    
#&gt; condition:Rx   61444  2   0.4860    0.6210    
#&gt; Residuals    1517030 24                       
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
</div>
</section>
<section id="unbalanced-data" class="level3">
<h3 class="anchored" data-anchor-id="unbalanced-data">Unbalanced data</h3>
<p>However, <code>condition</code> and <code>group</code> are NOT balanced:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb75" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb75-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>group, d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>condition)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt;     
#&gt;      Ca Cb
#&gt;   Gb  6  6
#&gt;   Ga  5  6
#&gt;   Gc  4  3</code></pre>
</div>
<div class="sourceCode cell-code" id="cb77" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb77-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">chisq.test</span>(d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>group, d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>condition)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>statistic <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Chisq is NOT 0</span></span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>#&gt; Warning in chisq.test(d$group, d$condition): Chi-squared approximation may be
#&gt; incorrect</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; X-squared 
#&gt; 0.2337662</code></pre>
</div>
</div>
<p>And so type 1, 2 and type 3 ANOVA tables are NOT identical (recall how type 2 and 3 estimate marginal means differently in the presence of interactions):</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb80" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb80-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(m_factorial)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Analysis of Variance Table
#&gt; 
#&gt; Response: Y
#&gt;                 Df  Sum Sq Mean Sq F value Pr(&gt;F)
#&gt; condition        1   13666   13666  0.2087 0.6519
#&gt; group            2    7165    3582  0.0547 0.9469
#&gt; condition:group  2   41204   20602  0.3146 0.7330
#&gt; Residuals       24 1571485   65479</code></pre>
</div>
<div class="sourceCode cell-code" id="cb82" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb82-1">car<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Anova</span>(m_factorial, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Anova Table (Type II tests)
#&gt; 
#&gt; Response: Y
#&gt;                  Sum Sq Df F value Pr(&gt;F)
#&gt; condition         12048  1  0.1840 0.6718
#&gt; group              7165  2  0.0547 0.9469
#&gt; condition:group   41204  2  0.3146 0.7330
#&gt; Residuals       1571485 24</code></pre>
</div>
<div class="sourceCode cell-code" id="cb84" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb84-1">car<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Anova</span>(m_factorial, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Anova Table (Type III tests)
#&gt; 
#&gt; Response: Y
#&gt;                  Sum Sq Df F value    Pr(&gt;F)    
#&gt; (Intercept)     5858845  1 89.4773 1.439e-09 ***
#&gt; condition          3452  1  0.0527    0.8203    
#&gt; group              8913  2  0.0681    0.9344    
#&gt; condition:group   41204  2  0.3146    0.7330    
#&gt; Residuals       1571485 24                      
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
</div>
<p>As the deviation from perfect balance (i.e.&nbsp;independence) is larger, so will the differences between the types increase.</p>
</section>
<section id="why-does-this-happen" class="level3">
<h3 class="anchored" data-anchor-id="why-does-this-happen">Why Does This Happen?</h3>
<p>We’ve seen that types 1, 2 and 3 all attribute variance in <img src="https://latex.codecogs.com/png.latex?Y"> to the model’s terms by partialling out the model’s other terms in some way - sequentially, simultaneously, or some mix of both.</p>
<p>However, if the predictors in the model are independent (such as in a balanced design, zero collinearity), then regardless of the order of their inclusion in the model, all of the variance that can be attributable to some term A is unique to A - none of it is <em>also</em> attributable to any of the other term in the model, and vice versa - there is nothing to partial out, so the order does not matter!</p>
<p>This also means that because the SS returned by both type 2 and 3 ANOVA tables represent the terms’ uniquely attributable variation in <img src="https://latex.codecogs.com/png.latex?Y">, then <strong>when the design is <em>not</em> balanced / there <em>is</em> collinearity in the data, the SS in the ANOVA table will not sum to the total SS</strong> - as there is some overlap (some non-unique variation) that is not represented in the ANOVA table. However… this is where type 1 ANOVA tables shine, as their sequential nature means the SS in the ANOVA table <em>will</em> sum to the total SS!</p>
<p>Let’s look at an extreme example of collinearity:</p>
<div class="cell">
<details class="code-fold">
<summary><i>Collinear data</i></summary>
<div class="sourceCode cell-code" id="cb86" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb86-1">d2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> MASS<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mvrnorm</span>(</span>
<span id="cb86-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,</span>
<span id="cb86-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),</span>
<span id="cb86-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Sigma =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>,</span>
<span id="cb86-5">                   <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.41</span>,</span>
<span id="cb86-6">                   <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.41</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb86-7">)</span>
<span id="cb86-8"></span>
<span id="cb86-9">d2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(d2)</span>
<span id="cb86-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(d2) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Z"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Y"</span>)</span></code></pre></div>
</details>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb87" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb87-1">m_collinear <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Z, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> d2)</span>
<span id="cb87-2"></span>
<span id="cb87-3">performance<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">check_collinearity</span>(m_collinear)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; # Check for Multicollinearity
#&gt; 
#&gt; High Correlation
#&gt; 
#&gt;  Term   VIF     VIF 95% CI Increased SE Tolerance Tolerance 95% CI
#&gt;     X 47.02 [32.64, 67.95]         6.86      0.02     [0.01, 0.03]
#&gt;     Z 47.02 [32.64, 67.95]         6.86      0.02     [0.01, 0.03]</code></pre>
</div>
</div>
<p>Looking a type 1 ANOVA table we can see that <code>X</code> accounts for a significant amount of variation in <code>Y</code>, but that <code>Z</code> does not <em>add</em> anything significant on top of <code>X</code>:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb89" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb89-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(m_collinear)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Analysis of Variance Table
#&gt; 
#&gt; Response: Y
#&gt;           Df Sum Sq Mean Sq F value    Pr(&gt;F)    
#&gt; X          1 16.927 16.9265  22.664 6.743e-06 ***
#&gt; Z          1  0.000  0.0000   0.000    0.9999    
#&gt; Residuals 97 72.444  0.7468                      
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
</div>
<p>However, were we to look at a type 2 ANOVA table, we might get the impression that neither <code>X</code> nor <code>Z</code> contribute to the model:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb91" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb91-1">car<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Anova</span>(m_collinear, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Anova Table (Type II tests)
#&gt; 
#&gt; Response: Y
#&gt;           Sum Sq Df F value Pr(&gt;F)
#&gt; X          0.360  1  0.4822 0.4891
#&gt; Z          0.000  1  0.0000 0.9999
#&gt; Residuals 72.444 97</code></pre>
</div>
</div>
<p>This demonstrates the importance of <em>always</em> interpreting type 2 and 3 ANOVA tables in light of any collinearity that might exist between your predictors; Remember: <strong><em>ABC - Always Be mindful of Collinearity</em></strong> <sub>(okay, that one was a bit of a stretch)</sub>.</p>
</section>
</section>
<section id="anova-made-easy" class="level2">
<h2 class="anchored" data-anchor-id="anova-made-easy">ANOVA Made Easy</h2>
<p>We can use the <code>lm()</code> -&gt; <code>car::Anova()</code> method to conduct a proper ANOVA on a maximal factorial design. However, making sure that our factors are orthogonally coded is a pain in the @$$.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb93" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb93-1">d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>group <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>group)</span>
<span id="cb93-2">d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>condition <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>condition)</span>
<span id="cb93-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrasts</span>(d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>group) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> contr.sum</span>
<span id="cb93-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrasts</span>(d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>condition) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> contr.sum</span>
<span id="cb93-5">m_lm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> group <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> condition, d)</span>
<span id="cb93-6">car<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Anova</span>(m_lm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Anova Table (Type III tests)
#&gt; 
#&gt; Response: Y
#&gt;                  Sum Sq Df F value    Pr(&gt;F)    
#&gt; (Intercept)     5858845  1 89.4773 1.439e-09 ***
#&gt; group              8913  2  0.0681    0.9344    
#&gt; condition          3452  1  0.0527    0.8203    
#&gt; group:condition   41204  2  0.3146    0.7330    
#&gt; Residuals       1571485 24                      
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
</div>
<p>Thankfully, we have the <code>afex</code> package which turns all of that mess into something much more palatable:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb95" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb95-1">afex<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aov_car</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> group <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> condition <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Error</span>(id), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> d)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Anova Table (Type 3 tests)
#&gt; 
#&gt; Response: Y
#&gt;            Effect    df      MSE    F  ges p.value
#&gt; 1           group 2, 24 65478.53 0.07 .006    .934
#&gt; 2       condition 1, 24 65478.53 0.05 .002    .820
#&gt; 3 group:condition 2, 24 65478.53 0.31 .026    .733
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1</code></pre>
</div>
</div>
<p>Much easier!</p>
</section>
</section>
<section id="other-types-of-models" class="level1">
<h1>Other Types of Models</h1>
<p>So far we’ve seen how ANOVA tables are applied to non-factorial linear OLS models. However the idea of omnibus tests per-term can be extended to many other types of models. For models where SS cannot be calculated, analogous methods based on deviance or likelihood are used instead (read more in the <code>car::Anova()</code> docs). Here are some examples:</p>
<section id="glms" class="level2">
<h2 class="anchored" data-anchor-id="glms">GLMs</h2>
<section id="logistic" class="level3">
<h3 class="anchored" data-anchor-id="logistic">Logistic</h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb97" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb97-1">m_logistic <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glm</span>(condition <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> group <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> X_c, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> d,</span>
<span id="cb97-2">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">binomial</span>())</span>
<span id="cb97-3">car<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Anova</span>(m_logistic, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Analysis of Deviance Table (Type II tests)
#&gt; 
#&gt; Response: condition
#&gt;           LR Chisq Df Pr(&gt;Chisq)
#&gt; group      0.15679  2     0.9246
#&gt; X_c        0.75134  1     0.3861
#&gt; group:X_c  1.10225  2     0.5763</code></pre>
</div>
</div>
</section>
<section id="poisson" class="level3">
<h3 class="anchored" data-anchor-id="poisson">Poisson</h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb99" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb99-1">m_poisson <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glm</span>(Z <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> group <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> X_c, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> d,</span>
<span id="cb99-2">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">poisson</span>())</span>
<span id="cb99-3">car<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Anova</span>(m_poisson, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Analysis of Deviance Table (Type III tests)
#&gt; 
#&gt; Response: Z
#&gt;           LR Chisq Df Pr(&gt;Chisq)
#&gt; group      0.88074  2     0.6438
#&gt; X_c        0.04370  1     0.8344
#&gt; group:X_c  0.11357  2     0.9448</code></pre>
</div>
</div>
</section>
<section id="ordinal" class="level3">
<h3 class="anchored" data-anchor-id="ordinal">Ordinal</h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb101" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb101-1">m_ordinal <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ordinal<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">clm</span>(group <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> condition, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> d)</span>
<span id="cb101-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(m_ordinal)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Type I Analysis of Deviance Table with Wald chi-square tests
#&gt; 
#&gt;               Df  Chisq Pr(&gt;Chisq)
#&gt; X_c            1 0.4469     0.5038
#&gt; condition      1 0.1584     0.6907
#&gt; X_c:condition  1 0.6129     0.4337</code></pre>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p><strong>Note</strong> that all of the models summarized here with an ANOVA table do not have a continuous (conditionally normal) outcome, and not <em>all</em> of their predictors are categorical. We may have been inclined to summarize these models with a coefficient table, but it is equally valid to present an ANOVA table!</p>
</div>
</div>
</section>
</section>
<section id="glmms" class="level2">
<h2 class="anchored" data-anchor-id="glmms">(G)LMMs</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb103" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb103-1">m_mixed <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> lmerTest<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lmer</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> group <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (group <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> Z), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> d)</span>
<span id="cb103-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(m_mixed, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Type II Analysis of Variance Table with Satterthwaite's method
#&gt;           Sum Sq Mean Sq NumDF  DenDF F value    Pr(&gt;F)    
#&gt; X_c       290878  290878     1 23.645 14.4210 0.0008951 ***
#&gt; group       7198    3599     2  9.520  0.1784 0.8393243    
#&gt; X_c:group 582071  291036     2 21.938 14.4288 0.0001001 ***
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
</div>
<p>Also for GLMMs:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb105" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb105-1">m_mixed2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> lme4<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glmer</span>(condition <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> group <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> Z), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> d,</span>
<span id="cb105-2">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">binomial</span>())</span>
<span id="cb105-3">car<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Anova</span>(m_mixed2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Analysis of Deviance Table (Type III Wald chisquare tests)
#&gt; 
#&gt; Response: condition
#&gt;              Chisq Df Pr(&gt;Chisq)
#&gt; (Intercept) 0.0886  1     0.7660
#&gt; X_c         1.1917  1     0.2750
#&gt; group       0.0829  2     0.9594
#&gt; X_c:group   0.9972  2     0.6074</code></pre>
</div>
</div>
</section>
</section>
<section id="concluding-remarks" class="level1">
<h1>Concluding Remarks</h1>
<p>Although we might be more inclined to summarize our model with an ANOVA table when our model contains categorical predictors, especially when interactions are involved, we’ve seen that ANOVA tables do not require any special data (factorial, balanced, normal outcome), and can be used as an alternative to a coefficient table.</p>
<p>Regardless of how we summarize our model - with a coefficient table or with an ANOVA table, using type 1, 2 or 3 SS, with orthogonal or treatment coding, with centered or uncentered covariables - our underlying model is equivalent - and will produce the same estimated simple effects, marginal means and contrasts. That is, the method we use to summarize our model will not have any bearing on whatever follow-up analysis we may wish to carry out (using <code>emmeans</code> of course! Check out the materials from my R course: <a href="https://github.com/mattansb/Analysis-of-Factorial-Designs-foR-Psychologists"><strong>Analysis of Factorial Designs</strong></a>).<sup>6</sup></p>
<p>I hope you now have a fuller grasp of what goes on behind the scenes when producing ANOVA tables, how the different types of ANOVA tables work, when they should be used, and how to interpret their results. ANOVA tables are a powerful tool that can be applied not only to factorial data coupled with an OLS model, but also to a wide variety of (generalized) linear (mixed) regression models.</p>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>via the generalized linear model framework↩︎</p></li>
<li id="fn2"><p>What we might consider “unexplained” variance is <em>also</em> attributed to some source; e.g.&nbsp;to variation between subjects.↩︎</p></li>
<li id="fn3"><p>While the SS are the same, the test statistics are different - this is because <code>car::Anova()</code> uses the total error term of the full model for all of the tests.↩︎</p></li>
<li id="fn4"><p>You might instead use <code>contr.helmert()</code>.↩︎</p></li>
<li id="fn5"><p>Honestly, coefficient tables also make little sense without centering↩︎</p></li>
<li id="fn6"><p>It will also not alter the model’s predictions or <img src="https://latex.codecogs.com/png.latex?R%5E2">.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>statistics</category>
  <category>R</category>
  <category>regression</category>
  <category>ANOVA</category>
  <category>interactions</category>
  <category>linear models</category>
  <category>code</category>
  <category>glm</category>
  <guid>https://blog.msbstats.info/posts/2021-05-25-everything-about-ANOVA/</guid>
  <pubDate>Mon, 24 May 2021 21:00:00 GMT</pubDate>
</item>
<item>
  <title>ggplot: the placing and order of aesthetics matters</title>
  <link>https://blog.msbstats.info/posts/2021-05-16-ggplot-where-to-aes/</link>
  <description><![CDATA[ 





<p>A group of people were asked to what degree they agree or disagree with a statement at two time points.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">Agreement <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">794</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">86</span>,</span>
<span id="cb1-2">                       <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">888</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">34</span>,</span>
<span id="cb1-3">                      <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">570</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">333</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">23</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb1-4">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dimnames =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Before =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Agree"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Meh"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Disagree"</span>), </span>
<span id="cb1-5">                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">After =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Agree"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Meh"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Disagree"</span>)))</span></code></pre></div>
</div>
<p>Our question is how many people changed their minds. Statistically we might use <code>mcnemar.test()</code> and <code>effectsize::cohens_g()</code>, but we will be focusing on visualization of the data with <code>ggplot2</code>.</p>
<p>We first need to re-structure this matrix into a data frame:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">(Agreement_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.table</span>(Agreement)))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt;     Before    After Freq
#&gt; 1    Agree    Agree  794
#&gt; 2      Meh    Agree  150
#&gt; 3 Disagree    Agree   86
#&gt; 4    Agree      Meh   12
#&gt; 5      Meh      Meh  888
#&gt; 6 Disagree      Meh   34
#&gt; 7    Agree Disagree  570
#&gt; 8      Meh Disagree  333
#&gt; 9 Disagree Disagree   23</code></pre>
</div>
</div>
<p>The basic plot is:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_set</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>())</span>
<span id="cb4-3"></span>
<span id="cb4-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(Agreement_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(Before, Freq, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> After)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>(</span>
<span id="cb4-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fill"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.85</span>, </span>
<span id="cb4-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb4-8">  )</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2021-05-16-ggplot-where-to-aes/index_files/figure-html/p1-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Simple enough.</p>
<p>What we want to do is mark the cells where people did not change their response - where <code>Before</code> is equal to <code>After</code> - with a different line type. We can do this by adding <code>linetype = Before == After</code> into the plots aesthetics. This <em>should</em> give diagonal cells a different line-type compared to the other cells. Simple enough, no?</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(Agreement_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(Before, Freq, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> After)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb5-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>(</span>
<span id="cb5-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fill"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.85</span>, </span>
<span id="cb5-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb5-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> Before <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> After) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;</span></span>
<span id="cb5-6">  )</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2021-05-16-ggplot-where-to-aes/index_files/figure-html/p2-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>What the hell happened?? The <strong><em>order</em></strong> of cells has changed!</p>
<section id="grouping-order-of-mapping" class="level1">
<h1>Grouping &amp; Order of Mapping</h1>
<p>The first thing to understand is that we have some implicit grouping going on.</p>
<blockquote class="blockquote">
<p>The group aesthetic is by default set to the interaction of all discrete variables in the plot. […] For most applications the grouping is set implicitly by mapping one or more discrete variables to <code>x</code>, <code>y</code>, <code>colour</code>, <code>fill</code>, <code>alpha</code>, <code>shape</code>, <code>size</code>, and/or <code>linetype</code>.</p>
</blockquote>
<p><em>From the <code>ggplot2</code> manual on <a href="https://ggplot2.tidyverse.org/reference/aes_group_order.html"><strong>Aesthetics: grouping</strong></a></em></p>
<p>This means that our mapping of <code>fill</code> and <code>linetype</code> has been used to set the <code>group</code>ing of the cells.</p>
<p>The second thing to understand is the <em>order</em> in which these <code>group</code>ing aesthetics are used for grouping:</p>
<ul>
<li>First, the layer-specific aesthetics are used (in our case, <code>linetype = Before == After</code>, which is in the <code>geom_col()</code> layer).</li>
<li>Then (if <code>inherit.aes = TRUE</code>, which is the default) any global aesthetics are used (<code>fill = After</code>, which is set in the call to <code>ggplot()</code>).</li>
</ul>
<p>This is why the order of the cells has changed: Cells were grouped first by the before-after equality, and only then by the type of “after” response.</p>
</section>
<section id="the-fix" class="level1">
<h1>The Fix</h1>
<p>The fix is easy, we have to make sure the grouping aesthetics are specified in a way that <code>ggplot</code> pulls them in the correct order; that is first by “after” and then by the before-after equality.</p>
<p>Here are all the ways to do that:</p>
<section id="option-1-be-explicit" class="level2">
<h2 class="anchored" data-anchor-id="option-1-be-explicit">Option 1: Be Explicit</h2>
<p>We can explicitly set the <code>group</code> aesthetic, using the <code>interaction()</code> function, but to add insult to injury, this function must be supplied with the grouping variables in the <em>reverse</em> order (unless you set <code>lex.order = TRUE</code>):</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(Agreement_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(Before, Freq, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> After)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>(</span>
<span id="cb6-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fill"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.85</span>, </span>
<span id="cb6-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb6-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> Before <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> After,</span>
<span id="cb6-6">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">interaction</span>(Before <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> After, After)) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;</span></span>
<span id="cb6-7">  )</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2021-05-16-ggplot-where-to-aes/index_files/figure-html/explicit-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(Agreement_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(Before, Freq, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> After)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb7-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>(</span>
<span id="cb7-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fill"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.85</span>, </span>
<span id="cb7-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb7-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> Before <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> After,</span>
<span id="cb7-6">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">interaction</span>(After, Before <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> After,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;</span></span>
<span id="cb7-7">                                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lex.order =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;</span></span>
<span id="cb7-8">  ) </span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2021-05-16-ggplot-where-to-aes/index_files/figure-html/explicit-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="option-2-set-all-grouping-aesthetics-globally-by-layer" class="level2">
<h2 class="anchored" data-anchor-id="option-2-set-all-grouping-aesthetics-globally-by-layer">Option 2: Set All Grouping Aesthetics Globally / By Layer</h2>
<p>We can also keep using the implicit setting for the grouping, but set all of the relevant aesthetics globally:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set both in the global aesthetics:</span></span>
<span id="cb8-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(Agreement_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(Before, Freq,</span>
<span id="cb8-3">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> After, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> Before <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> After)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb8-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>(</span>
<span id="cb8-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fill"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.85</span>, </span>
<span id="cb8-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb8-7">  )</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2021-05-16-ggplot-where-to-aes/index_files/figure-html/globally-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Or in the layer itself:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set both in the layer aesthetics:</span></span>
<span id="cb9-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(Agreement_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(Before, Freq)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb9-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>(</span>
<span id="cb9-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fill"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.85</span>, </span>
<span id="cb9-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb9-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> After, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> Before <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> After)</span>
<span id="cb9-7">  )</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2021-05-16-ggplot-where-to-aes/index_files/figure-html/by_layer-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Note then even when setting them globally or in the layer, the <em>order</em> still matters:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(Agreement_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(Before, Freq)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb10-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>(</span>
<span id="cb10-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fill"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.85</span>, </span>
<span id="cb10-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb10-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> Before <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> After, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> After) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Wrong order</span></span>
<span id="cb10-6">  )</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2021-05-16-ggplot-where-to-aes/index_files/figure-html/wrong_order-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="conclusion" class="level1">
<h1>Conclusion</h1>
<p>The location (global or by layer) and order of aesthetics matters. I didn’t know this, and I felt like I was losing my mind; I hope that by writing this post I will be able to spare you some precious keyboard banging and yelps of sorrow.</p>
<p>Code away!</p>


</section>

 ]]></description>
  <category>R</category>
  <category>ggplot2</category>
  <guid>https://blog.msbstats.info/posts/2021-05-16-ggplot-where-to-aes/</guid>
  <pubDate>Sat, 15 May 2021 21:00:00 GMT</pubDate>
</item>
<item>
  <title>Testing The Equality of Regression Coefficients</title>
  <link>https://blog.msbstats.info/posts/2021-02-16-testing-coefficients-equality/</link>
  <description><![CDATA[ 





<section id="the-problem" class="level1">
<h1>The Problem</h1>
<p>You have two predictors in your model. One seems to have a stronger coefficient than the other. But is it significant?</p>
<p>Example: when predicting a worker’s salary, is the standardized coefficient of <em>number of extra hours</em> (<code>xtra_hours</code>) really larger than that of <em>number of compliments given the to boss</em> <code>n_comps</code>?</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(parameters)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(effectsize)</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hardlyworking"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">package =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"effectsize"</span>)</span>
<span id="cb1-5"></span>
<span id="cb1-6">hardlyworkingZ <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">standardize</span>(hardlyworking)</span>
<span id="cb1-7"></span>
<span id="cb1-8">m <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(salary <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> xtra_hours <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n_comps, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> hardlyworkingZ)</span>
<span id="cb1-9"></span>
<span id="cb1-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model_parameters</span>(m)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Parameter   | Coefficient |   SE |        95% CI |   t(497) |      p
#&gt; --------------------------------------------------------------------
#&gt; (Intercept) |    2.76e-16 | 0.02 | [-0.03, 0.03] | 1.59e-14 | &gt; .999
#&gt; xtra hours  |        0.81 | 0.02 | [ 0.78, 0.84] |    46.60 | &lt; .001
#&gt; n comps     |        0.41 | 0.02 | [ 0.37, 0.44] |    23.51 | &lt; .001</code></pre>
</div>
</div>
<p>Here are <s>4</s> 6 methods to test coefficient equality in <code>R</code>.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<ul>
<li>If we were interested in the unstandardized coefficient, we would not need to first standardize the data.<br>
</li>
<li>Note that if one parameter was positive, and the other was negative, one of the terms would need to be first reversed (-X) to make this work.</li>
</ul>
</div>
</div>
<section id="method-1-as-model-comparisons" class="level2">
<h2 class="anchored" data-anchor-id="method-1-as-model-comparisons">Method 1: As Model Comparisons</h2>
<p><em>Based on <a href="https://twitter.com/JamesUanhoro/status/1310682951923961858">this awesome tweet</a>.</em></p>
<p>Since:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Chat%7BY%7D%20=%20a%20%5Ctimes%20%5Ctext%7Bxtra_hours%7D%20+%20a%20%5Ctimes%20%5Ctext%7Bn_comps%7D%20%5C%5C%20=%20a%20%5Ctimes%20(%5Ctext%7Bxtra_hours%7D%20+%20%5Ctext%7Bn_comps%7D)%0A"></p>
<p>We can essentially force a constraint on the coefficient to be equal by using a composite variable.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">m0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(salary <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">I</span>(xtra_hours <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n_comps), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> hardlyworkingZ)</span>
<span id="cb3-2"></span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model_parameters</span>(m0)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Parameter            | Coefficient |   SE |        95% CI |   t(498) |      p
#&gt; -----------------------------------------------------------------------------
#&gt; (Intercept)          |    2.80e-16 | 0.02 | [-0.04, 0.04] | 1.31e-14 | &gt; .999
#&gt; xtra hours + n comps |        0.61 | 0.01 | [ 0.58, 0.64] |    41.09 | &lt; .001</code></pre>
</div>
</div>
<p>We can then compare how this model compares to our first model without this constraint:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(m0, m)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Analysis of Variance Table
#&gt; 
#&gt; Model 1: salary ~ I(xtra_hours + n_comps)
#&gt; Model 2: salary ~ xtra_hours + n_comps
#&gt;   Res.Df     RSS Df Sum of Sq      F    Pr(&gt;F)    
#&gt; 1    498 113.662                                  
#&gt; 2    497  74.942  1     38.72 256.78 &lt; 2.2e-16 ***
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
</div>
<p>We can conclude that the unconstrained model is significantly better than the constrained model - meaning that <img src="https://latex.codecogs.com/png.latex?%5Cbeta_%7B%5Ctext%7Bxtra_hours%7D%7D%20%3E%20%5Cbeta_%7B%5Ctext%7Bn_comps%7D%7D">.</p>
<section id="method-1b-composite-variable-difference" class="level3">
<h3 class="anchored" data-anchor-id="method-1b-composite-variable-difference">Method 1b: Composite Variable + Difference</h3>
<p><em><strong>Edit</strong> (Feb-17, 2021): Thanks to <a href="https://twitter.com/joejps84/status/1361977265823784960">@joejps84 for pointing this out!</a></em></p>
<p>We can achieve the same thing in a single model. If we say that the slope of <code>n_comps</code> as equal to the slope of <code>xtra_hours</code> + some change:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Chat%7BY%7D%20=%20a%20%5Ctimes%20%5Ctext%7Bxtra_hours%7D%20+%20(a%20+%20b)%20%5Ctimes%20%5Ctext%7Bn_comps%7D%20%5C%5C%20=%20a%20%5Ctimes%20%5Ctext%7Bxtra_hours%7D%20+%20a%20%5Ctimes%20%5Ctext%7Bn_comps%7D%20+%20b%20%5Ctimes%20%5Ctext%7Bn_comps%7D%20%5C%5C%20=%20a%20%5Ctimes%20(%5Ctext%7Bxtra_hours%7D%20+%20%5Ctext%7Bn_comps%7D)%20+%20b%20%5Ctimes%20%5Ctext%7Bn_comps%7D%0A"></p>
<p>So the slope of <code>n_comps</code> in such a model is the difference between the slope of <code>n_comps</code> and <code>xtra_hours</code>:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">m1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(salary <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">I</span>(xtra_hours <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n_comps) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n_comps, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> hardlyworkingZ)</span>
<span id="cb7-2"></span>
<span id="cb7-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model_parameters</span>(m1)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Parameter            | Coefficient |   SE |         95% CI |   t(497) |      p
#&gt; ------------------------------------------------------------------------------
#&gt; (Intercept)          |    2.56e-16 | 0.02 | [-0.03,  0.03] | 1.47e-14 | &gt; .999
#&gt; xtra hours + n comps |        0.81 | 0.02 | [ 0.78,  0.84] |    46.60 | &lt; .001
#&gt; n comps              |       -0.40 | 0.03 | [-0.45, -0.35] |   -16.02 | &lt; .001</code></pre>
</div>
</div>
<p>This give the exact same result (<img src="https://latex.codecogs.com/png.latex?t%5E2%20=%20(-16)%5E2%20=%20256"> is the same as the <em>F</em>-value from the <code>anova()</code> test)!</p>
</section>
</section>
<section id="method-2-paternoster-et-al-1998" class="level2">
<h2 class="anchored" data-anchor-id="method-2-paternoster-et-al-1998">Method 2: Paternoster et al (1998)</h2>
<p>According to <a href="https://doi.org/10.1111/j.1745-9125.1998.tb01268.x">Paternoster et al.&nbsp;(1998)</a>, we can compute a <em>t</em>-test to compare the coefficients:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">b <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(m)</span>
<span id="cb9-2">V <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vcov</span>(m)</span>
<span id="cb9-3"></span>
<span id="cb9-4">tibble<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb9-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">diff_estim =</span> b[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> b[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>],</span>
<span id="cb9-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">diff_SE =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(V[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> V[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> V[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]),</span>
<span id="cb9-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t_stat =</span> diff_estim <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> diff_SE,</span>
<span id="cb9-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">df.residual</span>(m),</span>
<span id="cb9-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p_value =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(t_stat), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df =</span> df, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower.tail =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb9-10">)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; # A tibble: 1 × 5
#&gt;   diff_estim diff_SE t_stat    df  p_value
#&gt;        &lt;dbl&gt;   &lt;dbl&gt;  &lt;dbl&gt; &lt;int&gt;    &lt;dbl&gt;
#&gt; 1      0.402  0.0251   16.0   497 6.83e-47</code></pre>
</div>
</div>
<p>This gives the exact same results as the first method!</p>
<p><em><strong>Edit</strong> (Feb-17, 2021): As <a href="https://twitter.com/bmwiernik/status/1361888581942468611">@bmwiernik pointed out</a>, this can also be done with some fancy matrix multiplication:</em></p>
<div class="cell">
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">contr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb11-2"></span>
<span id="cb11-3">tibble<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb11-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">diff_estim =</span> b <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> contr,</span>
<span id="cb11-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">diff_SE =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(contr <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> V <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> contr),</span>
<span id="cb11-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t_stat =</span> diff_estim <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> diff_SE,</span>
<span id="cb11-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">df.residual</span>(m),</span>
<span id="cb11-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p_value =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(t_stat), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df =</span> df, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower.tail =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb11-9">)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; # A tibble: 1 × 5
#&gt;   diff_estim[,1] diff_SE[,1] t_stat[,1]    df p_value[,1]
#&gt;            &lt;dbl&gt;       &lt;dbl&gt;      &lt;dbl&gt; &lt;int&gt;       &lt;dbl&gt;
#&gt; 1          0.402      0.0251       16.0   497    6.83e-47</code></pre>
</div>
</div>
<p>All of the following solutions are essentially this method, wrapped in some nice functions.</p>
</section>
<section id="method-3-emmeans-3" class="level2">
<h2 class="anchored" data-anchor-id="method-3-emmeans-3">Method 3: <code>emmeans</code> &lt;3</h2>
<p>We can estimate the slopes from the model using <code>emmeans</code>, and then, with some trickery, compare them!</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(emmeans)</span>
<span id="cb13-2"></span>
<span id="cb13-3">trends <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(</span>
<span id="cb13-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emtrends</span>(m, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"xtra_hours"</span>),</span>
<span id="cb13-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emtrends</span>(m, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n_comps"</span>)</span>
<span id="cb13-6">)</span>
<span id="cb13-7"></span>
<span id="cb13-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># clean up so it does not error later</span></span>
<span id="cb13-9">trends<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">@</span>grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">1</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"xtra_hours"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n_comps"</span>)</span>
<span id="cb13-10">trends<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">@</span>misc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estName <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trend"</span></span>
<span id="cb13-11"></span>
<span id="cb13-12">trends</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt;  1          trend     SE  df lower.CL upper.CL
#&gt;  xtra_hours 0.811 0.0174 497    0.772    0.850
#&gt;  n_comps    0.409 0.0174 497    0.370    0.448
#&gt; 
#&gt; Confidence level used: 0.95 
#&gt; Conf-level adjustment: bonferroni method for 2 estimates</code></pre>
</div>
</div>
<p>Compare them:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pairs</span>(trends)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt;  contrast             estimate     SE  df t.ratio p.value
#&gt;  xtra_hours - n_comps    0.402 0.0251 497  16.024  &lt;.0001</code></pre>
</div>
</div>
<p>Once again - exact same results!</p>
</section>
<section id="method-4-lavaan" class="level2">
<h2 class="anchored" data-anchor-id="method-4-lavaan">Method 4: <code>lavaan</code></h2>
<p>The <code>lavaan</code> package for latent variable analysis and structural equation modeling allows for parameter constraining. So let’s do just that:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lavaan)</span>
<span id="cb17-2"></span>
<span id="cb17-3">L <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sem</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"salary ~ xtra_hours + n_comps"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> hardlyworkingZ)</span>
<span id="cb17-4">L0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sem</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"salary ~ a * xtra_hours + a * n_comps"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> hardlyworkingZ)</span>
<span id="cb17-5"></span>
<span id="cb17-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(L0, L)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; 
#&gt; Chi-Squared Difference Test
#&gt; 
#&gt;    Df    AIC    BIC  Chisq Chisq diff   RMSEA Df diff Pr(&gt;Chisq)    
#&gt; L   0 475.99 488.63   0.00                                          
#&gt; L0  1 682.25 690.68 208.26     208.26 0.64383       1  &lt; 2.2e-16 ***
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
</div>
<p>This too yields similar results! (Only slightly different due to different estimation methods.)</p>
<p>We can also directly estimate the difference using <code>lavaan</code>’s “defined parameters” - defines with the <code>:=</code> operator:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1">L <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sem</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"salary ~ b1 * xtra_hours + b2 * n_comps</span></span>
<span id="cb19-2"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">         diff := b1 - b2"</span>, </span>
<span id="cb19-3">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> hardlyworkingZ)</span>
<span id="cb19-4"></span>
<span id="cb19-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">parameterEstimates</span>(L, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">output =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>,]</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; 
#&gt; Defined Parameters:
#&gt;                    Estimate  Std.Err  z-value  P(&gt;|z|) ci.lower ci.upper
#&gt;     diff              0.402    0.025   16.073    0.000    0.353    0.451</code></pre>
</div>
</div>
<p>Which, again, gives the same results.</p>
</section>
<section id="method-5-car" class="level2">
<h2 class="anchored" data-anchor-id="method-5-car">Method 5: <code>car</code></h2>
<p><em><strong>Edit</strong> (Feb-17, 2021): Thanks to <a href="https://twitter.com/DouglasKGAraujo/status/1361733549188128772">@DouglasKGAraujo for his suggestion!</a></em></p>
<p>Even more methods!</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(car)</span>
<span id="cb21-2"></span>
<span id="cb21-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">linearHypothesis</span>(m, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"xtra_hours - n_comps"</span>))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; Linear hypothesis test
#&gt; 
#&gt; Hypothesis:
#&gt; xtra_hours - n_comps = 0
#&gt; 
#&gt; Model 1: restricted model
#&gt; Model 2: salary ~ xtra_hours + n_comps
#&gt; 
#&gt;   Res.Df     RSS Df Sum of Sq      F    Pr(&gt;F)    
#&gt; 1    498 113.662                                  
#&gt; 2    497  74.942  1     38.72 256.78 &lt; 2.2e-16 ***
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
</div>
<p>Which once again gives the same result!</p>
</section>
<section id="method-6-multcomp" class="level2">
<h2 class="anchored" data-anchor-id="method-6-multcomp">Method 6: <code>multcomp</code></h2>
<p><em><strong>Edit</strong> (Feb-17, 2021): Thanks to <a href="http://www.stgries.info/">Stefan Th. Gries for his suggestion!</a></em></p>
<p>Even more more methods!</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(multcomp)</span>
<span id="cb23-2"></span>
<span id="cb23-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glht</span>(m, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; 
#&gt;   Simultaneous Tests for General Linear Hypotheses
#&gt; 
#&gt; Fit: lm(formula = salary ~ xtra_hours + n_comps, data = hardlyworkingZ)
#&gt; 
#&gt; Linear Hypotheses:
#&gt;        Estimate Std. Error t value Pr(&gt;|t|)    
#&gt; 1 == 0  0.40171    0.02507   16.02   &lt;2e-16 ***
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#&gt; (Adjusted p values reported -- single-step method)</code></pre>
</div>
</div>
<p>Which once again again gives the same result!</p>
</section>
</section>
<section id="summary" class="level1">
<h1>Summary</h1>
<p>That’s it really - these <s>4</s> 6 simple methods have wide applications to GL(M)M’s, SEM, and more.</p>
<p>Enjoy!</p>


</section>

 ]]></description>
  <category>statistics</category>
  <category>R</category>
  <category>regression</category>
  <category>emmeans</category>
  <guid>https://blog.msbstats.info/posts/2021-02-16-testing-coefficients-equality/</guid>
  <pubDate>Mon, 15 Feb 2021 22:00:00 GMT</pubDate>
</item>
<item>
  <title>(What to do) When Predictors Co-Vary</title>
  <link>https://blog.msbstats.info/posts/2020-08-11-when-predictors-covary/</link>
  <description><![CDATA[ 





<p>Co-varying predictors can be a messy business. They make estimates unstable, reducing our statistical power and making interpretation more difficult. In this post I will demonstrate how ignoring the presence of co-variation between predictors when exploring our models can lead to odd results and how we might deal with this issue.</p>
<section id="our-model" class="level2">
<h2 class="anchored" data-anchor-id="our-model">Our Model</h2>
<p>For our example, we will use some (fake) developmental-growth data - we have the height and weight of 500 individuals between the ages of 5 and 24. We can plot our tri-variate data like so:</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2020-08-11-when-predictors-covary/index_files/figure-html/tri_plot-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>This generally looks as one might expect - taller people weigh more, older people are taller and weigh more as well.</p>
</section>
<section id="predicting-weight-from-height" class="level2">
<h2 class="anchored" data-anchor-id="predicting-weight-from-height">Predicting Weight from Height</h2>
<p>Let us fit an OLS linear model<sup>1</sup> to predict weight from height and age:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(weight <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> height <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> age, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data)</span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(fit)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; 
#&gt; Call:
#&gt; lm(formula = weight ~ height + age, data = data)
#&gt; 
#&gt; Residuals:
#&gt;      Min       1Q   Median       3Q      Max 
#&gt; -23.8960  -3.9637  -0.0438   4.3013  25.8607 
#&gt; 
#&gt; Coefficients:
#&gt;              Estimate Std. Error t value Pr(&gt;|t|)    
#&gt; (Intercept) -51.97123    5.27409  -9.854   &lt;2e-16 ***
#&gt; height        0.53458    0.05741   9.312   &lt;2e-16 ***
#&gt; age           2.47319    0.25311   9.771   &lt;2e-16 ***
#&gt; ---
#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#&gt; 
#&gt; Residual standard error: 7.009 on 497 degrees of freedom
#&gt; Multiple R-squared:  0.7856, Adjusted R-squared:  0.7847 
#&gt; F-statistic: 910.4 on 2 and 497 DF,  p-value: &lt; 2.2e-16</code></pre>
</div>
</div>
<p>Unsurprisingly, both <code>age</code> and <code>height</code> are positively related to <code>weight</code>.</p>
<p>Let’s explore our model - specifically, we want to see what weight can we expect for 3 individuals of the following heights: <strong>120, 145, and 170cm</strong>. We can do this with <code>emmeans</code>:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(emmeans)</span>
<span id="cb3-2"></span>
<span id="cb3-3">em_by_height <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emmeans</span>(fit, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> height, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">at =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">145</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">170</span>)))</span>
<span id="cb3-4">em_by_height</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt;  height emmean    SE  df lower.CL upper.CL
#&gt;     120   49.0 1.536 497     46.0     52.0
#&gt;     145   62.4 0.321 497     61.7     63.0
#&gt;     170   75.7 1.402 497     73.0     78.5
#&gt; 
#&gt; Confidence level used: 0.95</code></pre>
</div>
</div>
<p>We can also add these expected values to the plot from above:</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2020-08-11-when-predictors-covary/index_files/figure-html/em_plot1-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Something seems off - the expected value for a person 120cm tall seems too high, while the expected value for a person 170cm tall seems too low. Why is that? What’s going on?</p>
<p>First, let’s talk about the elephant in the room - there is obvious multicollinearity between our predictors! If we’re not sure, we can use <code>performance::check_collinearity()</code> to validate this:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">performance<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">check_collinearity</span>(fit)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; # Check for Multicollinearity
#&gt; 
#&gt; Moderate Correlation
#&gt; 
#&gt;    Term  VIF   VIF 95% CI Increased SE Tolerance Tolerance 95% CI
#&gt;  height 5.26 [4.51, 6.17]         2.29      0.19     [0.16, 0.22]
#&gt;     age 5.26 [4.51, 6.17]         2.29      0.19     [0.16, 0.22]</code></pre>
</div>
</div>
<p>But how is this related to the funky estimates we got?</p>
<p>Well, this has to do with what we do with the <em>other</em> predictors when exploring a single predictor. In our case, what happens to <code>age</code> when we’re exploring the role of <code>height</code> in our model?</p>
<p>The common and almost default approach is to fix <code>age</code> to a constant. This is really what our model does in the first place: the coefficient of <code>height</code> represents the expected change in <code>weight</code> while <code>age</code> is fixed and not allowed to vary. What constant? A natural candidate (and indeed <code>emmeans</code>’ default) is the mean. In our case, the mean age is 14.9 years. So the expected values produced above are for three 14.9 year olds with different heights. But is this data plausible? If I told you I saw a person who was 120cm tall, would you also assume they were 14.9 years old?</p>
<p>No, you would not. And that is exactly what covariance and multicollinearity mean - that some combinations of predictors are more likely than others.</p>
</section>
<section id="predicting-weight-from-height-accounting-for-covariance-among-predictors" class="level2">
<h2 class="anchored" data-anchor-id="predicting-weight-from-height-accounting-for-covariance-among-predictors">Predicting Weight from Height (accounting for covariance among predictors)</h2>
<p>So how do we get more reasonable expected weights?</p>
<p>Well, we can allow <code>age</code> to vary in our prediction. Vary how? Well, vary with height! So instead of asking <em>“what are the expected heights of 3 individuals that are 120, 145, and 170cm tall, all of the same age”</em>, we can ask <em>“what are the expected heights of 3 individuals that are 120, 145, and 170cm tall, of height-appropriate ages”</em>.</p>
<p>In <code>emmeans</code> this can be done by specifying a predictive formula in <code>cov.reduce</code>. For example:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">(rg_multicov <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ref_grid</span>(fit,</span>
<span id="cb7-2">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">at =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">145</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">170</span>)),</span>
<span id="cb7-3">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cov.reduce =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> height))) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This!</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; 'emmGrid' object with variables:
#&gt;     height = 120, 145, 170
#&gt;     age = (predicted by other variables)</code></pre>
</div>
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">rg_multicov<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">@</span>grid</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt;   height       age .wgt.
#&gt; 1    120  9.537649     1
#&gt; 2    145 14.640867     1
#&gt; 3    170 19.744085     1</code></pre>
</div>
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emmeans</span>(rg_multicov, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> height)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt;  height emmean    SE  df lower.CL upper.CL
#&gt;     120   35.8 0.727 497     34.3     37.2
#&gt;     145   61.8 0.315 497     61.1     62.4
#&gt;     170   87.7 0.673 497     86.4     89.1
#&gt; 
#&gt; Confidence level used: 0.95</code></pre>
</div>
</div>
<p>We can see from the reference grid (<code>rg_multicov@grid</code>) that <code>age</code> is not fixed, but varies with <code>height</code>.</p>
<p>We can also achieve the same this in a single call to <code>emmeans()</code>:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">em_by_height2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emmeans</span>(fit, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> height, </span>
<span id="cb13-2">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">at =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">145</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">170</span>)),</span>
<span id="cb13-3">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cov.red =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> height))</span>
<span id="cb13-4">em_by_height2</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt;  height emmean    SE  df lower.CL upper.CL
#&gt;     120   35.8 0.727 497     34.3     37.2
#&gt;     145   61.8 0.315 497     61.1     62.4
#&gt;     170   87.7 0.673 497     86.4     89.1
#&gt; 
#&gt; Confidence level used: 0.95</code></pre>
</div>
</div>
<p>What we’ve done is essentially re-introducing multicollinearity into our estimates by allowing <code>age</code> to co-vary with <code>height</code>: each expected value is no longer just a function of <code>height</code>, but it is also a function of the expected <code>age</code> for that <code>height</code><sup>2</sup>.</p>
<p>If we plot <em>these</em> estimates, we get…</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2020-08-11-when-predictors-covary/index_files/figure-html/em_plot2-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>…which seems more reasonable.</p>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>Should you do this whenever you have multicollinearity in your data?</p>
<p>No - not always and not only!</p>
<p>You might want to do this whenever exploring one predictor while fixing other to a constant produces uncommon <em>and unlikely</em> combinations of predictors. In fact, such a situation may arise even when measures of multicollinearity (such as the VIF) are not “too” high, as these usually only measure co-<em>linearity</em>, but other patterns of co-variation my be present in your data. In fact this may be useful in <strong><em>any instance of co-variation</em></strong> among predictors, such as <strong>suppression / confounding / mediation</strong> (take your pick).<sup>3</sup></p>
<p>Note also that producing predictions in this manner does not actually “solve” any of the difficulties of interpretations or any of the oddities that arise in statistical suppression / confounding / mediation. (Extra) care should be taken when interpreting the parameters and the predictions of models with co-varying predictors. In this our case, the question of “<em>does weight vary more with age, or with height</em>” can’t cleanly be answered - what is the meaning of looking at the coefficient (or standardized coefficient) of one predictor while holding the other constant when we know they strongly co-vary?</p>
<section id="just-the-code-please" class="level3">
<h3 class="anchored" data-anchor-id="just-the-code-please">Just the code please:</h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>opts_chunk<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">echo =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb15-2">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">message =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, </span>
<span id="cb15-3">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">comment =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#&gt;"</span>)</span>
<span id="cb15-4"></span>
<span id="cb15-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(emmeans)</span>
<span id="cb15-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb15-7"></span>
<span id="cb15-8"></span>
<span id="cb15-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">source</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'../msbblog_theme_pallets.R'</span>)</span>
<span id="cb15-10"></span>
<span id="cb15-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_set</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_msbblog</span>())</span>
<span id="cb15-12"></span>
<span id="cb15-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb15-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(datawizard)</span>
<span id="cb15-15"></span>
<span id="cb15-16">S <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb15-17">S[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> S[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add multicollinearity</span></span>
<span id="cb15-18"></span>
<span id="cb15-19"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb15-20">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> MASS<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mvrnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), S, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">empirical =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb15-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb15-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> X1,</span>
<span id="cb15-23">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">age =</span> X2,</span>
<span id="cb15-24">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">e =</span> X3) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb15-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">age =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">change_scale</span>(age, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">to =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span>)),</span>
<span id="cb15-26">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">change_scale</span>(height, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">to =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">108</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">189</span>)),</span>
<span id="cb15-27">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weight =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale</span>(height) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale</span>(age) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale</span>(age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> e)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">63</span>)</span>
<span id="cb15-28"></span>
<span id="cb15-29"></span>
<span id="cb15-30">base_plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(height, weight, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> age)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_gradientn</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Age [yrs]"</span>,</span>
<span id="cb15-33">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colours =</span> msbblog_colors[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"purple"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"orange"</span>)],</span>
<span id="cb15-34">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Weight [Kg]"</span>,</span>
<span id="cb15-36">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Height [cm]"</span>)</span>
<span id="cb15-37"></span>
<span id="cb15-38">base_plot</span>
<span id="cb15-39">fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(weight <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> height <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> age, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data)</span>
<span id="cb15-40"></span>
<span id="cb15-41"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(fit)</span>
<span id="cb15-42"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(emmeans)</span>
<span id="cb15-43"></span>
<span id="cb15-44">em_by_height <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emmeans</span>(fit, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> height, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">at =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">145</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">170</span>)))</span>
<span id="cb15-45">em_by_height</span>
<span id="cb15-46">p_dat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(em_by_height)</span>
<span id="cb15-47">p_dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>age <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> em_by_height<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">@</span>linfct[,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age"</span>]</span>
<span id="cb15-48"></span>
<span id="cb15-49">base_plot <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb15-50">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> p_dat, </span>
<span id="cb15-51">             <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> emmean, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> age), </span>
<span id="cb15-52">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stroke =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>)</span>
<span id="cb15-53">performance<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">check_collinearity</span>(fit)</span>
<span id="cb15-54"></span>
<span id="cb15-55">(rg_multicov <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ref_grid</span>(fit,</span>
<span id="cb15-56">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">at =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">145</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">170</span>)),</span>
<span id="cb15-57">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cov.reduce =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> height))) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This!</span></span>
<span id="cb15-58">rg_multicov<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">@</span>grid</span>
<span id="cb15-59"></span>
<span id="cb15-60"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emmeans</span>(rg_multicov, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> height)</span>
<span id="cb15-61">em_by_height2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emmeans</span>(fit, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> height, </span>
<span id="cb15-62">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">at =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">145</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">170</span>)),</span>
<span id="cb15-63">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cov.red =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> height))</span>
<span id="cb15-64">em_by_height2</span>
<span id="cb15-65">p_dat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(em_by_height2)</span>
<span id="cb15-66">p_dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>age <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> em_by_height2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">@</span>linfct[,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age"</span>]</span>
<span id="cb15-67"></span>
<span id="cb15-68">base_plot <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb15-69">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> p_dat, </span>
<span id="cb15-70">             <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> emmean, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> age), </span>
<span id="cb15-71">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stroke =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>)</span></code></pre></div>
</div>


</section>
</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>Ignoring the obvious heteroscedasticity in the data.↩︎</p></li>
<li id="fn2"><p>Note that here the expected <code>age</code> is a <em>linear</em> function of <code>height</code>, but this can really take any form: <code>age ~ poly(height,3)</code>, <code>age ~ log(height)</code>, etc.↩︎</p></li>
<li id="fn3"><p>Thus it is also related to the idea of <em>conditioning on a collider</em>, which is really “just” a case of co-varrying predictors with a specific causal structure. Here we are conditioning the association between weight and height on age, which has a similar statistical confounding effect. For more, read <a href="http://www.the100.ci/2017/03/14/that-one-weird-third-variable-problem-nobody-ever-mentions-conditioning-on-a-collider/">this post and comments from 100%CI</a>.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>statistics</category>
  <category>R</category>
  <category>emmeans</category>
  <category>code</category>
  <category>glm</category>
  <category>multicollinearity</category>
  <category>assumptions</category>
  <guid>https://blog.msbstats.info/posts/2020-08-11-when-predictors-covary/</guid>
  <pubDate>Mon, 10 Aug 2020 21:00:00 GMT</pubDate>
</item>
<item>
  <title>Estimating and testing GLMs with emmeans</title>
  <link>https://blog.msbstats.info/posts/2020-04-13-estimating-and-testing-GLMs-with-emmeans/</link>
  <description><![CDATA[ 





<p><sub><em>This post was written in collaboration with Almog Simchon (<a href="https://twitter.com/almogsi/">@almogsi</a>) and Shachar Hochman (<a href="https://twitter.com/HochmanShachar/">@HochmanShachar</a>). Go follow them.</em></sub></p>
<section id="the-fictional-simplicity-of-generalized-linear-models" class="level1">
<h1>The fictional simplicity of Generalized Linear Models</h1>
<p>Who doesn’t love GLMs? The ingenious idea of taking a response level variable (e.g.&nbsp;binary or count) and getting some link function magic to treat it as if it was our long-time friend, linear regression.</p>
<p>In the last few days, a <a href="https://doi.org/10.31234/osf.io/th94c">preprint by McCabe <em>et al.</em></a> popped up in our twitter feed (recommended reading!) and re-focused our attention on the problem with interpreting effects and interactions within the GLM framework. McCabe <em>et al.</em> state in the abstract that:</p>
<blockquote class="blockquote">
<p>“To date, typical practice in evaluating interaction effects in GLMs extends directly from linear approaches, in which the product term coefficient between variables of interest are used to provide evidence of an interaction effect. However, unlike linear models, interaction effects in GLMs are not equal to product terms between predictor variables and are instead a function of all predictors of a model.”</p>
</blockquote>
<p>The what-now?</p>
<p>When fitting a GLM we think of the response level (the binary outcome or the counts that interest us), however, the model is fit (and often all statistical inferences are done) on the latent level - in the realm of the link function.</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2020-04-13-estimating-and-testing-GLMs-with-emmeans/index_files/figure-html/fig1-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>For interactions (but as you will see soon, not only), it means that when we test the effect of <code>X1</code> at different levels of <code>X2</code>, we’re testing these effects <strong>on the latent (e.g., logistic) level, which might not represent these effects on the response level!</strong></p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2020-04-13-estimating-and-testing-GLMs-with-emmeans/index_files/figure-html/fig2-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>For example, both plots above represent corresponding predictions from the same interaction-model - on the left we have those predictions on the latent level, and on the right these have been transformed back to the response level (the probability). We can see that at the latent level, the effect of <code>X1</code> on y is very different at all levels of <code>X2</code>, but at the response level these differences can shrink or possibly disappear (e.g., red vs.&nbsp;blue lines), or get larger (red and blue vs.&nbsp;purple line).</p>
<p>This is true regardless of whether or not an interaction was included in the model! And in fact, even main effects on the latent level do not always correspond to the response level the way we would have intuitively imagined.</p>
<p>What follows are 3 methods for testing interactions in GLMs, using <code>emmeans</code>. Again, we highly recommend reading McCabe <em>et al.</em>’s original paper.</p>
<p>Let’s load up some packages:</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(emmeans)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1.8.4.1</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(magrittr) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2.0.3 </span></span></code></pre></div>
</section>
<section id="the-model" class="level1">
<h1>The Model</h1>
<p>The model used here is a logistic regression model, using data adapted from <a href="https://github.com/connorjmccabe/modglm">McCabe <em>et al.</em></a>, except we’re using a binomial outcome (see code for data generation at the end of the post):</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plogis</span>(xb) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span></span>
<span id="cb2-2"></span>
<span id="cb2-3">model <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glm</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> female <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x2,</span>
<span id="cb2-4">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> df,</span>
<span id="cb2-5">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"binomial"</span>)</span></code></pre></div>
</div>
</section>
<section id="using-emmeans-for-estimation-testing" class="level1">
<h1>Using <code>emmeans</code> for estimation / testing</h1>
<p>If you’re not yet familiar with <a href="https://cran.r-project.org/package=emmeans"><code>emmeans</code></a>, it is a package for estimating, testing, and plotting marginal and conditional means / effects from a <a href="https://cran.r-project.org/web/packages/emmeans/vignettes/models.html">variety of linear models</a>, including GLMs.</p>
<p>So let’s answer the question:</p>
<blockquote class="blockquote">
<p>Does the effect of sex (<code>female</code>) differ as a function of <code>x1</code>, and how does this interaction differ as a function of <code>x2</code>.</p>
</blockquote>
<p>We will use the pick-a-point method for both continuous variables:</p>
<ul>
<li>For <code>x1</code>: -1, +1</li>
<li>For <code>x2</code>: mean +- sd</li>
</ul>
<section id="on-the-latent-level" class="level2">
<h2 class="anchored" data-anchor-id="on-the-latent-level">1. On the latent level</h2>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2020-04-13-estimating-and-testing-GLMs-with-emmeans/index_files/figure-html/plot_latent-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<center>
<em>Predictions from the model on the latent scale.</em>
</center>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emmeans</span>(model, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> female <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x2,</span>
<span id="cb3-2">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">at =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)),</span>
<span id="cb3-3">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cov.reduce =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x2 =</span> mean_sd)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrast</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">interaction =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pairwise"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pairwise"</span>),</span>
<span id="cb3-5">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x2"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>x2 = -1.020:
 x1_pairwise female_pairwise estimate    SE  df z.ratio p.value
 (-1) - 1    0 - 1              -1.17 0.497 Inf  -2.350  0.0188

x2 = -0.007:
 x1_pairwise female_pairwise estimate    SE  df z.ratio p.value
 (-1) - 1    0 - 1              -1.17 0.497 Inf  -2.350  0.0188

x2 =  1.006:
 x1_pairwise female_pairwise estimate    SE  df z.ratio p.value
 (-1) - 1    0 - 1              -1.17 0.497 Inf  -2.350  0.0188

Results are given on the log odds ratio (not the response) scale. </code></pre>
</div>
</div>
<p>It seems that on the latent level the (estimated) difference of differences (the interaction) between <code>female</code> and <code>x1</code> is unaffected by which level of <code>x2</code> they are conditioned on. This makes sense - we did not model a 3-way interaction, so why should it? Everything is acting as expected.</p>
<p>Or is it? Well, that depends…</p>
</section>
<section id="on-the-response-level-the-delta-method" class="level2">
<h2 class="anchored" data-anchor-id="on-the-response-level-the-delta-method">2. On the response level (the delta method)</h2>
<p>We can also try and answer the same question on the response level using the delta method (baked into <code>emmeans</code>). Here we have two options for defining an “effect”:</p>
<ol type="1">
<li>An effect is a difference in probabilities.</li>
<li>An effect is a ratio of probabilities.</li>
</ol>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2020-04-13-estimating-and-testing-GLMs-with-emmeans/index_files/figure-html/plot_outcome-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<center>
<em>Predictions from the model on the response scale.</em>
</center>
<section id="differences-in-probabilities" class="level3">
<h3 class="anchored" data-anchor-id="differences-in-probabilities">2.1. Differences in probabilities</h3>
<p>For this, we just need to add <code>trans = "response"</code> in the call to <code>emmeans()</code>:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emmeans</span>(model, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> female <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x2,</span>
<span id="cb5-2">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">at =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)),</span>
<span id="cb5-3">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cov.reduce =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x2 =</span> mean_sd),</span>
<span id="cb5-4">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trans =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"response"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrast</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">interaction =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pairwise"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pairwise"</span>),</span>
<span id="cb5-6">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x2"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>x2 = -1.020:
 x1_pairwise female_pairwise estimate     SE  df z.ratio p.value
 (-1) - 1    0 - 1            -0.0265 0.0787 Inf  -0.336  0.7365

x2 = -0.007:
 x1_pairwise female_pairwise estimate     SE  df z.ratio p.value
 (-1) - 1    0 - 1             0.0976 0.0470 Inf   2.075  0.0380

x2 =  1.006:
 x1_pairwise female_pairwise estimate     SE  df z.ratio p.value
 (-1) - 1    0 - 1             0.0371 0.0163 Inf   2.279  0.0227</code></pre>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Difference of differences when x2 = -1.02</span></span>
<span id="cb7-2">(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.69457</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.44178</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.32986</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05059</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] -0.02648</code></pre>
</div>
</div>
<p>It seems that on the response level, we get different results than on the latent level. And not only that, but even though the model did not include a 3-way interaction, the 2-way <code>female:x1</code> interaction is conditional on the level of <code>x2</code> - changing in size as a function of <code>x2</code>, and is not significant in low levels of <code>x2</code>!</p>
</section>
<section id="ratios-of-probabilities" class="level3">
<h3 class="anchored" data-anchor-id="ratios-of-probabilities">2.2. Ratios of probabilities</h3>
<p>(Also called <em>risk ratios</em>.)</p>
<p>For this, we just need to add <code>trans = "log"</code> and <code>type = "response"</code> in the call to <code>emmeans()</code>:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emmeans</span>(model, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> female <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x2,</span>
<span id="cb9-2">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">at =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)),</span>
<span id="cb9-3">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cov.reduce =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x2 =</span> mean_sd),</span>
<span id="cb9-4">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trans =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"log"</span>,</span>
<span id="cb9-5">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"response"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb9-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrast</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">interaction =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pairwise"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pairwise"</span>), </span>
<span id="cb9-7">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x2"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>x2 = -1.02:
 x1_pairwise female_pairwise ratio    SE  df null z.ratio p.value
 (-1) / 1    0 / 1           0.241 0.097 Inf    1  -3.534  0.0004

x2 = -0.007:
 x1_pairwise female_pairwise ratio    SE  df null z.ratio p.value
 (-1) / 1    0 / 1           0.268 0.124 Inf    1  -2.856  0.0043

x2 = 1.006:
 x1_pairwise female_pairwise ratio    SE  df null z.ratio p.value
 (-1) / 1    0 / 1           0.299 0.146 Inf    1  -2.480  0.0131

Tests are performed on the log scale </code></pre>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ratio of ratios when x2 = -1.02</span></span>
<span id="cb11-2">(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.69457</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.44178</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.32986</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05059</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.2411265</code></pre>
</div>
</div>
<p>It seems that even on the response level, different delta methods produce different results!<sup>1</sup> Although we maintain the finding that the size of the 2-way <code>female:x1</code> interaction is conditional on the level of <code>x2</code>, here it <em>decreases</em> as a function of <code>x2</code> (but is significant across all (tested) values of <code>x2</code>)!</p>
<p>(Note: as we are interested in the slope of <code>x1</code>, we could have used <code>emtrends</code> instead of <code>emmeans</code>. See code at the end of the post for what that would look like.)</p>
</section>
</section>
<section id="not-just-a-problem-with-interactions" class="level2">
<h2 class="anchored" data-anchor-id="not-just-a-problem-with-interactions">Not just a problem with interactions…</h2>
<p>As stated above, this is not only an issue of interactions. For example, when looking at the response level, the effect of <code>x2</code> is itself conditional on the value of <code>x2</code>!</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emmeans</span>(model, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x2,</span>
<span id="cb13-2">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cov.reduce =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x2 =</span> mean_sd),</span>
<span id="cb13-3">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trans =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"response"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb13-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrast</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"consec"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> contrast               estimate      SE  df z.ratio p.value
 (x2-0.007) - (x2-1.02)  -0.2361 0.02190 Inf -10.782  &lt;.0001
 x21.006 - (x2-0.007)    -0.0965 0.00825 Inf -11.695  &lt;.0001

Results are averaged over the levels of: female 
P value adjustment: mvt method for 2 tests </code></pre>
</div>
</div>
<p>Note how the estimated effect of <code>x2</code> is smaller for larger values of <code>x2</code>!</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2020-04-13-estimating-and-testing-GLMs-with-emmeans/index_files/figure-html/plot_x2-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="conclusion" class="level1">
<h1>Conclusion</h1>
<p>The growing popularity of GLMs (and GLMMs) in social research seems to come with another source of researcher degrees of freedom (<a href="https://doi.org/10.1177/2515245917747646/">and we all know how well that works for us</a>)…</p>
<p>What should you do?</p>
<br>
<center>
<img src="https://media.giphy.com/media/3oFzm1GJRMrsTXtU2Y/giphy.gif" class="img-fluid">
</center>
<p><br></p>
<p>Honestly, we don’t know. Some of us feel that since the response variable is our variable of interest, that’s what we should be focusing on; some of us feel that with no common practice, we should stick to the latent level; some of us are agnostic (that covers all of us). We can’t recommend one approach, but we do think that when fitting and working with GLMs, this is a consideration one has to face.<sup>2</sup> Good luck!</p>
<br>
<center>
<img src="https://media.giphy.com/media/8FfoOibYhSd0XFyVy7/giphy.gif" class="img-fluid">
</center>
<p><br></p>
<section id="make-the-data-and-model" class="level4">
<h4 class="anchored" data-anchor-id="make-the-data-and-model">Make the data and model</h4>
<div class="cell">
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1678</span>)</span>
<span id="cb15-2"></span>
<span id="cb15-3">b0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.8</span>      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Intercept</span></span>
<span id="cb15-4">b1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">35</span>       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># X1 Effect</span></span>
<span id="cb15-5">b2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># X2 Effect</span></span>
<span id="cb15-6">b3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sex covariate effect</span></span>
<span id="cb15-7">b13 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># product term coefficient</span></span>
<span id="cb15-8">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sample Size</span></span>
<span id="cb15-9">mu <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Specify means</span></span>
<span id="cb15-10"></span>
<span id="cb15-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Specify covariance matrix</span></span>
<span id="cb15-12">S <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), </span>
<span id="cb15-13">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) </span>
<span id="cb15-14">sigma <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Level 1 error</span></span>
<span id="cb15-15"></span>
<span id="cb15-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># simulates our continuous predictors from a multivariate </span></span>
<span id="cb15-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># normal distribution</span></span>
<span id="cb15-18">rawvars <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> MASS<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mvrnorm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> mu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Sigma =</span> S) </span>
<span id="cb15-19"></span>
<span id="cb15-20">cat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbinom</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> n, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb15-21">id <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n)</span>
<span id="cb15-22">eij <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(id, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, sigma))</span>
<span id="cb15-23"></span>
<span id="cb15-24">xb <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (b0) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-25">  (b1) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (rawvars[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-26">  (b2) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (rawvars[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb15-27">  (b3) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> cat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb15-28">  b13 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> cat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (rawvars[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb15-29">  eij</span>
<span id="cb15-30"></span>
<span id="cb15-31">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x1 =</span> rawvars[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb15-32">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x2 =</span> rawvars[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],</span>
<span id="cb15-33">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">female =</span> cat)</span>
<span id="cb15-34">y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plogis</span>(xb) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span></span>
<span id="cb15-35"></span>
<span id="cb15-36">model <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glm</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> female <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x2,</span>
<span id="cb15-37">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> df,</span>
<span id="cb15-38">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"binomial"</span>)</span></code></pre></div>
</div>
</section>
<section id="using-emtrends" class="level4">
<h4 class="anchored" data-anchor-id="using-emtrends">Using <code>emtrends</code></h4>
<p>Note that the inferential results (<img src="https://latex.codecogs.com/png.latex?z"> and <img src="https://latex.codecogs.com/png.latex?p"> values) are similar (though not identical) to those obtained using <code>emmeans</code>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># log(odds)</span></span>
<span id="cb16-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emtrends</span>(model, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> female <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x2, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x1"</span>,</span>
<span id="cb16-3">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cov.reduce =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x2 =</span> mean_sd)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb16-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrast</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pairwise"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x2"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>x2 = -1.020:
 contrast          estimate    SE  df z.ratio p.value
 female0 - female1    0.584 0.248 Inf   2.350  0.0188

x2 = -0.007:
 contrast          estimate    SE  df z.ratio p.value
 female0 - female1    0.584 0.248 Inf   2.350  0.0188

x2 =  1.006:
 contrast          estimate    SE  df z.ratio p.value
 female0 - female1    0.584 0.248 Inf   2.350  0.0188</code></pre>
</div>
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># diffs</span></span>
<span id="cb18-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emtrends</span>(model, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> female <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x2, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x1"</span>,</span>
<span id="cb18-3">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cov.reduce =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x2 =</span> mean_sd),</span>
<span id="cb18-4">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trans =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"response"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb18-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrast</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pairwise"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x2"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>x2 = -1.020:
 contrast          estimate      SE  df z.ratio p.value
 female0 - female1   0.0107 0.04117 Inf   0.259  0.7957

x2 = -0.007:
 contrast          estimate      SE  df z.ratio p.value
 female0 - female1  -0.0542 0.02399 Inf  -2.259  0.0239

x2 =  1.006:
 contrast          estimate      SE  df z.ratio p.value
 female0 - female1  -0.0195 0.00793 Inf  -2.457  0.0140</code></pre>
</div>
<div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ratios</span></span>
<span id="cb20-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emtrends</span>(model, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> female <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x2, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x1"</span>,</span>
<span id="cb20-3">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cov.reduce =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x2 =</span> mean_sd),</span>
<span id="cb20-4">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trans =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"log"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"response"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb20-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrast</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pairwise"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x2"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>x2 = -1.020:
 contrast          estimate    SE  df z.ratio p.value
 female0 - female1    0.727 0.207 Inf   3.509  0.0004

x2 = -0.007:
 contrast          estimate    SE  df z.ratio p.value
 female0 - female1    0.663 0.233 Inf   2.848  0.0044

x2 =  1.006:
 contrast          estimate    SE  df z.ratio p.value
 female0 - female1    0.605 0.244 Inf   2.476  0.0133</code></pre>
</div>
</div>


</section>
</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>Note that for Poisson models with a “log” link function, this is the same as working on the latent level!↩︎</p></li>
<li id="fn2"><p>But hey, whatever you do - <a href="https://doi.org/10.1890/10-0340.1">don’t model binary / count data with a linear model</a>, okay?↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>R</category>
  <category>statistics</category>
  <category>code</category>
  <category>glm</category>
  <category>emmeans</category>
  <guid>https://blog.msbstats.info/posts/2020-04-13-estimating-and-testing-GLMs-with-emmeans/</guid>
  <pubDate>Sun, 12 Apr 2020 21:00:00 GMT</pubDate>
</item>
<item>
  <title>The Mysterious Case of the Ghost Interaction</title>
  <link>https://blog.msbstats.info/posts/2019-10-30-ghost-interactions/</link>
  <description><![CDATA[ 





<p><sub><em>This spooky post was written in collaboration with Yoav Kessler (<a href="https://twitter.com/yoav_kessler">@yoav_kessler</a>) and Naama Yavor (<a href="https://twitter.com/namivor">@namivor</a>).</em></sub>.</p>
<hr>
<p>Experimental psychology is moving away from repeated-measures-ANOVAs, and towards linear mixed models (LMM<sup>1</sup>). LMMs have many advantages over rmANOVA, including (but not limited to):</p>
<ul>
<li>Analysis of single trial data (as opposed to aggregated means per condition).</li>
<li>Specifying more than one random factor (typically crossed random intercepts of <em>subject</em> and <em>item</em>).</li>
<li>The use of continuous variables as predictors.<br>
</li>
<li><del>Making you look like you know what you’re doing.</del><br>
</li>
<li><del>Defeating the un-dead / reviewer 2.</del><br>
</li>
<li>The ability to specify custom models.<sup>2</sup></li>
</ul>
<p>This post will focus on this last point. Specifically, why you should always include main-effects when modeling interactions, and what happens if you don’t (<strong><em>spooky</em></strong>).</p>
<section id="fitting-the-right-yet-oh-so-wrong-model" class="level2">
<h2 class="anchored" data-anchor-id="fitting-the-right-yet-oh-so-wrong-model">Fitting the Right (yet oh so wrong) Model</h2>
<p>Say you’ve finally won that grant you submitted to study candy consumption during ghostly themed holidays. As part of your first study, you decide to measure the effects of <em>costume type</em> (scary / cute) and level of neighborhood <em>decor</em> (high / low levels of house decorations) on the total weight of collected candy (in Kg). A simple, yet informative 2-by-2 design.</p>
<p>Being the serious scientist you are, you have several hypotheses:</p>
<ol type="1">
<li><strong>A main effect for <em>decor level</em></strong> - neighborhoods with more decorations will overall give out more candy.</li>
<li><strong>No main effect for <em>costume</em></strong> - overall, children with cute and scary costumes will receive the same amount of candy (in Kg).</li>
<li><strong>A <em>decor level</em> <img src="https://latex.codecogs.com/png.latex?%5Ctimes"> <em>costume</em> interaction</strong> - high <em>decor</em> neighborhoods will favor scary costumes, while low <em>decor</em> neighborhoods will favor cute costumes.</li>
</ol>
<p>It would only make sense to specify your statistical model accordingly - after all, why shouldn’t your model represent your hypotheses?</p>
<p>In R, such a model is described as <code>candy_kg ~ decor + decor:costume</code>, instructing R to model <code>candy_kg</code> as a function of the effect for <code>decor</code> + the interaction <code>decor:costume</code>.</p>
<p>And so, you fit the model:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">options</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">contrasts =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'contr.sum'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'contr.poly'</span>)) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># set effects coding (just once)</span></span>
<span id="cb1-2"></span>
<span id="cb1-3">fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aov</span>(candy_kg <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> decor <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> decor<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>costume, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> spooky_data)</span></code></pre></div>
</div>
<div class="cell">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<colgroup>
<col style="width: 20%">
<col style="width: 20%">
<col style="width: 5%">
<col style="width: 10%">
<col style="width: 8%">
<col style="width: 8%">
<col style="width: 11%">
<col style="width: 13%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;"></th>
<th style="text-align: left;">Term</th>
<th style="text-align: right;">df</th>
<th style="text-align: left;">SS</th>
<th style="text-align: left;">MS</th>
<th style="text-align: left;">F</th>
<th style="text-align: left;">p-value</th>
<th style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Ceta%5E2"></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">decor</td>
<td style="text-align: left;">decor</td>
<td style="text-align: right;">1</td>
<td style="text-align: left;">30.00</td>
<td style="text-align: left;">30.00</td>
<td style="text-align: left;">23.64</td>
<td style="text-align: left;">&lt;0.001</td>
<td style="text-align: left;">0.10</td>
</tr>
<tr class="even">
<td style="text-align: left;">decor:costume</td>
<td style="text-align: left;">decor:costume</td>
<td style="text-align: right;">2</td>
<td style="text-align: left;">120.00</td>
<td style="text-align: left;">60.00</td>
<td style="text-align: left;">47.28</td>
<td style="text-align: left;">&lt;0.001</td>
<td style="text-align: left;">0.40</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Residuals</td>
<td style="text-align: left;">Residuals</td>
<td style="text-align: right;">116</td>
<td style="text-align: left;">147.20</td>
<td style="text-align: left;">1.27</td>
<td style="text-align: left;"></td>
<td style="text-align: left;"></td>
<td style="text-align: left;"></td>
</tr>
</tbody>
</table>
</div>
</div>
<p>As predicted, you find both a significant main effect for <em>decor</em> and the interaction <em>decor</em> <img src="https://latex.codecogs.com/png.latex?%5Ctimes"> <em>costume</em>, with the interaction explaining 40% of the variance in collected candy weight. So far so good - your results reflect your hypotheses!</p>
<p>But then you plot your data, and to your horror you find…</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2019-10-30-ghost-interactions/index_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>It looks like there is no interaction <em>at all</em>! Your interaction was nothing more than a ghost! An apparition! How is this possible?? Where has all of variance explained by it gone???</p>
<center>
<p><img src="https://blog.msbstats.info/posts/2019-10-30-ghost-interactions/jack_what_is_this.gif" class="img-fluid"></p>
<em>What IS This??</em>
</center>
<p>In fact, had you fit the full model, you would have found:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aov</span>(candy_kg <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> decor <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> costume, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> spooky_data)</span></code></pre></div>
</div>
<div class="cell">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<colgroup>
<col style="width: 20%">
<col style="width: 20%">
<col style="width: 5%">
<col style="width: 10%">
<col style="width: 10%">
<col style="width: 8%">
<col style="width: 11%">
<col style="width: 13%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;"></th>
<th style="text-align: left;">Term</th>
<th style="text-align: right;">df</th>
<th style="text-align: left;">SS</th>
<th style="text-align: left;">MS</th>
<th style="text-align: left;">F</th>
<th style="text-align: left;">p-value</th>
<th style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Ceta%5E2"></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">decor</td>
<td style="text-align: left;">decor</td>
<td style="text-align: right;">1</td>
<td style="text-align: left;">30.00</td>
<td style="text-align: left;">30.00</td>
<td style="text-align: left;">23.64</td>
<td style="text-align: left;">&lt;0.001</td>
<td style="text-align: left;">0.10</td>
</tr>
<tr class="even">
<td style="text-align: left;">costume</td>
<td style="text-align: left;">costume</td>
<td style="text-align: right;">1</td>
<td style="text-align: left;">120.00</td>
<td style="text-align: left;">120.00</td>
<td style="text-align: left;">94.56</td>
<td style="text-align: left;">&lt;0.001</td>
<td style="text-align: left;">0.40</td>
</tr>
<tr class="odd">
<td style="text-align: left;">decor:costume</td>
<td style="text-align: left;">decor:costume</td>
<td style="text-align: right;">1</td>
<td style="text-align: left;">0.00</td>
<td style="text-align: left;">0.00</td>
<td style="text-align: left;">0.00</td>
<td style="text-align: left;">&gt;0.999</td>
<td style="text-align: left;">0.00</td>
</tr>
<tr class="even">
<td style="text-align: left;">Residuals</td>
<td style="text-align: left;">Residuals</td>
<td style="text-align: right;">116</td>
<td style="text-align: left;">147.20</td>
<td style="text-align: left;">1.27</td>
<td style="text-align: left;"></td>
<td style="text-align: left;"></td>
<td style="text-align: left;"></td>
</tr>
</tbody>
</table>
</div>
</div>
<p>The interaction actually explains 0% of the variance! And the effect of <em>costume</em> is the one that explains 40% of the variance!<sup>3</sup> How could this be?? Have we angered Fisher’s spirit somehow?</p>
<p>What happened was that because we did not account for <em>costume</em> in our model, the variance explained by <em>costume</em> was swallowed by the interaction <em>decor</em> <img src="https://latex.codecogs.com/png.latex?%5Ctimes"> <em>costume</em>!</p>
</section>
<section id="the-math" class="level2">
<h2 class="anchored" data-anchor-id="the-math">The Math</h2>
<p><sub><em>If you find math too scary, feel free to skip to conclusion.</em></sub></p>
<p>Travel back to <em>Intro to Stats</em>, and recall that the interaction’s sum-of-squares - <img src="https://latex.codecogs.com/png.latex?SS_%7BA%5Ctimes%20B%7D"> - is calculated as:</p>
<center>
<img src="https://latex.codecogs.com/png.latex?SS_%7BA%5Ctimes%20B%7D%20=%20(%5Cbar%7Bx%7D_%7Bij%7D%20-%20%5Cbar%7Bx%7D_%7Bi.%7D%20-%20%5Cbar%7Bx%7D_%7B.j%7D%20+%20%5Cbar%7B%5Cbar%7Bx%7D%7D_%7B..%7D)%5E2">
</center>
<p>This is a simplification of the following equation:</p>
<center>
<img src="https://latex.codecogs.com/png.latex?SS_%7BA%5Ctimes%20B%7D%20=%20%5Csum%20%5Csum%20(%5Cbar%7Bx%7D_%7Bij%7D%20-%20(%5Cbar%7Bx%7D_%7Bi.%7D%20-%20%5Cbar%7B%5Cbar%7Bx%7D%7D_%7B..%7D)%20-%20(%5Cbar%7Bx%7D_%7B.j%7D%20-%20%5Cbar%7B%5Cbar%7Bx%7D%7D_%7B..%7D)%20+%20%5Cbar%7B%5Cbar%7Bx%7D%7D_%7B..%7D)%5E2">
</center>
<p>Where <img src="https://latex.codecogs.com/png.latex?(%5Cbar%7Bx%7D_%7Bi.%7D%20-%20%5Cbar%7B%5Cbar%7Bx%7D%7D_%7B..%7D)"> represents the main effect for <img src="https://latex.codecogs.com/png.latex?A"> and <img src="https://latex.codecogs.com/png.latex?(%5Cbar%7Bx%7D_%7B.j%7D%20-%20%5Cbar%7B%5Cbar%7Bx%7D%7D_%7B..%7D)"> represents the main effect for <img src="https://latex.codecogs.com/png.latex?B">. We can see that <img src="https://latex.codecogs.com/png.latex?SS_%7BA%5Ctimes%20B%7D"> represents the <strong><em>deviation from the additive model</em></strong> - i.e., it is the degree by which the observed cells’ means deviate from what would be expected if there were only the two main effects.</p>
<p>When we exclude the main effect of <img src="https://latex.codecogs.com/png.latex?B"> from out model, we are telling our model that there is no need to estimate the main effect. That is, we set <img src="https://latex.codecogs.com/png.latex?(%5Cbar%7Bx%7D_%7B.j%7D%20-%20%5Cbar%7B%5Cbar%7Bx%7D%7D_%7B..%7D)=0">. The resulting <img src="https://latex.codecogs.com/png.latex?SS_%7BA%5Ctimes%20B%7D"> is computed not as above, but as:</p>
<center>
<img src="https://latex.codecogs.com/png.latex?SS_%7BA%5Ctimes%20B%7D%20=%20%5Csum%20%5Csum%20(%5Cbar%7Bx%7D_%7Bij%7D%20-%20(%5Cbar%7Bx%7D_%7Bi.%7D%20-%20%5Cbar%7B%5Cbar%7Bx%7D%7D_%7B..%7D)%20+%20%5Cbar%7B%5Cbar%7Bx%7D%7D_%7B..%7D)%5E2">
</center>
<p>This formula represents the degree by which the observed cells’ means deviate from what would be expected if there was only the main effect of <img src="https://latex.codecogs.com/png.latex?A">. But now if the cells’ means deviate in a way that would otherwise have been part of a main effect for <img src="https://latex.codecogs.com/png.latex?B">, the cells’ deviations from the main effect for <img src="https://latex.codecogs.com/png.latex?A"> will now include the deviations that would otherwise have been accounted for by a main effect of <img src="https://latex.codecogs.com/png.latex?B">! This results in the main effect for <img src="https://latex.codecogs.com/png.latex?B"> essentially getting “pooled” into <img src="https://latex.codecogs.com/png.latex?SS_%7BA%5Ctimes%20B%7D">. Furthermore, had you also excluded a main effect for <img src="https://latex.codecogs.com/png.latex?A">, this effect too would have been “pooled” into the so-called <img src="https://latex.codecogs.com/png.latex?A%5Ctimes%20B"> interaction.</p>
<p>In other words:</p>
<blockquote class="blockquote">
<p>When we don’t estimate (model) main effects, <strong>we change the meaning of interactions</strong> - they no longer represents a deviation from the additive model.</p>
</blockquote>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>Sure, you can specify a model with no main effect and only interactions, but in such a case the interactions no longer mean what we expect them to mean. If we want interactions to represent deviation from the additive model, <strong><em>our model must also include the additive model!</em></strong></p>
<p>For simplicity’s sake, this example has focused on a simple 2-by-2 between subject design, but the conclusions drawn here are relevant for any design in which a factor interacts with or moderates the effect of another factor or continuous variable.</p>
<section id="code-for-data-generation" class="level4">
<h4 class="anchored" data-anchor-id="code-for-data-generation">code for data generation</h4>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb3-2">M <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4 means with no interaction</span></span>
<span id="cb3-3">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span></span>
<span id="cb3-4">e <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(n),<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">center =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># some error</span></span>
<span id="cb3-5"></span>
<span id="cb3-6">spooky_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand.grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">decor =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Low"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"High"</span>),</span>
<span id="cb3-7">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">costume =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Scary"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cute"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">candy_kg =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(M,  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> .x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> e)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> candy_kg)</span></code></pre></div>
</div>


</section>
</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>Or hierarchical linear models (HLM)… or mixed linear models (MLM)…↩︎</p></li>
<li id="fn2"><p>Whereas in an AVONA analysis with 4 factors you always have: Four main effects + Six 2-way interaction + Four 3-way interaction + One 4-way interaction.↩︎</p></li>
<li id="fn3"><p>Note also that the <img src="https://latex.codecogs.com/png.latex?df_%7Bresidual%7D"> is the same for both models, indicating the same number of parameters overall have been estimated in both. E.g., while in the full model we would have 3 parameters - one for each main effect + one for the interaction, in the misspecified model we have one for the main effect, and two for the interaction. That is, no matter how you tell the model to split the <img src="https://latex.codecogs.com/png.latex?SS">s, the number of parameters needed to model 4 cells will always be 3.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>R</category>
  <category>statistics</category>
  <category>ANOVA</category>
  <category>code</category>
  <category>linear mixed models</category>
  <guid>https://blog.msbstats.info/posts/2019-10-30-ghost-interactions/</guid>
  <pubDate>Tue, 29 Oct 2019 22:00:00 GMT</pubDate>
</item>
<item>
  <title>Accounting for Within- AND Between-Subject Effects</title>
  <link>https://blog.msbstats.info/posts/2019-10-21-accounting-for-within-and-between-subject-effect/</link>
  <description><![CDATA[ 





<p>This is a comment on Solomon Kurz’s (<a href="https://twitter.com/SolomonKurz">@SolomonKurz</a>) <a href="https://solomonkurz.netlify.com/post/individuals-are-not-small-groups-ii-the-ecological-fallacy/">recent post</a><sup>1</sup> where he discusses how group-level data does not always reflect individual-level processes. I highly recommend reading his series of posts on the topic!</p>
<p>In this post I will demonstrate:</p>
<ol type="1">
<li>How to model <strong>both</strong> group-level processes and individual-level processes from individual-level data, using linear-mixed models.</li>
<li>When you shouldn’t worry about group-level processes differing from individual-level processes. (controlled experiments for the win!)</li>
</ol>
<section id="modeling-individual--and-group-level-effects" class="level2">
<h2 class="anchored" data-anchor-id="modeling-individual--and-group-level-effects">Modeling individual- <em>and</em> group-level effects</h2>
<p>Let’s work with the now-classic typing speed example. We take a group of 5 typists, and measure the speed of their typing (words per minute), and the rate of typing errors (errors per 100-words). Looking at the data we might get something like this:</p>
<div class="cell">
<div class="cell-output cell-output-stderr">
<pre><code>`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2019-10-21-accounting-for-within-and-between-subject-effect/index_files/figure-html/plot_data-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>As we can see, we have two sources of variation that can be used to explain or predict the rate of errors:</p>
<ol type="1">
<li>Overall, faster typists make less mistakes (group-level pattern).<br>
</li>
<li>When typing faster, typists make more mistakes (individual-level pattern).</li>
</ol>
<p>We can model these using liner mixed models, but first we need to <em>split</em> our predictor (<em>speed</em>) into two variables, each representing a different source of variance - each typist’s average typing speed, and the deviation of each measurement from the typist’s overall mean:<sup>2</sup></p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb2-2">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(ID) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">speed_M =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(speed),</span>
<span id="cb2-5">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">speed_E =</span> speed <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> speed_M) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>()</span>
<span id="cb2-7"></span>
<span id="cb2-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(data)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 × 5
     ID  speed errors speed_M speed_E
  &lt;int&gt;  &lt;dbl&gt;  &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;
1     1 -0.773 -1.74   -0.188 -0.585 
2     1 -0.144 -0.703  -0.188  0.0438
3     1 -0.686 -1.73   -0.188 -0.498 
4     1  0.560  1.17   -0.188  0.748 
5     1  0.214  0.316  -0.188  0.402 
6     1  0.179  0.392  -0.188  0.367 </code></pre>
</div>
</div>
<p>Let’s fit a liner mixed model and see how we can detect both patterns correctly.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lmerTest)</span>
<span id="cb4-2"></span>
<span id="cb4-3">fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lmer</span>(errors <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> speed_M <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> speed_E <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> speed_E <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> ID), </span>
<span id="cb4-4">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data)</span></code></pre></div>
</div>
<div class="cell">
<div class="cell-output cell-output-stderr">
<pre><code>Your model may suffer from singularity (see see `?lme4::isSingular` and
  `?performance::check_singularity`).
  Some of the standard errors and confidence intervals of the random
  effects parameters are probably not meaningful!</code></pre>
</div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<colgroup>
<col style="width: 22%">
<col style="width: 11%">
<col style="width: 10%">
<col style="width: 4%">
<col style="width: 6%">
<col style="width: 7%">
<col style="width: 6%">
<col style="width: 8%">
<col style="width: 5%">
<col style="width: 7%">
<col style="width: 8%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Parameter</th>
<th style="text-align: right;">Coefficient</th>
<th style="text-align: right;">SE</th>
<th style="text-align: right;">CI</th>
<th style="text-align: right;">CI_low</th>
<th style="text-align: right;">CI_high</th>
<th style="text-align: right;">t</th>
<th style="text-align: right;">df_error</th>
<th style="text-align: right;">p</th>
<th style="text-align: left;">Effects</th>
<th style="text-align: left;">Group</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">(Intercept)</td>
<td style="text-align: right;">0.000</td>
<td style="text-align: right;">0.620</td>
<td style="text-align: right;">0.95</td>
<td style="text-align: right;">-1.225</td>
<td style="text-align: right;">1.225</td>
<td style="text-align: right;">0.000</td>
<td style="text-align: right;">143</td>
<td style="text-align: right;">1.000</td>
<td style="text-align: left;">fixed</td>
<td style="text-align: left;"></td>
</tr>
<tr class="even">
<td style="text-align: left;">speed_M</td>
<td style="text-align: right;">-1.600</td>
<td style="text-align: right;">0.693</td>
<td style="text-align: right;">0.95</td>
<td style="text-align: right;">-2.969</td>
<td style="text-align: right;">-0.231</td>
<td style="text-align: right;">-2.309</td>
<td style="text-align: right;">143</td>
<td style="text-align: right;">0.022</td>
<td style="text-align: left;">fixed</td>
<td style="text-align: left;"></td>
</tr>
<tr class="odd">
<td style="text-align: left;">speed_E</td>
<td style="text-align: right;">1.400</td>
<td style="text-align: right;">0.119</td>
<td style="text-align: right;">0.95</td>
<td style="text-align: right;">1.165</td>
<td style="text-align: right;">1.635</td>
<td style="text-align: right;">11.762</td>
<td style="text-align: right;">143</td>
<td style="text-align: right;">0.000</td>
<td style="text-align: left;">fixed</td>
<td style="text-align: left;"></td>
</tr>
<tr class="even">
<td style="text-align: left;">SD (Intercept)</td>
<td style="text-align: right;">1.379</td>
<td style="text-align: right;">0.568</td>
<td style="text-align: right;">0.95</td>
<td style="text-align: right;">0.615</td>
<td style="text-align: right;">3.093</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">NA</td>
<td style="text-align: left;">random</td>
<td style="text-align: left;">ID</td>
</tr>
<tr class="odd">
<td style="text-align: left;">SD (speed_E)</td>
<td style="text-align: right;">0.000</td>
<td style="text-align: right;">49027.294</td>
<td style="text-align: right;">0.95</td>
<td style="text-align: right;">0.000</td>
<td style="text-align: right;">Inf</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">NA</td>
<td style="text-align: left;">random</td>
<td style="text-align: left;">ID</td>
</tr>
<tr class="even">
<td style="text-align: left;">Cor (Intercept~speed_E)</td>
<td style="text-align: right;">1.000</td>
<td style="text-align: right;">297941.433</td>
<td style="text-align: right;">0.95</td>
<td style="text-align: right;">NaN</td>
<td style="text-align: right;">1.000</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">NA</td>
<td style="text-align: left;">random</td>
<td style="text-align: left;">ID</td>
</tr>
<tr class="odd">
<td style="text-align: left;">SD (Observations)</td>
<td style="text-align: right;">0.717</td>
<td style="text-align: right;">0.043</td>
<td style="text-align: right;">0.95</td>
<td style="text-align: right;">0.637</td>
<td style="text-align: right;">0.806</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">NA</td>
<td style="text-align: left;">random</td>
<td style="text-align: left;">Residual</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>As we can see, the slope for <code>speed_M</code> is negative (-1.6), reflecting the group-level pattern where typists who are overall faster have fewer errors; whereas the slope for <code>speed_E</code> is positive (1.4), reflecting the individual-level pattern where faster typing leads to more errors.</p>
</section>
<section id="when-are-individual-level-the-same-as-group-level-patterns" class="level2">
<h2 class="anchored" data-anchor-id="when-are-individual-level-the-same-as-group-level-patterns">When are individual-level the same as group-level patterns?</h2>
<p>Experiments!<br>
Or to be more precise, when <em>we</em> control the values of the independent variable. Why is this so? Because we control the values of the independent variable, the independent variable cannot be split into different sources of variance: there is either variance between subjects (the variable is manipulated in a between-subjects design) or there is variance within subjects (the variable is manipulated in a within-subjects design), but never both. Thus, although there can be huge heterogeneity in the way subjects present an effect, the average individual-level effect will be the same as the group-level effect (depending on the design).<sup>3</sup></p>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>and <a href="https://twitter.com/mattansb/status/1184707867913400322">the short twitter discussion</a> that followed.↩︎</p></li>
<li id="fn2"><p>Read more in: Hoffman, L. (2015). Time-varying predictors in models of within-person fluctuation. In <em>Longitudinal analysis: Modeling within-person fluctuation and change</em> (pp.&nbsp;327-392). Routledge.↩︎</p></li>
<li id="fn3"><p>Ignoring any differences or artifacts that may arise from the differences in the design itself, such as order effects, etc.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>statistics</category>
  <category>mixed-design</category>
  <category>code</category>
  <category>linear models</category>
  <guid>https://blog.msbstats.info/posts/2019-10-21-accounting-for-within-and-between-subject-effect/</guid>
  <pubDate>Sun, 20 Oct 2019 21:00:00 GMT</pubDate>
</item>
<item>
  <title>(Bootstrapping) Follow-Up Contrasts for Within-Subject ANOVAs (part 2)</title>
  <link>https://blog.msbstats.info/posts/2019-08-14-bootstraping-rm-contrasts2/</link>
  <description><![CDATA[ 





<p>A while back I wrote a post demonstrating how to <a href="../..\posts/2019-05-09-bootstraping-rm-contrasts/"><em>bootstrap</em> follow-up contrasts for repeated-measure ANOVAs</a> for cases where you data violates some / any <a href="../..\posts/2018-08-30-linear-regression-assumptions/">assumptions</a>. Here is a demo of how to conduct the same bootstrap analysis, more simply (no need to make your data wide!)</p>
<section id="fit-your-repeated-measures-model-with-lmer" class="level2">
<h2 class="anchored" data-anchor-id="fit-your-repeated-measures-model-with-lmer">1. Fit your repeated-measures model with <code>lmer</code></h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lme4)</span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(obk.long, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">package =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"afex"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># data from the afex package</span></span>
<span id="cb1-4"></span>
<span id="cb1-5">fit_mixed <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lmer</span>(value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> treatment <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> gender <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> phase <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> hour <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>id),</span>
<span id="cb1-6">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> obk.long)</span></code></pre></div>
</div>
<p>Note that I assume here data is aggregated (one value per cell/subject), as it would be in a rmANOVA, as so it is sufficient to model only a random intercept.</p>
</section>
<section id="define-the-contrasts-of-interest" class="level2">
<h2 class="anchored" data-anchor-id="define-the-contrasts-of-interest">2. Define the contrast(s) of interest</h2>
<p>For this step we will be using <code>emmeans</code> to get the estimates of the pairwise differences between the treatment groups within each phase of the study:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(emmeans)</span>
<span id="cb2-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># get the correct reference  grid with the correct ultivariate levels!</span></span>
<span id="cb2-3">rg <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ref_grid</span>(fit_mixed, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mult.levs =</span> rm_levels)</span>
<span id="cb2-4">rg</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>'emmGrid' object with variables:
    treatment = control, A, B
    gender = F, M
    phase = fup, post, pre
    hour = 1, 2, 3, 4, 5</code></pre>
</div>
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># get the expected means:</span></span>
<span id="cb4-2">em_ <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emmeans</span>(rg, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> phase <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> treatment)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>NOTE: Results may be misleading due to involvement in interactions</code></pre>
</div>
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">em_</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> phase treatment emmean    SE   df lower.CL upper.CL
 fup   control     4.33 0.603 13.2     3.03     5.64
 post  control     4.08 0.603 13.2     2.78     5.39
 pre   control     4.25 0.603 13.2     2.95     5.55
 fup   A           7.25 0.661 13.2     5.82     8.68
 post  A           6.50 0.661 13.2     5.07     7.93
 pre   A           5.00 0.661 13.2     3.57     6.43
 fup   B           7.29 0.505 13.2     6.20     8.38
 post  B           6.62 0.505 13.2     5.54     7.71
 pre   B           4.17 0.505 13.2     3.08     5.26

Results are averaged over the levels of: gender, hour 
Degrees-of-freedom method: kenward-roger 
Confidence level used: 0.95 </code></pre>
</div>
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># run pairwise tests between the treatment groups within each phase</span></span>
<span id="cb8-2">c_ <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrast</span>(em_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pairwise"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'phase'</span>)</span>
<span id="cb8-3">c_</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>phase = fup:
 contrast    estimate    SE   df t.ratio p.value
 control - A  -2.9167 0.895 13.2  -3.259  0.0157
 control - B  -2.9583 0.787 13.2  -3.760  0.0061
 A - B        -0.0417 0.832 13.2  -0.050  0.9986

phase = post:
 contrast    estimate    SE   df t.ratio p.value
 control - A  -2.4167 0.895 13.2  -2.700  0.0445
 control - B  -2.5417 0.787 13.2  -3.230  0.0166
 A - B        -0.1250 0.832 13.2  -0.150  0.9876

phase = pre:
 contrast    estimate    SE   df t.ratio p.value
 control - A  -0.7500 0.895 13.2  -0.838  0.6869
 control - B   0.0833 0.787 13.2   0.106  0.9938
 A - B         0.8333 0.832 13.2   1.002  0.5885

Results are averaged over the levels of: gender, hour 
Degrees-of-freedom method: kenward-roger 
P value adjustment: tukey method for comparing a family of 3 estimates </code></pre>
</div>
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># extract the estimates</span></span>
<span id="cb10-2">est_names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fup:  control - A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fup:  control - B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fup:  A - B"</span>,</span>
<span id="cb10-3">               <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"post: control - A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"post: control - B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"post: A - B"</span>,</span>
<span id="cb10-4">               <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pre:  control - A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pre:  control - B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pre:  A - B"</span>)</span>
<span id="cb10-5">est_values <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(c_)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estimate</span>
<span id="cb10-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(est_values) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> est_names</span>
<span id="cb10-7">est_values</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>fup:  control - A fup:  control - B       fup:  A - B post: control - A 
      -2.91666667       -2.95833333       -0.04166667       -2.41666667 
post: control - B       post: A - B pre:  control - A pre:  control - B 
      -2.54166667       -0.12500000       -0.75000000        0.08333333 
      pre:  A - B 
       0.83333333 </code></pre>
</div>
</div>
</section>
<section id="run-the-bootstrap" class="level2">
<h2 class="anchored" data-anchor-id="run-the-bootstrap">3. Run the bootstrap</h2>
<p>Now let’s wrap this all in a function that accepts the fitted model as an argument:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">treatment_phase_contrasts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(mod){</span>
<span id="cb12-2">  rg <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ref_grid</span>(mod, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mult.levs =</span> rm_levels)</span>
<span id="cb12-3">  </span>
<span id="cb12-4">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># get the expected means:</span></span>
<span id="cb12-5">  em_ <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emmeans</span>(rg, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> phase <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> treatment)</span>
<span id="cb12-6">  </span>
<span id="cb12-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># run pairwise tests between the treatment groups within each phase</span></span>
<span id="cb12-8">  c_ <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrast</span>(em_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pairwise"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'phase'</span>)</span>
<span id="cb12-9">  </span>
<span id="cb12-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># extract the estimates</span></span>
<span id="cb12-11">  est_names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fup:  control - A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fup:  control - B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fup:  A - B"</span>,</span>
<span id="cb12-12">                 <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"post: control - A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"post: control - B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"post: A - B"</span>,</span>
<span id="cb12-13">                 <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pre:  control - A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pre:  control - B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pre:  A - B"</span>)</span>
<span id="cb12-14">  est_values <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(c_)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estimate</span>
<span id="cb12-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(est_values) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> est_names</span>
<span id="cb12-16">  est_values</span>
<span id="cb12-17">}</span>
<span id="cb12-18"></span>
<span id="cb12-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># test it</span></span>
<span id="cb12-20"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">treatment_phase_contrasts</span>(fit_mixed)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>NOTE: Results may be misleading due to involvement in interactions</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>fup:  control - A fup:  control - B       fup:  A - B post: control - A 
      -2.91666667       -2.95833333       -0.04166667       -2.41666667 
post: control - B       post: A - B pre:  control - A pre:  control - B 
      -2.54166667       -0.12500000       -0.75000000        0.08333333 
      pre:  A - B 
       0.83333333 </code></pre>
</div>
</div>
<p>Finally, we will use <code>lme4::bootMer</code> to get the bootstrapped estimates!</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">treatment_phase_results <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb15-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bootMer</span>(fit_mixed, treatment_phase_contrasts, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nsim =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># R = 599 at least</span></span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>NOTE: Results may be misleading due to involvement in interactions</code></pre>
</div>
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(treatment_phase_results) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># original vs. bootstrapped estimate (bootMed)</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Number of bootstrap replications R = 50 
                   original  bootBias  bootSE   bootMed
fup:  control - A -2.916667  0.017263 0.77841 -2.801902
fup:  control - B -2.958333 -0.017880 0.86119 -3.025705
fup:  A - B       -0.041667 -0.035143 0.98850 -0.066474
post: control - A -2.416667  0.031072 0.82654 -2.383370
post: control - B -2.541667 -0.024860 0.82351 -2.520263
post: A - B       -0.125000 -0.055932 1.03670 -0.216929
pre:  control - A -0.750000 -0.065397 0.73276 -0.851533
pre:  control - B  0.083333  0.024664 0.78592  0.111930
pre:  A - B        0.833333  0.090061 0.95015  0.994195</code></pre>
</div>
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">confint</span>(treatment_phase_results, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"perc"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># does include zero?</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>                      2.5 %     97.5 %
fup:  control - A -5.062951 -1.2782764
fup:  control - B -4.985715 -1.0325666
fup:  A - B       -2.348035  2.1660820
post: control - A -4.451445 -0.5162071
post: control - B -4.840519 -1.1705024
post: A - B       -2.349137  2.3025369
pre:  control - A -2.427992  0.8830127
pre:  control - B -1.915388  1.7159931
pre:  A - B       -1.530049  2.7527436</code></pre>
</div>
</div>
<p>Results indicate that the Control group is lower than both treatment groups in the post and fup (follow -up) phases.</p>
<p>If we wanted p-values, we could use this little function (based on <a href="https://blogs.sas.com/content/iml/2011/11/02/how-to-compute-p-values-for-a-bootstrap-distribution.html">this demo</a>):</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1">boot_pvalues <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">side =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) {</span>
<span id="cb21-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Based on:</span></span>
<span id="cb21-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># https://blogs.sas.com/content/iml/2011/11/02/how-to-compute-p-values-for-a-bootstrap-distribution.html</span></span>
<span id="cb21-4">  side <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> side[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb21-5">  x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>t)</span>
<span id="cb21-6"></span>
<span id="cb21-7">  ps <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(x, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(.x) {</span>
<span id="cb21-8">    s <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">na.omit</span>(.x)</span>
<span id="cb21-9">    s0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb21-10">    N <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(s)</span>
<span id="cb21-11"></span>
<span id="cb21-12">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (side <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) {</span>
<span id="cb21-13">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>((<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(s <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> s0)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (N <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb21-14">          (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(s <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> s0)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (N <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb21-15">    } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (side <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) {</span>
<span id="cb21-16">      (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(s <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> s0)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (N <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb21-17">    } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (side <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) {</span>
<span id="cb21-18">      (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(s <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> s0)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (N <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb21-19">    }</span>
<span id="cb21-20">  })</span>
<span id="cb21-21">  </span>
<span id="cb21-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setNames</span>(ps,<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(x))</span>
<span id="cb21-23">}</span>
<span id="cb21-24"></span>
<span id="cb21-25"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">boot_pvalues</span>(treatment_phase_results)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>fup:  control - A fup:  control - B       fup:  A - B post: control - A 
       0.03921569        0.03921569        0.94117647        0.03921569 
post: control - B       post: A - B pre:  control - A pre:  control - B 
       0.03921569        0.74509804        0.23529412        0.94117647 
      pre:  A - B 
       0.27450980 </code></pre>
</div>
</div>
<p>These p-values can then be passed to <code>p.adjust()</code> for the p-value adjustment method of your choosing.</p>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>I’ve demonstrated (again!) how to run permutation tests on main effects / interactions, with follow-up analysis using the bootstrap method. Using this code as a basis for any analysis you might have in mind gives you all the flexibility of <code>emmeans</code>, which supports many (many) models!</p>
<center>
<img src="https://blog.msbstats.info/posts/2019-08-14-bootstraping-rm-contrasts2/boot.png" class="img-fluid" style="width:50.0%">
</center>


</section>

 ]]></description>
  <category>R</category>
  <category>statistics</category>
  <category>ANOVA</category>
  <category>bootstrap</category>
  <category>code</category>
  <category>mixed-design</category>
  <category>permutation</category>
  <guid>https://blog.msbstats.info/posts/2019-08-14-bootstraping-rm-contrasts2/</guid>
  <pubDate>Tue, 13 Aug 2019 21:00:00 GMT</pubDate>
</item>
<item>
  <title>Signal Detection Theory vs. Logistic Regression</title>
  <link>https://blog.msbstats.info/posts/2019-07-29-SDT-logistic-regression/</link>
  <description><![CDATA[ 





<p>I recently came across a paper that explained the equality between the parameters of signal detection theory (SDT) and the parameters of logistic regression in which the state (“absent”/“present”) is used to predict the response (“yes”/“no”, but also applicable in scale-rating designs) (<a href="http://dx.doi.org/10.1037/1082-989X.3.2.186">DeCarlo, 1998</a>).</p>
<p>Here is a short simulation-proof for this equality.</p>
<section id="setup" class="level2">
<h2 class="anchored" data-anchor-id="setup">Setup</h2>
<p>For this simulations we will need the following packages:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For plotting</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For extracting SDT parameters</span></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(neuropsychology)</span></code></pre></div>
</div>
<p>We will also need to make sure, for the logistic regression analysis, that our factors’ dummy coding is set to effects-coding - otherwise the intercept’s meaning will not correspond to the criterion (aka the <em>overall</em> response bias):</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">options</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">contrasts =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'contr.sum'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'contr.poly'</span>))</span></code></pre></div>
</div>
</section>
<section id="the-simulations" class="level2">
<h2 class="anchored" data-anchor-id="the-simulations">The Simulations</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>L</span>
<span id="cb3-2">B <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>L</span></code></pre></div>
</div>
<p>We’ll run 100 simulations with 100 trials each.</p>
<section id="simulation-code" class="level3">
<h3 class="anchored" data-anchor-id="simulation-code">Simulation Code</h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb4-2"></span>
<span id="cb4-3">SDT_params <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(state,resp) {</span>
<span id="cb4-4">  tab <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(state,resp)</span>
<span id="cb4-5">  </span>
<span id="cb4-6">  sdt_res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dprime</span>(</span>
<span id="cb4-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_hit  =</span> tab[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],</span>
<span id="cb4-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_miss =</span> tab[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb4-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_fa   =</span> tab[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],</span>
<span id="cb4-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_cr   =</span> tab[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb4-11">  )</span>
<span id="cb4-12">  </span>
<span id="cb4-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(sdt_res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>dprime , sdt_res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>c)</span>
<span id="cb4-14">}</span>
<span id="cb4-15"></span>
<span id="cb4-16">logistic_reg_params <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(state,resp){</span>
<span id="cb4-17">  fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glm</span>(resp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> state, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">binomial</span>())</span>
<span id="cb4-18">  </span>
<span id="cb4-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(fit)</span>
<span id="cb4-20">}</span>
<span id="cb4-21"></span>
<span id="cb4-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># initialize</span></span>
<span id="cb4-23">res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">d_    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(B),</span>
<span id="cb4-24">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">c_    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(B),</span>
<span id="cb4-25">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">int   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(B),</span>
<span id="cb4-26">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">slope =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(B))</span>
<span id="cb4-27"></span>
<span id="cb4-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Loop</span></span>
<span id="cb4-29"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (b <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(B)) {</span>
<span id="cb4-30">  true_sensitivity <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rexp</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># random</span></span>
<span id="cb4-31">  true_criterion <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># random</span></span>
<span id="cb4-32">  </span>
<span id="cb4-33">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># true state vector</span></span>
<span id="cb4-34">  state_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(F,T), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> n<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb4-35">  </span>
<span id="cb4-36">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># response vector</span></span>
<span id="cb4-37">  Xn <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(n<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># noise dist</span></span>
<span id="cb4-38">  Xs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(n<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> true_sensitivity) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># signal + noise dist</span></span>
<span id="cb4-39">  X <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(Xn,Xs)</span>
<span id="cb4-40">  resp_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> true_criterion</span>
<span id="cb4-41">  </span>
<span id="cb4-42">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># SDT params</span></span>
<span id="cb4-43">  res[b,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">SDT_params</span>(state_i,resp_i)</span>
<span id="cb4-44">  </span>
<span id="cb4-45">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># logistic regression params</span></span>
<span id="cb4-46">  res[b,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">logistic_reg_params</span>(state_i,resp_i)</span>
<span id="cb4-47">}</span></code></pre></div>
</div>
</section>
<section id="results" class="level3">
<h3 class="anchored" data-anchor-id="results">Results</h3>
<p>SDT parameters are on a standardized normal scale, meaning they are scaled to <img src="https://latex.codecogs.com/png.latex?%5Csigma=1">. However, the logistic distribution’s scale is <img src="https://latex.codecogs.com/png.latex?%5Csigma=%5Cpi/%5Csqrt3">. Thus, to convert the logistic regression’s parameters to the SDT’s we need to scale both the <em>intercept</em> and the <em>slope</em> by <img src="https://latex.codecogs.com/png.latex?%5Csqrt3/%5Cpi"> to have them on the same scale as <img src="https://latex.codecogs.com/png.latex?c"> and <img src="https://latex.codecogs.com/png.latex?d'">.<sup>1</sup> Additionally,</p>
<ol type="1">
<li>The slope must be also scaled by <img src="https://latex.codecogs.com/png.latex?-2"> due to R’s default effects coding.<br>
</li>
<li>The intercept must also be scaled by <img src="https://latex.codecogs.com/png.latex?-1"> - see paper for the full rationale.</li>
</ol>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2019-07-29-SDT-logistic-regression/index_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The red-dashed line represents the expected regression line predicting the SDT parameters from their logistic counterparts:</p>
<ul>
<li><img src="https://latex.codecogs.com/png.latex?d'%20=%20-2%5Ctimes%5Cfrac%7B%5Csqrt%7B3%7D%7D%7B%5Cpi%7D%5Ctimes%20Slope"><br>
</li>
<li><img src="https://latex.codecogs.com/png.latex?c%20=%20-%5Cfrac%7B%5Csqrt%7B3%7D%7D%7B%5Cpi%7D%5Ctimes%20Intercept"></li>
</ul>
<p>(The blue line is the empirical regression line.)</p>
</section>
</section>
<section id="conclusions" class="level2">
<h2 class="anchored" data-anchor-id="conclusions">Conclusions</h2>
<p>It is also possible to extend this equality to multi-level designs with generalized linear mixed models (GLMM; see chapters 3 and 9 in <em><a href="http://doi.org/10.1007/978-1-4614-4475-6">Modeling Psychophysical Data in R</a></em>)<sup>2</sup>, but I see no reason this wouldn’t be possible… One could model random effects per subject, and the moderating effect of some <img src="https://latex.codecogs.com/png.latex?X"> on sensitivity could in theory be modeled by including an interaction between <img src="https://latex.codecogs.com/png.latex?X"> and <em>state</em>; similarly, the moderating effect of <img src="https://latex.codecogs.com/png.latex?X"> on the criterion can be modeled by including a main effect for <img src="https://latex.codecogs.com/png.latex?X"> (moderation the intercept).</p>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>Note that there is no need for this scaling if the <code>probit</code> link is used instead of the <code>logit</code> function. This demo has focused on the <code>logit</code> link only because it is the more popular option (insert “old man yells at cloud”-gif here).↩︎</p></li>
<li id="fn2"><p>Thanks to Kenneth Knoblauch for reaching out!↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>R</category>
  <category>statistics</category>
  <category>code</category>
  <category>logistic regression</category>
  <category>signal detection theory</category>
  <guid>https://blog.msbstats.info/posts/2019-07-29-SDT-logistic-regression/</guid>
  <pubDate>Sun, 28 Jul 2019 21:00:00 GMT</pubDate>
</item>
<item>
  <title>(Bootstrapping) Follow-Up Contrasts for Within-Subject ANOVAs</title>
  <link>https://blog.msbstats.info/posts/2019-05-09-bootstraping-rm-contrasts/</link>
  <description><![CDATA[ 





<p>So you’ve run a repeated-measures ANOVA and found that your residuals are neither normally distributed, nor homogeneous, or that you are in violation of any other <a href="../..\posts/2018-08-30-linear-regression-assumptions/">assumptions</a>. Naturally you want to run some a-parametric analysis… but how?</p>
<hr>
<p>In this post I will demonstrate how to run a permutation test ANOVA (easy!) and how to run bootstrap follow-up analysis (a bit more challenging) in a mixed design (both within- and between-subject factors) ANOVA. I’ve chosen to use a mixed design model in this demonstration for two reasons:</p>
<ol type="1">
<li>I’ve never seen this done before.</li>
<li>You can easily modify this code (change / skip some of these steps) to accommodate purely within- or purely between-subject designs.</li>
</ol>
<section id="permutation-anova" class="level2">
<h2 class="anchored" data-anchor-id="permutation-anova">Permutation ANOVA</h2>
<p>Running a permutation test for your ANOVA in R is as easy as… running an ANOVA in R, but substituting <code>aov</code> with <code>aovperm</code> from the <a href="https://cran.r-project.org/package=permuco"><code>permuco</code> package</a>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(permuco)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(obk.long, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">package =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"afex"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># data from the afex package </span></span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># permutation anova</span></span>
<span id="cb1-5">fit_mixed_p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb1-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aovperm</span>(value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> treatment <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> gender <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> phase <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> hour <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Error</span>(id <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (phase <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> hour)),</span>
<span id="cb1-7">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> obk.long)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning in checkBalancedData(fixed_formula = formula_f, data = cbind(y, : The
data are not balanced, the results may not be exact.</code></pre>
</div>
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">fit_mixed_p</span></code></pre></div>
</div>
<div class="cell">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<colgroup>
<col style="width: 21%">
<col style="width: 21%">
<col style="width: 5%">
<col style="width: 3%">
<col style="width: 5%">
<col style="width: 3%">
<col style="width: 4%">
<col style="width: 4%">
<col style="width: 4%">
<col style="width: 13%">
<col style="width: 12%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;"></th>
<th style="text-align: left;">term</th>
<th style="text-align: right;">SSn</th>
<th style="text-align: right;">dfn</th>
<th style="text-align: right;">SSd</th>
<th style="text-align: right;">dfd</th>
<th style="text-align: right;">MSEn</th>
<th style="text-align: right;">MSEd</th>
<th style="text-align: right;">F</th>
<th style="text-align: left;">parametric P(&gt;F)</th>
<th style="text-align: left;">resampled P(&gt;F)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">treatment</td>
<td style="text-align: left;">treatment</td>
<td style="text-align: right;">179.73</td>
<td style="text-align: right;">2</td>
<td style="text-align: right;">228.06</td>
<td style="text-align: right;">10</td>
<td style="text-align: right;">89.87</td>
<td style="text-align: right;">22.81</td>
<td style="text-align: right;">3.94</td>
<td style="text-align: left;">0.055</td>
<td style="text-align: left;">0.055</td>
</tr>
<tr class="even">
<td style="text-align: left;">gender</td>
<td style="text-align: left;">gender</td>
<td style="text-align: right;">83.45</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">228.06</td>
<td style="text-align: right;">10</td>
<td style="text-align: right;">83.45</td>
<td style="text-align: right;">22.81</td>
<td style="text-align: right;">3.66</td>
<td style="text-align: left;">0.085</td>
<td style="text-align: left;">0.082</td>
</tr>
<tr class="odd">
<td style="text-align: left;">treatment:gender</td>
<td style="text-align: left;">treatment:gender</td>
<td style="text-align: right;">130.24</td>
<td style="text-align: right;">2</td>
<td style="text-align: right;">228.06</td>
<td style="text-align: right;">10</td>
<td style="text-align: right;">65.12</td>
<td style="text-align: right;">22.81</td>
<td style="text-align: right;">2.86</td>
<td style="text-align: left;">0.104</td>
<td style="text-align: left;">0.104</td>
</tr>
<tr class="even">
<td style="text-align: left;">phase</td>
<td style="text-align: left;">phase</td>
<td style="text-align: right;">129.51</td>
<td style="text-align: right;">2</td>
<td style="text-align: right;">80.28</td>
<td style="text-align: right;">20</td>
<td style="text-align: right;">64.76</td>
<td style="text-align: right;">4.01</td>
<td style="text-align: right;">16.13</td>
<td style="text-align: left;">&lt;0.001</td>
<td style="text-align: left;">&lt;0.001</td>
</tr>
<tr class="odd">
<td style="text-align: left;">treatment:phase</td>
<td style="text-align: left;">treatment:phase</td>
<td style="text-align: right;">77.89</td>
<td style="text-align: right;">4</td>
<td style="text-align: right;">80.28</td>
<td style="text-align: right;">20</td>
<td style="text-align: right;">19.47</td>
<td style="text-align: right;">4.01</td>
<td style="text-align: right;">4.85</td>
<td style="text-align: left;">0.007</td>
<td style="text-align: left;">0.009</td>
</tr>
<tr class="even">
<td style="text-align: left;">gender:phase</td>
<td style="text-align: left;">gender:phase</td>
<td style="text-align: right;">2.27</td>
<td style="text-align: right;">2</td>
<td style="text-align: right;">80.28</td>
<td style="text-align: right;">20</td>
<td style="text-align: right;">1.14</td>
<td style="text-align: right;">4.01</td>
<td style="text-align: right;">0.28</td>
<td style="text-align: left;">0.757</td>
<td style="text-align: left;">0.765</td>
</tr>
<tr class="odd">
<td style="text-align: left;">treatment:gender:phase</td>
<td style="text-align: left;">treatment:gender:phase</td>
<td style="text-align: right;">10.22</td>
<td style="text-align: right;">4</td>
<td style="text-align: right;">80.28</td>
<td style="text-align: right;">20</td>
<td style="text-align: right;">2.56</td>
<td style="text-align: right;">4.01</td>
<td style="text-align: right;">0.64</td>
<td style="text-align: left;">0.642</td>
<td style="text-align: left;">0.641</td>
</tr>
<tr class="even">
<td style="text-align: left;">hour</td>
<td style="text-align: left;">hour</td>
<td style="text-align: right;">104.29</td>
<td style="text-align: right;">4</td>
<td style="text-align: right;">62.50</td>
<td style="text-align: right;">40</td>
<td style="text-align: right;">26.07</td>
<td style="text-align: right;">1.56</td>
<td style="text-align: right;">16.69</td>
<td style="text-align: left;">&lt;0.001</td>
<td style="text-align: left;">&lt;0.001</td>
</tr>
<tr class="odd">
<td style="text-align: left;">treatment:hour</td>
<td style="text-align: left;">treatment:hour</td>
<td style="text-align: right;">1.17</td>
<td style="text-align: right;">8</td>
<td style="text-align: right;">62.50</td>
<td style="text-align: right;">40</td>
<td style="text-align: right;">0.15</td>
<td style="text-align: right;">1.56</td>
<td style="text-align: right;">0.09</td>
<td style="text-align: left;">&gt;0.999</td>
<td style="text-align: left;">&gt;0.999</td>
</tr>
<tr class="even">
<td style="text-align: left;">gender:hour</td>
<td style="text-align: left;">gender:hour</td>
<td style="text-align: right;">2.81</td>
<td style="text-align: right;">4</td>
<td style="text-align: right;">62.50</td>
<td style="text-align: right;">40</td>
<td style="text-align: right;">0.70</td>
<td style="text-align: right;">1.56</td>
<td style="text-align: right;">0.45</td>
<td style="text-align: left;">0.772</td>
<td style="text-align: left;">0.772</td>
</tr>
<tr class="odd">
<td style="text-align: left;">treatment:gender:hour</td>
<td style="text-align: left;">treatment:gender:hour</td>
<td style="text-align: right;">7.76</td>
<td style="text-align: right;">8</td>
<td style="text-align: right;">62.50</td>
<td style="text-align: right;">40</td>
<td style="text-align: right;">0.97</td>
<td style="text-align: right;">1.56</td>
<td style="text-align: right;">0.62</td>
<td style="text-align: left;">0.755</td>
<td style="text-align: left;">0.755</td>
</tr>
<tr class="even">
<td style="text-align: left;">phase:hour</td>
<td style="text-align: left;">phase:hour</td>
<td style="text-align: right;">11.35</td>
<td style="text-align: right;">8</td>
<td style="text-align: right;">96.17</td>
<td style="text-align: right;">80</td>
<td style="text-align: right;">1.42</td>
<td style="text-align: right;">1.20</td>
<td style="text-align: right;">1.18</td>
<td style="text-align: left;">0.322</td>
<td style="text-align: left;">0.319</td>
</tr>
<tr class="odd">
<td style="text-align: left;">treatment:phase:hour</td>
<td style="text-align: left;">treatment:phase:hour</td>
<td style="text-align: right;">6.64</td>
<td style="text-align: right;">16</td>
<td style="text-align: right;">96.17</td>
<td style="text-align: right;">80</td>
<td style="text-align: right;">0.42</td>
<td style="text-align: right;">1.20</td>
<td style="text-align: right;">0.35</td>
<td style="text-align: left;">0.990</td>
<td style="text-align: left;">0.990</td>
</tr>
<tr class="even">
<td style="text-align: left;">gender:phase:hour</td>
<td style="text-align: left;">gender:phase:hour</td>
<td style="text-align: right;">8.96</td>
<td style="text-align: right;">8</td>
<td style="text-align: right;">96.17</td>
<td style="text-align: right;">80</td>
<td style="text-align: right;">1.12</td>
<td style="text-align: right;">1.20</td>
<td style="text-align: right;">0.93</td>
<td style="text-align: left;">0.496</td>
<td style="text-align: left;">0.498</td>
</tr>
<tr class="odd">
<td style="text-align: left;">treatment:gender:phase:hour</td>
<td style="text-align: left;">treatment:gender:phase:hour</td>
<td style="text-align: right;">14.15</td>
<td style="text-align: right;">16</td>
<td style="text-align: right;">96.17</td>
<td style="text-align: right;">80</td>
<td style="text-align: right;">0.88</td>
<td style="text-align: right;">1.20</td>
<td style="text-align: right;">0.74</td>
<td style="text-align: left;">0.750</td>
<td style="text-align: left;">0.753</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>The results of the permutation test suggest an interaction between Treatment (a between subject factor) and Phase (a within-subject factor). To fully understand this interaction, we would like to conduct some sort of follow-up analysis (planned comparisons or post hoc tests). Usually I would pass the model to <code>emmeans</code> for any follow-ups, but here, due to our assumption violations, we need some sort of bootstrapping method.</p>
</section>
<section id="bootstrapping-with-car-and-emmeans" class="level2">
<h2 class="anchored" data-anchor-id="bootstrapping-with-car-and-emmeans">Bootstrapping with <code>car</code> and <code>emmeans</code></h2>
<p>For bootstrapping we will be using the <code>Boot</code> function from the <code>car</code> package. In the case of within-subject factors, this function requires that they be specified in a multivariate data structure. Let’s see how this is done.</p>
<section id="make-your-data-wiiiiiiiiiideeeeeeee" class="level3">
<h3 class="anchored" data-anchor-id="make-your-data-wiiiiiiiiiideeeeeeee">1. Make your data WIIIIIIIIIIDEEEEEEEE</h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyr)</span>
<span id="cb4-3"></span>
<span id="cb4-4">obk_mixed_wide <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> obk.long <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unite</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cond"</span>, phase, hour) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">spread</span>(cond, value)</span>
<span id="cb4-7"></span>
<span id="cb4-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(obk_mixed_wide)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>  id treatment gender   age fup_1 fup_2 fup_3 fup_4 fup_5 post_1 post_2 post_3
1  1   control      M -4.75     2     3     2     4     4      3      2      5
2  2   control      M -2.75     4     5     6     4     1      2      2      3
3  3   control      M  1.25     7     6     9     7     6      4      5      7
4  4   control      F  7.25     4     4     5     3     4      2      2      3
5  5   control      F -5.75     4     3     6     4     3      6      7      8
6  6         A      M  7.25     9    10    11     9     6      9      9     10
  post_4 post_5 pre_1 pre_2 pre_3 pre_4 pre_5
1      3      2     1     2     4     2     1
2      5      3     4     4     5     3     4
3      5      4     5     6     5     7     7
4      5      3     5     4     7     5     4
5      6      3     3     4     6     4     3
6      8      9     7     8     7     9     9</code></pre>
</div>
</div>
<p>This is not enough, as we <em>also</em> need our new columns (representing the different levels of the within subject factors) to be in a matrix column.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">obk_mixed_matrixDV <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> obk_mixed_wide <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(id, age, treatment, gender)</span>
<span id="cb6-3"></span>
<span id="cb6-4">obk_mixed_matrixDV<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>M <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> obk_mixed_wide <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>id, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>age, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>treatment, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>gender) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>()</span>
<span id="cb6-7"></span>
<span id="cb6-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>(obk_mixed_matrixDV)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Rows: 16
Columns: 5
$ id        &lt;fct&gt; 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
$ age       &lt;dbl&gt; -4.75, -2.75, 1.25, 7.25, -5.75, 7.25, 8.25, 2.25, 2.25, -7.…
$ treatment &lt;fct&gt; control, control, control, control, control, A, A, A, A, B, …
$ gender    &lt;fct&gt; M, M, M, F, F, M, M, F, F, M, M, M, F, F, F, F
$ M         &lt;dbl[,15]&gt; &lt;matrix[16 x 15]&gt;</code></pre>
</div>
</div>
</section>
<section id="fit-your-regular-model" class="level3">
<h3 class="anchored" data-anchor-id="fit-your-regular-model">2. Fit your <em>regular</em> model</h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">fit_mixed <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aov</span>(M <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> treatment <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> gender, obk_mixed_matrixDV)</span></code></pre></div>
</div>
<p>Note that the left-hand-side of the formula (the <code>M</code>) is a matrix representing all the within-subject factors and their levels, and the right-hand-side of the formula (<code>treatment * gender</code>) includes only the between-subject factors.</p>
</section>
<section id="define-the-contrasts-of-interest" class="level3">
<h3 class="anchored" data-anchor-id="define-the-contrasts-of-interest">3. Define the contrast(s) of interest</h3>
<p>For this step we will be using <code>emmeans</code>. But first, we need to create a list of the within-subject factors and their levels (I did say this was difficult - bear with me!). This list needs to correspond to the order of the multi-variate column in our data, such that if there is more than one factor, the combinations of factor levels are used in expand.grid order. In our case:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(obk_mixed_matrixDV<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>M)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] "fup_1"  "fup_2"  "fup_3"  "fup_4"  "fup_5"  "post_1" "post_2" "post_3"
 [9] "post_4" "post_5" "pre_1"  "pre_2"  "pre_3"  "pre_4"  "pre_5" </code></pre>
</div>
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">rm_levels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hour =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"4"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5"</span>),</span>
<span id="cb11-2">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">phase =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fup"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"post"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pre"</span>))</span></code></pre></div>
</div>
<p>Make sure you get the order of the variables and their levels correct! This will affect your results!</p>
<p>Let’s use <code>emmeans</code> to get the estimates of the pairwise differences between the treatment groups within each phase of the study:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(emmeans)</span>
<span id="cb12-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># get the correct reference  grid with the correct ultivariate levels!</span></span>
<span id="cb12-3">rg <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ref_grid</span>(fit_mixed, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mult.levs =</span> rm_levels)</span>
<span id="cb12-4">rg</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>'emmGrid' object with variables:
    treatment = control, A, B
    gender = F, M
    hour = multivariate response levels: 1, 2, 3, 4, 5
    phase = multivariate response levels: fup, post, pre</code></pre>
</div>
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># get the expected means:</span></span>
<span id="cb14-2">em_ <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emmeans</span>(rg, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> phase <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> treatment)</span>
<span id="cb14-3">em_</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> phase treatment emmean    SE df lower.CL upper.CL
 fup   control     4.33 0.551 10     3.11     5.56
 post  control     4.08 0.628 10     2.68     5.48
 pre   control     4.25 0.766 10     2.54     5.96
 fup   A           7.25 0.604 10     5.90     8.60
 post  A           6.50 0.688 10     4.97     8.03
 pre   A           5.00 0.839 10     3.13     6.87
 fup   B           7.29 0.461 10     6.26     8.32
 post  B           6.62 0.525 10     5.45     7.80
 pre   B           4.17 0.641 10     2.74     5.59

Results are averaged over the levels of: gender, hour 
Confidence level used: 0.95 </code></pre>
</div>
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># run pairwise tests between the treatment groups within each phase</span></span>
<span id="cb16-2">c_ <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrast</span>(em_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pairwise"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'phase'</span>)</span>
<span id="cb16-3">c_</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>phase = fup:
 contrast    estimate    SE df t.ratio p.value
 control - A  -2.9167 0.818 10  -3.568  0.0129
 control - B  -2.9583 0.719 10  -4.116  0.0054
 A - B        -0.0417 0.760 10  -0.055  0.9983

phase = post:
 contrast    estimate    SE df t.ratio p.value
 control - A  -2.4167 0.931 10  -2.595  0.0634
 control - B  -2.5417 0.819 10  -3.105  0.0275
 A - B        -0.1250 0.865 10  -0.144  0.9886

phase = pre:
 contrast    estimate    SE df t.ratio p.value
 control - A  -0.7500 1.136 10  -0.660  0.7911
 control - B   0.0833 0.999 10   0.083  0.9962
 A - B         0.8333 1.056 10   0.789  0.7177

Results are averaged over the levels of: gender, hour 
P value adjustment: tukey method for comparing a family of 3 estimates </code></pre>
</div>
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># extract the estimates</span></span>
<span id="cb18-2">est_names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fup:  control - A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fup:  control - B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fup:  A - B"</span>,</span>
<span id="cb18-3">               <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"post: control - A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"post: control - B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"post: A - B"</span>,</span>
<span id="cb18-4">               <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pre:  control - A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pre:  control - B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pre:  A - B"</span>)</span>
<span id="cb18-5">est_values <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(c_)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estimate</span>
<span id="cb18-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(est_values) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> est_names</span>
<span id="cb18-7">est_values</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>fup:  control - A fup:  control - B       fup:  A - B post: control - A 
      -2.91666667       -2.95833333       -0.04166667       -2.41666667 
post: control - B       post: A - B pre:  control - A pre:  control - B 
      -2.54166667       -0.12500000       -0.75000000        0.08333333 
      pre:  A - B 
       0.83333333 </code></pre>
</div>
</div>
</section>
<section id="run-the-bootstrap" class="level3">
<h3 class="anchored" data-anchor-id="run-the-bootstrap">4. Run the bootstrap</h3>
<p>Now let’s wrap this all in a function that accepts the fitted model as an argument:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1">treatment_phase_contrasts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(mod){</span>
<span id="cb20-2">  rg <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ref_grid</span>(mod, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mult.levs =</span> rm_levels)</span>
<span id="cb20-3">  </span>
<span id="cb20-4">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># get the expected means:</span></span>
<span id="cb20-5">  em_ <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emmeans</span>(rg, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> phase <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> treatment)</span>
<span id="cb20-6">  </span>
<span id="cb20-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># run pairwise tests between the treatment groups within each phase</span></span>
<span id="cb20-8">  c_ <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrast</span>(em_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pairwise"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'phase'</span>)</span>
<span id="cb20-9">  </span>
<span id="cb20-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># extract the estimates</span></span>
<span id="cb20-11">  est_names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fup:  control - A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fup:  control - B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fup:  A - B"</span>,</span>
<span id="cb20-12">                 <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"post: control - A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"post: control - B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"post: A - B"</span>,</span>
<span id="cb20-13">                 <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pre:  control - A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pre:  control - B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pre:  A - B"</span>)</span>
<span id="cb20-14">  est_values <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(c_)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estimate</span>
<span id="cb20-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(est_values) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> est_names</span>
<span id="cb20-16">  est_values</span>
<span id="cb20-17">}</span>
<span id="cb20-18"></span>
<span id="cb20-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># test it</span></span>
<span id="cb20-20"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">treatment_phase_contrasts</span>(fit_mixed)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>fup:  control - A fup:  control - B       fup:  A - B post: control - A 
      -2.91666667       -2.95833333       -0.04166667       -2.41666667 
post: control - B       post: A - B pre:  control - A pre:  control - B 
      -2.54166667       -0.12500000       -0.75000000        0.08333333 
      pre:  A - B 
       0.83333333 </code></pre>
</div>
</div>
<p>Finally, we will use <code>car::Boot</code> to get the bootstrapped estimates!</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(car)</span>
<span id="cb22-2">treatment_phase_results <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb22-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Boot</span>(fit_mixed, treatment_phase_contrasts, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">R =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># R = 599 at least</span></span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Loading required namespace: boot</code></pre>
</div>
<div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(treatment_phase_results) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># original vs. bootstrapped estimate (bootMed)</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Number of bootstrap replications R = 31 
                   original  bootBias  bootSE     bootMed
fup:  control - A -2.916667  0.044892 0.65137 -2.8333e+00
fup:  control - B -2.958333 -0.026805 0.82950 -3.0000e+00
fup:  A - B       -0.041667 -0.071697 0.40960 -1.6667e-01
post: control - A -2.416667 -0.011444 0.74882 -2.5000e+00
post: control - B -2.541667  0.048310 0.94075 -2.4167e+00
post: A - B       -0.125000  0.059754 0.64484  4.3374e-15
pre:  control - A -0.750000 -0.129339 0.63190 -7.0000e-01
pre:  control - B  0.083333 -0.099923 1.01857  9.1667e-02
pre:  A - B        0.833333  0.029416 0.89102  8.3333e-01</code></pre>
</div>
<div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">confint</span>(treatment_phase_results, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"perc"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># does include zero?</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Bootstrap percent confidence intervals

                       2.5 %     97.5 %
fup:  control - A -4.0000000 -1.8000000
fup:  control - B -4.3571429 -1.4000000
fup:  A - B       -0.8571429  0.7083333
post: control - A -4.0000000 -1.3000000
post: control - B -4.0000000 -0.7500000
post: A - B       -1.3809524  1.0000000
pre:  control - A -2.0000000  0.7500000
pre:  control - B -2.2500000  2.0416667
pre:  A - B       -0.9666667  2.2083333</code></pre>
</div>
</div>
<p>Results indicate that the Control group is lower than both treatment groups in the post and fup (follow -up) phases.</p>
<p>If we wanted p-values, we could use this little function (based on <a href="https://blogs.sas.com/content/iml/2011/11/02/how-to-compute-p-values-for-a-bootstrap-distribution.html">this demo</a>):</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1">boot_pvalues <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">side =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) {</span>
<span id="cb28-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Based on:</span></span>
<span id="cb28-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># https://blogs.sas.com/content/iml/2011/11/02/how-to-compute-p-values-for-a-bootstrap-distribution.html</span></span>
<span id="cb28-4">  side <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> side[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb28-5">  x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>t)</span>
<span id="cb28-6"></span>
<span id="cb28-7">  ps <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(x, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(.x) {</span>
<span id="cb28-8">    s <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">na.omit</span>(.x)</span>
<span id="cb28-9">    s0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb28-10">    N <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(s)</span>
<span id="cb28-11"></span>
<span id="cb28-12">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (side <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) {</span>
<span id="cb28-13">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>((<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(s <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> s0)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (N <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb28-14">          (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(s <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> s0)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (N <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb28-15">    } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (side <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) {</span>
<span id="cb28-16">      (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(s <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> s0)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (N <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb28-17">    } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (side <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) {</span>
<span id="cb28-18">      (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(s <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> s0)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (N <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb28-19">    }</span>
<span id="cb28-20">  })</span>
<span id="cb28-21">  </span>
<span id="cb28-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setNames</span>(ps,<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(x))</span>
<span id="cb28-23">}</span>
<span id="cb28-24"></span>
<span id="cb28-25"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">boot_pvalues</span>(treatment_phase_results)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>fup:  control - A fup:  control - B       fup:  A - B post: control - A 
           0.0625            0.0625            0.6875            0.0625 
post: control - B       post: A - B pre:  control - A pre:  control - B 
           0.0625            0.9375            0.1250            0.9375 
      pre:  A - B 
           0.3750 </code></pre>
</div>
</div>
<p>These p-values can then be passed to <code>p.adjust()</code> for the p-value adjustment method of your choosing.</p>
</section>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>I’ve demonstrated how to run permutation tests on main effects / interactions, with follow-up analysis using the bootstrap method. Using this code as a basis for any analysis you might have in mind gives you all the flexibility of <code>emmeans</code>, which supports many (many) models!</p>
<center>
<img src="https://blog.msbstats.info/posts/2019-05-09-bootstraping-rm-contrasts/boot.png" class="img-fluid" style="width:50.0%">
</center>


</section>

 ]]></description>
  <category>R</category>
  <category>statistics</category>
  <category>ANOVA</category>
  <category>bootstrap</category>
  <category>code</category>
  <category>mixed-design</category>
  <category>permutation</category>
  <guid>https://blog.msbstats.info/posts/2019-05-09-bootstraping-rm-contrasts/</guid>
  <pubDate>Wed, 08 May 2019 21:00:00 GMT</pubDate>
</item>
<item>
  <title>Quidditch: Is It All About the Snitch?</title>
  <link>https://blog.msbstats.info/posts/2018-11-25-quidditch/</link>
  <description><![CDATA[ 





<p>Much has been said about how the game of Quidditch is ruined by the scoring system - specifically how it makes no sense that the snitch is worth 150 points <em>and</em> that catching it ends the game (<a href="https://www.reddit.com/r/HPMOR/comments/2cqq7n/why_does_the_snitch_ruin_quidditch/">1</a>, <a href="https://www.youtube.com/watch?v=Zfg1I5f2HZY">2</a>, <a href="https://www.reddit.com/r/FanTheories/comments/2nk118/in_defense_of_quidditch/">3</a>). Most of these arguments seem to revolve around the claim that it is nearly impossible to win a match of Quidditch without catching the snitch. Is this true? Let’s try and answer this question formally using statistics and R simulations:</p>
<blockquote class="blockquote">
<p>What is the probability of winning a Quidditch match without catching the snitch?</p>
</blockquote>
<center>
<p><img src="https://blog.msbstats.info/posts/2018-11-25-quidditch/krum.gif" class="img-fluid"></p>
<em>Victor Krum caught the snitch for Bulgaria, but Ireland still won (1994 World cup)</em>
</center>
<section id="the-game-the-rules" class="level2">
<h2 class="anchored" data-anchor-id="the-game-the-rules">The Game, the Rules</h2>
<p>Quidditch is a fictional team sport played on broomsticks, devised by author J. K. Rowling, that is played by wizards in the fantasy world of Harry Potter. Like any sport, the game of Quidditch has many rules, but I will summarize here only the ones pertinent to our discussion:</p>
<ul>
<li>10 points are gained by throwing the Quaffle through one of the opponent team’s three hoops.</li>
<li>150 points are gained by catching the Golden Snitch, which can only be caught by the Seeker.</li>
<li>The game ends when the Golden Snitch is caught.</li>
<li>The winning team is the one has the most points at the end of the game.</li>
</ul>
<p>Given these rules and game objectives, in order to estimate the probability of winning a match we must first estimate (1) how long does it take a seeker to catch the Snitch on average, which determines how long a game lasts and thus how much time is allotted for the teams to score goals, and (2) what is the rate of goal scoring.</p>
<section id="estimating-the-average-game-length" class="level3">
<h3 class="anchored" data-anchor-id="estimating-the-average-game-length">Estimating the Average Game Length</h3>
<p>Unfortunately, not many pro-Quidditch matches are described in the books, so this is quite hard to estimate. On the one hand, the only game that is described in its entirety is the 1994 Quidditch World Cup final, and it was extremely short: only 15 minutes or so (<a href="http://harrypotter.wikia.com/wiki/1994_Quidditch_World_Cup">4</a>]); On the other hand, the longest game ever lasted 3 months (<a href="https://www.quora.com/What-was-the-longest-Quidditch-match-ever-played-and-how-did-it-end">5</a>]). So the average is somewhere between 15 minutes, and 130,000 minutes… great.</p>
<p>For the sake of our simulations, then, I will assume the average game lasts 90 minutes (assuming Rowling has soccer or rugby in mind). I will also assume that the length of a Quidditch game is exponentially distributed, since the probability of catching the Snitch remains constant (as opposed to it getting easier as the game progresses) - the Snitch’s ability to escape capture does not change (magic) and players are substituted throughout the game, so let’s assume an unchanging level of vigilance.</p>
<p>Together, the distribution of game lengths looks something like this:</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2018-11-25-quidditch/index_files/figure-html/unnamed-chunk-1-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>This makes the 3 month long game very unlikely ( <img src="https://latex.codecogs.com/png.latex?Pr(time%3E130,000)%3C10%5E%7B-600%7D"> ), but the 15 minute game rather probable ( <img src="https://latex.codecogs.com/png.latex?Pr(time%3C15)=0.15"> ).</p>
</section>
<section id="estimating-the-rate-of-goals-scored" class="level3">
<h3 class="anchored" data-anchor-id="estimating-the-rate-of-goals-scored">Estimating the Rate of Goals Scored</h3>
<p>Throughout the books, the final scores of several Quidditch games are mentioned, but since we don’t know their lengths, it is hard to estimate the rate of goals-per-minute (GPM). Going by the 15 minute world cup, in which the final score was 170-160, or 170-10 if only counting goal points, we can see that there were 18 goals in 15 minutes, or an average of 9 goals per 15 minutes per team, or a GPM of 0.6 (or 0.07 for Bulgaria, and 1.13 for Ireland). Though this is only one game, and a very short one at that, these numbers seem close to the averages seen in basketball, where GPM are around 40/48 = 0.83 (<a href="https://www.teamrankings.com/nba/stat/field-goals-made-per-game">6</a>). This is also in line with the game’s depiction in the films as a fast sport, with many goals scored quickly and in succession.</p>
<p>I will also assume that this rate is constant and unchanging throughout the game, regardless of the score (or fatigue of the players, see above), and so will assume GPM has a poisson distribution, with an average of 0.8, making the GPM distribution look something like this:</p>
<div class="cell">
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2018-11-25-quidditch/index_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="simulating-the-games" class="level2">
<h2 class="anchored" data-anchor-id="simulating-the-games">Simulating the Games</h2>
<p>(If you’re not that into reading raw code, you can just skip to the conclusions at the end.)</p>
<section id="r-setup" class="level3">
<h3 class="anchored" data-anchor-id="r-setup">R Setup</h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1994</span>)</span>
<span id="cb2-2"></span>
<span id="cb2-3">sim_quidditch <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(mean_game_durA, mean_game_durB, GPM_A, GPM_B,</span>
<span id="cb2-4">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">snitch_points =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">goal_points =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,</span>
<span id="cb2-5">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_sims =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e6</span>) {</span>
<span id="cb2-6"></span>
<span id="cb2-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sim game durations and snitch Points ------------------------------------</span></span>
<span id="cb2-8">  mean_game_dur_rateA <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>mean_game_durA</span>
<span id="cb2-9">  mean_game_dur_rateB <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>mean_game_durB</span>
<span id="cb2-10"></span>
<span id="cb2-11">  game_dursA <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rexp</span>(n_sims, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rate =</span> mean_game_dur_rateA)</span>
<span id="cb2-12">  game_dursB <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rexp</span>(n_sims, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rate =</span> mean_game_dur_rateB)</span>
<span id="cb2-13"></span>
<span id="cb2-14">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Final game durations</span></span>
<span id="cb2-15">  game_durs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(game_dursA <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> game_dursB, game_dursA, game_dursB)</span>
<span id="cb2-16"></span>
<span id="cb2-17">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Who got the snitch (and the points)</span></span>
<span id="cb2-18">  got_snitch <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> game_dursA <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> game_dursB</span>
<span id="cb2-19">  snitch_pointsA <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> got_snitch     <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> snitch_points</span>
<span id="cb2-20">  snitch_pointsB <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>got_snitch) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> snitch_points</span>
<span id="cb2-21"></span>
<span id="cb2-22"></span>
<span id="cb2-23"></span>
<span id="cb2-24">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sim goals and goal points -----------------------------------------------</span></span>
<span id="cb2-25">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Based on the length of the game</span></span>
<span id="cb2-26">  goal_pointsA <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rpois</span>(n_sims,GPM_A<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">floor</span>(game_durs)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> goal_points</span>
<span id="cb2-27">  goal_pointsB <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rpois</span>(n_sims,GPM_B<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">floor</span>(game_durs)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> goal_points</span>
<span id="cb2-28"></span>
<span id="cb2-29"></span>
<span id="cb2-30">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Find Winner -------------------------------------------------------------</span></span>
<span id="cb2-31">  total_pointA <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> snitch_pointsA <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> goal_pointsA</span>
<span id="cb2-32">  total_pointB <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> snitch_pointsB <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> goal_pointsB</span>
<span id="cb2-33"></span>
<span id="cb2-34">  who_won <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_character_</span>,n_sims)</span>
<span id="cb2-35">  who_won[total_pointA <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> total_pointB] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'A'</span></span>
<span id="cb2-36">  who_won[total_pointA <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> total_pointB] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span></span>
<span id="cb2-37"></span>
<span id="cb2-38">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">teamA_points =</span> total_pointA,</span>
<span id="cb2-39">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">teamB_points =</span> total_pointB,</span>
<span id="cb2-40">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Winner =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(who_won),</span>
<span id="cb2-41">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Got_snitch =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(got_snitch,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'A'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>)))</span>
<span id="cb2-42">}</span></code></pre></div>
</div>
</section>
<section id="equal-teams" class="level3">
<h3 class="anchored" data-anchor-id="equal-teams">Equal Teams</h3>
<p>Let’s start by simulating 1 million games between two teams with equal GPMs and equally good seekers.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sim_quidditch</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean_game_durA =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># in mins</span></span>
<span id="cb3-2">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean_game_durB =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>,</span>
<span id="cb3-3">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">GPM_A =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>,</span>
<span id="cb3-4">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">GPM_B =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>)</span>
<span id="cb3-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(res,{</span>
<span id="cb3-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(Winner) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> </span>
<span id="cb3-7">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(Winner)</span>
<span id="cb3-8">})</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Winner
       A        B 
0.497066 0.496060 </code></pre>
</div>
</div>
<p>Under these conditions, the probability of winning a game without catching the snitch is:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(res, {</span>
<span id="cb5-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(Winner[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(Winner)] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> Got_snitch[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(Winner)])</span>
<span id="cb5-3">})</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.03747158</code></pre>
</div>
</div>
<p>3.75%! Hmm, that does seem quite unfair… Though it does put Fred and George’s gambling odds at around 1:20!</p>
<p>How many games were determined by the snitch - i.e., had the snitch been caught at the same time by the other team, they would have won?</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(res, {</span>
<span id="cb7-2">  (</span>
<span id="cb7-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(teamA_points[Winner <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'A'</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> teamB_points[Winner <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'A'</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> T) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-4">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(teamA_points[Winner <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> teamB_points[Winner <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> T)</span>
<span id="cb7-5">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e6</span></span>
<span id="cb7-6">})</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.94918</code></pre>
</div>
</div>
<p>94.9%! Not the best of odds…</p>
</section>
<section id="unequal-but-matched-teams" class="level3">
<h3 class="anchored" data-anchor-id="unequal-but-matched-teams">Unequal (but matched) Teams</h3>
<p>But just like in real sports, different teams have different strengths… Let’s see what happens when we create two teams who have an equal probability of winning, but differ in terms of their seekers and GPMs.</p>
<p>After some trial and error, I found that if we have a team with a seeker who catches the snitch within 67 minutes on average and a GPM of 0.71, versus a team with a seeker who catches the snitch in within 113 minutes on average but with a GPM of 0.90, both teams have a 50% chance of winning.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sim_quidditch</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean_game_durA =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">67</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># in mins</span></span>
<span id="cb9-2">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean_game_durB =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">113</span>,</span>
<span id="cb9-3">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">GPM_A =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.71</span>,</span>
<span id="cb9-4">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">GPM_B =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.90</span>)</span>
<span id="cb9-5"></span>
<span id="cb9-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(res,{</span>
<span id="cb9-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(Winner)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(Winner)</span>
<span id="cb9-8">})</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Winner
       A        B 
0.497701 0.490796 </code></pre>
</div>
</div>
<p>So, what is the chance of winning without catching the snitch?</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(res,{</span>
<span id="cb11-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(Winner[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(Winner)] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> Got_snitch[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(Winner)])</span>
<span id="cb11-3">})</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.1213732</code></pre>
</div>
</div>
<p>12.2%! Still pretty low… very disappointing. Let’s explore each team separately:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(res,{</span>
<span id="cb13-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(Winner, Got_snitch)</span>
<span id="cb13-3">})</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>      Got_snitch
Winner      A      B
     A 497218    483
     B 119494 371302</code></pre>
</div>
</div>
<p>It seems that 99.9% of team A’s wins were due to their better seeker…</p>
<p>…but that only 24.3% of team B’s wins were achieved by their weaker seeker T!</p>
<p>How many games were determined by the snitch?</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(res, {</span>
<span id="cb15-2">  (</span>
<span id="cb15-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(teamA_points[Winner <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'A'</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> teamB_points[Winner <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'A'</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> T) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-4">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(teamA_points[Winner <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> teamB_points[Winner <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> T)</span>
<span id="cb15-5">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e6</span></span>
<span id="cb15-6">})</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.901089</code></pre>
</div>
</div>
<p>90.1%!</p>
</section>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<ul>
<li>If you’ve caught the snitch, you’ve probably won.</li>
<li>If you’ve won, you probably caught the snitch.</li>
<li>If you’ve lost, catching the snitch would have probably changed that.</li>
</ul>
<p>But… the majority of points are non-snitch related. And it seems to me unlikely that you would have a seeker that was twice as good as the opposing team’s, and so compensating for a weaker seeker seems very likely given this simulation. SO it’s probably best to invest in a better chaser who can bring up your GPM and a better keeper who can lower your opponents’ GPM, than in a seeker who can shave 5 minutes off of his snitch catching time…</p>
<p>Also, maybe someone should change the rules? Perhaps the snitch should <a href="https://en.wikipedia.org/wiki/Quidditch_(sport)">only be worth 30 points</a>?</p>


</section>

 ]]></description>
  <category>R</category>
  <category>statistics</category>
  <category>code</category>
  <category>harry potter</category>
  <category>simulation</category>
  <guid>https://blog.msbstats.info/posts/2018-11-25-quidditch/</guid>
  <pubDate>Sat, 24 Nov 2018 22:00:00 GMT</pubDate>
</item>
<item>
  <title>Examining Inter-Rater Reliability in a Reality Baking Show</title>
  <link>https://blog.msbstats.info/posts/2018-10-18-baking-show-reliability/</link>
  <description><![CDATA[ 





<p><a href="https://en.wikipedia.org/wiki/Game_of_Chefs">Game of Chefs</a> is an Israeli reality cooking competition show, where chefs compete for the title of “Israel’s most talented chef”. The show has four stages: blind auditions, training camp, kitchen battles, and finals. Here I will examine the 4th season’s (<a href="https://reshet.tv/yummies/games-of-chef-konditor/"><em>Game of Chefs: Confectioner</em></a>) inter-rater reliability in the blind auditions, using some simple R code.</p>
<section id="the-format" class="level2">
<h2 class="anchored" data-anchor-id="the-format">The Format</h2>
<p>In the blind auditions, candidates have 90 minutes to prepare a dessert, confection or other baked good, which is then sent to the show’s four judges for a blind taste test. The judges don’t see the candidate, and know nothing about him or her, until after they deliver their decision - A “pass” decision is signified by the judge granting the candidate a kitchen knife; a “fail” decision is signified by the judge not granting a knife. A candidate who receives a knife from at least three of the judges continues on to the next stage, the training camp.</p>
<center>
<p><img src="https://blog.msbstats.info/posts/2018-10-18-baking-show-reliability/mishakai.webp" class="img-fluid"></p>
<p><em>The 4 judges and host, from left to right: Yossi Shitrit, Erez Komarovsky, Miri Bohadana (host), Assaf Granit, and Moshik Roth</em></p>
</center>
</section>
<section id="inter-rater-reliability" class="level2">
<h2 class="anchored" data-anchor-id="inter-rater-reliability">Inter-Rater Reliability</h2>
<p>I’ve watched all 4 episodes of the blind auditions (for purely academic reasons!), for a total of 31 candidates. For each dish, I recorded each of the four judges’ verdict (fail / pass).</p>
<p>Let’s load the data.<sup>1</sup></p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'IJR.csv'</span>)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(df)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>  Moshik Assaf Erez Yosi
1      1     1    1    1
2      1     0    0    0
3      1     1    1    0
4      1     0    0    0
5      0     1    1    1
6      1     1    1    1</code></pre>
</div>
</div>
<p>We will need the following packages:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for manipulating the data</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(magrittr) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># &lt;3</span></span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(psych) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for computing Choen's Kappa</span></span>
<span id="cb3-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(corrr) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for manipulating matricies</span></span></code></pre></div>
</div>
<p>We can now use the <code>psych</code> package to compute <a href="https://en.wikipedia.org/wiki/Cohen%27s_kappa">Cohen’s Kappa coefficient</a> for inter-rater agreement for categorical items, such as the fail/pass categories we have here. <img src="https://latex.codecogs.com/png.latex?%5Ckappa"> ranges from -1 (full disagreement) through 0 (no pattern of agreement) to +1 (full agreement). Normally, <img src="https://latex.codecogs.com/png.latex?%5Ckappa"> is computed between two raters, and for the case of more than two raters, the mean <img src="https://latex.codecogs.com/png.latex?%5Ckappa"> across all pair-wise raters is used.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cohen.kappa</span>(df)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Cohen Kappa (below the diagonal) and Weighted Kappa (above the diagonal) 
For confidence intervals and detail print with all=TRUE
       Moshik Assaf  Erez  Yosi
Moshik   1.00  0.34  0.17  0.29
Assaf    0.34  1.00  0.17  0.68
Erez     0.17  0.17  1.00 -0.16
Yosi     0.29  0.68 -0.16  1.00

Average Cohen kappa for all raters  0.25
Average weighted kappa for all raters  0.25</code></pre>
</div>
</div>
<p>We can see that overall <img src="https://latex.codecogs.com/png.latex?%5Ckappa=0.25"> - surprisingly low for what might be expected from a group of pristine, accomplished, professional chefs and bakers in a blind taste test.</p>
<p>When examining the pair-wise coefficients, we can also see that Erez seems to be in lowest agreement with each of the other judges (and even in a slight disagreement with Yossi!). This might be because Erez is new on the show (this is his first season as judge), but it might also be because of the four judges, he is the only one who is actually a confectioner (the other 3 are restaurant chefs).</p>
<p>For curiosity’s sake, let’s also look at the <img src="https://latex.codecogs.com/png.latex?%5Ckappa"> coefficient between each judge’s rating and the total fail/pass decision, based on whether a dish got a “pass” from at least 3 judges.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">PASS =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowSums</span>(.) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))</span>
<span id="cb6-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(df)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>  Moshik Assaf Erez Yosi PASS
1      1     1    1    1    1
2      1     0    0    0    0
3      1     1    1    0    1
4      1     0    0    0    0
5      0     1    1    1    1
6      1     1    1    1    1</code></pre>
</div>
</div>
<p>We can now use the wonderful new <a href="https://cran.r-project.org/package=corrr"><code>corrr</code></a> package, which is intended for exploring correlations, but can also generally be used to manipulate any symmetric matrix in a tidy-fashion.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cohen.kappa</span>(df)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cohen.kappa <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_cordf</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">focus</span>(PASS)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 × 2
  term    PASS
  &lt;chr&gt;  &lt;dbl&gt;
1 Moshik 0.619
2 Assaf  0.746
3 Erez   0.159
4 Yosi   0.676</code></pre>
</div>
</div>
<p>Perhaps unsurprisingly (to people familiar with previous seasons of the show), it seems that Assaf’s judgment of a dish is a good indicator of whether or not a candidate will continue on to the next stage. Also, once again we see that Erez is barely aligned with the other judges’ total decision.</p>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<blockquote class="blockquote">
<p>Every man to his taste…</p>
</blockquote>
<p>Even among the experts there is little agreement on what is fine cuisine and what is not worth a doggy bag. Having said that, if you still have your heart set on competing in Game of Chefs, it seems that you should at least appeal to Assaf’s palate.</p>
<p>Bon Appetit!</p>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>The data is availabe <a href="../..\appendices/2018-10-18-baking-show-reliability/">here</a>↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>R</category>
  <category>statistics</category>
  <category>code</category>
  <category>reliability</category>
  <category>test theory</category>
  <guid>https://blog.msbstats.info/posts/2018-10-18-baking-show-reliability/</guid>
  <pubDate>Wed, 17 Oct 2018 21:00:00 GMT</pubDate>
</item>
<item>
  <title>Common Practice (misuse of) Moderation in Regression</title>
  <link>https://blog.msbstats.info/posts/2018-09-12-misuse-of-moderation/</link>
  <description><![CDATA[ 





<blockquote class="blockquote">
<p>… or how I stopped trusting standardized coefficients.</p>
</blockquote>
<p>Say you want to know how perceived importance of job security is related to feelings of job satisfaction. Let’s also say that you suspect that this relationship might differ between Gen-X-ers and millennials with their MTV and their hip-hop. So you conduct a survey, asking 100 Gen-X-ers and 100 millennials to rank how important they think job security is when job searching and their current job satisfaction. You then plug your data into a 2-step hierarchical regression to test the moderating effect of Generation on the effect of Job Security in your favorite stats program (If you’re a Gen-X-er, SPSS. If you’re a millennial, or a reasonable human being, R). You find:</p>
<div class="cell">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">Term</th>
<th style="text-align: right;">Estimate</th>
<th style="text-align: right;">SE</th>
<th style="text-align: right;">t-value</th>
<th style="text-align: left;">p-value</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">(Intercept)</td>
<td style="text-align: right;">4.400</td>
<td style="text-align: right;">0.393</td>
<td style="text-align: right;">11.203</td>
<td style="text-align: left;">&lt;0.001</td>
</tr>
<tr class="even">
<td style="text-align: left;">Group [Millennial]</td>
<td style="text-align: right;">0.575</td>
<td style="text-align: right;">0.245</td>
<td style="text-align: right;">2.343</td>
<td style="text-align: left;">0.020</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Job Security</td>
<td style="text-align: right;">0.450</td>
<td style="text-align: right;">0.046</td>
<td style="text-align: right;">9.717</td>
<td style="text-align: left;">&lt;0.001</td>
</tr>
</tbody>
</table>
</div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">Term</th>
<th style="text-align: right;">Estimate</th>
<th style="text-align: right;">SE</th>
<th style="text-align: right;">t-value</th>
<th style="text-align: left;">p-value</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">(Intercept)</td>
<td style="text-align: right;">3.20</td>
<td style="text-align: right;">0.527</td>
<td style="text-align: right;">6.076</td>
<td style="text-align: left;">&lt;0.001</td>
</tr>
<tr class="even">
<td style="text-align: left;">Group [Millennial]</td>
<td style="text-align: right;">2.45</td>
<td style="text-align: right;">0.613</td>
<td style="text-align: right;">3.994</td>
<td style="text-align: left;">&lt;0.001</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Job Security</td>
<td style="text-align: right;">0.60</td>
<td style="text-align: right;">0.064</td>
<td style="text-align: right;">9.391</td>
<td style="text-align: left;">&lt;0.001</td>
</tr>
<tr class="even">
<td style="text-align: left;">Group [Millennial] * Job Security</td>
<td style="text-align: right;">-0.30</td>
<td style="text-align: right;">0.090</td>
<td style="text-align: right;">-3.320</td>
<td style="text-align: left;">0.001</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>“Very interesting”, you think to yourself. But before drafting your Science submission, you decide to plot your data, just to make sure this trend isn’t somehow driven by outliers, and you find that it is not!</p>
<div class="cell">
<div class="cell-output cell-output-stderr">
<pre><code>`geom_smooth()` using formula = 'y ~ x'</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2018-09-12-misuse-of-moderation/index_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>“This is great!” you confidently think to yourself, “The relationship between the perceived importance of job security and reported job satisfaction is weaker among millennials!”. But how much weaker? You extract the simple slopes for both groups:</p>
<div class="cell">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">Group</th>
<th style="text-align: right;">Simple Slope</th>
<th style="text-align: right;">SE</th>
<th style="text-align: right;">df</th>
<th style="text-align: right;">t-value</th>
<th style="text-align: left;">p-value</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Gen-X-ers</td>
<td style="text-align: right;">0.6</td>
<td style="text-align: right;">0.064</td>
<td style="text-align: right;">196</td>
<td style="text-align: right;">9.391</td>
<td style="text-align: left;">&lt;0.001</td>
</tr>
<tr class="even">
<td style="text-align: left;">Millennials</td>
<td style="text-align: right;">0.3</td>
<td style="text-align: right;">0.064</td>
<td style="text-align: right;">196</td>
<td style="text-align: right;">4.696</td>
<td style="text-align: left;">&lt;0.001</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>A slope of 0.6 for Gen-X-ers and of 0.3 for Millennials. You find it hard to interpret these slopes, so you do the next reasonable thing: you decide to standardize job security importance and job satisfaction.</p>
<p>Just to make sure nothing funny happened, you plot your data again:</p>
<div class="cell">
<div class="cell-output cell-output-stderr">
<pre><code>`geom_smooth()` using formula = 'y ~ x'</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2018-09-12-misuse-of-moderation/index_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>As expected - your data is now scaled and centered! You compute the simple standardized slopes and find that:</p>
<div class="cell">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">Group</th>
<th style="text-align: right;">Simple Slope</th>
<th style="text-align: right;">SE</th>
<th style="text-align: right;">df</th>
<th style="text-align: right;">t-value</th>
<th style="text-align: left;">p-value</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Gen-X-ers</td>
<td style="text-align: right;">0.963</td>
<td style="text-align: right;">0.103</td>
<td style="text-align: right;">196</td>
<td style="text-align: right;">9.391</td>
<td style="text-align: left;">&lt;0.001</td>
</tr>
<tr class="even">
<td style="text-align: left;">Millennials</td>
<td style="text-align: right;">0.482</td>
<td style="text-align: right;">0.103</td>
<td style="text-align: right;">196</td>
<td style="text-align: right;">4.696</td>
<td style="text-align: left;">&lt;0.001</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>“Amazing,” you exclaim to yourself,” the relationship between importance of job security and job satisfaction is nearly perfect among Gen-X-ers, and is half as strong among millennials!” You open MS Word and start typing away…</p>
<section id="somethings-not-right." class="level2">
<h2 class="anchored" data-anchor-id="somethings-not-right.">Something’s not right.</h2>
<p>But wait - looking at the plot above, it is clear that the <em>correlation</em> among Gen-X-ers is far from perfect! How is this possible? What’s really going on here?</p>
<p>The short answer is that that the strength of the association (i.e., the correlation) does not equal the strength of the effect (i.e., the slope). Plotting the relationship for each group separately will shed some light on what’s actually going on:</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://blog.msbstats.info/posts/2018-09-12-misuse-of-moderation/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>As is now apparent, the strength of the correlation is equal between the groups - if we were to calculate the correlation between within each group, we would find that they are both exactly <img src="https://latex.codecogs.com/png.latex?r=0.6"> (in fact, you can see that I made the scatter of points literally identical between the groups), with the only difference between the groups being the difference in scale (and center) of the two variables.<sup>1</sup></p>
</section>
<section id="why-does-this-happen" class="level2">
<h2 class="anchored" data-anchor-id="why-does-this-happen">Why does this happen?</h2>
<p>The slope, even the standardized slope, only measures the expected change in our outcome variable (e.g., Job Satisfaction) as a function of change in a predictor’s values (e.g., Perceived Importance of Job Security). If we’ve standardized our data, the only thing we’ve changed is that the slope now represents the expected change in an outcome’s standardized values as a function of a change in the predictor’s standardized values.</p>
<p>But why does this not yield what we expect? If in a simple regression analysis the standardized slope <strong><em>is</em></strong> the correlation (i.e., the strength of the relationship), why is that not the case here when measuring simple standardized slopes?</p>
<p>The reason is that when we standardize a variable, we standardize it <em>across</em> any other variables that might interact with it, while our question regards the standardized change in <img src="https://latex.codecogs.com/png.latex?Y"> as a function of <img src="https://latex.codecogs.com/png.latex?X"> <em>within</em> different levels of a moderator variable.</p>
</section>
<section id="what-can-we-do" class="level2">
<h2 class="anchored" data-anchor-id="what-can-we-do">What can we do?</h2>
<p>The first thing we should do is understand what we are estimating and testing when fitting a moderation model: we are testing whether the size of variable <img src="https://latex.codecogs.com/png.latex?X">’s effect on variable <img src="https://latex.codecogs.com/png.latex?Y"> is different within different levels of some other <img src="https://latex.codecogs.com/png.latex?M"> variable (or, that the slope of <img src="https://latex.codecogs.com/png.latex?X"> is different within different levels of <img src="https://latex.codecogs.com/png.latex?M">). Nothing more. Nothing less. In other words, no matter how you slice it, regression can only answer questions regarding the size of an effect (What is the size of the effect? Does this size change under some conditions?). However, if we instead have questions regarding the strength of an association (i.e., the correlation) between X and Y (How strongly are they correlated? Does the correlation change under some conditions?), we are using the wrong tool.</p>
<p>A more appropriate tool for testing if the strength of association changes between levels of an <img src="https://latex.codecogs.com/png.latex?M"> is to use Fisher’s transformation to conduct a simple t-test to compare two correlations (read about the logic behind this transformation <a href="https://en.wikipedia.org/wiki/Fisher_transformation">here</a>, <a href="http://dx.doi.org/10.1037/0021-9010.72.1.146">here</a>, and <a href="https://doi.org/10.1037/1082-989X.12.4.399">here</a>). This can be achieved in R using <a href="https://cran.r-project.org/package=psych"><code>psych::paired.r()</code></a> … or if you’re a Gen-X-er, I guess you <em>can</em> do this in SAS or SPSS…</p>
<center>
<img src="https://blog.msbstats.info/posts/2018-09-12-misuse-of-moderation/why.gif" class="img-fluid" style="width:50.0%">
</center>
<hr>
<p><strong>P.S.</strong>: Above I demonstrated that standardized simple slopes are not the same as “simple” correlations in the context of a moderation model. Similarly, in a multiple regression analysis, standardized coefficients (<img src="https://latex.codecogs.com/png.latex?%5Cbeta">s) are <em>not</em> semi-partial (part) correlations. If you have questions regarding semi-partial correlations / strength of unique associations, you can:</p>
<ol type="1">
<li>Convert a standardized coefficient into a semi-partial (part) correlation via <img src="https://latex.codecogs.com/png.latex?r_%7By(x.Z)%7D=%5Cbeta_%7Byx%7D%5Ctimes%5Csqrt%7B1-r%5E2_%7BxZ%7D%7D"> (or simply by using <a href="https://cran.r-project.org/package=ppcor"><code>ppcor::spcor()</code></a> in R).</li>
<li>Determine if two unique association differ in strength by way of a <em>dominance analysis</em> (read about it <a href="https://doi.org/10.1037/0033-2909.114.3.542">here</a> and <a href="https://cran.r-project.org/package=yhat">here</a>).<sup>2</sup></li>
</ol>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>If this sounds like we violated the assumption of homoscedasticity, you’re absolutely right! In a <a href="../..\posts/2018-08-30-linear-regression-assumptions/">previous post</a> I wrote that we can overcome such a violation with a permutation test. In this case, this would not help, since the problem here is not with the statistical inference, but with the conclusions we draw from the fitted model parameters - we are assigning the wrong meaning to the numbers we observe. We’re using wrong tool for the question we’re asking. This can only be solved by using the right tool.↩︎</p></li>
<li id="fn2"><p>In R, <code>lavaan</code> can be used to compare standardized coefficients <a href="http://lavaan.ugent.be/tutorial/groups.html">between groups</a> as well as <a href="../..\appendices/2018-09-12-misuse-of-moderation/">between predictors</a> using <code>standardizedSolution()</code>.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>statistics</category>
  <category>assumptions</category>
  <category>interactions</category>
  <category>linear models</category>
  <category>moderation</category>
  <category>regression</category>
  <guid>https://blog.msbstats.info/posts/2018-09-12-misuse-of-moderation/</guid>
  <pubDate>Tue, 11 Sep 2018 21:00:00 GMT</pubDate>
</item>
</channel>
</rss>
