<?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>Statistics Snippets</title>
<link>https://nwickel.codeberg.page/statistics_snippets/</link>
<atom:link href="https://nwickel.codeberg.page/statistics_snippets/index.xml" rel="self" type="application/rss+xml"/>
<description></description>
<generator>quarto-1.5.56</generator>
<lastBuildDate>Thu, 29 Oct 2026 23:00:00 GMT</lastBuildDate>
<item>
  <title>Power for regression models</title>
  <dc:creator>Nora Wickelmaier</dc:creator>
  <link>https://nwickel.codeberg.page/statistics_snippets/posts/2026-10-30_powerregression/</link>
  <description><![CDATA[ 





<section id="predictors-of-a-regression-model-for-power-simulation" class="level1">
<h1>Predictors of a regression model for power simulation</h1>
<p>Power simulation for regression starts with the question if one should draw the values for the predictors “inside” or “outside” of the power simulation. There is a reason why Florian gives a regression example where the predictor is dose of medication and has therefore fixed values <span class="citation" data-cites="Wickelmaier26">(Wickelmaier 2026, sec. 4)</span>. But often we have predictors that can take all kinds of values and we think about these predictors in terms of also being drawn from a distribution. If we draw the predictor in each iteration of a power simulation, we are adding additional variance that the regression model does not account for. Where does this variance go? We will stick to simple regression here.</p>
<section id="drawing-values-for-a-predictor-once" class="level2">
<h2 class="anchored" data-anchor-id="drawing-values-for-a-predictor-once">Drawing values for a predictor once</h2>
<p>We will assume that values for our predictor come from a normal distribution. Since I am making this point without any context, let’s just assume a standard normal distribution.</p>
<div class="cell" data-layout-align="center">
<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;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1238</span>)</span>
<span id="cb1-2"></span>
<span id="cb1-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;">20</span></span>
<span id="cb1-4">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;">rnorm</span>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div>
</div>
<p>We have now drawn our predictor and will use this in a power simulation. Next, we need to specify additional parameters for the simple regression model</p>
<p><img src="https://latex.codecogs.com/png.latex?%20y%20=%20%5Cbeta_0%20+%20%5Cbeta_1%20x%20+%20%5Cvarepsilon"> with <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon%20%5Csim%20N(0,%20%5Csigma%5E2_%5Cvarepsilon)">.</p>
<div class="cell" data-layout-align="center">
<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">beta <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="at" style="color: #657422;
background-color: null;
font-style: inherit;">beta0 =</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;">beta1 =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb2-2">se <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></code></pre></div>
</div>
<p>We will now run a power simulation to find out how many participants we need to find an effect of <img src="https://latex.codecogs.com/png.latex?%5Cbeta_1%20=%200.5">.</p>
<div class="cell" data-layout-align="center">
<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">pval <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;">replicate</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>, {</span>
<span id="cb3-2">  y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> beta[<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> beta[<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> 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>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> se)</span>
<span id="cb3-3">  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>
<span id="cb3-4">  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> x)</span>
<span id="cb3-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(m0, m1)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pr(&gt;F)"</span>]</span>
<span id="cb3-6">  }</span>
<span id="cb3-7">)</span>
<span id="cb3-8"></span>
<span id="cb3-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(pval <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.05</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.458</code></pre>
</div>
</div>
<p>This results in a power that is not sufficient, yet, so let us adjust our N and rerun it.</p>
<div class="cell" data-layout-align="center">
<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">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;">25</span></span>
<span id="cb5-2">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;">rnorm</span>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb5-3"></span>
<span id="cb5-4">pval <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;">replicate</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>, {</span>
<span id="cb5-5">  y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> beta[<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> beta[<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> 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>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> se)</span>
<span id="cb5-6">  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>
<span id="cb5-7">  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> x)</span>
<span id="cb5-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(m0, m1)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pr(&gt;F)"</span>]</span>
<span id="cb5-9">  }</span>
<span id="cb5-10">)</span>
<span id="cb5-11"></span>
<span id="cb5-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(pval <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.05</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.7965</code></pre>
</div>
</div>
<p>OK, this looks good. So we need to collect 25 subjects, right? Looks like the power increased quite a bit for just 5 more subjects. Let’s make sure and rerun it.</p>
<div class="cell" data-layout-align="center">
<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;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)</span>
<span id="cb7-2"></span>
<span id="cb7-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;">25</span></span>
<span id="cb7-4">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;">rnorm</span>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</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">pval <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;">replicate</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>, {</span>
<span id="cb7-7">  y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> beta[<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> beta[<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> 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>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> se)</span>
<span id="cb7-8">  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>
<span id="cb7-9">  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> x)</span>
<span id="cb7-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(m0, m1)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pr(&gt;F)"</span>]</span>
<span id="cb7-11">  }</span>
<span id="cb7-12">)</span>
<span id="cb7-13"></span>
<span id="cb7-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(pval <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.05</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.6125</code></pre>
</div>
</div>
<p>Obviously, the power is strongly influenced by our predictor and what values it takes. What happens when we draw the predictor within each iteration of the power simulation? And are there differences in parameter recovery for these two approaches?</p>
</section>
<section id="drawing-values-for-the-predictor-in-each-iteration" class="level2">
<h2 class="anchored" data-anchor-id="drawing-values-for-the-predictor-in-each-iteration">Drawing values for the predictor in each iteration</h2>
<div class="cell" data-layout-align="center">
<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;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1450</span>)</span>
<span id="cb9-2"></span>
<span id="cb9-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;">25</span></span>
<span id="cb9-4"></span>
<span id="cb9-5">pval <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;">replicate</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>, {</span>
<span id="cb9-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Draw predictor</span></span>
<span id="cb9-7">  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;">rnorm</span>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb9-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulate dependent variable</span></span>
<span id="cb9-9">  y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> beta[<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> beta[<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> 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>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> se)</span>
<span id="cb9-10">  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>
<span id="cb9-11">  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> x)</span>
<span id="cb9-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(m0, m1)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pr(&gt;F)"</span>]</span>
<span id="cb9-13">  }</span>
<span id="cb9-14">)</span>
<span id="cb9-15"></span>
<span id="cb9-16"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(pval <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.05</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.6365</code></pre>
</div>
</div>
<p>With this approach we see that N = 25 is not enough to find our effect with a power of 80%.</p>
<div class="cell" data-layout-align="center">
<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">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;">37</span></span>
<span id="cb11-2"></span>
<span id="cb11-3">pval <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;">replicate</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>, {</span>
<span id="cb11-4">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Draw predictor</span></span>
<span id="cb11-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;">rnorm</span>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb11-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulate dependent variable</span></span>
<span id="cb11-7">  y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> beta[<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> beta[<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> 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>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> se)</span>
<span id="cb11-8">  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>
<span id="cb11-9">  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> x)</span>
<span id="cb11-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(m0, m1)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pr(&gt;F)"</span>]</span>
<span id="cb11-11">  }</span>
<span id="cb11-12">)</span>
<span id="cb11-13"></span>
<span id="cb11-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(pval <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.05</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.81</code></pre>
</div>
</div>
<p>Power estimation with this approach is more stable than when we draw the predictor only once. But what does this mean for parameter recovery?</p>
</section>
</section>
<section id="parameter-recovery" class="level1">
<h1>Parameter recovery</h1>
<section id="drawing-values-for-a-predictor-once-1" class="level2">
<h2 class="anchored" data-anchor-id="drawing-values-for-a-predictor-once-1">Drawing values for a predictor once</h2>
<div class="cell" data-layout-align="center">
<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">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;">200</span></span>
<span id="cb13-2"></span>
<span id="cb13-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Draw predictor</span></span>
<span id="cb13-4">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;">rnorm</span>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb13-5"></span>
<span id="cb13-6">out <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;">replicate</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>, {</span>
<span id="cb13-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulate dependent variable</span></span>
<span id="cb13-8">  y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> beta[<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> beta[<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> 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>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> se)</span>
<span id="cb13-9">  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> x)</span>
<span id="cb13-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(m), <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;">sigma</span>(m))</span>
<span id="cb13-11">  }</span>
<span id="cb13-12">)</span></code></pre></div>
</div>
<div class="cell" data-layout-align="center">
<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">boxplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>(out))</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://nwickel.codeberg.page/statistics_snippets/posts/2026-10-30_powerregression/index_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="480"></p>
</figure>
</div>
</div>
<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;">rowMeans</span>(out)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>(Intercept)           x       sigma 
  0.9986695   0.5022547   0.9995984 </code></pre>
</div>
</div>
</section>
<section id="drawing-values-for-the-predictor-in-each-iteration-1" class="level2">
<h2 class="anchored" data-anchor-id="drawing-values-for-the-predictor-in-each-iteration-1">Drawing values for the predictor in each iteration</h2>
<div class="cell" data-layout-align="center">
<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">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;">200</span></span>
<span id="cb17-2"></span>
<span id="cb17-3">out <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;">replicate</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>, {</span>
<span id="cb17-4">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Draw predictor</span></span>
<span id="cb17-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;">rnorm</span>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb17-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulate dependent variable</span></span>
<span id="cb17-7">  y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> beta[<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> beta[<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> 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>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> se)</span>
<span id="cb17-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>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x)</span>
<span id="cb17-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(m), <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;">sigma</span>(m))</span>
<span id="cb17-10">  }</span>
<span id="cb17-11">)</span></code></pre></div>
</div>
<div class="cell" data-layout-align="center">
<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">boxplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>(out))</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://nwickel.codeberg.page/statistics_snippets/posts/2026-10-30_powerregression/index_files/figure-html/unnamed-chunk-12-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="480"></p>
</figure>
</div>
</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;">rowMeans</span>(out)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>(Intercept)           x       sigma 
  0.9983379   0.5017602   0.9996439 </code></pre>
</div>
</div>
<p>Interestingly, there is no difference in parameter recovery. That is actually not what I expected.</p>
</section>
</section>
<section id="conlusions" class="level1">
<h1>Conlusions</h1>
<p>But that makes the conclusion easy. Then I would recommend drawing the predictor in each iteration of the power simulation since this makes the power estimation much more stable. This is, of course, the easiest version of this simulation. What happens if one draws more than one predictor or if the predictors are correlated?</p>
</section>
<section id="session-info" class="level1">
<h1>Session info</h1>
<div class="cell" data-layout-align="center">
<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;">sessionInfo</span>()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>R version 4.6.1 (2026-06-24 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 26100)

Matrix products: default
  LAPACK version 3.12.1

locale:
[1] LC_COLLATE=German_Germany.utf8  LC_CTYPE=German_Germany.utf8   
[3] LC_MONETARY=German_Germany.utf8 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.utf8    

time zone: Europe/Berlin
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] htmlwidgets_1.6.4 compiler_4.6.1    fastmap_1.2.0     cli_3.6.6        
 [5] tools_4.6.1       htmltools_0.5.9   otel_0.2.0        yaml_2.3.12      
 [9] rmarkdown_2.32    knitr_1.52        jsonlite_2.0.0    xfun_0.62        
[13] digest_0.6.39     rlang_1.3.0       evaluate_1.0.5   </code></pre>
</div>
</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">
<div id="ref-Wickelmaier26" class="csl-entry">
Wickelmaier, F. 2026. <span>“Simulating the Power of Statistical Tests: A Collection of <span>R</span> Examples.”</span> <em>ArXiv</em>. <a href="https://doi.org/10.48550/arXiv.2110.09836">https://doi.org/10.48550/arXiv.2110.09836</a>.
</div>
</div></section></div> ]]></description>
  <category>Power</category>
  <category>Regression</category>
  <category>R</category>
  <guid>https://nwickel.codeberg.page/statistics_snippets/posts/2026-10-30_powerregression/</guid>
  <pubDate>Thu, 29 Oct 2026 23:00:00 GMT</pubDate>
