<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/easy-peasy-stata-like-marginal-effects-with-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.brodrigues.co/blog/2017-10-26-margins_r/"> Econometrics and Free Software</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Model interpretation is essential in the social sciences. If one wants to know the effect of variable <code>x</code> on the dependent variable <code>y</code>, marginal effects are an easy way to get the answer. STATA includes a <code>margins</code> command that has been ported to R by <a rel="nofollow" href="http://thomasleeper.com/" target="_blank">Thomas J. Leeper</a> of the London School of Economics and Political Science. You can find the source code of the package <a rel="nofollow" href="https://github.com/leeper/margins" target="_blank">on github</a>. In this short blog post, I demo some of the functionality of <code>margins</code>.</p>
<p>First, let’s load some packages:</p>
<pre><code>library(ggplot2)
library(tibble)
library(broom)
library(margins)
library(Ecdat)</code></pre>
<p>As an example, we are going to use the <code>Participation</code> data from the <code>Ecdat</code> package:</p>
<pre><code>data(Participation)</code></pre>
<pre><code>?Participation</code></pre>
<pre><code>Labor Force Participation

Description

a cross-section

number of observations : 872

observation : individuals

country : Switzerland

Usage

data(Participation)
Format

A dataframe containing :

lfp
labour force participation ?

lnnlinc
the log of nonlabour income

age
age in years divided by 10

educ
years of formal education

nyc
the number of young children (younger than 7)

noc
number of older children

foreign
foreigner ?

Source

Gerfin, Michael (1996) “Parametric and semiparametric estimation of the binary response”, Journal of Applied Econometrics, 11(3), 321-340.

References

Davidson, R. and James G. MacKinnon (2004) Econometric Theory and Methods, New York, Oxford University Press, http://www.econ.queensu.ca/ETM/, chapter 11.

Journal of Applied Econometrics data archive : http://qed.econ.queensu.ca/jae/.</code></pre>
<p>The variable of interest is <code>lfp</code>: whether the individual participates in the labour force or not. To know which variables are relevant in the decision to participate in the labour force, one could estimate a logit model, using <code>glm()</code>.</p>
<pre><code>logit_participation = glm(lfp ~ ., data = Participation, family = &quot;binomial&quot;)</code></pre>
<p>Now that we ran the regression, we can take a look at the results. I like to use <code>broom::tidy()</code> to look at the results of regressions, as <code>tidy()</code> returns a nice <code>data.frame</code>, but you could use <code>summary()</code> if you’re only interested in reading the output:</p>
<pre><code>tidy(logit_participation)</code></pre>
<pre><code>##          term    estimate  std.error  statistic      p.value
## 1 (Intercept) 10.37434616 2.16685216  4.7877499 1.686617e-06
## 2     lnnlinc -0.81504064 0.20550116 -3.9661122 7.305449e-05
## 3         age -0.51032975 0.09051783 -5.6378920 1.721444e-08
## 4        educ  0.03172803 0.02903580  1.0927211 2.745163e-01
## 5         nyc -1.33072362 0.18017027 -7.3859224 1.514000e-13
## 6         noc -0.02198573 0.07376636 -0.2980454 7.656685e-01
## 7  foreignyes  1.31040497 0.19975784  6.5599678 5.381941e-11</code></pre>
<p>From the results above, one can only interpret the sign of the coefficients. To know how much a variable influences the labour force participation, one has to use <code>margins()</code>:</p>
<pre><code>effects_logit_participation = margins(logit_participation) </code></pre>
<pre><code>## Warning in warn_for_weights(model): 'weights' used in model estimation are
## currently ignored!</code></pre>
<pre><code>print(effects_logit_participation)</code></pre>
<pre><code>## Average marginal effects</code></pre>
<pre><code>## glm(formula = lfp ~ ., family = &quot;binomial&quot;, data = Participation)</code></pre>
<pre><code>##  lnnlinc     age     educ     nyc       noc foreignyes
##  -0.1699 -0.1064 0.006616 -0.2775 -0.004584     0.2834</code></pre>
<p>Using <code>summary()</code> on the object returned by <code>margins()</code> provides more details:</p>
<pre><code>summary(effects_logit_participation)</code></pre>
<pre><code>##      factor     AME     SE       z      p   lower   upper
##         age -0.1064 0.0176 -6.0494 0.0000 -0.1409 -0.0719
##        educ  0.0066 0.0060  1.0955 0.2733 -0.0052  0.0185
##  foreignyes  0.2834 0.0399  7.1102 0.0000  0.2053  0.3615
##     lnnlinc -0.1699 0.0415 -4.0994 0.0000 -0.2512 -0.0887
##         noc -0.0046 0.0154 -0.2981 0.7656 -0.0347  0.0256
##         nyc -0.2775 0.0333 -8.3433 0.0000 -0.3426 -0.2123</code></pre>
<p>And it is also possible to plot the effects with base graphics:</p>
<pre><code>plot(effects_logit_participation)</code></pre>
<p><img src="https://i1.wp.com/www.brodrigues.co/blog/2017-10-26-margins_r/blog/2017-10-26-margins_r_files/figure-html/unnamed-chunk-9-1.png?w=450" data-recalc-dims="1"></p>
<p>This uses the basic R plotting capabilities, which is useful because it is a simple call to the function <code>plot()</code> but if you’ve been using <code>ggplot2</code> and want this graph to have the same look as the others made with <code>ggplot2</code> you first need to save the summary in a variable. Let’s overwrite this <code>effects_logit_participation</code> variable with its summary:</p>
<pre><code>effects_logit_participation = summary(effects_logit_participation)</code></pre>
<p>And now it is possible to use <code>ggplot2</code> to create the same plot:</p>
<pre><code>ggplot(data = effects_logit_participation) +
  geom_point(aes(factor, AME)) +
  geom_errorbar(aes(x = factor, ymin = lower, ymax = upper)) +
  geom_hline(yintercept = 0) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45))</code></pre>
