<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/when-you-use-inverse-probability-weighting-for-estimation-what-are-the-weights-actually-doing/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.rdatagen.net/post/inverse-probability-weighting-when-the-outcome-is-binary/"> ouR data generation</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Towards the end of <a rel="nofollow" href="https://www.rdatagen.net/post/potential-outcomes-confounding/" target="_blank">Part 1</a> of this short series on confounding, IPW, and (hopefully) marginal structural models, I talked a little bit about the fact that inverse probability weighting (IPW) can provide unbiased estimates of marginal causal effects in the context of confounding just as more traditional regression models like OLS can. I used an example based on a normally distributed outcome. Now, that example wasn’t super interesting, because in the case of a linear model with homogeneous treatment effects (i.e. no interaction), the marginal causal effect is the same as the conditional effect (that is, conditional on the confounders.) There was no real reason to use IPW in that example – I just wanted to illustrate that the estimates looked reasonable.</p>
<p>But in many cases, the conditional effect <em>is</em> different from the marginal effect. (And in other cases, there may not even be an obvious way to estimate the conditional effect – that will be the topic for the last post in this series). When the outcome is binary, the notion that conditional effects are equal to marginal effects is no longer the case. (I’ve touched on this <a rel="nofollow" href="https://www.rdatagen.net/post/marginal-v-conditional/" target="_blank">before</a>.) What this means, is that we can recover the true conditional effects using logistic regression, but we cannot estimate the marginal effect. This is directly related to the fact that logistic regression is linear on the logit (or log-odds) scale, not on the probability scale. The issue here is collapsibility, or rather, non-collapsibility.</p>
<div>
<h3>A simulation</h3>
<p>Because binary outcomes are less amenable to visual illustration, I am going to stick with model estimation to see how this plays out:</p>
<pre><code>library(simstudy)

# define the data

defB &lt;- defData(varname = &quot;L&quot;, formula =0.27, 
                dist = &quot;binary&quot;)
defB &lt;- defData(defB, varname = &quot;Y0&quot;, formula = &quot;-2.5 + 1.75*L&quot;, 
                dist = &quot;binary&quot;, link = &quot;logit&quot;)
defB &lt;- defData(defB, varname = &quot;Y1&quot;, formula = &quot;-1.5 + 1.75*L&quot;, 
                dist = &quot;binary&quot;, link = &quot;logit&quot;)
defB &lt;- defData(defB, varname = &quot;A&quot;, formula = &quot;0.315 + 0.352 * L&quot;, 
                dist = &quot;binary&quot;)
defB &lt;- defData(defB, varname = &quot;Y&quot;, formula = &quot;Y0 + A * (Y1 - Y0)&quot;, 
                dist = &quot;nonrandom&quot;)

# generate the data

set.seed(2002)
dtB &lt;- genData(200000, defB)
dtB[1:6]</code></pre>
<pre><code>##    id L Y0 Y1 A Y
## 1:  1 0  0  0 0 0
## 2:  2 0  0  0 0 0
## 3:  3 1  0  1 1 1
## 4:  4 0  1  1 1 1
## 5:  5 1  0  0 1 0
## 6:  6 1  0  0 0 0</code></pre>
<p>We can look directly at the potential outcomes to see the true causal effect, measured as a log odds ratio (LOR):</p>
<pre><code>odds &lt;- function (p) {
    return((p/(1 - p)))
}

# log odds ratio for entire sample (marginal LOR)

