-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
56e4862
commit 6e80ad0
Showing
3 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
title: "Causal Inference" | ||
title: "Causal Inference (G-Computation)" | ||
--- | ||
|
||
This vignette has 3 goals: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
--- | ||
title: "Matching" | ||
author: "Vincent Arel-Bundock" | ||
--- | ||
|
||
This chapter introduces how to use `marginaleffects` to estimate treatment effects after pre-processing a dataset to achieve better covariate balance. The presentation is very short. Readers who seek a more comprehensive understanding and application of these methods should refer to [Noah Greifer's excellent and detailed work on the topic](https://ngreifer.github.io/) and to the [`MatchIt` package vignettes and website](https://kosukeimai.github.io/MatchIt/) | ||
|
||
The procedure we highlight can be broken down into three steps: | ||
|
||
1. Use `MatchIt` to pre-process the data and achieve better covariate balance | ||
2. Fit a regression model to the outcome of interest | ||
3. Use `marginaleffects` and [G-Computation](https://marginaleffects.com/articles/gformula.html) to estimate a quantity of interest, such as the Average treatment effect on the treated (ATT) | ||
|
||
To begin, we load libraries and the data from the classic Lalonde experiment: | ||
|
||
```{r} | ||
library("MatchIt") | ||
library("marginaleffects") | ||
data("lalonde", package = "MatchIt") | ||
head(lalonde) | ||
``` | ||
|
||
We are interested in the treatment effect of the `treat` variable on the `re78` outcome. The `treat` variable is a binary variable indicating whether the individual received job training. The `re78` variable is the individual's earnings in 1978. | ||
|
||
|
||
## Matching | ||
|
||
The first step is to pre-process the dataset to achieve better covariate balance. To do this, we use the `MatchIt::matchit()` function and a 1-to-1 nearest neighbor matching with replacement on the Mahaloanobis distance. This function supports many other matching methods, see `?matchit`. | ||
|
||
```{r} | ||
dat <- matchit( | ||
treat ~ age + educ + race + married + nodegree + re74 + re75, | ||
data = lalonde, distance = "mahalanobis", | ||
replace = TRUE) | ||
dat <- match.data(dat) | ||
``` | ||
|
||
## Fitting | ||
|
||
Now, we estimate a linear regression model with interactions between the treatment and covariates. Note that we use the `weights` argument to use the weights supplied by our matching method: | ||
|
||
```{r} | ||
fit <- lm( | ||
re78 ~ treat * (age + educ + race + married + nodegree), | ||
data = dat, | ||
weights = weights) | ||
``` | ||
|
||
## Quantity of interest | ||
|
||
Finally, we use the `avg_comparisons()` function of the `marginaleffects` package to estimate the ATT and its standard error. In effect, this function applies [G-Computation](https://marginaleffects.com/articles/gformula.html) to estimate the quantity of interest. We use the following arguments: | ||
|
||
* `variables="treat"` indicates that we are interested in the effect of the `treat` variable. | ||
* `newdata=subset(dat, treat == 1)` indicates that we want to estimate the effect for the treated individuals only (i.e., the ATT). | ||
* `wts="weights"` indicates that we want to use the weights supplied by the matching method. | ||
|
||
```{r, warning=FALSE} | ||
avg_comparisons( | ||
fit, | ||
variables = "treat", | ||
newdata = subset(dat, treat == 1), | ||
wts = "weights") | ||
``` | ||
|
||
## Learn more | ||
|
||
[The `MatchIt` vignette titled "Estimating Effects After Matching"](https://kosukeimai.github.io/MatchIt/articles/estimating-effects.html) describes many more options, including different measures of uncertainty (bootstrap, clustering, etc.), different estimands (ATE, etc.), and different strategies for adjustment. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters