<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/introducing-purging-an-r-package-for-addressing-mediation-effects/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-posts.com/introducing-purging-an-r-package-for-addressing-mediation-effects/"> R-posts.com</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><strong>A Simple Method for Purging Mediation Effects among Independent Variables</strong></p>
<p> Mediation can occur when one independent variable swamps the effect of another, suggesting high correlation between the two variables. Though there are some great packages for mediation analysis out there, the simple intuition of its need is often ambiguous, especially for younger graduate students. Thus, in this blog post, it is my goal to introduce an intuitive overview of mediation and offer a simple method for “purging” variables of mediation effects for their simultaneous use in multivariate analysis. The purging process detailed in this blog is available in my recently released R package, <em>purging</em>, which is available on <a rel="nofollow" href="https://cran.r-project.org/package=purging" target="_blank">CRAN</a> or at my <a rel="nofollow" href="https://github.com/pdwaggoner/purging" target="_blank">GitHub</a>.  </p>
<p> Let’s consider a couple practical examples from “real life” research contexts. First, suppose we are interested in whether committee membership relating to a specific issue domain influences the likelihood of sponsoring related issue-specific legislation. However, in the American context as representational responsibilities permeate legislative behavior, district characteristics in similar employment-related industries likely influence self-selection onto the issue-specific committees in the first place, which we also suggest should influence likelihood of related-issue bill sponsorship. Therefore, in this context, we have a mediation model, where employment/industry (<em>indirect</em>) -> committee membership (<em>direct</em>) -> sponsorship. Thus, we would want to purge committee membership of the effects of employment/industry in the district to observe the “pure” effect of committee membership on the likelihood of related sponsorship. This example is from my paper recently published in <a rel="nofollow" href="http://journals.sagepub.com/doi/abs/10.1177/1532673X18759644?journalCode=aprb" target="_blank">American Politics Research</a>.</p>
<p> Or consider a second example in a different realm. Let’s say we had a model where women’s level of labor force participation determines their level of contraceptive use, and that the effect of female labor force participation on fertility is indirect, essentially filtered through its impact on contraceptive use. Once we control for contraceptive use, the direct effect of labor force participation may go away. In other words, the effect of labor force participation on fertility is likely indirect, and filtered through contraceptive use, which means the variables are also highly correlated. This second example was borrowed from Scott Basinger’s and Patrick Shea’s (University of Houston) graduate statistics labs, which originally gave me the idea of expanding this out to develop an R package dedicated to addressing this issue in a variety of contexts and for several functional forms.</p>
<p> These two examples offer simple ways of thinking about mediation effects (e.g., labor force (<em>indirect</em>) -> contraception (<em>direct</em>) -> fertility). If we run into this problem, a simple solution is “purging”. The steps to purge are to, first, regress the <em>direct</em> variable (in the second case, “contraceptive use”) on the <em>indirect</em> variable (in the second case, “labor force participation”). Then, store those residuals, which is the <em>direct</em> effect of contraception after accounting for the <em>indirect</em> effect of labor force participation. Then, we add the stored residuals as their own “purged variable” in the updated specification. Essentially, this purging process allows for a new <em>direct</em> variable that is uncorrelated with the <em>indirect</em> variable. When we do this, we will see that each variable is explaining unique variance in the DV of interest (you can double check this several ways, such as comparing correlation coefficients (which we will do below) or by comparing R^2 across specifications).</p>
<p> <strong>An Applied Example</strong></p>
<p> With the intuition behind mediation and the purging solution in mind, let’s walk through a simple example using some fake data. For an example based on the second case described above using real data from the United Nations Human Development Programme, see the code file, <em>purging example.R, </em>at my <a rel="nofollow" href="https://github.com/pdwaggoner/purging" target="_blank">GitHub repository</a>.</p>
<pre># First, install the MASS package for the &quot;mvrnorm&quot; function
install.packages(&quot;MASS&quot;)
library(MASS)
# Second, install the purging package directly from CRAN for the &quot;purge.lm&quot; function
install.packages(&quot;purging&quot;)
library(purging)

# Set some paramters to guide our simulation
n = 5000
rho = 0.9

# Create some fake data
d = mvrnorm(n = n, mu = c(0, 0), Sigma = matrix(c(1, rho, rho, 1), nrow = 2), empirical = TRUE)

# Store each correlated variable as its own object 
X = d[, 1]
Y = d[, 2]

# Create a dataframe of your two variables
d = data.frame(X, Y)

# Verify the correlation between these two normally distributed variables is what we set (rho = 0.9)
cor(d$X, d$Y)
plot(d$X, d$Y) 
 </pre>