</item>
<item>
  <title>A volleyball example of differentiation and integration</title>
  <dc:creator>Florian Wickelmaier</dc:creator>
  <link>https://nwickel.codeberg.page/statistics_snippets/posts/2026-10-15_integralvolleyball/</link>
  <description><![CDATA[ 





<section id="use-case" class="level1">
<h1>Use case</h1>
<p>In the lecture on probability theory, I’m often in need of a basic example of the derivative, the integral, and their relationship. So here it is.</p>
</section>
<section id="the-example" class="level1">
<h1>The example</h1>
<p>Suppose you roll a volleyball from the back boundary line to the middle line under the net. The ball will travel a distance of 9m. It will start fast, but gradually slow down to come to a stop under the net. The top of Figure&nbsp;1 shows the distance-time function. For example, after the first second, the ball has travelled 5m, after another second, it has hit the 8m mark, and within the third second, it only travels 1m to its final resting point, so it has constantly slowed down. The bottom of Figure&nbsp;1 shows the instantaneous velocity. The ball starts out with 6m/s, but within the first second, it slows down to 4m/s. After three seconds, the velocity is 0m/s.</p>
<div class="cell" data-layout-align="center">
<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">s <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>(t) <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>t <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> t<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="cb1-2">v <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>(t) <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;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>t</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</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;">2</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;">mai =</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;">6</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</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="at" style="color: #657422;
background-color: null;
font-style: inherit;">mgp =</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;">2</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">curve</span>(s, <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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xname =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"t"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"darkblue"</span>,</span>
<span id="cb1-6">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Time, t (in seconds)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Travelled distance (m), s(t)"</span>,</span>
<span id="cb1-7">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.first =</span> {</span>
<span id="cb1-8">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-9">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</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;">9</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb1-10">})</span>
<span id="cb1-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">text</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.4</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Back boundary line"</span>)</span>
<span id="cb1-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">text</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.4</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8.5</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Middle line, net"</span>)</span>
<span id="cb1-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">text</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.5</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ball movement"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">srt =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>)</span>
<span id="cb1-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrows</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.1</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;">8.9</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>)</span>
<span id="cb1-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">curve</span>(v, <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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xname =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"t"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"darkblue"</span>,</span>
<span id="cb1-16">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Time, t (in seconds)"</span>,</span>
<span id="cb1-17">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Instantaneous velocity (m/s), v(t)"</span>,</span>
<span id="cb1-18">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.first =</span> {</span>
<span id="cb1-19">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">polygon</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;">3</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;">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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>)</span>
<span id="cb1-20">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-21">})</span></code></pre></div>
</details>
<div class="cell-output-display">
<div id="fig-dist-time" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-dist-time-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://nwickel.codeberg.page/statistics_snippets/posts/2026-10-15_integralvolleyball/index_files/figure-html/fig-dist-time-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="336">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-dist-time-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Distance-time function (top) and instantaneous velocity function (bottom) of a volleyball that rolls from the back boundary line to the middle line.
</figcaption>
</figure>
</div>
</div>
</div>
<p>Formally, the distance-time function displayed in Figure&nbsp;1 is <img src="https://latex.codecogs.com/png.latex?%0A%20%20s(t)%20=%206t%20-%20t%5E2%0A"> (it shows the travelled distance over time), and the instantaneous velocity <img src="https://latex.codecogs.com/png.latex?%0A%20%20v(t)%20=%20%5Ctfrac%7Bd%7D%7Bdt%7D%20s(t)%20=%20s'(t)%20=%206%20-%202t%0A"> is its derivate (it shows how much the travelled distance <em>changes</em> over time). In general, for well-behaved functions, there is the relation <img src="https://latex.codecogs.com/png.latex?%0A%20%20%5Cint_a%5Eb%20f'(t)%20dt%20=%20f(b)%20-%20f(a),%0A"> which can be interpreted in the context of the example as follows: If we “sum up” (integrate) small time increments <img src="https://latex.codecogs.com/png.latex?dt"> multiplied by the velocity the ball had at this time, we should get the total distance that the ball has travelled. And indeed, <img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Balign*%7D%0A%20%20%5Cint_0%5E3%20v(t)%20dt%20&amp;=%20s(3)%20-%20s(0)%5C%5C%0A%20%20%20%20&amp;=%206%20%5Ccdot%203%20-%203%5E2%20-%200%5C%5C%0A%20%20%20%20&amp;=%209,%0A%5Cend%7Balign*%7D"> the ball has travelled 9m. And there are two equivalent ways to get the solution, via the antiderivative <img src="https://latex.codecogs.com/png.latex?%0A%20%20%5Cint_0%5E3%20(6%20-%202t)%20dt%20=%20%5Cbig%5B6t%20-%20t%5E2%5Cbig%5D_%7Bt%20=%200%7D%5E%7Bt%20=%203%7D%20=%209%0A"> and via basic geometry: The area of the triangle under the curve of <img src="https://latex.codecogs.com/png.latex?v(t)"> is <img src="https://latex.codecogs.com/png.latex?%0A%20%20%5Ctfrac%7B1%7D%7B2%7D%20%5Ccdot%203%20%5Ccdot%206%20=%209.%0A"> By whatever way we arrive there, we see that the integral of the instantaneous velocity amounts to the total travelled distance.</p>


</section>

 ]]></description>
  <category>Probability</category>
  <guid>https://nwickel.codeberg.page/statistics_snippets/posts/2026-10-15_integralvolleyball/</guid>
  <pubDate>Thu, 15 Oct 2026 22:00:00 GMT</pubDate>
</item>
<item>
  <title>Power for designs with two or more measurement points</title>
  <dc:creator>Nora Wickelmaier</dc:creator>
  <link>https://nwickel.codeberg.page/statistics_snippets/posts/2026-10-09_powerseveraltimepoints/</link>
  <description><![CDATA[ 





<section id="question-from-statistics-consulting" class="level1">
<h1>Question from statistics consulting</h1>
<blockquote class="blockquote">
<p>A few weeks ago, we met to talk about a power analysis for my pre/post study. Now, I want to add a second delayed post-test assessment one week later. Does this change my power simulation?</p>
</blockquote>
<p>Short answer: Yes, quite dramatically. ;)</p>
</section>
<section id="power-simulation-for-ancova-two-time-points" class="level1">
<h1>Power simulation for ANCOVA (two time points)</h1>
<p>This power simulation for the original pre/post design assumed that the data will be analyzed with an ANCOVA model <span class="citation" data-cites="VickersAltman01">(based on the paper by Vickers and Altman 2001)</span>. This is the original power simulation based on values found for previous data collected by this PhD student.</p>
<div class="cell" data-layout-align="center">
<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">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;">87</span></span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Draw plausible pre values and setup data frame</span></span>
<span id="cb1-4">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;">data.frame</span>(</span>
<span id="cb1-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pre =</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;">mean =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">12.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.5</span>),</span>
<span id="cb1-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">grp =</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;">rep</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;">3</span>, <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;">3</span>), <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;">"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="cb1-7">)</span>
<span id="cb1-8"></span>
<span id="cb1-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Specify the minimum relevant average treatment effects and set the remaining</span></span>
<span id="cb1-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># parameters to plausible values</span></span>
<span id="cb1-11"></span>
<span id="cb1-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># mu = mean learning outcome (expected pre-post effect in reference group)</span></span>
<span id="cb1-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># b  = slope between pre and post values</span></span>
<span id="cb1-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># a2 = expected difference between group 2 and group 1</span></span>
<span id="cb1-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># a3 = expected difference between group 3 and group 1</span></span>
<span id="cb1-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># se = sigma of ANCOVA model</span></span>
<span id="cb1-17"></span>
<span id="cb1-18">beta <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="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</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;">b =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">a2 =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">a3 =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb1-19">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;">model.matrix</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> pre <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> grp, dat) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> beta</span>
<span id="cb1-20">se <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>
<span id="cb1-21"></span>
<span id="cb1-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Estimate power</span></span>
<span id="cb1-23">pval <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;">replicate</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>, {</span>
<span id="cb1-24">  post <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> means <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>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> se)</span>
<span id="cb1-25">  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>(post <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> pre <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> grp, dat)</span>
<span id="cb1-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(m)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>coef[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grpg2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pr(&gt;|t|)"</span>]</span>
<span id="cb1-27">})</span>
<span id="cb1-28"></span>
<span id="cb1-29"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(pval <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.05</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.8015</code></pre>
</div>
</div>
<p>I will not go into detail, why this is the appropriate analysis for this design. Maybe this will become another post at some point. Read the paper by <span class="citation" data-cites="VickersAltman01">Vickers and Altman (2001)</span> in the meantime.</p>
</section>
<section id="power-simulation-for-mixed-effects-model-three-time-points" class="level1">
<h1>Power simulation for mixed-effects model (three time points)</h1>
<p>Unfortunately, when we add another time point into the mix, we now have to switch the analysis to a mixed-effects model with a group x time interaction. It was also much harder for the PhD student to term her hypothesis in terms of model parameters. So, I made her draw the expected pattern and add mean values to the plot. This is what she came up with.</p>
<div class="cell" data-layout-align="center">
<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;">3</span></span>
<span id="cb3-2"></span>
<span id="cb3-3">design <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;">id =</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;">rep</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 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;">3</span>)),</span>
<span id="cb3-4">                  <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;">factor</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>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pre"</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;">"delayed"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> n),</span>
<span id="cb3-5">                                <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;">"pre"</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;">"delayed"</span>)),</span>
<span id="cb3-6">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">grp =</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;">rep</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;">3</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;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</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;">3</span>),</span>
<span id="cb3-7">                               <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;">"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="cb3-8">)</span>
<span id="cb3-9"></span>
<span id="cb3-10"></span>
<span id="cb3-11">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;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.8</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.8</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8.5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)</span>
<span id="cb3-12"></span>
<span id="cb3-13">lattice<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;">xyplot</span>(means <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> design, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">groups =</span> grp,</span>
<span id="cb3-14">                <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;">"a"</span>,</span>
<span id="cb3-15">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</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;">10</span>,</span>
<span id="cb3-16">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">auto.key =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://nwickel.codeberg.page/statistics_snippets/posts/2026-10-09_powerseveraltimepoints/index_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>We can now use these means and the “design” data frame to calculate the fixed model parameters with</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbf%7B%5Cbeta%7D%20=%20X%5E%7B-1%7D%20%5Cmathbf%7B%5Cmu%7D%0A"></p>
<div class="cell" data-layout-align="center">
<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">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>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> grp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> design)</span>
<span id="cb4-2">(beta <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.vector</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">solve</span>(mm) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> means))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 5.5 0.0 0.0 1.3 1.0 0.0 1.7 0.0 1.5</code></pre>
</div>
</div>
<p>Now, we still need the variance terms for the random effects. For simplicity reasons, we still stick to a random intercept model here. So we need to specify a residual variance and a person variance.</p>
<div class="cell" data-layout-align="center">
<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">sp <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.5</span></span>
<span id="cb6-2">se <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></code></pre></div>
</div>
<p>The power simulation then looks something like this.</p>
<div class="cell" data-layout-align="center">
<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;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lme4"</span>)</span>
<span id="cb7-2"></span>
<span id="cb7-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;">120</span></span>
<span id="cb7-4"></span>
<span id="cb7-5">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;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id =</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;">rep</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 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;">3</span>)),</span>
<span id="cb7-6">                  <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;">factor</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>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pre"</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;">"delayed"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> n),</span>
<span id="cb7-7">                                <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;">"pre"</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;">"delayed"</span>)),</span>
<span id="cb7-8">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">grp =</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;">rep</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;">3</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;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</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;">3</span>),</span>
<span id="cb7-9">                               <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;">"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="cb7-10">)</span>
<span id="cb7-11"></span>
<span id="cb7-12"></span>
<span id="cb7-13">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>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> grp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dat)</span>
<span id="cb7-14">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;">as.vector</span>(mm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> beta)</span>
<span id="cb7-15"></span>
<span id="cb7-16"></span>
<span id="cb7-17">pval <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;">replicate</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, {</span>
<span id="cb7-18">  y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> means <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>                                        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># structural part</span></span>
<span id="cb7-19">       <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>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> sp), <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;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># person effect</span></span>
<span id="cb7-20">       <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;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> se)                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># random error</span></span>
<span id="cb7-21">  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;">lmer</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> grp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> time <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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">REML =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb7-22">  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;">lmer</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> grp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> time <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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">REML =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb7-23">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Likelihood-Ratio-Test</span></span>
<span id="cb7-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(m0, m1)[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"m1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pr(&gt;Chisq)"</span>]</span>
<span id="cb7-25">  }</span>
<span id="cb7-26">)</span>
<span id="cb7-27"></span>
<span id="cb7-28"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(pval <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.05</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.774</code></pre>
</div>
</div>
<p>For more examples how to run power simulations in R see <span class="citation" data-cites="Wickelmaier26">Wickelmaier (2026)</span>.</p>
</section>
<section id="session-info" class="level1">
<h1>Session info</h1>
<div class="cell" data-layout-align="center">
<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;">sessionInfo</span>()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>R version 4.6.1 (2026-06-24 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 26100)

Matrix products: default
  LAPACK version 3.12.1

locale:
[1] LC_COLLATE=German_Germany.utf8  LC_CTYPE=German_Germany.utf8   
[3] LC_MONETARY=German_Germany.utf8 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.utf8    

time zone: Europe/Berlin
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] lme4_2.1-0   Matrix_1.7-6

loaded via a namespace (and not attached):
 [1] digest_0.6.39     fastmap_1.2.0     xfun_0.62         lattice_0.23-1   
 [5] splines_4.6.1     knitr_1.52        htmltools_0.5.9   rmarkdown_2.32   
 [9] Rdpack_2.6.6      cli_3.6.6         nloptr_2.2.1      grid_4.6.1       
[13] reformulas_0.4.4  compiler_4.6.1    rbibutils_2.4.1   boot_1.3-32      
[17] tools_4.6.1       nlme_3.1-171      minqa_1.2.8       evaluate_1.0.5   
[21] Rcpp_1.1.2        yaml_2.3.12       otel_0.2.0        rlang_1.3.0      
[25] jsonlite_2.0.0    htmlwidgets_1.6.4 MASS_7.3-66      </code></pre>
</div>
</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">
<div id="ref-VickersAltman01" class="csl-entry">
Vickers, A. J., and D. G. Altman. 2001. <span>“Analysing Controlled Trials with Baseline and Follow up Measurements.”</span> <em>British Medical Journal</em> 323 (7321): 1123–24. <a href="https://doi.org/10.1136/bmj.323.7321.1123">https://doi.org/10.1136/bmj.323.7321.1123</a>.
</div>
<div id="ref-Wickelmaier26" class="csl-entry">
Wickelmaier, F. 2026. <span>“Simulating the Power of Statistical Tests: A Collection of <span>R</span> Examples.”</span> <em>ArXiv</em>. <a href="https://doi.org/10.48550/arXiv.2110.09836">https://doi.org/10.48550/arXiv.2110.09836</a>.
</div>
</div></section></div> ]]></description>
  <category>Mixed-effects models</category>
  <category>Power</category>
  <category>R</category>
  <guid>https://nwickel.codeberg.page/statistics_snippets/posts/2026-10-09_powerseveraltimepoints/</guid>
  <pubDate>Thu, 08 Oct 2026 22:00:00 GMT</pubDate>
