Skip to content

Commit

Permalink
matching vignette
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Oct 2, 2023
1 parent 56e4862 commit 6e80ad0
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion book/articles/gformula.qmd
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:
Expand Down
68 changes: 68 additions & 0 deletions book/articles/matching.qmd
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.
1 change: 1 addition & 0 deletions book/utils/_quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ book:
- articles/gam.qmd
- articles/logit.qmd
- articles/lme4.qmd
- articles/matching.qmd
- articles/multiple_imputation.qmd
- articles/mrp.qmd
- articles/svalues.qmd
Expand Down

0 comments on commit 6e80ad0

Please sign in to comment.