<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/exploring-propensity-score-matching-and-weighting/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ellisp.github.io/blog/2017/04/09/propensity-v-regression"> Peter's stats stuff - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This post jots down some playing around with the pros, cons and limits of propensity score matching or weighting for causal social science research.</p>
<h2>Intro to propensity score matching</h2>
<p>One is often faced with an analytical question about causality and effect sizes when the only data around is from a quasi-experiment, not the random controlled trial one would hope for. This is common in many fields, but some of the most important occurrences are in public policy. For example, government programs to help individuals or firms are typically not allocated at random, but go to those with higher need, or higher potential to make something out of the assistance. This makes isolating the effect in the data of the treatment difficult, to say the least.</p>
<p>A frequently-used family of analytical methods to deal with this are grouped under <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Propensity_score_matching">propensity score matching</a> (although not all these methods literally “match”). Such methods model the probability of each unit (eg individual or firm) receiving the treatment; and then using these predicted probabilities or “propensities” to somehow balance the sample to make up for the confounding of the treatment with the other variablers of interest.</p>
<p>The simplest to explain member of this family involves creating a pseudo control group from the non-treatment individuals that resembles the treatment group in that they have similar propensities to get the treatment, but differs in that they just didn’t get it. Then analysis can proceed “as though” the data had been generated by an experiment. This approach is easy to explain to non-statisticians and has the great benefit of creating a tangible, concrete “control” group.</p>
<p>But everything depends on the model of the probabilities of getting the treatment. If it’s a good model, you’re fine. If not – and in particular, if the chance of getting the treatment is related to unobserved variables which also impact on your response variable of interest – the propensity score approach helps very little if at all. A good text on all this (and much more) is Morgan and Winship’s <a target="_blank" rel="nofollow" href="http://www.cambridge.org/vu/academic/subjects/sociology/sociology-general-interest/counterfactuals-and-causal-inference-methods-and-principles-social-research-2nd-edition?format=PB&isbn=9781107694163">Counterfactuals and Causal Inference: Methods and Principles for Social Research</a>.</p>
<h2>Job training example</h2>
<h3>Basics</h3>
<p>A pretty thorough implementation of various propensity score methods in R comes in <a target="_blank" rel="nofollow" href="http://ellisp.github.io/blog/2017/04/09/propensity-v-regression">Daniel E. Ho, Kosuke Imai, Gary King, Elizabeth A. Stuart (2011). MatchIt: Nonparametric Preprocessing for Parametric Causal Inference. Journal of Statistical<br>
Software, Vol. 42, No. 8, pp. 1-28</a>. There’s a good overview in the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/MatchIt/vignettes/matchit.pdf">MatchIt vignette</a>. I’ll get started with data from one of their examples, which shows a typical application of this technique:</p>
<p>“Our example data set is a subset of the job training program analyzed in Lalonde (1986)<br>
and Dehejia and Wahba (1999). MatchIt includes a subsample of the original data consisting<br>
of the National Supported Work Demonstration (NSW) treated group and the comparison<br>
sample from the Population Survey of Income Dynamics (PSID).1 The variables in<br>
this data set include participation in the job training program (treat, which is equal to 1<br>
if participated in the program, and 0 otherwise), age (age), years of education (educ), race<br>
(black which is equal to 1 if black, and 0 otherwise; hispan which is equal to 1 if hispanic,<br>
and 0 otherwise), marital status (married, which is equal to 1 if married, 0 otherwise), high<br>
school degree (nodegree, which is equal to 1 if no degree, 0 otherwise), 1974 real earnings<br>
(re74), 1975 real earnings (re75), and the main outcome variable, 1978 real earnings (re78).”</p>
<p>First, loading up the functionality I need for the rest of this post</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>tidyverse</span><span>)</span><span>
</span><span>library</span><span>(</span><span>forcats</span><span>)</span><span>
</span><span>library</span><span>(</span><span>scales</span><span>)</span><span>
</span><span>library</span><span>(</span><span>MASS</span><span>)</span><span> </span><span># for rlm
</span><span>library</span><span>(</span><span>boot</span><span>)</span><span> </span><span># for inv.logit
</span><span>library</span><span>(</span><span>MatchIt</span><span>)</span><span>
</span><span>library</span><span>(</span><span>boot</span><span>)</span><span> </span><span># for bootstrapping
</span><span>library</span><span>(</span><span>doParallel</span><span>)</span><span> </span><span># for parallel processing
</span><span>library</span><span>(</span><span>mgcv</span><span>)</span><span> </span><span># for gam
</span><span>
</span><span>#==============example from the MatchIt vignette=================
</span><span>data</span><span>(</span><span>lalonde</span><span>)</span><span>
</span><span># naive comparison - people who got the job training program
# have lower incomes in 1978 - because the training was given
# to people with income problems. So comparison is unfair:
</span><span>lalonde</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>treat</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>Income1978</span><span> </span><span>=</span><span> </span><span>mean</span><span>(</span><span>re78</span><span>),</span><span>
</span><span>n</span><span> </span><span>=</span><span> </span><span>n</span><span>())</span></code></pre>
</figure>
<div>
<pre><code>  treat Income1978     n
1     0   6984.170   429
2     1   6349.144   185
</code></pre>
</div>
<p>So we see that the 429 people who didn’t get the job training “treatment” had an average income about $635 more than the 185 beneficiaries. Program failure!</p>
<p>Obviously that’s unfair on the program, so we use <code>matchit</code> and <code>match.data</code> to create an artificial control group that resembles the treatment group in terms of age, education, ethnicity, marital stats, and income in 1974 and 1975:</p>
<figure>
<pre><code data-lang="r"><span># Choose one of the large variety of propensity score matching methods to model propensity
</span><span>match_model</span><span> </span><span>&lt;-</span><span> </span><span>matchit</span><span>(</span><span>treat</span><span> </span><span>~</span><span> </span><span>age</span><span> </span><span>+</span><span> </span><span>educ</span><span> </span><span>+</span><span> </span><span>black</span><span> </span><span>+</span><span> </span><span>hispan</span><span> </span><span>+</span><span> </span><span>nodegree</span><span> </span><span>+</span><span> </span><span>married</span><span> </span><span>+</span><span> </span><span>re74</span><span> </span><span>+</span><span> </span><span>re75</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>lalonde</span><span>,</span><span> </span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;nearest&quot;</span><span>)</span><span>
</span><span>match_data</span><span> </span><span>&lt;-</span><span> </span><span>match.data</span><span>(</span><span>match_model</span><span>)</span><span>
</span><span># Simple comparison is now much fairer
</span><span>match_data</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>treat</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>Income1978</span><span> </span><span>=</span><span> </span><span>mean</span><span>(</span><span>re78</span><span>),</span><span>
</span><span>n</span><span> </span><span>=</span><span> </span><span>n</span><span>())</span></code></pre>
</figure>
<div>
<pre><code>  treat Income1978     n
1     0   5440.941   185
2     1   6349.144   185
</code></pre>
</div>
<p>That gives us an average treatment effect of <strong>$908</strong>. Notice that the new control group is the same size as the treatment group; the rest of the observations have been discarded.</p>
<p>You don’t need to limit yourself to simple comparisons, although in principle they should work. A regression with the matched control and treatment data, even using the same explanatory variables as were used in the matching model, helps address the inevitable lack of complete balance between the two groups. That gives us a result of <strong>$960</strong> (output not shown). I use the robust M-estimator rather than ordinary least squares to deal with breaches of the classical regression assumptions (particularly the non-Normal response, with variance increasing as its mean increases).</p>
<figure>
<pre><code data-lang="r"><span># regression model estimate with matched data
</span><span>coef</span><span>(</span><span>rlm</span><span>(</span><span>re78</span><span> </span><span>~</span><span> </span><span>age</span><span> </span><span>+</span><span> </span><span>treat</span><span> </span><span>+</span><span> </span><span>educ</span><span> </span><span>+</span><span> </span><span>black</span><span> </span><span>+</span><span> </span><span>hispan</span><span> </span><span>+</span><span> </span><span>nodegree</span><span> </span><span>+</span><span> </span><span>married</span><span> </span><span>+</span><span> </span><span>re74</span><span> </span><span>+</span><span> </span><span>re75</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>match_data</span><span>))[</span><span>&quot;treat&quot;</span><span>]</span></code></pre>
</figure>
<h3>Regression is simpler and gives similar results</h3>
<p>However, a similar estimate could have come from a simpler, single-step regression with the original data, skipping the propensity score modelling altogether (there are arguments pro and con). The regression below estimates a treatment effect of <strong>$1,183</strong>. I call this “similar” because the uncertainty around all these estimates is huge, which I’ll demonstrate further down the post.</p>
<figure>
<pre><code data-lang="r"><span># regression model estimate with original data
</span><span>coef</span><span>(</span><span>rlm</span><span>(</span><span>re78</span><span> </span><span>~</span><span> </span><span>age</span><span> </span><span>+</span><span> </span><span>treat</span><span> </span><span>+</span><span> </span><span>educ</span><span> </span><span>+</span><span> </span><span>black</span><span> </span><span>+</span><span> </span><span>hispan</span><span> </span><span>+</span><span> </span><span>nodegree</span><span> </span><span>+</span><span> </span><span>married</span><span> </span><span>+</span><span> </span><span>re74</span><span> </span><span>+</span><span> </span><span>re75</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>lalonde</span><span>))[</span><span>&quot;treat&quot;</span><span>]</span></code></pre>
</figure>
<h3>Weighting might be better than matching</h3>
<p>Creating a control group by matching has the distressing side-effect of throwing away large amounts of the data, because the control group is shrunk down to the same size as the treatment group. A possibly better use of the propensity scores is to keep all observations in play but weight them according to the propensity score – one of the methods described by Peter Austin in <a target="_blank" rel="nofollow" href="https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3144483/">this article on “An introduction to propensity score methods for reducing the effects of confounding in observational studies”</a>.</p>
<p>Under “inverse probability of treatment weighting”, proposed by Imbens in 2000, observations that receive the treatment are given weight of  and those that did not receive the treatment are given weight of , where p is the probability of getting the treatment. That is, each observation is given weight of the inverse of the probability of the treatment they actually got. Intuitively, treatment cases that resemble the controls are interesting and given more weight, and control cases that look like they should have got the treatment are interesting and get more weight. Analysis can then proceed via weighted average values, or a regression with explanatory variables (which may or may not be the same variables as those used in the model of propensity for treatment).</p>
<p>I implemented this with Lalonde’s data without using the <code>MatchIt</code> package, partly to convince myself I understood how it worked, and partly because I wanted more flexibility in modelling propensity than is supported by that package. I’d looked at the generalized linear model with binomial family response that was used for its propensity score matching, and noticed that age was showing up as unhelpful in determining treatment. My expectation is that age is probably related in a non-linear fashion to the probability of getting job training treatment, so I used a generalized additive model to allow for this…</p>
<figure>
<pre><code data-lang="r"><span>mod</span><span> </span><span>&lt;-</span><span> </span><span>gam</span><span>(</span><span>treat</span><span> </span><span>~</span><span> </span><span>s</span><span>(</span><span>age</span><span>)</span><span> </span><span>+</span><span> </span><span>educ</span><span> </span><span>+</span><span> </span><span>black</span><span> </span><span>+</span><span> </span><span>hispan</span><span> </span><span>+</span><span> </span><span>nodegree</span><span> </span><span>+</span><span> </span><span>married</span><span> </span><span>+</span><span> </span><span>re74</span><span> </span><span>+</span><span> </span><span>re75</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>lalonde</span><span>,</span><span> </span><span>family</span><span> </span><span>=</span><span> </span><span>&quot;binomial&quot;</span><span>)</span><span>
</span><span>plot</span><span>(</span><span>mod</span><span>,</span><span> </span><span>shade</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;Non-linear function of age in treatment propensity&quot;</span><span>)</span></code></pre>
</figure>
<p>…which turns out to be the case:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0090-gam.svg"></p>
<p>Converting predicted probabilities into weights is straightforward. In the code below I have a quick look at the resulting density of weights.</p>
<figure>
<pre><code data-lang="r"><span>lalonde</span><span> </span><span>&lt;-</span><span> </span><span>lalonde</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>propensity_gam</span><span> </span><span>=</span><span> </span><span>predict</span><span>(</span><span>mod</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;response&quot;</span><span>),</span><span>
</span><span>weight_gam</span><span> </span><span>=</span><span> </span><span>1</span><span> </span><span>/</span><span> </span><span>propensity_gam</span><span> </span><span>*</span><span> </span><span>treat</span><span> </span><span>+</span><span> </span><span>1</span><span> </span><span>/</span><span> </span><span>(</span><span>1</span><span> </span><span>-</span><span> </span><span>propensity_gam</span><span>)</span><span> </span><span>*</span><span> </span><span>(</span><span>1</span><span> </span><span>-</span><span> </span><span>treat</span><span>))</span><span>
</span><span>ggplot</span><span>(</span><span>lalonde</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>weight_gam</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>as.factor</span><span>(</span><span>treat</span><span>)))</span><span> </span><span>+</span><span>
</span><span>geom_density</span><span>(</span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey50&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_rug</span><span>()</span><span> </span><span>+</span><span>
</span><span>scale_x_log10</span><span>(</span><span>breaks</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>5</span><span>,</span><span> </span><span>10</span><span>,</span><span> </span><span>20</span><span>,</span><span> </span><span>40</span><span>))</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Distribution of inverse probability weights&quot;</span><span>)</span></code></pre>
</figure>
<p><img width="100%" src="http://ellisp.github.io/img/0090-weights.svg"></p>
<p>One of the criticisms of this inverse probability of treatment weighting approach is that individual observations can get very high weights and become unduly influential. For example, in an <a target="_blank" rel="nofollow" href="http://www.stat.columbia.edu/~gelman/stuff_for_blog/posner.pdf">incomplete article by Posner and Ash</a> (there may be a complete version somewhere else):</p>
<blockquote>
<p>“While this method can be shown to have nice mathematical properties, it does not work well in practice. Consider a lone treated observation that happens to have a very low probability of being treated…. The value of the inverse of the propensity score will be extremely high, asymptotically infinity. The effect size obtained will be dominated by this single value, and any fluctuations in it will produce wildly varied results, which is an undesirable property.”</p>
</blockquote>
<p>Ouch. Posner and Ash go on to suggest alternative ways of weighting less vulnerable to these problems. As a simple starter, in the below I try the obvious first step of truncating the weights at 10. Here are the average incomes of the treatment and non-treatment groups using the full set of inverse probability weights, and another set truncated at 10.</p>
<figure>
<pre><code data-lang="r"><span>lalonde</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>treat</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>Income_allweights</span><span> </span><span>=</span><span> </span><span>round</span><span>(</span><span>weighted.mean</span><span>(</span><span>re78</span><span>,</span><span> </span><span>weight_gam</span><span>)),</span><span>
</span><span>Income_truncweights</span><span> </span><span>=</span><span> </span><span>round</span><span>(</span><span>weighted.mean</span><span>(</span><span>re78</span><span>,</span><span> </span><span>pmin</span><span>(</span><span>10</span><span>,</span><span> </span><span>weight_gam</span><span>))),</span><span>
</span><span>n</span><span> </span><span>=</span><span> </span><span>n</span><span>())</span></code></pre>
</figure>
<div>
<pre><code>  treat Income_allweights Income_truncweights     n  
1     0              6446                6435   429
2     1              6814                7167   185
</code></pre>
</div>
<p>Note that we now have all 429 of the non-treatment cases, a definite advantage over the matching methods.</p>
<p>We’re not limited to simple weighted averages of course; we can use these weights in the analysis of our choice including our robust regression model. Ho et al (2007) suggest that (in Morgan and Winship’s paraphrase):</p>
<blockquote>
<p>“the general procedure one should carry out in any multivariate analysis that aspires to generate causal inferences is to first balance one’s data as much as possible with a matching routine and then estimate a regression model on the matched data. From this perspective, matching is a preprocessor, which can be used to prepare the data for subsequent analysis with something such as a regression model.”</p>
</blockquote>
<p>The “doubly robust” approach to regression modelling to identify causal effects uses weights from propensity score matching as well as a regression.</p>
<blockquote>
<p>“There has been considerable debate as to which approach to confounder control is to be preferred, as the first [<em>ie single step regression</em>] is biased if the outcome regression model is misspecified while the second approach [<em>ie propensity score matching</em>] is biased if the treatment regression, ie propensity, model is misspecified. This controversy could be resolved if an estimator were available that was guaranteed to be consistent … whenever at least one of the two models was correct. … We refer to such combined methods as doubly-robust or doubly-protected as they can protect against misspecification of either the outcome or treatment model, although not against simultaneous misspecification of both.”</p>
</blockquote>
<p><em>Robins and Rotnitzky 2001, quoted in Morgan and Winship Counterfactuals and Causal Analysis</em></p>
<p>So here is my robust M estimator regression, using inverse propensity of treatment as weights, where propensity of treatment was modelled earlier as a generalized additive model on all explanatory variables and non-linearly with age:</p>
<figure>
<pre><code data-lang="r"><span>coef</span><span>(</span><span>rlm</span><span>(</span><span>re78</span><span> </span><span>~</span><span> </span><span>age</span><span> </span><span>+</span><span> </span><span>treat</span><span> </span><span>+</span><span> </span><span>educ</span><span> </span><span>+</span><span> </span><span>black</span><span> </span><span>+</span><span> </span><span>hispan</span><span> </span><span>+</span><span> </span><span>nodegree</span><span> </span><span>+</span><span> </span><span>married</span><span> </span><span>+</span><span> </span><span>re74</span><span> </span><span>+</span><span> </span><span>re75</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>lalonde</span><span>,</span><span> </span><span>weights</span><span> </span><span>=</span><span> </span><span>as.vector</span><span>(</span><span>weight_gam</span><span>)))[</span><span>&quot;treat&quot;</span><span>]</span></code></pre>
</figure>
<p>This gives a treatment estimate of <em>$910</em> and I think it’s my best point estimate so far.</p>
<p>These weighting methods seem better than simply matching, if only because they retain all the data, but crucially they are still vulnerable to model mis-specification and unobserved explanatory variables. There’s a good <a target="_blank" rel="nofollow" href="https://www.stat.berkeley.edu/~census/weight.pdf">critical discussion in this article by Freedman and Berk</a>:</p>
<blockquote>
<p>“Regressions can be weighted by propensity scores in order to reduce bias. However,<br>
weighting is likely to increase random error in the estimates, and to bias the<br>
estimated standard errors downward, even when selection mechanisms are well understood.<br>
Moreover, in some cases, weighting will increase the bias in estimated<br>
causal parameters. If investigators have a good causal model, it seems better just to<br>
fit the model without weights. If the causal model is improperly specified, there can<br>
be significant problems in retrieving the situation by weighting, although weighting<br>
may help under some circumstances.”</p>
</blockquote>
<p>And with respect to the “doubly robust” approach:</p>
<blockquote>
<p>“you need to get at least one of the two models (and preferably both) nearly right in order for weighting to help much. If both models are wrong, weighting could easily be a dead end. There are papers suggesting that under some circumstances, estimating a shaky causal model and a shaky selection model should be doubly robust. Our results indicate that under other circumstances, the technique is doubly frail.”</p>
</blockquote>
<p>When I see multi-stage approaches like propensity score matching or weighting – just like structural equation models, and two or three stage least squares – that aim to deal with causality by adding complexity, I always get very nervous; the more so when I read criticisms like those above. I’ve done some simulations exploring this issue but a write-up will have to wait for a separate post.</p>
<h3>Bootstrap or cross-validation must envelope the propensity modelling</h3>
<p>The literature has a range of (conflicting) views on estimating uncertainty of statistics estimated after propensity score matching or weighting. Morgan and Winship report in a footnote that the bootstrap does not to work particularly well for matching in particular, because the resampling process leaves fewer distinct cases to match to during the propensity modelling stage. However, <a target="_blank" rel="nofollow" href="https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4260115/">Austin and Small reported in 2014</a> tests of bootstrap methods that seem quite effective. Definitely, one of the appeals of weighting (rather than matching) is that it should make the overall process more suitable for sitting inside a bootstrap. However, I don’t have time for a really thorough review of this literature, so let’s just note that estimating uncertainty after propensity score matching is a moving area of uncertainty itself.</p>
<p>I use the method described by Austin and Small as the “complex bootstrap”, which involves resampling from the original data and performing the propensity modelling and matching for each resample. The propensity modelling is a big source of our uncertainty in the final estimates of interest. So that source of uncertainty needs to be repeated each time we mimic the sampling process with our bootstrap (the same applies to other pre-processing steps, like imputation). Austin and Small report that this results in a small overestimate of sampling variability (standard error higher by 7% than it should be; compared to 4% for a simpler bootstrap approach) but my principle, following the approach in Harrell’s <em>Regression Modeling Strategies</em> and elsewhere, is to always include pre-processing inside the bootstrap. I suspect that with less ideal data than Austin and Small use in their simulations (on my reading, they assume all relevant variables are available, amongst other ideals) this will pay off. Anyway, the results reported below look plausible.</p>
<p>Here’s code that bootstraps four of the methods of estimating treatment effect above:</p>
<ul>
<li>simple comparison of matched data;</li>
<li>robust regression with matched data;</li>
<li>robust regression with data weighted by inverse propensity of treatment;</li>
<li>robust regression with the original data.</li>
</ul>
<figure>
<pre><code data-lang="r"><span>#=================bootstrap for confidence intervals=================
# Set up a cluster for parallel computing
</span><span>cluster</span><span> </span><span>&lt;-</span><span> </span><span>makeCluster</span><span>(</span><span>7</span><span>)</span><span> </span><span># only any good if you have at least 7 processors :)
</span><span>registerDoParallel</span><span>(</span><span>cluster</span><span>)</span><span>
</span><span>clusterEvalQ</span><span>(</span><span>cluster</span><span>,</span><span> </span><span>{</span><span>
</span><span>library</span><span>(</span><span>MatchIt</span><span>)</span><span>
</span><span>data</span><span>(</span><span>lalonde</span><span>)</span><span>
</span><span>})</span><span>
</span><span>#' Function to estimate treatment effect three different methods
#' @return a vector of four estimates of treatment effect. See comments
#' in function for description of which is which
</span><span>my_function</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>x</span><span>,</span><span> </span><span>i</span><span>){</span><span>
</span><span>resampled_data</span><span> </span><span>&lt;-</span><span> </span><span>x</span><span>[</span><span>i</span><span>,</span><span> </span><span>]</span><span>
</span><span>match_data</span><span> </span><span>&lt;-</span><span> </span><span>match.data</span><span>(</span><span>
</span><span>matchit</span><span>(</span><span>treat</span><span> </span><span>~</span><span> </span><span>age</span><span> </span><span>+</span><span> </span><span>educ</span><span> </span><span>+</span><span> </span><span>black</span><span> </span><span>+</span><span> </span><span>hispan</span><span> </span><span>+</span><span> </span><span>nodegree</span><span> </span><span>+</span><span> </span><span>married</span><span> </span><span>+</span><span> </span><span>re74</span><span> </span><span>+</span><span> </span><span>re75</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>resampled_data</span><span>,</span><span> </span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;nearest&quot;</span><span>)</span><span>
</span><span>)</span><span>
</span><span># simple mean of matched groups
</span><span> </span><span>est1</span><span> </span><span>&lt;-</span><span> </span><span>with</span><span>(</span><span>match_data</span><span>,</span><span>
</span><span>mean</span><span>(</span><span>re78</span><span>[</span><span>treat</span><span> </span><span>==</span><span> </span><span>1</span><span>])</span><span> </span><span>-</span><span>
</span><span>mean</span><span>(</span><span>re78</span><span>[</span><span>treat</span><span> </span><span>==</span><span> </span><span>0</span><span>]))</span><span>
</span><span># regression model estimate with matched groups
</span><span> </span><span>est2</span><span> </span><span>&lt;-</span><span> </span><span>coef</span><span>(</span><span>rlm</span><span>(</span><span>re78</span><span> </span><span>~</span><span> </span><span>treat</span><span> </span><span>+</span><span> </span><span>age</span><span> </span><span>+</span><span> </span><span>educ</span><span> </span><span>+</span><span> </span><span>black</span><span> </span><span>+</span><span> </span><span>hispan</span><span> </span><span>+</span><span> </span><span>nodegree</span><span> </span><span>+</span><span> </span><span>married</span><span> </span><span>+</span><span> </span><span>re74</span><span> </span><span>+</span><span> </span><span>re75</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>match_data</span><span>))[</span><span>&quot;treat&quot;</span><span>]</span><span>
</span><span># regression model with IPTW
</span><span> </span><span>mod</span><span> </span><span>&lt;-</span><span> </span><span>gam</span><span>(</span><span>treat</span><span> </span><span>~</span><span> </span><span>s</span><span>(</span><span>age</span><span>)</span><span> </span><span>+</span><span> </span><span>educ</span><span> </span><span>+</span><span> </span><span>black</span><span> </span><span>+</span><span> </span><span>hispan</span><span> </span><span>+</span><span> </span><span>nodegree</span><span> </span><span>+</span><span> </span><span>married</span><span> </span><span>+</span><span> </span><span>re74</span><span> </span><span>+</span><span> </span><span>re75</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>resampled_data</span><span>,</span><span> </span><span>family</span><span> </span><span>=</span><span> </span><span>&quot;binomial&quot;</span><span>)</span><span>
</span><span>resampled_data</span><span> </span><span>&lt;-</span><span> </span><span>resampled_data</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>propensity_gam</span><span> </span><span>=</span><span> </span><span>predict</span><span>(</span><span>mod</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;response&quot;</span><span>),</span><span>
</span><span>weight_gam</span><span> </span><span>=</span><span> </span><span>1</span><span> </span><span>/</span><span> </span><span>propensity_gam</span><span> </span><span>*</span><span> </span><span>treat</span><span> </span><span>+</span><span> </span><span>1</span><span> </span><span>/</span><span> </span><span>(</span><span>1</span><span> </span><span>-</span><span> </span><span>propensity_gam</span><span>)</span><span> </span><span>*</span><span> </span><span>(</span><span>1</span><span> </span><span>-</span><span> </span><span>treat</span><span>))</span><span>
</span><span>est3</span><span> </span><span>&lt;-</span><span> </span><span>coef</span><span>(</span><span>rlm</span><span>(</span><span>re78</span><span> </span><span>~</span><span> </span><span>age</span><span> </span><span>+</span><span> </span><span>treat</span><span> </span><span>+</span><span> </span><span>educ</span><span> </span><span>+</span><span> </span><span>black</span><span> </span><span>+</span><span> </span><span>hispan</span><span> </span><span>+</span><span> </span><span>nodegree</span><span> </span><span>+</span><span> </span><span>married</span><span> </span><span>+</span><span> </span><span>re74</span><span> </span><span>+</span><span> </span><span>re75</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>resampled_data</span><span>,</span><span> </span><span>weights</span><span> </span><span>=</span><span> </span><span>as.vector</span><span>(</span><span>weight_gam</span><span>)))[</span><span>&quot;treat&quot;</span><span>]</span><span>
</span><span># regression model estimate with original data
</span><span> </span><span>est4</span><span> </span><span>&lt;-</span><span> </span><span>coef</span><span>(</span><span>rlm</span><span>(</span><span>re78</span><span> </span><span>~</span><span> </span><span>treat</span><span> </span><span>+</span><span> </span><span>age</span><span> </span><span>+</span><span> </span><span>educ</span><span> </span><span>+</span><span> </span><span>black</span><span> </span><span>+</span><span> </span><span>hispan</span><span> </span><span>+</span><span> </span><span>nodegree</span><span> </span><span>+</span><span> </span><span>married</span><span> </span><span>+</span><span> </span><span>re74</span><span> </span><span>+</span><span> </span><span>re75</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>resampled_data</span><span>))[</span><span>&quot;treat&quot;</span><span>]</span><span>
</span><span>return</span><span>(</span><span>c</span><span>(</span><span>est1</span><span>,</span><span> </span><span>est2</span><span>,</span><span> </span><span>est3</span><span>,</span><span> </span><span>est4</span><span>))</span><span>
</span><span>}</span><span>
</span><span># test gives same results as when done earlier in the post:
</span><span>my_function</span><span>(</span><span>lalonde</span><span>,</span><span> </span><span>1</span><span>:</span><span>nrow</span><span>(</span><span>lalonde</span><span>))</span><span>
</span><span>booted</span><span> </span><span>&lt;-</span><span> </span><span>boot</span><span>(</span><span>lalonde</span><span>,</span><span> </span><span>statistic</span><span> </span><span>=</span><span> </span><span>my_function</span><span>,</span><span> </span><span>R</span><span> </span><span>=</span><span> </span><span>5000</span><span>,</span><span> </span><span>parallel</span><span> </span><span>=</span><span> </span><span>&quot;snow&quot;</span><span>,</span><span> </span><span>cl</span><span> </span><span>=</span><span> </span><span>cluster</span><span>)</span><span>
</span><span>booted_df</span><span> </span><span>&lt;-</span><span> </span><span>as.data.frame</span><span>(</span><span>booted</span><span>$</span><span>t</span><span>)</span><span>
</span><span>names</span><span>(</span><span>booted_df</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;Simple difference with matched data&quot;</span><span>,</span><span> </span><span>&quot;Regression with matched data&quot;</span><span>,</span><span> </span><span>&quot;Regression with weighted data&quot;</span><span>,</span><span>
</span><span>&quot;Regression with original data&quot;</span><span>)</span><span>
</span><span>booted_tidy</span><span> </span><span>&lt;-</span><span> </span><span>booted_df</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>&quot;Method&quot;</span><span>,</span><span> </span><span>&quot;Estimate&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>Method</span><span> </span><span>=</span><span> </span><span>fct_reorder</span><span>(</span><span>Method</span><span>,</span><span> </span><span>Estimate</span><span>))</span><span> </span><span>booted_summary</span><span> </span><span>&lt;-</span><span> </span><span>booted_tidy</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>Method</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>lower</span><span> </span><span>=</span><span> </span><span>quantile</span><span>(</span><span>Estimate</span><span>,</span><span> </span><span>0.025</span><span>),</span><span>
</span><span>upper</span><span> </span><span>=</span><span> </span><span>quantile</span><span>(</span><span>Estimate</span><span>,</span><span> </span><span>0.975</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>type</span><span>,</span><span> </span><span>Estimate</span><span>,</span><span> </span><span>-</span><span>Method</span><span>)</span><span>
</span><span>ggplot</span><span>(</span><span>booted_tidy</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>Estimate</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>Method</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_density</span><span>(</span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.4</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey50&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_segment</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>booted_summary</span><span>,</span><span> </span><span>aes</span><span>(</span><span>xend</span><span> </span><span>=</span><span> </span><span>Estimate</span><span>),</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>yend</span><span> </span><span>=</span><span> </span><span>2e-04</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_text</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>booted_summary</span><span>,</span><span> </span><span>aes</span><span>(</span><span>label</span><span> </span><span>=</span><span> </span><span>round</span><span>(</span><span>Estimate</span><span>)),</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>2.5e-04</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>3</span><span>)</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>Method</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>&quot;none&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_x_continuous</span><span>(</span><span>label</span><span> </span><span>=</span><span> </span><span>dollar</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Treatment effects from four different modelling methods&quot;</span><span>,</span><span>
</span><span>&quot;Distribution and 95% confidence intervals of estimated impact on income in 1978 of a job training program&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Estimated treatment effect&quot;</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Lalonde's 1986 data on a job training program&quot;</span><span>,</span><span>
</span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>)</span></code></pre>
</figure>
<p><img width="100%" src="http://ellisp.github.io/img/0090-job-treatment.svg"></p>
<div>
<pre><code>> # biases revealed by the bootstrap:
> booted

Bootstrap Statistics :
     original     bias    std. error
t1*  908.2022 -268.85736    766.5107
t2*  959.5030  -75.55153    655.4119
t3*  909.9001   12.09643    838.2337
t4* 1182.5056   33.35265    682.6993
</code></pre>
</div>
<p>Those are big confidence intervals – reflecting the small sample size and the difficulty of picking up an impact of an intervention amongst all the complexities of income determinants. I’m satisfied that those are ok depictions of the uncertainty.</p>
<p>All three of the two stage methods that model propensity first give extremely similar results; and the simpler one-stage regression on the original unweighted data gives a result within a third of a standard error. In practice which would I do? I’d probably do it both ways and if there are wildly different results I’d worry, treating that as a diagnostic warning.</p>
<p>Basically with this sort of data, the big gain comes from any modelling strategy that accepts treatment as endogenous and hence you need to control for other variables that are related to both it and the response. Once you’ve adopted a method to do that, the benefits of which particular method are, to me, not particularly material.</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://ellisp.github.io/blog/2017/04/09/propensity-v-regression"> Peter's stats stuff - R</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/Eyed_MxZ23c"></a></div>