</item>
<item>
  <title>Internal meta-analysis - why, when and how?</title>
  <dc:creator>Nora Wickelmaier</dc:creator>
  <link>https://nwickel.codeberg.page/statistics_snippets/posts/2026-09-30_internalmetaanalysis/</link>
  <description><![CDATA[ 





<section id="question-from-statistics-consulting" class="level1">
<h1>Question from statistics consulting</h1>
<blockquote class="blockquote">
<p>I want to report an aggregated effect size over several of my studies. Should I use a meta-analysis for that or is there a better way?</p>
</blockquote>
<p>Shortly before I started working as a statistical consultant, Daniel Lakens gave a workshop at our institute on “How to better your inference”. Unfortunately, I missed it, but a lot of the doctoral students at our institute were greatly influenced by this workshop. My guess is that this led to the practice of “internal meta-analyses” for a lot of the PhD projects (Lakens mentions it <a href="https://lakens.github.io/statistical_inferences/11-meta.html,%20for%20example">here</a>). I was suprised, when I was confronted with this practice for the first time. It seemed silly to me to do a meta-analysis for studies where one has full access to the complete data (since those studies were conducted by oneself).</p>
<p>In order to get a clearer idea about this practice, I ran a small simulation where I simulated data for several studies with different N with the same underlying effect and then ran a meta-analysis with <code>metafor</code> and a simple regression model where I added a variable <code>study</code> as a covariate.</p>
<div class="cell" data-layout-align="center">
<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>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lattice"</span>)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"metafor"</span>)</span></code></pre></div>
</div>
</section>
<section id="data-simulation" class="level1">
<h1>Data simulation</h1>
<div class="cell" data-layout-align="center">
<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;">1109</span>)</span>
<span id="cb2-2"></span>
<span id="cb2-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set effect coding to get the treatment effect for an "average" study</span></span>
<span id="cb2-4"><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>
<span id="cb2-5"></span>
<span id="cb2-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulate data for 10 studies</span></span>
<span id="cb2-7">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;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">26</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">46</span>)</span>
<span id="cb2-8">mu <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;"># mean in control group</span></span>
<span id="cb2-9">d_true <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;"># "true" effect</span></span></code></pre></div>
</div>
<p>What excatly did we do here? We assume that we conducted 10 different studies with a different number of participants ranging from 10 to 100. We have two groups: Control (<code>ctr</code>) vs.&nbsp;Treatment (<code>trt</code>) and want to compare the treatment effect (the difference in the DV between the two groups). We set the mean in the control group to <img src="https://latex.codecogs.com/png.latex?%5Cmu%20=%201"> and assume a true treatment effect <img src="https://latex.codecogs.com/png.latex?%5Cdelta_%7Btrue%7D%20=%201">.</p>
<div class="cell" data-layout-align="center">
<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">sd <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></code></pre></div>
</div>
<p>The next thing we need to decide on is the variation in the data. We set the varianz in the data to <img src="https://latex.codecogs.com/png.latex?%5Csigma%20=%201">.</p>
<div class="cell" data-layout-align="center">
<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">d_obs <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>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(n), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> d_true, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</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;">sqrt</span>(n))</span></code></pre></div>
</div>
<p>Next we draw the effect sizes for each of the studies. We assume that they come from a normal distribution with <img src="https://latex.codecogs.com/png.latex?N(%5Cmu%20=%201,%20%5Csigma%20=%0A%5Cfrac%7B2%7D%7B%5Csqrt%7Bn%7D%7D)">. Doing it like this is in line with a fixed-effect model for meta-analysis. Since we are in the “theoretical” mindframe that all studies originated in the same lab under very similar conditions this <em>might</em> be a reasonable choice? Go ahead and adjust the simulation so the underlying model corresponds to a random-effects model for meta-analysis. Do the results change? (I did not try it.)</p>
<p>I must admit that I have no good idea how to draw the effect sizes for the study. I decided to weigh the variance by <img src="https://latex.codecogs.com/png.latex?%5Cfrac%7B1%7D%7B%5Csqrt%7Bn%7D%7D"> so that for bigger <img src="https://latex.codecogs.com/png.latex?n"> the variance is smaller and, therefore, the probability to draw a value closer to the “true” value is higher. I added this later on. When you leave this out and draw all effect sizes with same variance, then the meta-analysis has a much wider confidence intervall for the estimate of the treatment effect. I guess that makes sense. Well, I guess doing it with a weighted variance is in line with a frequentist view and also with the underlying assumptions for meta-analysis.</p>
<div class="cell" data-layout-align="center">
<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">dat_list <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="cb5-2"></span>
<span id="cb5-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</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;">10</span>) {</span>
<span id="cb5-4">  dat0 <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;">id =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</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[i]),</span>
<span id="cb5-5">                     <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;">factor</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>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ctr"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trt"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> n[i] <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="cb5-6">                                    <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;">"trt"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ctr"</span>)),</span>
<span id="cb5-7">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">resp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(n[i] <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> mu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> sd),</span>
<span id="cb5-8">                              <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(n[i] <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> mu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> d_obs[i], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> sd)),</span>
<span id="cb5-9">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> n[i],</span>
<span id="cb5-10">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">study =</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;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"study"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%02d"</span>, i))))</span>
<span id="cb5-11">  dat_list[[i]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dat0</span>
<span id="cb5-12">}</span>
<span id="cb5-13"></span>
<span id="cb5-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rm</span>(dat0)</span>
<span id="cb5-15"></span>
<span id="cb5-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Integrate into one dataframe</span></span>
<span id="cb5-17">dat_all <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;">do.call</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rbind"</span>, dat_list)</span>
<span id="cb5-18"></span>
<span id="cb5-19"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bwplot</span>(resp <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;">factor</span>(group, <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;">"ctr"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trt"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> study, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dat_all,</span>
<span id="cb5-20">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layout =</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;">2</span>))</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://nwickel.codeberg.page/statistics_snippets/posts/2026-09-30_internalmetaanalysis/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="analyze-the-data" class="level1">
<h1>Analyze the data</h1>
<div class="cell" data-layout-align="center">
<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;"># Analyzing all data in one model</span></span>
<span id="cb6-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>(resp <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> dat_all)</span>
<span id="cb6-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(m1)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
lm(formula = resp ~ group, data = dat_all)

Residuals:
     Min       1Q   Median       3Q      Max 
-3.04447 -0.70303  0.01982  0.79411  2.80768 

Coefficients:
            Estimate Std. Error t value Pr(&gt;|t|)    
(Intercept)   1.4982     0.0539  27.798   &lt;2e-16 ***
group1        0.4772     0.0539   8.854   &lt;2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 1.011 on 350 degrees of freedom
Multiple R-squared:  0.183, Adjusted R-squared:  0.1806 
F-statistic: 78.39 on 1 and 350 DF,  p-value: &lt; 2.2e-16</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;"># Estimated treatment effect</span></span>
<span id="cb8-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(m1)[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"group1"</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></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>   group1 
0.9543521 </code></pre>
</div>
</div>
<p>This ignores completely that the data were collected in different studies. Let us add a covariate <code>study</code> to the model and see what the estimated treatment effect for an “average” study looks like.</p>
<div class="cell" data-layout-align="center">
<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;"># Adding study as a covariate to the model</span></span>
<span id="cb10-2">m2 <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>(resp <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> study, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dat_all)</span>
<span id="cb10-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(m2)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
lm(formula = resp ~ group + study, data = dat_all)

Residuals:
     Min       1Q   Median       3Q      Max 
-2.78958 -0.70356  0.00348  0.72203  2.72573 

Coefficients:
            Estimate Std. Error t value Pr(&gt;|t|)    
(Intercept)  1.44624    0.06319  22.888  &lt; 2e-16 ***
group1       0.47718    0.05280   9.038  &lt; 2e-16 ***
study1      -0.44367    0.20795  -2.133  0.03360 *  
study2       0.10850    0.14033   0.773  0.43997    
study3       0.49978    0.17367   2.878  0.00426 ** 
study4       0.24583    0.18490   1.330  0.18455    
study5      -0.01158    0.28722  -0.040  0.96786    
study6       0.22640    0.20795   1.089  0.27706    
study7      -0.31949    0.17367  -1.840  0.06669 .  
study8       0.17633    0.10883   1.620  0.10609    
study9      -0.38748    0.20795  -1.863  0.06328 .  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.9906 on 341 degrees of freedom
Multiple R-squared:  0.2361,    Adjusted R-squared:  0.2137 
F-statistic: 10.54 on 10 and 341 DF,  p-value: 1.442e-15</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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Estimated treatment effect</span></span>
<span id="cb12-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(m2)[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"group1"</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></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>   group1 
0.9543521 </code></pre>
</div>
</div>
<p>The effect is exactly the same… Is this always true? Why would I even add the covariate then?</p>
<div class="cell" data-layout-align="center">
<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;"># Doing a metaanalysis on these data</span></span>
<span id="cb14-2">datm    <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;">aggregate</span>(resp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> study <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> dat_all, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FUN =</span> mean)</span>
<span id="cb14-3">datm<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sd <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;">aggregate</span>(resp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> study <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> dat_all, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FUN =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sd"</span>)[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]</span>
<span id="cb14-4">datm<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>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;">aggregate</span>(resp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> study <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> dat_all, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FUN =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"length"</span>)[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]</span>
<span id="cb14-5"></span>
<span id="cb14-6">es <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;">escalc</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MD"</span>,</span>
<span id="cb14-7">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">m1i  =</span> datm<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>resp[datm<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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trt"</span>],</span>
<span id="cb14-8">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">m2i  =</span> datm<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>resp[datm<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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ctr"</span>],</span>
<span id="cb14-9">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd1i =</span> datm<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sd[datm<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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trt"</span>],</span>
<span id="cb14-10">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd2i =</span> datm<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sd[datm<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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ctr"</span>],</span>
<span id="cb14-11">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n1i  =</span> datm<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n[datm<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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trt"</span>],</span>
<span id="cb14-12">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n2i  =</span> datm<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n[datm<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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ctr"</span>])</span>
<span id="cb14-13"></span>
<span id="cb14-14">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;">rma</span>(yi, vi, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>es)</span>
<span id="cb14-15"></span>
<span id="cb14-16"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">forest</span>(res)</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://nwickel.codeberg.page/statistics_snippets/posts/2026-09-30_internalmetaanalysis/index_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="conclusions" class="level1">
<h1>Conclusions</h1>
<div class="cell" data-layout-align="center">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compare true and estimated treatment effects</span></span>
<span id="cb15-2">est <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>(d_true, res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>beta, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(m2)[<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;">2</span>)</span>
<span id="cb15-3">cilb <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="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ci.lb, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">confint</span>(m2)[<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="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="cb15-4">ciub <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="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ci.ub, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">confint</span>(m2)[<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> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb15-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(est, cilb, ciub)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>             est      cilb     ciub
       1.0000000        NA       NA
       0.9767077 0.7728563 1.180559
group1 0.9543521 0.7466444 1.162060</code></pre>
</div>
</div>
<div class="cell" data-layout-align="center">
<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;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mai =</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;">6</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</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="at" style="color: #657422;
background-color: null;
font-style: inherit;">mgp =</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;">2.4</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;">0</span>))</span>
<span id="cb17-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(est, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axes =</span> F, <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="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;">3</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pch =</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;">cex =</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;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, </span>
<span id="cb17-3">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mean difference"</span>)</span>
<span id="cb17-4"></span>
<span id="cb17-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrows</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;">3</span>, cilb, <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;">3</span>, ciub, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">code =</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;">05</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>)</span>
<span id="cb17-6"></span>
<span id="cb17-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">axis</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;">at =</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;">3</span>, <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;">"true"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ma"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>))</span>
<span id="cb17-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">axis</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb17-9"></span>
<span id="cb17-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</span> est[<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;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb17-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</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;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)</span>
<span id="cb17-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span>()</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://nwickel.codeberg.page/statistics_snippets/posts/2026-09-30_internalmetaanalysis/index_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="480"></p>
</figure>
</div>
</div>
</div>
<p>The results (for this one draw of 10 studies!) show that the meta-analysis and the regression model with study as covariate show pretty similar estimates for the treatment effect. The confidence intervalls look also similar. I guess, the main advantage of the meta-analysis is the beautiful plot that you can create with metafor out of the box… I am still not sure why we even need the covariate in this. Guess, I have to think about this some more.</p>
</section>
<section id="do-it-ten-times-for-10-studies" class="level1">
<h1>Do it ten times for 10 studies</h1>
<p>I repeated the code from above ten times to get an idea how stable the results are. Here are the plots. Looks like the estimate obtained via meta-analysis is a bit more “uncertain”. Guess, that is a good thing.</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://nwickel.codeberg.page/statistics_snippets/posts/2026-09-30_internalmetaanalysis/index_files/figure-html/unnamed-chunk-12-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="1152"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="session-info" class="level1">
<h1>Session info</h1>
<div class="cell" data-layout-align="center">
<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sessionInfo</span>()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>R version 4.6.1 (2026-06-24 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 26100)

Matrix products: default
  LAPACK version 3.12.1

locale:
[1] LC_COLLATE=German_Germany.utf8  LC_CTYPE=German_Germany.utf8   
[3] LC_MONETARY=German_Germany.utf8 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.utf8    

time zone: Europe/Berlin
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] metafor_5.2-1       numDeriv_2016.8-1.1 metadat_1.6-0      
[4] Matrix_1.7-6        lattice_0.23-1     

loaded via a namespace (and not attached):
 [1] digest_0.6.39     fastmap_1.2.0     xfun_0.62         knitr_1.52       
 [5] htmltools_0.5.9   rmarkdown_2.32    mathjaxr_2.0-0    cli_3.6.6        
 [9] grid_4.6.1        compiler_4.6.1    tools_4.6.1       nlme_3.1-171     
[13] evaluate_1.0.5    yaml_2.3.12       otel_0.2.0        rlang_1.3.0      
[17] jsonlite_2.0.0    htmlwidgets_1.6.4</code></pre>
</div>
</div>


</section>

 ]]></description>
  <category>Meta-analysis</category>
  <category>R</category>
  <guid>https://nwickel.codeberg.page/statistics_snippets/posts/2026-09-30_internalmetaanalysis/</guid>
  <pubDate>Tue, 29 Sep 2026 22:00:00 GMT</pubDate>
</item>
<item>
  <title>Conditional testing when normality assumption is violated</title>
  <dc:creator>Nora Wickelmaier</dc:creator>
  <link>https://nwickel.codeberg.page/statistics_snippets/posts/2026-09-25-conditionaltesting/</link>
  <description><![CDATA[ 





<p>In the era of preregistration and after the <a href="https://statmodeling.stat.columbia.edu/2016/09/21/what-has-happened-down-here-is-the-winds-have-changed/">replication crisis</a> in psychology (and, of course, other disciplines), I am inclined to use the sentence “No analysis decisions after we have seen the data” quite often during statistical consultations. If you want to read up on this, read the <a href="https://stat.columbia.edu/~gelman/research/unpublished/forking.pdf">paper by Gelman and Loken</a>.</p>
<p>But this is very different from what most people learned in their statistics courses, where they have been taught to first test, e.g., for variance homogeneity and <em>then</em> decide on an apropriate test: either a t-test or a Welch test, for example. If you want to play around with this, check out this shiny app: <a href="https://apps.mathpsy.uni-tuebingen.de/fw/condtest/">https://apps.mathpsy.uni-tuebingen.de/fw/condtest/</a>. It is quite instructive and strongly suggests that one should always perform a Welch test, independently of violations of variance homogeneity.</p>
<p>A common reason for people to seek statistics consultation from me, is that the normality assumption of their data is violated and now they wonder if their planned (and usually preregistered) analysis is still the best choice. Ususally, we are talking analysis of variance or regression here.</p>
<p>Based on the shiny app mentioned above, I thought, I could maybe try out what happens if the normality assumption is violated.</p>
<section id="simulate-skewed-data" class="level1">
<h1>Simulate skewed data</h1>
<p>I found an R package that lets you simulate data from a “skew normal distribution” (I guess, that’s an oxymoron, but what the heck): <a href="https://CRAN.R-project.org/package=fGarch"><code>fGarch</code></a>.</p>
<p>With the function <code>fGarch::rsnorm()</code> we can simulate skewed data. The argument <code>xi</code> controls the extend of the skewness. Let us have a look.</p>
<div class="cell" data-layout-align="center">
<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>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fGarch"</span>)</span>
<span id="cb1-2"></span>
<span id="cb1-3">xi <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>
<span id="cb1-4"></span>
<span id="cb1-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;">rsnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</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;">xi =</span> xi)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hist</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">freq =</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;">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;">0</span>, .<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;">border =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">curve</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dsnorm</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xi =</span> xi), <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;">2.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">add =</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;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://nwickel.codeberg.page/statistics_snippets/posts/2026-09-25-conditionaltesting/index_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="528"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="testing-for-normality" class="level1">
<h1>Testing for normality</h1>
<p>The first step in an analysis procedure like this is to decide which test to use when testing for normality. There are many different options. Honestly, I have never given too much thought to this. I learned at some point that most of these tests have bad statistical properties and luckily I was never in s situation where I had to apply these tests. Two common choices are the Kolmogorov-Smirnov test and the Shapiro-Wilk test of normality. Both are available in the stats package in R. I ran a small simulation and was a bit suprised by the results. (I am sure that shows my ignorance on the topic and that there are much better choices for testing normality. However, these are common choices that I regularly encounter in statistics consultation.)</p>
<div class="cell" data-layout-align="center">
<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">compare_normtest <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>(xi, n_group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">test =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Shapiro"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">all_skewed =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) {</span>
<span id="cb2-2">  pval <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;">replicate</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>, {</span>
<span id="cb2-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (all_skewed) {</span>
<span id="cb2-4">      resp1 <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;">rsnorm</span>(n_group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</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;">xi =</span> xi)</span>
<span id="cb2-5">    } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb2-6">      resp1 <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;">rsnorm</span>(n_group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</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;">xi =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb2-7">    }</span>
<span id="cb2-8">    resp2 <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;">rsnorm</span>(n_group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</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;">xi =</span> xi)</span>
<span id="cb2-9">    resp3 <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;">rsnorm</span>(n_group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</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;">xi =</span> xi)</span>
<span id="cb2-10">    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;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cond =</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>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"c1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"c2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"c3"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> n_group),</span>
<span id="cb2-11">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">resp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(resp1, resp2, resp3))</span>
<span id="cb2-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(test <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;">"Shapiro"</span>,</span>
<span id="cb2-13">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">shapiro.test</span>(dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>resp)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p.value,</span>
<span id="cb2-14">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ks.test</span>(dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>resp, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pnorm"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</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>p.value)</span>
<span id="cb2-15">  })</span>
<span id="cb2-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(pval <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;">05</span>)</span>
<span id="cb2-17">}</span>
<span id="cb2-18"></span>
<span id="cb2-19">comb <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;">xi =</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;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>),</span>
<span id="cb2-20">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_group =</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;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>),</span>
<span id="cb2-21">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">test =</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;">"Shapiro"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Kolmogorov-Smirnov"</span>)),</span>
<span id="cb2-22">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">all_skewed =</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;">TRUE</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>))</span>
<span id="cb2-23"></span>
<span id="cb2-24">comb<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pwr <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;">mapply</span>(compare_normtest, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_group =</span> comb<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n_group,</span>
<span id="cb2-25">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xi =</span> comb<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>xi, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">test =</span> comb<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>test, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">all_skewed =</span> comb<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>all_skewed)</span>
<span id="cb2-26"></span>
<span id="cb2-27">lattice<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;">xyplot</span>(pwr <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> n_group <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;">factor</span>(all_skewed,</span>
<span id="cb2-28">                                       <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;">"one normal group"</span>,</span>
<span id="cb2-29">                                                  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"all groups skewed"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> test,</span>
<span id="cb2-30">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> comb, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">groups =</span> xi,</span>
<span id="cb2-31">                <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;">c</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;">"b"</span>),</span>
<span id="cb2-32">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N in each group"</span>,</span>
<span id="cb2-33">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Power"</span>,</span>
<span id="cb2-34">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">auto.key =</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;">space =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"top"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">columns =</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;">title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(xi)))</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://nwickel.codeberg.page/statistics_snippets/posts/2026-09-25-conditionaltesting/index_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="528"></p>
</figure>
</div>
</div>
</div>
<p>Based on this plot I am inclined to go with the Shapiro test here. But go on and try it out with other tests testing the normality assumption yourself!</p>
</section>
<section id="one-way-anova-vs-kruskal-wallis-rank-sum-test" class="level1">
<h1>One-way ANOVA vs Kruskal-Wallis rank sum test</h1>
<p>Next, I will simulate data for three independent groups. I will simulate data for each of these groups separately, but always assuming the same size of skewness and the same sample size N. I will let <img src="https://latex.codecogs.com/png.latex?%5Cxi"> vary from 1 (normal distribution) to 10 (very skewed) and N take the values 10, 100, and 500. To each data set, we will fit an ANOVA with <code>aov()</code>, a Kruskal-Wallis rank sum test with <code>kruskal.test()</code> which is a nonparametric test meaning its only assumption is that the data are continuous but not which distribution it follows. Additionally, we will first test for normality of the data and then decide if we do an ANOVA or a Kruskal-Wallis rank sum test based on the results. Hence, we condition our analysis on the data.</p>
<div class="cell" data-layout-align="center">
<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">compare_parametric <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>(xi, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_group =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, test, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">all_skewed =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) {</span>
<span id="cb3-2">  pval <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;">replicate</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, {</span>
<span id="cb3-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (all_skewed) {</span>
<span id="cb3-4">      resp1 <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;">rsnorm</span>(n_group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</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;">xi =</span> xi)</span>
<span id="cb3-5">    } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb3-6">      resp1 <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;">rsnorm</span>(n_group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</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;">xi =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb3-7">    }</span>
<span id="cb3-8">    resp2 <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;">rsnorm</span>(n_group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</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;">xi =</span> xi)</span>
<span id="cb3-9">    resp3 <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;">rsnorm</span>(n_group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</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;">xi =</span> xi)</span>
<span id="cb3-10"></span>
<span id="cb3-11">    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;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cond =</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;">rep</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;">"c1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"c2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"c2"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> n_group)),</span>
<span id="cb3-12">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">resp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(resp1, resp2, resp3))</span>
<span id="cb3-13">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (test <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;">"ANOVA"</span>) {</span>
<span id="cb3-14">      <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>(resp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> cond, dat))[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cond"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pr(&gt;F)"</span>]</span>
<span id="cb3-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> (test <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;">"Kruskal"</span>) {</span>
<span id="cb3-16">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kruskal.test</span>(resp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> cond, dat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p.value</span>
<span id="cb3-17">    } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb3-18">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">shapiro.test</span>(dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>resp)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p.value <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;">05</span>,</span>
<span id="cb3-19">             <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>(resp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> cond, dat))[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cond"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pr(&gt;F)"</span>],</span>
<span id="cb3-20">             <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kruskal.test</span>(resp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> cond, dat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p.value)</span>
<span id="cb3-21">    }</span>
<span id="cb3-22">  })</span>
<span id="cb3-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(pval <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;">05</span>)</span>
<span id="cb3-24">}</span>
<span id="cb3-25"></span>
<span id="cb3-26">comb <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;">xi =</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;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_group =</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;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>),</span>
<span id="cb3-27">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">test =</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;">"ANOVA"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Kruskal"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Conditioned"</span>),</span>
<span id="cb3-28">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">all_skewed =</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;">TRUE</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>))</span>
<span id="cb3-29"></span>
<span id="cb3-30">comb<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pwr <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;">mapply</span>(compare_parametric,</span>
<span id="cb3-31">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xi =</span> comb<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>xi,</span>
<span id="cb3-32">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_group =</span> comb<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n_group,</span>
<span id="cb3-33">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">test =</span> comb<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>test,</span>
<span id="cb3-34">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">all_skewed =</span> comb<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>all_skewed)</span>
<span id="cb3-35"></span>
<span id="cb3-36">lattice<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;">xyplot</span>(pwr <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> xi <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;">factor</span>(all_skewed,</span>
<span id="cb3-37">                                  <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;">"one normal group"</span>,</span>
<span id="cb3-38">                                             <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"all groups skewed"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-39">                           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(n_group,</span>
<span id="cb3-40">                                  <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;">paste</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N ="</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(n_group))),</span>
<span id="cb3-41">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> comb,</span>
<span id="cb3-42">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">groups =</span> test,</span>
<span id="cb3-43">                <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;">c</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;">"b"</span>),</span>
<span id="cb3-44">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(xi),</span>
<span id="cb3-45">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Type I Error Rate"</span>,</span>
<span id="cb3-46">                <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;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>),</span>
<span id="cb3-47">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">auto.key =</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;">space =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"top"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">columns =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb3-48">                                <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;">"N in each group"</span>))</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://nwickel.codeberg.page/statistics_snippets/posts/2026-09-25-conditionaltesting/index_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="528"></p>
</figure>
</div>
</div>
</div>
<p>So what did we do here again? We tested for a group effect for a one-way design. We did not have an effect here, so the Type I Error Rate should not exceed <img src="https://latex.codecogs.com/png.latex?%5Calpha%20=%200.05">, no matter what sample size we have.</p>
<p>I would have expected that the error rate would actually increase for the ANOVA with increasing violation of the normality assumption (that means increasing <img src="https://latex.codecogs.com/png.latex?%5Cxi">). We can see that this is not the case. For the ANOVA the error rate stays at 5%, as it should. Interestingly, this is also TRUE for the Kruskal-Wallis rank sum test and for the conditioned test, when data in all groups is skewed. If data in one group is normal, then the Type I Error Rate increases for the Kruskal-Wallis rank sum test and this also implies an increase for the conditioned test. This is very pronounced for large N. I think this is very suprising and do not have an explanation for it.</p>
<p>But we do have an overall result: Stick to the ANOVA that you preregistered and don’t bother with testing for normality.</p>
<p>I only looked at one way to violate normality here. Another way could be to look at bimodal distributions or draw data from other distributions than the normal distribution. I do expect the results to look similar, but have not tried it, yet.</p>
</section>
<section id="session-info" class="level1">
<h1>Session info</h1>
<div class="cell" data-layout-align="center">
<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;">sessionInfo</span>()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>R version 4.6.1 (2026-06-24 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 26100)