<p>
In addition to the correlation coefficient between the two variables being exactly as we specified (0.90), see this positive correlation between the two random variables in the plot below.</p>
<p> <img data-orig-file="https://i1.wp.com/r-posts.com/wp-content/uploads/2018/03/correlation.plot_.png?fit=560%2C560" data-orig-size="560,560" data-medium-file="https://i1.wp.com/r-posts.com/wp-content/uploads/2018/03/correlation.plot_.png?fit=300%2C300" data-attachment-id="474" data-image-meta="{" data-image-description="" src="https://i1.wp.com/r-posts.com/wp-content/uploads/2018/03/correlation.plot_.png?resize=300%2C300" data-permalink="http://r-posts.com/introducing-purging-an-r-package-for-addressing-mediation-effects/correlation-plot/" data-large-file="https://i1.wp.com/r-posts.com/wp-content/uploads/2018/03/correlation.plot_.png?fit=450%2C450" alt="" data-image-title="correlation.plot" sizes="(max-width: 300px) 100vw, 300px" width="300" data-comments-opened="1" class="wp-image-474" data-recalc-dims="1" height="300"></p>
<p> Now, with our correlated data created, we can call the “purge.lm” command, given that our data are continuous. <em>Note</em>: the package supports a variety of functional forms for continuous (linear), binary (logit and probit), and event count data (Poisson and negative binomial).</p>
<p> The idea behind the package is to generate the new <em>direct</em>-impact variable to be used in the analysis, <em>purged of the effects of the indirect variable.</em> To do so, simply input the name of the data frame first, followed by the name of the <em>direct</em> variable in quotes, and then the <em>indirect</em> variable also in quotes in the function. Calling the function will generate a new object (i.e., the <em>direct</em> variable), which can then be added to a data frame using the $ operator, with the following line of code:</p>
<pre> df$purged.var &lt;- purged.var</pre>
<p>Let’s now see the purge command in action using our fake data.</p>
<pre> 
# Purge the &quot;direct&quot; variable, Y, of the mediation effects of X 
# (direct/indirect selection will depend on your model specification) 
purge.lm(d, &quot;Y&quot;, &quot;X&quot;) # df, &quot;direct&quot;, &quot;indirect&quot; 
 
# You will get an automatic suggestion message to store the values in the df 
purged = purge.lm(d, &quot;Y&quot;, &quot;X&quot;) # Store as its own object 
d$purged = purged # Attach to df 
 
# Finally, check the correlation and the plot to see the effects purged from the original &quot;Y&quot; (direct) variable 
cor(d$X, d$purged) 
plot(d$X, d$purged) 
</pre>
<p>
Note the correlation between the original indirect (X) variable and the new direct (Y) variable, purged of the effects of X, is -9.211365e-17, or essentially non-existent. For additional corroboration, let’s see the updated correlation plot between the X and purged-Y variables.</p>
<p> <img data-orig-file="https://i2.wp.com/r-posts.com/wp-content/uploads/2018/03/purged.plot_.png?fit=560%2C560" data-orig-size="560,560" data-medium-file="https://i2.wp.com/r-posts.com/wp-content/uploads/2018/03/purged.plot_.png?fit=300%2C300" data-attachment-id="475" data-image-meta="{" data-image-description="" src="https://i2.wp.com/r-posts.com/wp-content/uploads/2018/03/purged.plot_.png?resize=300%2C300" data-permalink="http://r-posts.com/introducing-purging-an-r-package-for-addressing-mediation-effects/purged-plot/" data-large-file="https://i2.wp.com/r-posts.com/wp-content/uploads/2018/03/purged.plot_.png?fit=450%2C450" alt="" data-image-title="purged.plot" sizes="(max-width: 300px) 100vw, 300px" width="300" data-comments-opened="1" class="wp-image-475" data-recalc-dims="1" height="300"></p>
<p> The purge command did as expected, with the correlation between the two variables essentially gone. You can download the package and documentation at <a rel="nofollow" href="https://cran.r-project.org/package=purging" target="_blank">CRAN</a>. If you have any questions or find any bugs requiring fixing, please feel free to <a rel="nofollow" href="http://www.philipdwaggoner.com/" target="_blank">contact me</a>. As this procedure was first developed and implemented (using the <em>binary/logit</em> iteration discussed above in the first example) in a now-published paper, please cite use of the package as: Waggoner, Philip D. 2018. “Do Constituents Influence Issue-Specific Bill Sponsorship?” <em>American Politics Research</em>,