diff --git a/book/articles/gformula.qmd b/book/articles/gformula.qmd index c0a7d8db6..9b89bff83 100755 --- a/book/articles/gformula.qmd +++ b/book/articles/gformula.qmd @@ -1,5 +1,5 @@ --- -title: "Causal Inference" +title: "Causal Inference (G-Computation)" --- This vignette has 3 goals: diff --git a/book/articles/matching.qmd b/book/articles/matching.qmd new file mode 100644 index 000000000..b2433ad28 --- /dev/null +++ b/book/articles/matching.qmd @@ -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. \ No newline at end of file diff --git a/book/utils/_quarto.yml b/book/utils/_quarto.yml index a8623f5b8..0372b3905 100644 --- a/book/utils/_quarto.yml +++ b/book/utils/_quarto.yml @@ -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