Matrix products: default
  LAPACK version 3.12.1

locale:
[1] LC_COLLATE=German_Germany.utf8  LC_CTYPE=German_Germany.utf8   
[3] LC_MONETARY=German_Germany.utf8 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.utf8    

time zone: Europe/Berlin
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] fGarch_4052.93

loaded via a namespace (and not attached):
 [1] fBasics_4052.98     digest_0.6.39       fastmap_1.2.0      
 [4] Matrix_1.7-6        xfun_0.62           lattice_0.23-1     
 [7] spatial_7.3-19      knitr_1.52          gbutils_0.5.1      
[10] htmltools_0.5.9     rmarkdown_2.32      Rdpack_2.6.6       
[13] cli_3.6.6           cvar_0.6.1          grid_4.6.1         
[16] timeDate_4052.112   compiler_4.6.1      rbibutils_2.4.1    
[19] tools_4.6.1         evaluate_1.0.5      yaml_2.3.12        
[22] otel_0.2.0          timeSeries_4052.112 rlang_1.3.0        
[25] jsonlite_2.0.0      htmlwidgets_1.6.4  </code></pre>
</div>
</div>


</section>

 ]]></description>
  <category>Categorical predictors</category>
  <category>Power</category>
  <category>R</category>
  <guid>https://nwickel.codeberg.page/statistics_snippets/posts/2026-09-25-conditionaltesting/</guid>
  <pubDate>Thu, 24 Sep 2026 22:00:00 GMT</pubDate>