dtB[, log( odds( mean(Y1) ) / odds( mean(Y0) ) )]</code></pre>
<pre><code>## [1] 0.8651611</code></pre>
<p>In the linear regression context, the conditional effect measured using observed data from the exposed and unexposed subgroups was in fact a good estimate of the marginal effect in the population. Not the case here, as the conditional causal effect (LOR) of A is 1.0, which is greater than the true marginal effect of 0.86:</p>
<pre><code>library(broom)
tidy(glm(Y ~ A + L , data = dtB, family=&quot;binomial&quot;)) </code></pre>
<pre><code>##          term   estimate  std.error  statistic p.value
## 1 (Intercept) -2.4895846 0.01053398 -236.33836       0
## 2           A  0.9947154 0.01268904   78.39167       0
## 3           L  1.7411358 0.01249180  139.38225       0</code></pre>
<p>This regression estimate for the coefficient of <span>\(A\)</span> <em>is</em> a good estimate of the conditional effect in the population (based on the potential outcomes at each level of <span>\(L\)</span>):</p>
<pre><code>dtB[, .(LOR = log( odds( mean(Y1) ) / odds( mean(Y0) ) ) ), keyby = L]</code></pre>
<pre><code>##    L       LOR
## 1: 0 0.9842565
## 2: 1 0.9865561</code></pre>
<p>Of course, ignoring the confounder <span>\(L\)</span> is not very useful if we are interested in recovering the marginal effect. The estimate of 1.4 is biased for <em>both</em> the conditional effect <em>and</em> the marginal effect – it is not really useful for anything:</p>
<pre><code>tidy(glm(Y ~ A , data = dtB, family=&quot;binomial&quot;))</code></pre>
<pre><code>##          term  estimate   std.error statistic p.value
## 1 (Intercept) -2.049994 0.009164085 -223.6987       0
## 2           A  1.433094 0.011723767  122.2384       0</code></pre>
</div>
<div>
<h3>How weighting reshapes the data …</h3>
<p>Here is a simple tree graph that shows the potential outcomes for 1000 individuals (based on the same distributions we’ve been using in our simulation). For 27% of the individuals, <span>\(L=1\)</span>, for 73% <span>\(L=0\)</span>. Each individual has a potential outcome under each level of treatment <span>\(A\)</span>. So, that is why there are 730 individuals with <span>\(L=0\)</span> who are both with and without treatment. Likewise each treatment arm for those with <span>\(L=0\)</span> has 270 individuals. We are not double counting.</p>
<div>
<img src="https://i0.wp.com/www.rdatagen.net/img/post-ipw/PO_flow_large.png?w=456&ssl=1" data-recalc-dims="1">
</div>
<p>Both the marginal and conditional estimates that we estimated before using the simulated data can be calculated directly using information from this tree. The conditional effects on the log-odds scale can be calculated as …</p>
<p><span>\[LOR_{A=1 \textbf{ vs } A=0|L = 0} = log \left (\frac{0.182/0.818}{0.076/0.924} \right)=log(2.705) = 0.995\]</span></p>
<p>and</p>
<p><span>\[LOR_{A=1 \textbf{ vs } A=0|L = 1} = log \left (\frac{0.562/0.438}{0.324/0.676} \right)=log(2.677) = 0.984\]</span></p>
<p>The marginal effect on the log odds scale is estimated marginal probabilities: <span>\(P(Y=1|A=0)\)</span> and <span>\(P(Y=1|A=1)\)</span>. Again, we can take this right from the tree …</p>
<p><span>\[P(Y=1|A=0) = 0.73\times0.076 + 0.27\times0.324 = 0.143\]</span> and</p>
<p><span>\[P(Y=1|A=1) = 0.73\times0.182 + 0.27\times0.562 = 0.285\]</span></p>
<p>Based on these average outcomes (probabilities) by exposure, the marginal log-odds for the sample is:</p>
<p><span>\[LOR_{A=1 \textbf{ vs } A=0} = log \left (\frac{0.285/0.715}{0.143/0.857} \right)=log(2.389) = 0.871\]</span></p>
<p>Back in the real world of observed data, this is what the tree diagram looks like:</p>
<div>
<img src="https://i0.wp.com/www.rdatagen.net/img/post-ipw/Obs_flow_large.png?w=456&ssl=1" data-recalc-dims="1">
</div>
<p>This tree tells us that the probability of exposure <span>\(A=1\)</span> is different depending upon that value of <span>\(L\)</span>. For <span>\(L=1\)</span>, <span>\(P(A=1) = 230/730 = 0.315\)</span> and for <span>\(L=0\)</span>, <span>\(P(A=1) = 180/270 = 0.667\)</span>. Because of this disparity, the crude estimate of the effect (ignoring <span>\(L\)</span>) is biased for the marginal causal effect:</p>
<p><span>\[P(Y=1|A=0) = \frac{500\times0.076 + 90\times0.324}{500+90}=0.114\]</span></p>
<p>and</p>
<p><span>\[P(Y=1|A=1) = \frac{230\times0.182 + 180\times0.562}{230+180}=0.349\]</span></p>
<p>The crude log odds ratio is</p>
<p><span>\[LOR_{A=1 \textbf{ vs } A=0} = log \left (\frac{0.349/0.651}{0.114/0.886} \right)=log(4.170) = 1.420\]</span></p>
<p>As mentioned in the prior post, the IPW is based on the probability of the actual exposure at each level of <span>\(L\)</span>: <span>\(P(A=a | L)\)</span>, where <span>\(a\in(0,1)\)</span> (and not on <span>\(P(A=1|L)\)</span>, the propensity score). Here are the simple weights for each group:</p>
<div>
<img src="https://i0.wp.com/www.rdatagen.net/img/post-ipw/Weights.png?w=456&ssl=1" data-recalc-dims="1">
</div>
<p>If we apply the weights to each of the respective groups, you can see that the number of individuals in each treatment arm is adjusted to the total number of individuals in the sub-group defined the level of <span>\(L\)</span>. For example, if we apply the weight of 3.17 (730/230) to each person observed with <span>\(L=0\)</span> and <span>\(A=1\)</span>, we end up with <span>\(230\times3.17=730\)</span>. Applying each of the respective weights to the subgroups of <span>\(L\)</span> and <span>\(A\)</span> results in a new sample of individuals that looks exactly like the one we started out with in the potential outcomes world:</p>
<div>
<img src="https://i0.wp.com/www.rdatagen.net/img/post-ipw/PO_flow_large.png?w=456&ssl=1" data-recalc-dims="1">
</div>
<p>This all works only if we make these two assumptions: <span>\[P(Y=1|A=0, L=l) = P(Y_0=1 | A=1, L=l)\]</span> and <span>\[P(Y=1|A=1, L=l) = P(Y_1=1 | A=0, L=l)\]</span></p>
<p>That is, we can make this claim <em>only under the assumption of no unmeasured confounding</em>. (This was discussed in the <a rel="nofollow" href="https://www.rdatagen.net/post/potential-outcomes-confounding/" target="_blank">Part 1</a> post.)</p>
</div>
<div>
<h3>Applying IPW to our data</h3>
<p>We need to estimate the weights using logistic regression (though other, more flexible methods, can also be used). First, we estimate <span>\(P(A=1|L)\)</span> …</p>
<pre><code>exposureModel &lt;- glm(A ~ L, data = dtB, family = &quot;binomial&quot;)
dtB[, pA := predict(exposureModel, type = &quot;response&quot;)]</code></pre>
<p>Now we can derive an estimate for <span>\(P(A=a|L=l)\)</span> and get the weight itself…</p>
<pre><code># Define two new columns
defB2 &lt;- defDataAdd(varname = &quot;pA_actual&quot;, 
                    formula = &quot;(A * pA) + ((1 - A) * (1 - pA))&quot;, 
                    dist = &quot;nonrandom&quot;)
