<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/marginal-effects-for-mixed-effects-regression-models-rstats/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://strengejacke.wordpress.com/2018/11/28/marginal-effects-for-mixed-effects-regression-models-rstats/"> R – Strenge Jacke!</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><strong>ggeffects</strong> (<a rel="nofollow" href="https://cran.r-project.org/package=ggeffects" target="_blank">CRAN</a>, <a rel="nofollow" href="https://strengejacke.github.io/ggeffects/" target="_blank">website</a>) is a package that computes marginal effects at the mean (MEMs) or representative values (MERs) for many different models, including mixed effects or Bayesian models. One of the advantages of the package is its easy-to-use interface: No matter if you fit a simple or complex model, with interactions or splines, the function call is always the same. This also holds true for the returned output, which is always a data frame with the same, consistent column names.</p>
<p>The past package-update introduced some new features I wanted to describe here: a revised <code>print()</code>-method as well as a new opportunity to plot marginal effects at different levels of random effects in mixed models…</p>
<p><span></span></p>
<h2>The new print()-method</h2>
<p>The former print()-method simply showed the first predicted values, including confidence intervals. For numeric predictor variables with many values, you could, for instance, only see the first 10 of more than 100 predicted values. While it makes sense to shorten the (console-)output, there was no information about the predictions for the last or other „representative“ values of the term in question. Now, the print()-method automatically prints a selection of representative values, so you get a quick and clean impression of the range of predicted values for continuous variables:</p>
<pre><code>library(ggeffects)
data(efc)
efc$c172code &lt;- as.factor(efc$c172code)
fit &lt;- lm(barthtot ~ c12hour * c172code + neg_c_7, data = efc)