</item>
<item>
  <title>Testing a group effect when sub-groups are present</title>
  <dc:creator>Nora Wickelmaier</dc:creator>
  <link>https://nwickel.codeberg.page/statistics_snippets/posts/2026-09-18-lmmvsttest/</link>
  <description><![CDATA[ 





<section id="question-from-statistics-consulting" class="level1">
<h1>Question from statistics consulting</h1>
<blockquote class="blockquote">
<p>Should I use a mixed model, wenn I have subgroups in my between groups?</p>
</blockquote>
<p>The dataframe we are considering here has the following structure. We have two between groups to which we assign students from 18 different classes. Due to organizational reasons, it is easier to assign all students from one class to one condition. The assignment of the classes is random.</p>
<div class="cell" data-layout-align="center">
<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;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1409</span>)</span>
<span id="cb1-2"></span>
<span id="cb1-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;">90</span></span>
<span id="cb1-4"></span>
<span id="cb1-5">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;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</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="cb1-6">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">class =</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;">rep</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;">18</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;">5</span>)),</span>
<span id="cb1-7">                  <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;">factor</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>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ctr"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trt"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</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;">40</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>))))</span>
<span id="cb1-8"></span>
<span id="cb1-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xtabs</span>( <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> class <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> dat)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>     group
class ctr trt
   1    5   0
   2    5   0
   3    5   0
   4    5   0
   5    5   0
   6    5   0
   7    5   0
   8    5   0
   9    0   5
   10   0   5
   11   0   5
   12   0   5
   13   0   5
   14   0   5
   15   0   5
   16   0   5
   17   0   5
   18   0   5</code></pre>
</div>
</div>
<p>The question I was asked was, if class should be considered as a random effect in a mixed-effects model or if an independent t-test can be used to analyze these data.</p>
<p>This is a great example how simulation can help answering these kind of questions. Let us assume that the data generating process in this case actually is a mixed-effects model with a random intercept for class. We simulate data for one dataset and test the group effect with the following code.</p>
<div class="cell" data-layout-align="center">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># One simulated dataset</span></span>
<span id="cb3-2"></span>
<span id="cb3-3">y <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;">5</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.2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</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;">dummy</span>(dat<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># fixed effects</span></span>
<span id="cb3-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)[dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>class] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># random intercept for class</span></span>
<span id="cb3-5">  <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;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># random error</span></span>
<span id="cb3-6"></span>
<span id="cb3-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aggregate</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> group, dat, mean)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>  group      trt
1   ctr 4.890459
2   trt 5.265856</code></pre>
</div>
<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;"># Testing the group effect</span></span>
<span id="cb5-2"></span>
<span id="cb5-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ...using mixed-effects models</span></span>
<span id="cb5-4">m0 <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;">lmer</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="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> class), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">REML =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb5-5">m1 <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;">lmer</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> (<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> class), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">REML =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb5-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(m1)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Linear mixed model fit by REML ['lmerMod']
Formula: y ~ group + (1 | class)
   Data: dat

REML criterion at convergence: 167.2

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-3.1897 -0.4888 -0.0401  0.6266  2.3779 

Random effects:
 Groups   Name        Variance Std.Dev.
 class    (Intercept) 0.2628   0.5127  
 Residual             0.2587   0.5087  
Number of obs: 90, groups:  class, 18

Fixed effects:
            Estimate Std. Error t value
(Intercept)   4.8905     0.1983  24.661
grouptrt      0.3754     0.2661   1.411

Correlation of Fixed Effects:
         (Intr)
grouptrt -0.745</code></pre>
</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;">anova</span>(m0, m1)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Data: dat
Models:
m0: y ~ 1 + (1 | class)
m1: y ~ group + (1 | class)
   npar    AIC    BIC  logLik -2*log(L) Chisq Df Pr(&gt;Chisq)
m0    3 172.22 179.72 -83.108    166.22                    
m1    4 172.10 182.10 -82.053    164.10 2.111  1     0.1462</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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ...using an independent t-test</span></span>
<span id="cb9-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t.test</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> dat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">var.equal =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
    Two Sample t-test

data:  y by group
t = -2.5084, df = 88, p-value = 0.01396
alternative hypothesis: true difference in means between group ctr and group trt is not equal to 0
95 percent confidence interval:
 -0.6728038 -0.0779896
sample estimates:
mean in group ctr mean in group trt 
         4.890459          5.265856 </code></pre>
</div>
</div>
<p>When we assume that class indeed introduces some variability into the data, then the results of the two analysis methods differ, of course. The t-test makes the assumption that all data points are indepedent, which is not the case when we have an effect of class.</p>
<p>Let us write a function so that we can investigate a bit more systematically.</p>
</section>
<section id="power-simulation" class="level1">
<h1>Power simulation</h1>
<div class="cell" data-layout-align="center">
<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">pwrFun <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>(d, model, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">raneff_class =</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;">nrep =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>) {</span>
<span id="cb11-2">  pval <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;">replicate</span>(nrep, {</span>
<span id="cb11-3">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># switch between data generating moel</span></span>
<span id="cb11-4">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (raneff_class) {</span>
<span id="cb11-5">       y <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;">5</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> d <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</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;">dummy</span>(dat<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>
<span id="cb11-6">         <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)[dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>class] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-7">         <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb11-8">    } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb11-9">       y <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;">5</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> d <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</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;">dummy</span>(dat<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb11-10">    }</span>
<span id="cb11-11">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># switch between analysis model</span></span>
<span id="cb11-12">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (model <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;">"LMM"</span>) {</span>
<span id="cb11-13">       m0 <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;">lmer</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="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> class), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">REML =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb11-14">       m1 <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;">lmer</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> (<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> class), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">REML =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb11-15">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(m0, m1)[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"m1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pr(&gt;Chisq)"</span>]</span>
<span id="cb11-16">    } <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> (model <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;">"ttest"</span>) {</span>
<span id="cb11-17">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t.test</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> dat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">var.equal =</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>p.value</span>
<span id="cb11-18">    }</span>
<span id="cb11-19">    }</span>
<span id="cb11-20">  )</span>
<span id="cb11-21">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># calculate power</span></span>
<span id="cb11-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(pval <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.05</span>)</span>
<span id="cb11-23">}</span>
<span id="cb11-24"></span>
<span id="cb11-25"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pwrFun</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</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;">"LMM"</span>,   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">raneff_class =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.151</code></pre>
</div>
<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;">pwrFun</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</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;">"ttest"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">raneff_class =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.404</code></pre>
</div>
</div>
<p>We can see that the power to detect a group effect of 0.2 for <img src="https://latex.codecogs.com/png.latex?%5Calpha%20=%200.05"> for the LMM and the t-test differs quite a bit when the data generating model includes a random intercept for class.</p>
<div class="cell" data-layout-align="center">
<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;">pwrFun</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</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;">"LMM"</span>,   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">raneff_class =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.415</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;">pwrFun</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</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;">"ttest"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">raneff_class =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.434</code></pre>
</div>
</div>
<p>If we generate the data without a random intercept for class – meaning the model does not know anything of the classes – then the power for the t-test and the LMM look very similar.</p>
<section id="power-curves" class="level2">
<h2 class="anchored" data-anchor-id="power-curves">Power curves</h2>
<p>With the function above, we can now look at different combinations of the effect size and the data generating process.</p>
<div class="cell" data-layout-align="center">
<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">comb <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;">d =</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.2</span>, <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.6</span>),</span>
<span id="cb19-2">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">raneff_class =</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;">TRUE</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>),</span>
<span id="cb19-3">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</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;">"LMM"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ttest"</span>), <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;">"ttest"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LMM"</span>)))</span>
<span id="cb19-4"></span>
<span id="cb19-5">comb<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pwr <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;">mapply</span>(pwrFun,</span>
<span id="cb19-6">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">d =</span> comb<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>d,</span>
<span id="cb19-7">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> comb<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>model,</span>
<span id="cb19-8">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">raneff_class =</span> comb<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>raneff_class)</span>
<span id="cb19-9"></span>
<span id="cb19-10">lattice<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;">xyplot</span>(pwr <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</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;">factor</span>(raneff_class,</span>
<span id="cb19-11">                                 <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;">"w/o class effect"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"w class effect"</span>)),</span>
<span id="cb19-12">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> comb,</span>
<span id="cb19-13">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">groups =</span> model,</span>
<span id="cb19-14">                <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;">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;">"g"</span>),</span>
<span id="cb19-15">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">auto.key =</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;">corner =</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;">0</span>)))</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://nwickel.codeberg.page/statistics_snippets/posts/2026-09-18-lmmvsttest/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The plot shows that when we generate the data without a random intercept for class, the power for different effect sizes is the same for both analysis methods. But if we generate the data with a random intercept for class, then the power for the t-test is systematically overestimated – not only when an effect is present, but also for <img src="https://latex.codecogs.com/png.latex?d%20=%200">. This means we find a lot of false positives when we neglect to include class as a random intercept into the model. Hence, the question to the answer above is: “Yes, you should!”</p>
</section>
</section>
<section id="session-info" class="level1">
<h1>Session info</h1>
<div class="cell" data-layout-align="center">
<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;">sessionInfo</span>()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>R version 4.6.1 (2026-06-24 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 26100)

Matrix products: default
  LAPACK version 3.12.1

locale:
[1] LC_COLLATE=German_Germany.utf8  LC_CTYPE=German_Germany.utf8   
[3] LC_MONETARY=German_Germany.utf8 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.utf8    

time zone: Europe/Berlin
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] lme4_2.1-0        digest_0.6.39     fastmap_1.2.0     Matrix_1.7-6     
 [5] xfun_0.62         lattice_0.23-1    splines_4.6.1     knitr_1.52       
 [9] htmltools_0.5.9   rmarkdown_2.32    Rdpack_2.6.6      cli_3.6.6        
[13] nloptr_2.2.1      grid_4.6.1        reformulas_0.4.4  compiler_4.6.1   
[17] rbibutils_2.4.1   boot_1.3-32       tools_4.6.1       nlme_3.1-171     
[21] minqa_1.2.8       evaluate_1.0.5    Rcpp_1.1.2        yaml_2.3.12      
[25] otel_0.2.0        rlang_1.3.0       jsonlite_2.0.0    htmlwidgets_1.6.4
[29] MASS_7.3-66      </code></pre>
</div>
</div>


</section>

 ]]></description>
  <category>Mixed-effects models</category>
  <category>R</category>
  <category>Power</category>
  <guid>https://nwickel.codeberg.page/statistics_snippets/posts/2026-09-18-lmmvsttest/</guid>
  <pubDate>Thu, 17 Sep 2026 22:00:00 GMT</pubDate>