<p><img src="https://i1.wp.com/www.brodrigues.co/blog/2017-10-26-margins_r/blog/2017-10-26-margins_r_files/figure-html/unnamed-chunk-11-1.png?w=450" data-recalc-dims="1"></p>
<p>So an infinitesimal increase, in say, non-labour income (<code>lnnlinc</code>) of 0.001 is associated with a decrease of the probability of labour force participation by 0.001*17 percentage points.</p>
<p>You can also extract the marginal effects of a single variable, with <code>dydx()</code>:</p>
<pre><code>head(dydx(Participation, logit_participation, &quot;lnnlinc&quot;))</code></pre>
<pre><code>##   dydx_lnnlinc
## 1  -0.15667764
## 2  -0.20014487
## 3  -0.18495109
## 4  -0.05377262
## 5  -0.18710476
## 6  -0.19586986</code></pre>
<p>Which makes it possible to extract the effects for a list of individuals that you can create yourself:</p>
<pre><code>my_subjects = tribble(
    ~lfp,  ~lnnlinc, ~age, ~educ, ~nyc, ~noc, ~foreign,
    &quot;yes&quot;,   10.780,  7.0,     4,    1,    1,    &quot;yes&quot;,
     &quot;no&quot;,     1.30,  9.0,     1,    4,    1,    &quot;yes&quot;
)

dydx(my_subjects, logit_participation, &quot;lnnlinc&quot;)</code></pre>
<pre><code>##   dydx_lnnlinc
## 1  -0.09228119
## 2  -0.17953451</code></pre>
<p>I used the <code>tribble()</code> function from the <code>tibble</code> package to create this test data set, row by row. Then, using <code>dydx()</code>, I get the marginal effect of variable <code>lnnlinc</code> for these two individuals. No doubt that this package will be a huge help convincing more social scientists to try out R and make a potential transition from STATA easier.</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="http://www.brodrigues.co/blog/2017-10-26-margins_r/"> Econometrics and Free Software</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 width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/b9N8r-sOhEE" height="1"></a></div>