ggpredict(fit, &quot;c12hour&quot;)
<span>#> #> # Predicted values of Total score BARTHEL INDEX #> # x = average number of hours of care per week #> #> x predicted std.error conf.low conf.high
#> 0 72.804 2.516 67.872 77.736
#> 20 68.060 2.097 63.951 72.170
#> 45 62.131 1.824 58.555 65.706
#> 65 57.387 1.886 53.691 61.083
#> 85 52.643 2.179 48.373 56.913
#> 105 47.900 2.626 42.752 53.047
#> 125 43.156 3.164 36.955 49.357
#> 170 32.482 4.531 23.602 41.363
#> #> Adjusted for:
#> * c172code = 1
#> * neg_c_7 = 11.83</span></code></pre>
<p>If you print predicted values of a term, grouped by the levels of another term (which makes sense in the above example due to the present interaction), the print()-method automatically adjusts the range of printed values to keep the console-output short. In the following example, only 6 instead of 8 values per „block“ are shown:</p>
<pre><code>ggpredict(fit, c(&quot;c12hour&quot;, &quot;c172code&quot;))
<span>#> #> # Predicted values of Total score BARTHEL INDEX #> # x = average number of hours of care per week #> #> # c172code = 1
#> x predicted std.error conf.low conf.high
#> 0 72.804 2.516 67.872 77.736
#> 30 65.689 1.946 61.874 69.503
#> 55 59.759 1.823 56.186 63.331
#> 85 52.643 2.179 48.373 56.913
#> 115 45.528 2.887 39.870 51.186
#> 170 32.482 4.531 23.602 41.363
#> #> # c172code = 2
#> x predicted std.error conf.low conf.high
#> 0 76.853 1.419 74.073 79.633
#> 30 68.921 1.115 66.737 71.106
#> 55 62.311 1.122 60.112 64.510
#> 85 54.379 1.438 51.560 57.198
#> 115 46.447 1.934 42.656 50.238
#> 170 31.905 3.007 26.011 37.800
#> #> # c172code = 3
#> x predicted std.error conf.low conf.high
#> 0 73.862 2.502 68.958 78.766
#> 30 66.925 1.976 63.053 70.798
#> 55 61.145 2.155 56.920 65.369
#> 85 54.208 2.963 48.400 60.016
#> 115 47.271 4.057 39.320 55.222
#> 170 34.554 6.303 22.200 46.907
#> #> Adjusted for:
#> * neg_c_7 = 11.83</span></code></pre>
<h2>Marginal effects at specific levels of random effects</h2>
<p>Marginal effects can also be calculated for each group level in mixed models. Simply add the name of the related random effects term to the <code>terms</code>-argument, and set <code>type = &quot;re&quot;</code>. In the following example, we fit a linear mixed model and first simply plot the marginal effetcs, <em>not</em> conditioned on random effects.</p>
<pre><code>library(sjlabelled)
library(lme4)
data(efc)
efc$e15relat &lt;- as_label(efc$e15relat)
m &lt;- lmer(neg_c_7 ~ c12hour + c160age + c161sex + (1 | e15relat), data = efc)
me &lt;- ggpredict(m, terms = &quot;c12hour&quot;)
plot(me)</code></pre>
<p><img data-orig-file="https://strengejacke.files.wordpress.com/2018/11/rplot.png?w=456" data-orig-size="700,500" data-medium-file="https://strengejacke.files.wordpress.com/2018/11/rplot.png?w=456?w=300" data-attachment-id="1637" data-image-meta="{" data-image-description="" src="https://strengejacke.files.wordpress.com/2018/11/rplot.png?w=456" data-permalink="https://strengejacke.wordpress.com/2018/11/28/marginal-effects-for-mixed-effects-regression-models-rstats/rplot-8/" data-large-file="https://strengejacke.files.wordpress.com/2018/11/rplot.png?w=456?w=700" alt="" data-image-title="Rplot" sizes="(max-width: 700px) 100vw, 700px" data-comments-opened="0" class="wp-image-1637" data-recalc-dims="1"></p>
<p>To compute marginal effects for each grouping level, add the related random term to the <code>terms</code>-argument. In this case, confidence intervals are not calculated, but marginal effects are conditioned on <em>each group level</em> of the random effects.</p>
<pre><code>me &lt;- ggpredict(m, terms = c(&quot;c12hour&quot;, &quot;e15relat&quot;), type = &quot;re&quot;)
plot(me)</code></pre>
<p><img data-orig-file="https://strengejacke.files.wordpress.com/2018/11/rplot01.png?w=456" data-orig-size="700,500" data-medium-file="https://strengejacke.files.wordpress.com/2018/11/rplot01.png?w=456?w=300" data-attachment-id="1638" data-image-meta="{" data-image-description="" src="https://strengejacke.files.wordpress.com/2018/11/rplot01.png?w=456" data-permalink="https://strengejacke.wordpress.com/2018/11/28/marginal-effects-for-mixed-effects-regression-models-rstats/rplot01-4/" data-large-file="https://strengejacke.files.wordpress.com/2018/11/rplot01.png?w=456?w=700" alt="" data-image-title="Rplot01" sizes="(max-width: 700px) 100vw, 700px" data-comments-opened="0" class="wp-image-1638" data-recalc-dims="1"></p>
<p>Marginal effects, conditioned on random effects, can also be calculated for specific levels only. Add the related values into brackets after the variable name in the <code>terms</code>-argument.</p>
<pre><code>me &lt;- ggpredict(m, terms = c(&quot;c12hour&quot;, &quot;e15relat [child,cousin]&quot;), type = &quot;re&quot;)
plot(me)</code></pre>
<p><img data-orig-file="https://strengejacke.files.wordpress.com/2018/11/rplot02.png?w=456" data-orig-size="700,500" data-medium-file="https://strengejacke.files.wordpress.com/2018/11/rplot02.png?w=456?w=300" data-attachment-id="1639" data-image-meta="{" data-image-description="" src="https://strengejacke.files.wordpress.com/2018/11/rplot02.png?w=456" data-permalink="https://strengejacke.wordpress.com/2018/11/28/marginal-effects-for-mixed-effects-regression-models-rstats/rplot02-5/" data-large-file="https://strengejacke.files.wordpress.com/2018/11/rplot02.png?w=456?w=700" alt="" data-image-title="Rplot02" sizes="(max-width: 700px) 100vw, 700px" data-comments-opened="0" class="wp-image-1639" data-recalc-dims="1"></p>
<p>If the group factor has too many levels, you can also take a random sample of all possible levels and plot the marginal effects for this subsample of group levels. To do this, use <code>term = &quot;groupfactor [sample=n]&quot;</code>.</p>
<pre><code>data(&quot;sleepstudy&quot;)
m &lt;- lmer(Reaction ~ Days + (1 + Days | Subject), data = sleepstudy)
me &lt;- ggpredict(m, terms = c(&quot;Days&quot;, &quot;Subject [sample=8]&quot;), type = &quot;re&quot;)
plot(me)</code></pre>
<p><img data-orig-file="https://strengejacke.files.wordpress.com/2018/11/rplot03.png?w=456" data-orig-size="700,500" data-medium-file="https://strengejacke.files.wordpress.com/2018/11/rplot03.png?w=456?w=300" data-attachment-id="1640" data-image-meta="{" data-image-description="" src="https://strengejacke.files.wordpress.com/2018/11/rplot03.png?w=456" data-permalink="https://strengejacke.wordpress.com/2018/11/28/marginal-effects-for-mixed-effects-regression-models-rstats/rplot03-2/" data-large-file="https://strengejacke.files.wordpress.com/2018/11/rplot03.png?w=456?w=700" alt="" data-image-title="Rplot03" sizes="(max-width: 700px) 100vw, 700px" data-comments-opened="0" class="wp-image-1640" data-recalc-dims="1"></p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://strengejacke.wordpress.com/2018/11/28/marginal-effects-for-mixed-effects-regression-models-rstats/"> R – Strenge Jacke!</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/pj4qe-E8uug" height="1"></a></div>