</item>
<item>
  <title>Minimum requirements for reproducible R scripts</title>
  <dc:creator>Nora Wickelmaier</dc:creator>
  <link>https://nwickel.codeberg.page/statistics_snippets/posts/2026-09-01-reproscripts/</link>
  <description><![CDATA[ 





<p>This is a collection of some simple steps to create an R script that you can share with others. The tips provided here will not make your analysis reproducible in a strict sense. But I often feel that “introductory” steps for reproducible scripts are too advanced when you are still at the very beginning of your reproducibility journey. I highly recommend to learn more about computational reproducibility, especially version control, but I wanted to provide some tips for just making it possible to share a script with a single analysis, e.g., to submit together with a research paper and a single data set.</p>
<section id="a-single-well-formatted-r-script-with-a-header" class="level1">
<h1>A single well formatted R script with a header</h1>
<p>If you just want to provide the analysis code for a single research paper then a single R script is often enough. This R script will include only the code needed to exactly reproduce the results presented in your paper. You will probably have done more analyses, e.g., some cross checks on your data or you looked at some contingency tables or similar. However, if those did not end up in your paper, do not include them in this script.</p>
<p>You should add a header to this script which contains some meta data. This is a simple example:</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;"># author_year_journal.R</span></span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Analysis accompanying Author (Year). Title of my paper. *Journal*, Vol., pages</span></span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#</span></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Input:  mydata.csv</span></span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Output: figure1.png</span></span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#         figure2.png</span></span>
<span id="cb1-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#</span></span>
<span id="cb1-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Contents: (1) Data preparation</span></span>
<span id="cb1-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#           (2) Descriptive statistics</span></span>
<span id="cb1-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#           (3) Hypothesis tests</span></span>
<span id="cb1-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#</span></span>
<span id="cb1-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Last mod: 2026-08-31, Your Name</span></span></code></pre></div>
</div>
<p>Like I said, this script should only include the code to reproduce your results. Do not add any fancy data manipulations or similar. Rather, create a data file <code>mydata.csv</code> that is already in the correct format. If you do several analyses that need different data formats, it might make sense to do the reformatting within the script, but always consider the readability of the script. You can always write two analysis scripts for two data sets. (This is advice that reproducibilty experts will probably challenge and rightfully though. But we are not experts, yet.)</p>
</section>
<section id="load-packages-at-the-beginning-of-your-script" class="level1">
<h1>Load packages at the beginning of your script</h1>
<p>Load packages at the very beginning of your script and only once. For a reproducible script, try to use as few packages as possible. If another person wants to tun your script, it will irk them when they have to install a bunch of packages to do so.</p>
<p>We will add the following to our script right after the header.</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>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lattice"</span>)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lme4"</span>)</span></code></pre></div>
</div>
</section>
<section id="use-packagefunction" class="level1">
<h1>Use <code>package::function()</code></h1>
<p>If you use a single function from a package you might consider not loading the package, but using <code>package::function()</code> instead. The lattice package, e.g., is always pre-installed with R. If you just want to use a single function for one plot from it, there is no need to load the whole package. You can maybe just use <code>lattice::xyplot(...)</code>.</p>
<p>It is sometimes also useful to explicitly state a function call like this, even if you loaded the whole package, since it makes it explicit from which package the function comes from.</p>
</section>
<section id="follow-a-style-guide-consistent-style" class="level1">
<h1>Follow a style guide / consistent style</h1>
<p>R has no mandatory or commonly accepted style guide. (A style guide consists of certain rules or suggestions how code should be organized.) However, Hadley Wickham and Google developed style guides which are now widely accepted:</p>
<ul>
<li><a href="https://google.github.io/styleguide/Rguide.html">https://google.github.io/styleguide/Rguide.html</a></li>
<li><a href="https://style.tidyverse.org/">https://style.tidyverse.org/</a></li>
</ul>
<p>It is always a good idea to follow a style guide and not “create” your own rules (if you deviate, be consistent!). A style guide helps with</p>
<ul>
<li>Keeping code clean which is easier to read and interpret</li>
<li>Making it easier to catch and fix mistakes</li>
<li>Making it easier for others to follow and adapt your code</li>
<li>Preventing possible problems, e.g., avoiding dots in function names</li>
</ul>
<p>The following rules are based on the style guide by Hadley Wickham. I highly suggest that you follow these rules in (all) your scripts.</p>
<section id="spacing" class="level3">
<h3 class="anchored" data-anchor-id="spacing">Spacing</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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Good</span></span>
<span id="cb3-2">x[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Bad</span></span>
<span id="cb3-5">x[,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb3-6">x[ ,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb3-7">x[ , <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb3-8"></span>
<span id="cb3-9"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Parantheses</span></span>
<span id="cb3-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Good</span></span>
<span id="cb3-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(x, <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="cb3-12"></span>
<span id="cb3-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Bad</span></span>
<span id="cb3-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span> (x, <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="cb3-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>( x, <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="cb3-16"></span>
<span id="cb3-17"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Infix operators</span></span>
<span id="cb3-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Good</span></span>
<span id="cb3-19">height <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (feet <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;">12</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> inches</span>
<span id="cb3-20"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(x, <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="cb3-21"></span>
<span id="cb3-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Bad</span></span>
<span id="cb3-23">height<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span>feet<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;">12</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>inches</span>
<span id="cb3-24"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(x, <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></code></pre></div>
</div>
</section>
<section id="exceptions-which-should-never-be-surrounded-by-spaces" class="level3">
<h3 class="anchored" data-anchor-id="exceptions-which-should-never-be-surrounded-by-spaces">Exceptions, which should never be surrounded by spaces</h3>
<p><code>::</code>, <code>:::</code>, <code>$</code>, <code>@</code>, <code>[</code>, <code>[[</code>, <code>^</code>, <code>-</code>, <code>+</code>, and <code>:</code></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;"># Good</span></span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</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;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</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>
<span id="cb4-3">df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>z</span>
<span id="cb4-4">x <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><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb4-5"></span>
<span id="cb4-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Bad</span></span>
<span id="cb4-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</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;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</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>
<span id="cb4-8">df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span> z</span>
<span id="cb4-9">x <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> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb4-10"></span>
<span id="cb4-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Good</span></span>
<span id="cb4-12">package?stats</span>
<span id="cb4-13">?mean</span>
<span id="cb4-14"></span>
<span id="cb4-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Bad</span></span>
<span id="cb4-16">package ? stats</span>
<span id="cb4-17">? mean</span></code></pre></div>
</div>
</section>
<section id="adding-extra-spaces-is-ok-if-it-improves-alignment-of-or--" class="level3">
<h3 class="anchored" data-anchor-id="adding-extra-spaces-is-ok-if-it-improves-alignment-of-or--">Adding extra spaces is ok if it improves alignment of <code>=</code> or <code>&lt;-</code></h3>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Good</span></span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb5-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">total =</span> a <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> b <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c,</span>
<span id="cb5-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean  =</span> (a <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> b <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n</span>
<span id="cb5-5">)</span>
<span id="cb5-6"></span>
<span id="cb5-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Also fine</span></span>
<span id="cb5-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb5-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">total =</span> a <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> b <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c,</span>
<span id="cb5-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> (a <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> b <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n</span>
<span id="cb5-11">)</span></code></pre></div>
</div>
</section>
<section id="function-calls" class="level3">
<h3 class="anchored" data-anchor-id="function-calls">Function calls</h3>
<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;"># Good</span></span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</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;">10</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="cb6-3"></span>
<span id="cb6-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Bad</span></span>
<span id="cb6-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, , <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb6-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</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;">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;">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;">10</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>))</span></code></pre></div>
</div>
</section>
<section id="avoid-partial-matching-also-for-true-and-false" class="level3">
<h3 class="anchored" data-anchor-id="avoid-partial-matching-also-for-true-and-false">Avoid partial matching (also for <code>TRUE</code> and <code>FALSE</code>)</h3>
<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;"># Good</span></span>
<span id="cb7-2"><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;">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="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb7-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cut</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;">10</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</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;">11</span>))</span>
<span id="cb7-4"></span>
<span id="cb7-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Bad</span></span>
<span id="cb7-6"><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;">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="at" style="color: #657422;
background-color: null;
font-style: inherit;">t =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb7-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cut</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;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">br =</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;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>))</span></code></pre></div>
</div>
</section>
</section>
<section id="use-sensible-variable-and-function-names" class="level1">
<h1>Use sensible variable and function names</h1>
<p>Variable and function names should use only lowercase letters, numbers, and _. Use underscores (so called snake case) to separate words within a name.</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;"># Good</span></span>
<span id="cb8-2">day_one</span>
<span id="cb8-3">day_1</span>
<span id="cb8-4"></span>
<span id="cb8-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Bad</span></span>
<span id="cb8-6">DayOne</span>
<span id="cb8-7">dayone</span></code></pre></div>
</div>
<p>Generally, variable names should be nouns and function names should be verbs. Strive for names that are concise and meaningful.</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;"># Good</span></span>
<span id="cb9-2">day_one</span>
<span id="cb9-3"></span>
<span id="cb9-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Bad</span></span>
<span id="cb9-5">first_day_of_the_month</span>
<span id="cb9-6">djm1</span></code></pre></div>
</div>
<p>Avoid re-using names of common functions and variables.</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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Bad</span></span>
<span id="cb10-2">T <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb10-3">c <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;">10</span></span>
<span id="cb10-4">mean <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(x)</span></code></pre></div>
</div>
</section>
<section id="use-not" class="level1">
<h1>Use <code>|&gt;</code> not <code>%&gt;%</code></h1>
<p>I do not use a lot of funtions from the <code>tidyverse</code> since I learned R before it was around. I also do not use a lot pipes in my code – same reason. However, when you use pipes, make sure that you use the native R pipe <code>|&gt;</code> and not the tidyverse pipe <code>%&gt;%</code>. The native pipe works with base R, for the tidyverse one you need to load extra packages. This itself is a good enough reason to not use it anymore.</p>
</section>
<section id="test-your-script-with-source" class="level1">
<h1>Test your script with <code>source()</code></h1>
<p>After you wrote your short and well structured script, close your R session (in RStudio you can use the shortcut <code>Ctrl + Shift + F10</code>). In a new and clean R session navigate to your script (e.g., with <code>setwd()</code>) and then run</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;">source</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"myscript.R"</span>)</span></code></pre></div>
</div>
<p>There should be no error and all output, like figures, should be created and saved.</p>
<p>This is the first minimal step to get a script reproducible. Do not <em>ever</em> skip it.</p>
</section>
<section id="let-a-colleague-test-the-script" class="level1">
<h1>Let a colleague test the script</h1>
<p>After you sourced your script and everything looked fine, zip the folder that contains the script and the data set(s) used in it and send it together with a copy of the accompanying paper to a colleague. Ask them to review and run the code. They should be able to</p>
<ul>
<li>reproduce all results in the paper</li>
<li>create all the figures in the paper</li>
<li>understand what the code does</li>
</ul>
</section>
<section id="session-info" class="level1">
<h1>Session info</h1>
<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;">sessionInfo</span>()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>R version 4.6.1 (2026-06-24 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 26100)

Matrix products: default
  LAPACK version 3.12.1

locale:
[1] LC_COLLATE=German_Germany.utf8  LC_CTYPE=German_Germany.utf8   
[3] LC_MONETARY=German_Germany.utf8 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.utf8    

time zone: Europe/Berlin
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] htmlwidgets_1.6.4 compiler_4.6.1    fastmap_1.2.0     cli_3.6.6        
 [5] tools_4.6.1       htmltools_0.5.9   otel_0.2.0        yaml_2.3.12      
 [9] rmarkdown_2.32    knitr_1.52        jsonlite_2.0.0    xfun_0.62        
[13] digest_0.6.39     rlang_1.3.0       evaluate_1.0.5   </code></pre>
</div>
</div>


</section>

 ]]></description>
  <category>Data management</category>
  <category>R</category>
  <category>Reproducibility</category>
  <guid>https://nwickel.codeberg.page/statistics_snippets/posts/2026-09-01-reproscripts/</guid>
  <pubDate>Mon, 31 Aug 2026 22:00:00 GMT</pubDate>
