<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/plotting-partial-pooling-in-mixed-effects-models/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://tjmahr.github.io/plotting-partial-pooling-in-mixed-effects-models/"> Higher Order Functions</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In this post, I demonstrate a few techniques for plotting information from a<br>
relatively simple mixed-effects model fit in R. These plots can help us develop<br>
intuitions about what these models are doing and what “partial pooling” means.</p>
<h2>The <code>sleepstudy</code> dataset</h2>
<p>For these examples, I’m going to use the <code>sleepstudy</code> dataset from the lme4<br>
package. The outcome measure is reaction time, the predictor measure is days of<br>
sleep deprivation, and these measurements are nested within participants—we<br>
have 10 observations per participant. I am also going to add two fake<br>
participants with incomplete data to illustrate partial pooling.</p>
<div>
<pre><code><span>library</span><span>(</span><span>lme4</span><span>)</span><span>
</span><span>#> Loading required package: Matrix
#> Loading required package: methods
</span><span>library</span><span>(</span><span>dplyr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>tibble</span><span>)</span><span>
</span><span># Convert to tibble for better printing. Convert factors to strings
</span><span>sleepstudy</span><span> </span><span>&lt;-</span><span> </span><span>sleepstudy</span><span> </span><span>%>%</span><span> </span><span>as_tibble</span><span>()</span><span> </span><span>%>%</span><span> </span><span>mutate</span><span>(</span><span>Subject</span><span> </span><span>=</span><span> </span><span>as.character</span><span>(</span><span>Subject</span><span>))</span><span>
</span><span># Add two fake participants
</span><span>df_sleep</span><span> </span><span>&lt;-</span><span> </span><span>bind_rows</span><span>(</span><span>
</span><span>sleepstudy</span><span>,</span><span>
</span><span>data_frame</span><span>(</span><span>Reaction</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>286</span><span>,</span><span> </span><span>288</span><span>),</span><span> </span><span>Days</span><span> </span><span>=</span><span> </span><span>0</span><span>:</span><span>1</span><span>,</span><span> </span><span>Subject</span><span> </span><span>=</span><span> </span><span>&quot;374&quot;</span><span>),</span><span>
</span><span>data_frame</span><span>(</span><span>Reaction</span><span> </span><span>=</span><span> </span><span>245</span><span>,</span><span> </span><span>Days</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>Subject</span><span> </span><span>=</span><span> </span><span>&quot;373&quot;</span><span>))</span><span>
</span><span>df_sleep</span><span>
</span><span>#> # A tibble: 183 x 3
#> Reaction Days Subject
#> </span></code></pre></div>