<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/be-careful-not-to-control-for-a-post-exposure-covariate/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.rdatagen.net/post/be-careful/"> ouR data generation</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A researcher was presenting an analysis of the impact various types of childhood trauma might have on subsequent substance abuse in adulthood. Obviously, a very interesting and challenging research question. The statistical model included adjustments for several factors that are plausible confounders of the relationship between trauma and substance use, such as childhood poverty. However, the model also include a measurement for poverty in adulthood – believing it was somehow confounding the relationship of trauma and substance use. A confounder is a common cause of an exposure/treatment and an outcome; it is hard to conceive of adult poverty as a cause of childhood events, even though it might be related to adult substance use (or maybe not). At best, controlling for adult poverty has no impact on the conclusions of the research; less good, though, is the possibility that it will lead to the conclusion that the effect of trauma is less than it actually is.</p>
<p>Using a highly contrived simulation of data and the abstract concept of <em>potential outcomes</em>, I am hoping to illuminate some of the issues raised by this type of analysis.</p>
<div>
<h2>Potential outcomes and causal effects</h2>
<p>The field of causal inference is a rich one, and I won’t even scratch the surface here. My goal is to present the concepts of potential outcomes so that we can articulate at least one clear way to think about what a causal effect can be defined. Under this framework, we generate data where we can find out the “true” measure of causal effect. And then we can use simple regression models to see how well (or not) they recapture these “known” causal effects.</p>
<p>If an individual <span>\(i\)</span> experiences a traumatic effect as a child, we say that the exposure <span>\(X_i = 1\)</span>. Otherwise <span>\(X_i = 0\)</span>, there was no traumatic event. (I am going to assume binary exposures just to keep things simple – exposed vs. not exposed.) In the potential outcomes world we say that every individual has possible outcomes <span>\(Y_{1i}\)</span> (the outcome we would observe <em>if</em> the individual had experienced trauma) and <span>\(Y_{0i}\)</span> (the outcome we would observe <em>if</em> the individual had not. Quite simply, we define the causal effect of <span>\(X\)</span> on <span>\(Y\)</span> as the difference in potential outcomes, <span>\(CE_i = Y_{1i} – Y_{0i}\)</span>. If <span>\(Y_{1i} = Y_{0i}\)</span> (i.e. the potential outcomes are the same), we would say that <span>\(X\)</span> does not cause <span>\(Y\)</span>, at least for individual <span>\(i\)</span>.</p>
<p>In the real world, we only observe one potential outcome – the one associated with the actual exposure. The field of causal inference has lots to say about the assumptions and conditions that are required for us to use observed data to estimate average causal effects; many would say that unless we use a randomized controlled study, those assumptions will never be reasonable. But in the world of simulation, we can generate potential outcomes and observed outcomes, so we know the causal effect both at the individual level and the average population level. And we can see how well our models do.</p>
</div>
<div>
<h2>Simple confounding</h2>
<p>Here’s a relatively straightforward example. Let’s say we are interested in understanding if some measure <span>\(X\)</span> causes an outcome <span>\(Y\)</span>, where there is a common cause <span>\(C\)</span> (the diagram is called a DAG – a directed acyclic graph – and is useful for many things, including laying out data generating process):</p>
<p><img src="https://i1.wp.com/www.rdatagen.net/img/post-careful/SimpleCausal.png?w=300&ssl=1" data-recalc-dims="1"></p>
<pre><code>library(broom)
library(data.table)
library(simstudy)

def &lt;- defData(varname = &quot;C&quot;, formula = 0.4, dist = &quot;binary&quot;)
def &lt;- defData(def, &quot;X&quot;, formula = &quot;0.3 + 0.4 * C&quot;, dist = &quot;binary&quot;)
def &lt;- defData(def, &quot;e&quot;, formula = 0, variance = 2, dist = &quot;normal&quot;)
def &lt;- defData(def, &quot;Y0&quot;, formula = &quot;2 * C + e&quot;, dist=&quot;nonrandom&quot;)
def &lt;- defData(def, &quot;Y1&quot;, formula = &quot;0.5 + 2 * C + e&quot;, dist=&quot;nonrandom&quot;)
def &lt;- defData(def, &quot;Y_obs&quot;, formula = &quot;Y0 + (Y1 - Y0) * X&quot;, dist = &quot;nonrandom&quot;)

def</code></pre>
<pre><code>##    varname            formula variance      dist     link
## 1:       C                0.4        0    binary identity
## 2:       X      0.3 + 0.4 * C        0    binary identity
## 3:       e                  0        2    normal identity
## 4:      Y0          2 * C + e        0 nonrandom identity
## 5:      Y1    0.5 + 2 * C + e        0 nonrandom identity
## 6:   Y_obs Y0 + (Y1 - Y0) * X        0 nonrandom identity</code></pre>
<p>In this example, <span>\(X\)</span> does have an effect on <span>\(Y\)</span>, but so does <span>\(C\)</span>. If we ignore <span>\(C\)</span> in assessing the size of the effect of <span>\(X\)</span> on <span>\(Y\)</span>, we will overestimate that effect, which is 0.5. We can generate data and see that this is the case:</p>
<pre><code>set.seed(5)
dt &lt;- genData(1000, def)</code></pre>
<p>We see that the true causal effect is easily recovered if we have access to the potential outcomes <span>\(Y_1\)</span> and <span>\(Y_0\)</span>, but of course we don’t:</p>
<pre><code>dt[, mean(Y1 - Y0)] # True causal effect</code></pre>
<pre><code>## [1] 0.5</code></pre>
<p>If we compare the average <em>observed</em> outcomes for each exposure group ignoring the confounder, we overestimate the effect of the exposure:</p>
<pre><code>dt[X == 1, mean(Y_obs)] - dt[X == 0, mean(Y_obs)]</code></pre>
<pre><code>## [1] 1.285009</code></pre>
<p>We can estimate the same effect using simple linear regression:</p>
<pre><code>lm1 &lt;- lm(Y_obs ~ X, data = dt)
tidy(lm1)</code></pre>
<pre><code>##          term  estimate  std.error statistic      p.value
## 1 (Intercept) 0.5515963 0.07325865  7.529436 1.137854e-13
## 2           X 1.2850091 0.10674523 12.038094 2.916261e-31</code></pre>
<p>And finally, if we adjust for the confounder <span>\(C\)</span>, we recover the true causal effect of <span>\(X\)</span> on <span>\(Y\)</span>, or at least get very close to it:</p>
<pre><code>lm2 &lt;- lm(Y_obs ~ X + C, data = dt)
tidy(lm2)</code></pre>
<pre><code>##          term   estimate  std.error statistic      p.value
## 1 (Intercept) 0.08491216 0.06502545  1.305830 1.919117e-01
## 2           X 0.48935880 0.09678110  5.056347 5.083959e-07
## 3           C 2.05729945 0.09825925 20.937464 5.767575e-81</code></pre>
</div>
<div>
<h2>Adjusting for a post-exposure covariate</h2>
<p>Now, we are ready to see what happens in a slightly more complicated setting that is defined by this DAG:</p>
<p><img src="https://i1.wp.com/www.rdatagen.net/img/post-careful/ComplexCausal.png?w=400&ssl=1" data-recalc-dims="1"></p>
<p>In this example <span>\(C\)</span> is measured in two time periods, and exposure in period 1 relates to exposure in period 2. (For example, if a child is poor, he is more likely to be poor as an adult.) We are primarily interested in whether or not <span>\(X\)</span> (trauma) causes <span>\(Y\)</span> (substance use). The difficulty is that <span>\(X\)</span> and <span>\(C_2\)</span> are related, as are <span>\(C_2\)</span> and <span>\(Y\)</span>.</p>
<p>I suggest that in order to fully understand the effect of <span>\(X\)</span> on <span>\(Y\)</span>, we cannot control for <span>\(C_2\)</span>, as tempting as it might be. The intuition is that part of the effect of <span>\(X\)</span> on <span>\(Y\)</span> is due to the fact that <span>\(X\)</span> has an effect on <span>\(C_2\)</span>, at least for some individuals. <em>If we control for <span>\(C_2\)</span>, we are actually removing a key component of the causal mechanism.</em> Below in is the data generating process – a few things to note: (1) <span>\(C_2\)</span> has potential outcomes based on the exposure <span>\(X\)</span>. (2) We have restricted the potential outcome <span>\(C_{21}\)</span> to be set to 1 if <span>\(C_{20}\)</span> is 1. For example, if someone would have been poor in adulthood <em>without</em> exposure to trauma, we assume that they also would have been poor in adulthood had they been exposed to trauma. (3) The potential outcome for <span>\(Y\)</span> is dependent on the relevant potential outcome for <span>\(C_2\)</span>. That is <span>\(Y_0\)</span> depends on <span>\(C_{20}\)</span> and <span>\(Y_1\)</span> depends on <span>\(C_{21}\)</span>.</p>
<pre><code>##     varname                  formula variance      dist     link
##  1:      C1                     0.25        0    binary identity
##  2:       X            -2 + 0.8 * C1        0    binary    logit
##  3:    C2.0            -2.0 + 1 * C1        0    binary    logit
##  4:   C2.1x            -1.5 + 1 * C1        0    binary    logit
##  5:    C2.1        pmax(C2.0, C2.1x)        0 nonrandom identity
##  6:       e                        0        4    normal identity
##  7:      Y0          -3 + 5*C2.0 + e        0 nonrandom identity
##  8:      Y1           0 + 5*C2.1 + e        0 nonrandom identity
##  9:  C2_obs C2.0 + (C2.1 - C2.0) * X        0 nonrandom identity
## 10:   Y_obs       Y0 + (Y1 - Y0) * X        0 nonrandom identity</code></pre>
<pre><code>set.seed(25)
dt &lt;- genData(5000, def2)</code></pre>
<p>Here is the true average causal effect, based on information we will never know:</p>
<pre><code>dt[, mean(Y1 - Y0)]</code></pre>
<pre><code>## [1] 3.903</code></pre>
<p>When we control for <span>\(C_2\)</span>, we are essentially estimating the effect of <span>\(X\)</span> at each level <span>\(C_2\)</span> (and <span>\(C_1\)</span>, since we are controlling for that as well), and then averaging across the sub-samples to arrive at an estimate for the entire sample. We can see that, based on the specification of the potential outcomes in the data generation process, the effect at each level of <span>\(C_2\)</span> will be centered around 3.0, which is different from the true causal effect of 3.9. The discrepancy is due to the fact each approach is effectively collecting different sub-samples (one defines groups based on set levels of <span>\(X\)</span> and <span>\(C_2\)</span>, and the other defines groups based on set levels of <span>\(X\)</span> alone) and estimating average effects based on weights determined by the sizes of those two sets of sub-samples.</p>
<p>Here is the inappropriate model that adjusts for <span>\(C_2\)</span>:</p>
<pre><code>lm2a &lt;- lm( Y_obs ~ C1 + C2_obs + X , data = dt)
tidy(lm2a)</code></pre>
<pre><code>##          term    estimate  std.error   statistic       p.value
## 1 (Intercept) -3.01360235 0.03481082 -86.5708464  0.000000e+00
## 2          C1 -0.02078171 0.06765129  -0.3071887  7.587126e-01
## 3      C2_obs  4.92972384 0.07625838  64.6450138  0.000000e+00
## 4           X  3.04600204 0.08114223  37.5390478 6.677416e-272</code></pre>
<p>The estimate for the coefficient of <span>\(X\)</span> is 3.0, just as anticipated. Here now is the correct model, and you will see that we recover the true causal effect in the coefficient estimate of <span>\(X\)</span> (or at least, we get much, much closer):</p>
<pre><code>lm2b &lt;- lm( Y_obs ~ C1 + X , data = dt)
tidy(lm2b)</code></pre>
<pre><code>##          term   estimate  std.error statistic       p.value
## 1 (Intercept) -2.4886726 0.04586832 -54.25689  0.000000e+00
## 2          C1  0.9665413 0.08930265  10.82321  5.315059e-27
## 3           X  3.9377832 0.10834926  36.34343 7.868650e-257</code></pre>
<p>Of course, in the real world, we don’t know the underlying data generating process or the true DAG. And what I have described here is a gross oversimplification of the underlying relationships, and have indeed left out many other factors that likely affect the relationship between childhood trauma and adult substance use. Other measures, such as parental substance use, may be related to both childhood trauma and adult substance use, and may affect poverty in the two time periods in different, complicated ways.</p>
<p>But the point is that one should give careful thought to what gets included in a model. We may not want to throw everything we measure into the model. Be careful.</p>
</div>
	<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://www.rdatagen.net/post/be-careful/"> ouR data generation</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/NqBiMsHIjN4" height="1"></a></div>