</item>
<item>
  <title>Effects vs. dummy coding</title>
  <dc:creator>Nora Wickelmaier</dc:creator>
  <link>https://nwickel.codeberg.page/statistics_snippets/posts/2024-11-06-encoding/</link>
  <description><![CDATA[ 





<p>Let us look at the differences between effects and dummy coding for regression models with categorical predictors.</p>
<section id="model-with-one-categorical-factor" class="level1">
<h1>Model with one categorical factor</h1>
<p>Let us simulate some data first, for <img src="https://latex.codecogs.com/png.latex?n%20=%2010"> subjects and a group predictor with levels <code>ctr</code> for the control group and <code>trt</code> for the treatment group.</p>
<div class="cell" data-layout-align="center">
<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">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;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</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;">10</span>),</span>
<span id="cb1-2">                  <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;">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;">"ctr"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trt"</span>)),</span>
<span id="cb1-3">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">resp  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<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;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)))</span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aggregate</span>(resp <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> dat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FUN =</span> mean)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>  group       resp
1   ctr  0.2609670
2   trt -0.1312885</code></pre>
</div>
</div>
<p>Per default R uses so-called dummy coding to represent factors. This means that the reference category (here <code>ctr</code>) is set to 0 and the second category is set to 1. R takes the first factor level as reference category in dummy coding.</p>
<div class="cell" data-layout-align="center">
<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;">contrasts</span>(dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>group)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>    trt
ctr   0
trt   1</code></pre>
</div>
</div>
<p>You can check the default settings of your R session with</p>
<div class="cell" data-layout-align="center">
<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;">options</span>()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>contrasts</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>        unordered           ordered 
"contr.treatment"      "contr.poly" </code></pre>
</div>
</div>
<p>The first entry <code>"contr.treatment"</code> indicates dummy coding.</p>
<section id="linear-model-with-dummy-coding" class="level2">
<h2 class="anchored" data-anchor-id="linear-model-with-dummy-coding">Linear model with dummy coding</h2>
<p>Let us now fit a simple linear regression</p>
<p><img src="https://latex.codecogs.com/png.latex?resp%20=%20%5Cbeta_0%20+%20%5Cbeta_1%20group%20+%20%5Cvarepsilon"></p>
<p>with <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon%20%5Csim%20N(0,%5Csigma_%7B%5Cvarepsilon%7D)">.</p>
<p>(For a two-level between factor, this is equivalent to an independent t-test.)</p>
<div class="cell" data-layout-align="center">
<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">lm1 <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>(resp <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> dat)</span>
<span id="cb7-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(lm1)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
lm(formula = resp ~ group, data = dat)

Residuals:
    Min      1Q  Median      3Q     Max 
-1.5820 -0.4338 -0.1752  0.6928  1.6374 

Coefficients:
            Estimate Std. Error t value Pr(&gt;|t|)
(Intercept)   0.2610     0.4451   0.586    0.574
grouptrt     -0.3923     0.6295  -0.623    0.551

Residual standard error: 0.9953 on 8 degrees of freedom
Multiple R-squared:  0.0463,    Adjusted R-squared:  -0.07292 
F-statistic: 0.3883 on 1 and 8 DF,  p-value: 0.5505</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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(lm1)[<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;"># mean ctr</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>(Intercept) 
   0.260967 </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;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(lm1))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># mean trt</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] -0.1312885</code></pre>
</div>
</div>
<p>The interpretation of the parameters is straightforward: The Intercept <img src="https://latex.codecogs.com/png.latex?%5Cbeta_0"> is the mean response in the control (here the reference) group <img src="https://latex.codecogs.com/png.latex?(%5Cbar%20x_%7Bctr%7D%20=%20%5Cbeta_0)"> and <img src="https://latex.codecogs.com/png.latex?%5Cbeta_1"> is the difference between the control and the treatment group <img src="https://latex.codecogs.com/png.latex?(%5Cbar%20x_%7Btrt%7D%20=%20%5Cbeta_0%20+%20%5Cbeta_1)">.</p>
</section>
<section id="linear-model-with-effects-coding" class="level2">
<h2 class="anchored" data-anchor-id="linear-model-with-effects-coding">Linear model with effects coding</h2>
<p>Next, we will fit the same model with effects coding. Hereby, the reference group is coded as <img src="https://latex.codecogs.com/png.latex?-1">.</p>
<div class="cell" data-layout-align="center">
<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;">contrasts</span>(dat<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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"contr.sum"</span></span>
<span id="cb13-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrasts</span>(dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>group)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>    [,1]
ctr    1
trt   -1</code></pre>
</div>
</div>
<p>You can see that for effects coding, R takes the <em>last</em> factor level as reference category.</p>
<p>Now, let us refit the model.</p>
<div class="cell" data-layout-align="center">
<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">lm2 <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>(resp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> group, dat)</span>
<span id="cb15-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(lm2)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
lm(formula = resp ~ group, data = dat)

Residuals:
    Min      1Q  Median      3Q     Max 
-1.5820 -0.4338 -0.1752  0.6928  1.6374 

Coefficients:
            Estimate Std. Error t value Pr(&gt;|t|)
(Intercept)  0.06484    0.31473   0.206    0.842
group1       0.19613    0.31473   0.623    0.551

Residual standard error: 0.9953 on 8 degrees of freedom
Multiple R-squared:  0.0463,    Adjusted R-squared:  -0.07292 
F-statistic: 0.3883 on 1 and 8 DF,  p-value: 0.5505</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;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(lm2))    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># mean ctr</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.260967</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diff</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(lm2))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># mean trt</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>    group1 
-0.1312885 </code></pre>
</div>
</div>
<p>In this model, the Intercept <img src="https://latex.codecogs.com/png.latex?%5Cbeta_0"> is now the overall mean (often called the grand mean).</p>
<div class="cell" data-layout-align="center">
<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;">mean</span>(dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>resp)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.06483924</code></pre>
</div>
</div>
<p>And <img src="https://latex.codecogs.com/png.latex?%5Cbeta_1"> is the difference of the mean in group 1 (here the control group) to the overall mean and its negative value is the difference of group 1 (treatment group) to the overall mean.</p>
</section>
</section>
<section id="model-with-two-categorical-factors" class="level1">
<h1>Model with two categorical factors</h1>
<p>Let us again simulate some data first. This time for <img src="https://latex.codecogs.com/png.latex?n%20=%2020"> subjects, a group predictor like before and an additional factor <code>study</code> with two levels <code>s1</code> and <code>s2</code>.</p>
<div class="cell" data-layout-align="center">
<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">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;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</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;">20</span>),</span>
<span id="cb23-2">                  <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;">factor</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>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ctr"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trt"</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;">10</span>)),</span>
<span id="cb23-3">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">study =</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;">rep</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;">"s1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s2"</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;">5</span>)),</span>
<span id="cb23-4">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">resp  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</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;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb23-5">                            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<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;">mean =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>),</span>
<span id="cb23-6">                            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)))</span>
<span id="cb23-7"></span>
<span id="cb23-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aggregate</span>(resp <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> study, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FUN =</span> mean)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>  group study       resp
1   ctr    s1  1.0575195
2   trt    s1  0.9622200
3   ctr    s2  1.2211857
4   trt    s2 -0.2307511</code></pre>
</div>
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>resp)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.7525435</code></pre>
</div>
</div>
<div class="cell" data-layout-align="center">
<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">lattice<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;">xyplot</span>(resp <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> dat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">groups =</span> study, <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;">"a"</span>,</span>
<span id="cb27-2">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">auto.key =</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;">corner =</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></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://nwickel.codeberg.page/statistics_snippets/posts/2024-11-06-encoding/index_files/figure-html/interactionplot-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="528"></p>
</figure>
</div>
</div>
</div>
<p>Both factors are now dummy coded, since we did not tell R to do otherwise.</p>
<div class="cell" data-layout-align="center">
<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;">contrasts</span>(dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>group)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>    trt
ctr   0
trt   1</code></pre>
</div>
<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrasts</span>(dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>study)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>   s2
s1  0
s2  1</code></pre>
</div>
</div>
<section id="linear-model-with-dummy-coding-1" class="level2">
<h2 class="anchored" data-anchor-id="linear-model-with-dummy-coding-1">Linear model with dummy coding</h2>
<p>Let us now fit regression model with an interaction term</p>
<p><img src="https://latex.codecogs.com/png.latex?resp%20=%20%5Cbeta_0%20+%20%5Cbeta_1%20group%20+%20%5Cbeta_2%20study%20+%20%5Cbeta_3%20(group%20%5Ctimes%0Astudy)%20+%20%5Cvarepsilon"></p>
<p>with <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon%20%5Csim%20N(0,%5Csigma_%7B%5Cvarepsilon%7D)">.</p>
<p>(This is equivalent to a 2x2 ANOVA with two between factors.)</p>
<div class="cell" data-layout-align="center">
<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">lm3 <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>(resp <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> study, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dat)</span>
<span id="cb32-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(lm3)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
lm(formula = resp ~ group * study, data = dat)

Residuals:
     Min       1Q   Median       3Q      Max 
-1.59696 -0.61529  0.00399  0.56974  1.86454 

Coefficients:
                 Estimate Std. Error t value Pr(&gt;|t|)  
(Intercept)        1.0575     0.4582   2.308   0.0347 *
grouptrt          -0.0953     0.6480  -0.147   0.8849  
studys2            0.1637     0.6480   0.253   0.8038  
grouptrt:studys2  -1.3566     0.9165  -1.480   0.1582  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 1.025 on 16 degrees of freedom
Multiple R-squared:  0.2826,    Adjusted R-squared:  0.1481 
F-statistic: 2.101 on 3 and 16 DF,  p-value: 0.1404</code></pre>
</div>
</div>
<p>We can now reconstruct the mean values for all four groups with our parameters. The Intercept <img src="https://latex.codecogs.com/png.latex?%5Cbeta_0"> is now the mean response in study 1 for the control group.</p>
<div class="cell" data-layout-align="center">
<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;">coef</span>(lm3)[<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>(Intercept) 
    1.05752 </code></pre>
</div>
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(lm3)[<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;">coef</span>(lm3)[<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>(Intercept) 
    0.96222 </code></pre>
</div>
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(lm3)[<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;">coef</span>(lm3)[<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>(Intercept) 
   1.221186 </code></pre>
</div>
<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;">coef</span>(lm3)[<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;">coef</span>(lm3)[<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;">coef</span>(lm3)[<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(lm3)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># sum(coef(lm3))</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>(Intercept) 
 -0.2307511 </code></pre>
</div>
</div>
<p>With dummy coding, we can think of the dummy variables as “controlling” which parameters are entered into the mean calculations and which are not. With only categorical predictors, we get a separate equation for each of the four group means.</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Balign*%7D%0A%20%20%20%20resp_%7B11%7D%20&amp;%20=%20%5Cbeta_0%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20&amp;%20%5Ctext%7Bmean%20s1%20and%20crt%7D%20%5C%5C%0A%20%20%20%20resp_%7B12%7D%20&amp;%20=%20%5Cbeta_0%20+%20%5Cbeta_1%20%20%20%20%20%20%20%20%20%20%20%20&amp;%20%5Ctext%7Bmean%20s1%20and%20trt%7D%20%5C%5C%0A%20%20%20%20resp_%7B21%7D%20&amp;%20=%20%5Cbeta_0%20+%20%5Cbeta_2%20%20%20%20%20%20%20%20%20%20%20%20&amp;%20%5Ctext%7Bmean%20s2%20and%20crt%7D%20%5C%5C%0A%20%20%20%20resp_%7B22%7D%20&amp;%20=%20%5Cbeta_0%20+%20%5Cbeta_1%20+%20%5Cbeta_2%20+%20%5Cbeta_3%20&amp;%20%5Ctext%7Bmean%20s2%20und%20trt%7D%0A%5Cend%7Balign*%7D"></p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://nwickel.codeberg.page/statistics_snippets/posts/2024-11-06-encoding/index_files/figure-html/dummyplot-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="432"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="linear-model-with-effects-coding-1" class="level2">
<h2 class="anchored" data-anchor-id="linear-model-with-effects-coding-1">Linear model with effects coding</h2>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb42" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb42-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrasts</span>(dat<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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"contr.sum"</span></span>
<span id="cb42-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrasts</span>(dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>study) <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;">"contr.sum"</span></span>
<span id="cb42-3"></span>
<span id="cb42-4">lm4 <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>(resp <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> study, dat)</span>
<span id="cb42-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(lm4)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
lm(formula = resp ~ group * study, data = dat)

Residuals:
     Min       1Q   Median       3Q      Max 
-1.59696 -0.61529  0.00399  0.56974  1.86454 

Coefficients:
              Estimate Std. Error t value Pr(&gt;|t|)   
(Intercept)     0.7525     0.2291   3.285  0.00467 **
group1          0.3868     0.2291   1.688  0.11075   
study1          0.2573     0.2291   1.123  0.27796   
group1:study1  -0.3392     0.2291  -1.480  0.15822   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 1.025 on 16 degrees of freedom
Multiple R-squared:  0.2826,    Adjusted R-squared:  0.1481 
F-statistic: 2.101 on 3 and 16 DF,  p-value: 0.1404</code></pre>
</div>
<div class="sourceCode cell-code" id="cb44" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb44-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(lm4)[<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;">coef</span>(lm4)[<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;">coef</span>(lm4)[<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(lm4)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>(Intercept) 
    1.05752 </code></pre>
</div>
<div class="sourceCode cell-code" id="cb46" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb46-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(lm4)[<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;">coef</span>(lm4)[<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;">coef</span>(lm4)[<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(lm4)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>(Intercept) 
    0.96222 </code></pre>
</div>
<div class="sourceCode cell-code" id="cb48" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb48-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(lm4)[<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;">coef</span>(lm4)[<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;">coef</span>(lm4)[<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(lm4)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>(Intercept) 
   1.221186 </code></pre>
</div>
<div class="sourceCode cell-code" id="cb50" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb50-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(lm4)[<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;">coef</span>(lm4)[<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;">coef</span>(lm4)[<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(lm4)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># sum(coef(lm3))</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>(Intercept) 
 -0.2307511 </code></pre>
</div>
</div>
<p>With effects coding, all parameters are needed for calculating each of the group means. Only the signs change.</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Balign*%7D%0A%20%20%20%20resp_%7B11%7D%20&amp;%20=%20%5Cbeta_0%20+%20%5Cbeta_1%20+%20%5Cbeta_2%20+%20%5Cbeta_3%20&amp;%20%5Ctext%7Bmean%20s1%20and%20crt%7D%20%5C%5C%0A%20%20%20%20resp_%7B12%7D%20&amp;%20=%20%5Cbeta_0%20-%20%5Cbeta_1%20+%20%5Cbeta_2%20-%20%5Cbeta_3%20&amp;%20%5Ctext%7Bmean%20s1%20and%20trt%7D%20%5C%5C%0A%20%20%20%20resp_%7B21%7D%20&amp;%20=%20%5Cbeta_0%20+%20%5Cbeta_1%20-%20%5Cbeta_2%20-%20%5Cbeta_3%20&amp;%20%5Ctext%7Bmean%20s2%20and%20crt%7D%20%5C%5C%0A%20%20%20%20resp_%7B22%7D%20&amp;%20=%20%5Cbeta_0%20-%20%5Cbeta_1%20-%20%5Cbeta_2%20+%20%5Cbeta_3%20&amp;%20%5Ctext%7Bmean%20s2%20und%20trt%7D%0A%5Cend%7Balign*%7D"></p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://nwickel.codeberg.page/statistics_snippets/posts/2024-11-06-encoding/index_files/figure-html/effectplot-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="432"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="setting-contrasts-globally" class="level1">
<h1>Setting contrasts globally</h1>
<p>You can also change the settings for the contrast coding globally in R.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb52" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb52-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.treatment"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"contr.poly"</span>))</span>
<span id="cb52-2"></span>
<span id="cb52-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">options</span>()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>contrasts</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "contr.treatment" "contr.poly"     </code></pre>
</div>
<div class="sourceCode cell-code" id="cb54" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb54-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrasts</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</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;">3</span>))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>  2 3
1 0 0
2 1 0
3 0 1</code></pre>
</div>
<div class="sourceCode cell-code" id="cb56" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb56-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>
<span id="cb56-2"></span>
<span id="cb56-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">options</span>()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>contrasts</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "contr.sum"  "contr.poly"</code></pre>
</div>
<div class="sourceCode cell-code" id="cb58" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb58-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrasts</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</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;">3</span>))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>  [,1] [,2]
1    1    0
2    0    1
3   -1   -1</code></pre>
</div>
</div>
</section>
<section id="session-info" class="level1">
<h1>Session info</h1>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb60" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb60-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sessionInfo</span>()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>R version 4.6.1 (2026-06-24 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 26100)

Matrix products: default
  LAPACK version 3.12.1

locale:
[1] LC_COLLATE=German_Germany.utf8  LC_CTYPE=German_Germany.utf8   
[3] LC_MONETARY=German_Germany.utf8 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.utf8    

time zone: Europe/Berlin
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] htmlwidgets_1.6.4 compiler_4.6.1    fastmap_1.2.0     cli_3.6.6        
 [5] tools_4.6.1       htmltools_0.5.9   otel_0.2.0        yaml_2.3.12      
 [9] rmarkdown_2.32    grid_4.6.1        knitr_1.52        jsonlite_2.0.0   
[13] xfun_0.62         digest_0.6.39     rlang_1.3.0       lattice_0.23-1   
[17] evaluate_1.0.5   </code></pre>
</div>
</div>


</section>

 ]]></description>
  <category>Categorical predictors</category>
  <category>R</category>
  <guid>https://nwickel.codeberg.page/statistics_snippets/posts/2024-11-06-encoding/</guid>
  <pubDate>Tue, 05 Nov 2024 23:00:00 GMT</pubDate>
</item>
<item>
  <title>Understanding categorical predictors in a mixed-effects model</title>
  <dc:creator>Nora Wickelmaier</dc:creator>
  <link>https://nwickel.codeberg.page/statistics_snippets/posts/2024-11-05-design/</link>
  <description><![CDATA[ 





<p>This post is meant to give you a better understanding how to interpret parameters for categorical predictors in a mixed-effects model. Understanding what the parameters mean is helpful when simulating data, e.g., for power simulations.</p>
<section id="example" class="level1">
<h1>Example</h1>
<p>This is an example where three different media are compared to each other: A Multi-Touch Table (MTT) and two different Virtual Reality conditions (VR1 and VR2). These are all presented to every subject (within-factor <code>mediatype</code>). Additionally, we have a between-factor <code>interaction</code> with levels <code>passive</code> and <code>active</code>. Subjects can either actively interact for all media types or they can just passively look at things presented to them with the three different media types. The dependent varible is immersion. Subjects should feel more immersed when actively interacting and Virtual Reality should also lead to more immersion than a Multi-Touch Table.</p>
<div class="cell" data-layout-align="center">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Data simulation for a 2 (between) x 3 (within) design</span></span>
<span id="cb1-4"><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;">1046</span>)</span>
<span id="cb1-5"></span>
<span id="cb1-6">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></span>
<span id="cb1-7">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;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id          =</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;">rep</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 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;">3</span>)),</span>
<span id="cb1-8">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mediatype   =</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;">"MTT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"VR1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"VR2"</span>)),</span>
<span id="cb1-9">                  <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;">factor</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>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"active"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"passive"</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;">3</span>),</span>
<span id="cb1-10">                                       <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;">"passive"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"active"</span>)))</span>
<span id="cb1-11"></span>
<span id="cb1-12"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Set parameters</span></span>
<span id="cb1-13">beta  <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;">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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb1-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;">8</span></span>
<span id="cb1-15">spi   <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;">5</span></span>
<span id="cb1-16">theta <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> spi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> sigma</span>
<span id="cb1-17"></span>
<span id="cb1-18"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Simulate data</span></span>
<span id="cb1-19">dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>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;">simulate</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> mediatype <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> interaction <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> mediatype<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>interaction <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-20">                  (<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-21">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newparam =</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;">beta =</span> beta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta =</span> theta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> sigma),</span>
<span id="cb1-22">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> dat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sim_1</span>
<span id="cb1-23"></span>
<span id="cb1-24"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Visualize simulated data</span></span>
<span id="cb1-25">datm <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;">aggregate</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> mediatype <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> interaction, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FUN =</span> mean)</span>
<span id="cb1-26"></span>
<span id="cb1-27">colors <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;">"#3CB4DC"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#91C86E"</span>)</span>
<span id="cb1-28"></span>
<span id="cb1-29"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mai =</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;">8</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;">1</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;">mgp =</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;">2.4</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;">0</span>))</span>
<span id="cb1-30"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</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>(<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;">3</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> datm[datm<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>interaction <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;">"passive"</span>, ],</span>
<span id="cb1-31">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Media type"</span>,</span>
<span id="cb1-32">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Immersion"</span>,</span>
<span id="cb1-33">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim =</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;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.5</span>),</span>
<span id="cb1-34">     <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="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;">4</span>),</span>
<span id="cb1-35">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axes =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb1-36">     <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;">"n"</span>)</span>
<span id="cb1-37"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">axis</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;">at =</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;">3</span>, <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;">"MTT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"VR1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"VR2"</span>))</span>
<span id="cb1-38"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">axis</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb1-39"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span>()</span>
<span id="cb1-40"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">points</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;">jitter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(mediatype)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dat,</span>
<span id="cb1-41">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> colors[dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>interaction])</span>
<span id="cb1-42"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">points</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>(<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;">3</span>), datm[datm<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>interaction <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;">"passive"</span>, ], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> colors[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb1-43">       <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;">"b"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pch =</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;">cex =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb1-44"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">points</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>(<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;">3</span>), datm[datm<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>interaction <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;">"active"</span>, ], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> colors[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],</span>
<span id="cb1-45">       <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;">"b"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pch =</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;">cex =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb1-46"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">text</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.45</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"passive"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> colors[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb1-47"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">text</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.4</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.6</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"active"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> colors[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://nwickel.codeberg.page/statistics_snippets/posts/2024-11-05-design/index_files/figure-html/data-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="576"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="linear-mixed-effects-model" class="level1">
<h1>Linear mixed-effects model</h1>
<p>The following model will be fitted to the data.</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Balign*%7D%0Ay%20=%20&amp;%20%5Cbeta_0%20+%0A%20%20%20%20%20%20%5Cbeta_1%20mediatype_%7BVR1%7D%20+%0A%20%20%20%20%20%20%5Cbeta_2%20mediatype_%7BVR2%7D%20+%0A%20%20%20%20%20%20%5Cbeta_3%20interaction_%7Bactive%7D%20+%0A%20%20%20%20%20%20%5Cbeta_4%20(mediatype_%7BVR1%7D%20%5Ctimes%20interaction_%7Bactive%7D)%20+%20%5C%5C%0A%20%20%20%20&amp;%20%5Cbeta_5%20(mediatype_%7BVR2%7D%20%5Ctimes%20interaction_%7Bactive%7D)%20+%0A%20%20%20%20%20%20%5Cupsilon_0%20+%0A%20%20%20%20%20%20%5Cvarepsilon%0A%5Cend%7Balign*%7D"></p>
<p>with <img src="https://latex.codecogs.com/png.latex?%5Cupsilon_0%20%5Csim%20N(0,%20%5Csigma%5E2_%7B%5Cupsilon_0%7D)"> and <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon%20%5Csim%20N(0,%20%5Csigma%5E2_%7B%5Cvarepsilon%7D)"></p>
<div class="cell" data-layout-align="center">
<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="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Fit linear mixed-effects model</span></span>
<span id="cb2-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;">lmer</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> mediatype <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> interaction <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> mediatype<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>interaction <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="cb2-3">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dat)</span>
<span id="cb2-4"></span>
<span id="cb2-5">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;">summary</span>(m1)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>coef</span>
<span id="cb2-6">ci  <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;">confint</span>(m1)</span></code></pre></div>
</div>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<colgroup>
<col style="width: 42%">
<col style="width: 12%">
<col style="width: 15%">
<col style="width: 10%">
<col style="width: 9%">
<col style="width: 9%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;"></th>
<th style="text-align: right;">Estimate</th>
<th style="text-align: right;">Std. Error</th>
<th style="text-align: right;">t value</th>
<th style="text-align: right;">2.5 %</th>
<th style="text-align: right;">97.5 %</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">(Intercept)</td>
<td style="text-align: right;">0.421</td>
<td style="text-align: right;">0.128</td>
<td style="text-align: right;">3.295</td>
<td style="text-align: right;">0.172</td>
<td style="text-align: right;">0.670</td>
</tr>
<tr class="even">
<td style="text-align: left;">mediatypeVR1</td>
<td style="text-align: right;">0.439</td>
<td style="text-align: right;">0.146</td>
<td style="text-align: right;">3.014</td>
<td style="text-align: right;">0.155</td>
<td style="text-align: right;">0.723</td>
</tr>
<tr class="odd">
<td style="text-align: left;">mediatypeVR2</td>
<td style="text-align: right;">0.393</td>
<td style="text-align: right;">0.146</td>
<td style="text-align: right;">2.702</td>
<td style="text-align: right;">0.110</td>
<td style="text-align: right;">0.677</td>
</tr>
<tr class="even">
<td style="text-align: left;">interactionactive</td>
<td style="text-align: right;">0.499</td>
<td style="text-align: right;">0.181</td>
<td style="text-align: right;">2.763</td>
<td style="text-align: right;">0.147</td>
<td style="text-align: right;">0.852</td>
</tr>
<tr class="odd">
<td style="text-align: left;">mediatypeVR1:interactionactive</td>
<td style="text-align: right;">0.474</td>
<td style="text-align: right;">0.206</td>
<td style="text-align: right;">2.304</td>
<td style="text-align: right;">0.073</td>
<td style="text-align: right;">0.876</td>
</tr>
<tr class="even">
<td style="text-align: left;">mediatypeVR2:interactionactive</td>
<td style="text-align: right;">0.280</td>
<td style="text-align: right;">0.206</td>
<td style="text-align: right;">1.357</td>
<td style="text-align: right;">-0.122</td>
<td style="text-align: right;">0.681</td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
<section id="implied-model-formulae" class="level1">
<h1>Implied model formulae</h1>
<p>The two categorical predictors are dummy coded. For <code>interaction</code> the level <code>passive</code> is set as reference category. The dummy variable <img src="https://latex.codecogs.com/png.latex?interaction_%7Bactive%7D"> is therefore 0 for <code>passive</code> and 1 for <code>active</code>.</p>
<div class="cell" data-layout-align="center">
<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;">contrasts</span>(dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>interaction)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>        active
passive      0
active       1</code></pre>
</div>
</div>
<p>The three level factor <code>mediatype</code> is represented by two dummy variables, namely <img src="https://latex.codecogs.com/png.latex?mediatype_%7BVR1%7D"> and <img src="https://latex.codecogs.com/png.latex?mediatype_%7BVR2%7D">. Since <code>MTT</code> is the reference category, the coding scheme is as folows.</p>
<div class="cell" data-layout-align="center">
<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;">contrasts</span>(dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mediatype)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>    VR1 VR2
MTT   0   0
VR1   1   0
VR2   0   1</code></pre>
</div>
</div>
<p>When we plug these dummy variables into the model formula from above, we get the following equations for each data point of the aggregated data set.</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Balign*%7D%0A%20%20%20y_%7B11%7D%20&amp;%20=%20%5Cbeta_0%20+%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5Cupsilon_0%20+%20%5Cvarepsilon%20&amp;%20%5Ctext%7Bvalue%20for%20an%20average%20person%20for%20the%20reference%20categories%20(MTT%20and%20passive)%7D%5C%5C%0A%20%20%20y_%7B12%7D%20&amp;%20=%20%5Cbeta_0%20+%20%5Cbeta_1%20+%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5Cupsilon_0%20+%20%5Cvarepsilon%20&amp;%20%5Ctext%7Beffect%20when%20going%20from%20MTT%20to%20VR1%20for%20passive%7D%5C%5C%0A%20%20%20y_%7B13%7D%20&amp;%20=%20%5Cbeta_0%20+%20%5Cbeta_2%20+%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5Cupsilon_0%20+%20%5Cvarepsilon%20&amp;%20%5Ctext%7Beffect%20when%20going%20from%20MTT%20to%20VR2%20for%20passive%7D%5C%5C%0A%20%20%20y_%7B21%7D%20&amp;%20=%20%5Cbeta_0%20+%20%5Cbeta_3%20+%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5Cupsilon_0%20+%20%5Cvarepsilon%20&amp;%20%5Ctext%7Beffect%20when%20going%20from%20passive%20to%20active%20for%20MTT%7D%5C%5C%0A%20%20%20y_%7B22%7D%20&amp;%20=%20%5Cbeta_0%20+%20%5Cbeta_1%20+%20%5Cbeta_3%20+%20%5Cbeta_4%20+%20%5Cupsilon_0%20+%20%5Cvarepsilon%20&amp;%20%5Ctext%7Beffect%20when%20going%20from%20MTT%20to%20VR1%20for%20active%7D%5C%5C%0A%20%20%20y_%7B23%7D%20&amp;%20=%20%5Cbeta_0%20+%20%5Cbeta_2%20+%20%5Cbeta_3%20+%20%5Cbeta_5%20+%20%5Cupsilon_0%20+%20%5Cvarepsilon%20&amp;%20%5Ctext%7Beffect%20when%20going%20from%20MTT%20to%20VR2%20for%20active%7D%5C%5C%0A%5Cend%7Balign*%7D"></p>
</section>
<section id="compare-to-plot" class="level1">
<h1>Compare to plot</h1>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://nwickel.codeberg.page/statistics_snippets/posts/2024-11-05-design/index_files/figure-html/plot-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="576"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="session-info" class="level1">
<h1>Session info</h1>
<div class="cell" data-layout-align="center">
<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;">sessionInfo</span>()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>R version 4.6.1 (2026-06-24 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 26100)

Matrix products: default
  LAPACK version 3.12.1

locale:
[1] LC_COLLATE=German_Germany.utf8  LC_CTYPE=German_Germany.utf8   
[3] LC_MONETARY=German_Germany.utf8 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.utf8    

time zone: Europe/Berlin
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] lme4_2.1-0   Matrix_1.7-6

loaded via a namespace (and not attached):
 [1] digest_0.6.39     fastmap_1.2.0     xfun_0.62         lattice_0.23-1   
 [5] splines_4.6.1     knitr_1.52        htmltools_0.5.9   rmarkdown_2.32   
 [9] Rdpack_2.6.6      cli_3.6.6         nloptr_2.2.1      grid_4.6.1       
[13] reformulas_0.4.4  compiler_4.6.1    rbibutils_2.4.1   boot_1.3-32      
[17] tools_4.6.1       nlme_3.1-171      minqa_1.2.8       evaluate_1.0.5   
[21] Rcpp_1.1.2        yaml_2.3.12       otel_0.2.0        rlang_1.3.0      
[25] jsonlite_2.0.0    htmlwidgets_1.6.4 MASS_7.3-66      </code></pre>
</div>
</div>


</section>

 ]]></description>
  <category>Categorical predictors</category>
  <category>Mixed-effects models</category>
  <category>R</category>
  <guid>https://nwickel.codeberg.page/statistics_snippets/posts/2024-11-05-design/</guid>
  <pubDate>Mon, 04 Nov 2024 23:00:00 GMT</pubDate>
</item>
</channel>
</rss>