defB2 &lt;- defDataAdd(defB2, varname = &quot;IPW&quot;, 
                    formula = &quot;1/pA_actual&quot;, 
                    dist = &quot;nonrandom&quot;)

# Add weights
dtB &lt;- addColumns(defB2, dtB)
dtB[1:6]</code></pre>
<pre><code>##    id L Y0 Y1 A Y        pA pA_actual      IPW
## 1:  1 0  0  0 0 0 0.3146009 0.6853991 1.459004
## 2:  2 0  0  0 0 0 0.3146009 0.6853991 1.459004
## 3:  3 1  0  1 1 1 0.6682351 0.6682351 1.496479
## 4:  4 0  1  1 1 1 0.3146009 0.3146009 3.178631
## 5:  5 1  0  0 1 0 0.6682351 0.6682351 1.496479
## 6:  6 1  0  0 0 0 0.6682351 0.3317649 3.014183</code></pre>
<p>To estimate the marginal effect on the log-odds scale, we use the function <code>glm</code> with weights specified by IPW. The true value of marginal effect (based on the population-wide potential outcomes) was 0.87 (as we estimated from the potential outcomes directly and from the tree graph). Our estimate here is spot on (but with such a large sample size, this is not so surprising):</p>
<pre><code>tidy(glm(Y ~ A , data = dtB, family=&quot;binomial&quot;, weights = IPW)) </code></pre>
<pre><code>##          term   estimate   std.error statistic p.value
## 1 (Intercept) -1.7879512 0.006381189 -280.1909       0
## 2           A  0.8743154 0.008074115  108.2862       0</code></pre>
<p>It may not seem like a big deal to be able to estimate the marginal effect – we may actually not be interested in it. However, in the next post, I will touch on the issue of estimating causal effects when there are repeated exposures (for example, administering a drug over time) and time dependent confounders that are both affected by prior exposures and affect future exposures <em>and</em> affect the outcome. Under this scenario, it is very difficult if not impossible to control for these confounders – the best we might be able to do is estimate a marginal, population-wide causal effect. That is where weighting will be really useful.</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/inverse-probability-weighting-when-the-outcome-is-binary/"> 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/uDlP3-PH0g4" height="1"></a></div>