<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-regression-models-in-r-rstats-dataviz/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://strengejacke.wordpress.com/2018/07/03/marginal-effects-for-regression-models-in-r-rstats-dataviz/"> R – Strenge Jacke!</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Regression coefficients are typically presented as tables that are easy to understand. Sometimes, estimates are difficult to interpret. This is especially true for interaction or transformed terms (quadratic or cubic terms, polynomials, splines), in particular for more complex models. In such cases, coefficients are no longer interpretable in a direct way and <em>marginal effects</em> are far easier to understand. Specifically, the visualization of marginal effects makes it possible to intuitively get the idea of how predictors and outcome are associated, even for complex models.</p>
<p><img data-orig-file="https://strengejacke.files.wordpress.com/2018/07/logo.png?w=456" data-orig-size="120,139" data-medium-file="https://strengejacke.files.wordpress.com/2018/07/logo.png?w=456?w=120" data-attachment-id="1628" data-image-meta="{" data-image-description="" src="https://strengejacke.files.wordpress.com/2018/07/logo.png?w=456" data-permalink="https://strengejacke.wordpress.com/2018/07/03/marginal-effects-for-regression-models-in-r-rstats-dataviz/logo-2/" data-large-file="https://strengejacke.files.wordpress.com/2018/07/logo.png?w=456?w=120" alt="" data-image-title="logo" data-comments-opened="0" class="wp-image-1628" data-recalc-dims="1"> The <a rel="nofollow" href="https://cran.r-project.org/package=ggeffects" target="_blank">ggeffects-package</a> (<a rel="nofollow" href="http://joss.theoj.org/papers/10.21105/joss.00772" target="_blank">Lüdecke 2018</a>) aims at easily calculating marginal effects for a broad range of different regression models, beginning with classical models fitted with <code>lm()</code> or <code>glm()</code> to complex mixed models fitted with <strong>lme4</strong> and <strong>glmmTMB</strong> or even Bayesian models from <strong>brms</strong> and <strong>rstanarm</strong>. The goal of the <strong>ggeffects-package</strong> is to provide a simple, user-friendly interface to calculate marginal effects, which is mainly achieved by one function: <code>ggpredict()</code>. Independent from the type of regression model, the output is always the same, a data frame with a consistent structure.</p>
<p><span></span></p>
<p>The idea behind this function is to compute (and visualize) the relationship between a model predictor (independent variable) and the model response (dependent variable). The predictor of interest needs to be specified in the <code>terms</code>-argument.</p>
<pre><code>data(mtcars)
m &lt;- lm(mpg ~ hp + wt + cyl + am, data = mtcars)
ggpredict(m, &quot;cyl&quot;)
<span>#> # A tibble: 3 x 5
#> x predicted conf.low conf.high group
#> #> 1 4 21.7 19.1 24.4 1 #> 2 6 20.2 19.3 21.1 1 #> 3 8 18.7 16.5 21.0 1</span></code></pre>
<p>The relationship can be differentiated depending on further predictors, which is useful e.g. for interaction terms. Up to two further predictors that indicate the „grouping“ structure can be used to calculate marginal effects. The names of these predictors need to be passed as character vector to <code>ggpredict()</code>.</p>
<pre><code>m &lt;- lm(mpg ~ wt * cyl + am + wt + cyl, data = mtcars)
p &lt;- ggpredict(m, c(&quot;wt&quot;, &quot;cyl&quot;))
p
<span>#> # A tibble: 27 x 5
#> x predicted conf.low conf.high group
#> #> 1 1.5 31.7 28.5 34.9 4 #> 2 1.5 26.3 23.5 29.2 6 #> 3 1.5 21.0 16.8 25.1 8 #> 4 2 28.7 26.6 30.8 4 #> 5 2 24.2 22.2 26.3 6 #> 6 2 19.8 16.4 23.1 8 #> 7 2.5 25.7 24.3 27.2 4 #> 8 2.5 22.1 20.7 23.5 6 #> 9 2.5 18.5 15.9 21.2 8 #> 10 3 22.8 20.8 24.7 4 #> # ... with 17 more rows</span></code></pre>
<p>There’s a <code>plot()</code>-method, based on <strong>ggplot2</strong>:</p>
<pre><code>plot(p)</code></pre>
<p><img data-orig-file="https://strengejacke.files.wordpress.com/2018/07/me1.png?w=456" data-orig-size="600,400" data-medium-file="https://strengejacke.files.wordpress.com/2018/07/me1.png?w=456?w=300" data-attachment-id="1626" data-image-meta="{" data-image-description="" src="https://strengejacke.files.wordpress.com/2018/07/me1.png?w=456" data-permalink="https://strengejacke.wordpress.com/2018/07/03/marginal-effects-for-regression-models-in-r-rstats-dataviz/me1/" data-large-file="https://strengejacke.files.wordpress.com/2018/07/me1.png?w=456?w=600" alt="" data-image-title="me1" sizes="(max-width: 600px) 100vw, 600px" data-comments-opened="0" class="wp-image-1626" data-recalc-dims="1"></p>
<p>The simple approach of <code>ggpredict()</code> can be used for all supported regression models. Thus, to calculate marginal effects with <code>ggpredict()</code>, it makes no differences if the model is a simpel linear model or a negative biniomial multilevel model or a cumulative link model etc. In case of cumulative link models, <code>ggpredict()</code> automatically takes care of proper grouping, in this case for the different levels of the response variable:</p>
<pre><code>library(MASS)
library(ordinal)
data(housing)
m &lt;- clm(Sat ~ Type * Cont + Infl, weights = Freq, data = housing)
p &lt;- ggpredict(m, c(&quot;Cont&quot;, &quot;Type&quot;))
plot(p)</code></pre>
<p><img data-orig-file="https://strengejacke.files.wordpress.com/2018/07/me2.png?w=456" data-orig-size="700,500" data-medium-file="https://strengejacke.files.wordpress.com/2018/07/me2.png?w=456?w=300" data-attachment-id="1630" data-image-meta="{" data-image-description="" src="https://strengejacke.files.wordpress.com/2018/07/me2.png?w=456" data-permalink="https://strengejacke.wordpress.com/2018/07/03/marginal-effects-for-regression-models-in-r-rstats-dataviz/me2/" data-large-file="https://strengejacke.files.wordpress.com/2018/07/me2.png?w=456?w=700" alt="" data-image-title="me2" sizes="(max-width: 700px) 100vw, 700px" data-comments-opened="0" class="wp-image-1630" data-recalc-dims="1"></p>
<p><strong>ggeffects</strong> also allows easily calculating marginal effects at specific levels of other predictors. This is particularly useful for interaction effects with continuous variables. In the following example, both variables of the interaction term have a larger range of values, which obscure the moderating effect:</p>
<pre><code>m &lt;- lm(mpg ~ wt * hp + am + wt, data = mtcars)
p &lt;- ggpredict(m, c(&quot;hp&quot;, &quot;wt&quot;))
plot(p)</code></pre>
<p><img data-orig-file="https://strengejacke.files.wordpress.com/2018/07/me3.png?w=456" data-orig-size="600,400" data-medium-file="https://strengejacke.files.wordpress.com/2018/07/me3.png?w=456?w=300" data-attachment-id="1631" data-image-meta="{" data-image-description="" src="https://strengejacke.files.wordpress.com/2018/07/me3.png?w=456" data-permalink="https://strengejacke.wordpress.com/2018/07/03/marginal-effects-for-regression-models-in-r-rstats-dataviz/me3/" data-large-file="https://strengejacke.files.wordpress.com/2018/07/me3.png?w=456?w=600" alt="" data-image-title="me3" sizes="(max-width: 600px) 100vw, 600px" data-comments-opened="0" class="wp-image-1631" data-recalc-dims="1"></p>
<p>However, you can directly specify certain values, at which marginal effects should be calculated, or use „shortcuts“ that compute convenient values, like mean +/- 1 SD etc.</p>
<pre><code>p &lt;- ggpredict(m, c(&quot;hp&quot;, &quot;wt [meansd]&quot;))
plot(p)</code></pre>
<p><img data-orig-file="https://strengejacke.files.wordpress.com/2018/07/me4.png?w=456" data-orig-size="600,400" data-medium-file="https://strengejacke.files.wordpress.com/2018/07/me4.png?w=456?w=300" data-attachment-id="1632" data-image-meta="{" data-image-description="" src="https://strengejacke.files.wordpress.com/2018/07/me4.png?w=456" data-permalink="https://strengejacke.wordpress.com/2018/07/03/marginal-effects-for-regression-models-in-r-rstats-dataviz/me4/" data-large-file="https://strengejacke.files.wordpress.com/2018/07/me4.png?w=456?w=600" alt="" data-image-title="me4" sizes="(max-width: 600px) 100vw, 600px" data-comments-opened="0" class="wp-image-1632" data-recalc-dims="1"></p>
<p>The latest update of <strong>ggeffects</strong> on CRAN introduced some new features. There is <a rel="nofollow" href="https://strengejacke.github.io/ggeffects/" target="_blank">a dedicated website</a> that describes all the details of this package, including some vignettes with lots of examples.</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/07/03/marginal-effects-for-regression-models-in-r-rstats-dataviz/"> 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/Hfzh2gEqvDE" height="1"></a></div>