knitr::opts_chunk$set( collapse = FALSE, warning = FALSE, message = FALSE, tidy = FALSE, fig.align='center', comment = "#>", fig.path = "man/figures/README-", R.options = list(width = 200) )
MatchIt
provides a simple and straightforward interface to various methods of matching for covariate balance in observational studies. Matching is one way to reduce confounding and model dependence when estimating treatment effects. Several matching methods are available, including nearest neighbor matching, optimal pair matching, optimal full matching, generalized full matching, genetic matching, exact matching, coarsened exact matching, cardinality matching, and subclassification, some of which rely on functions from other R packages. A variety of methods to estimate propensity scores for propensity score matching are included. Below is an example of the use of MatchIt
to perform Mahalanobis distance matching with replacement and assess balance:
library("MatchIt") data("lalonde", package = "MatchIt") # 1:1 nearest neighbor matching with replacement on # the Mahalanobis distance m.out <- matchit(treat ~ age + educ + race + married + nodegree + re74 + re75, data = lalonde, distance = "mahalanobis", replace = TRUE)
Printing the MatchIt
object provides details of the kind of matching performed.
m.out
We can check covariate balance for the original and matched samples using summary()
:
#Checking balance before and after matching: summary(m.out)
At the top is balance for the original sample. Below that is balance in the matched sample. Smaller values for the balance statistics indicate better balance. (In this case, fairly good balance was achieved, but other matching methods should be tried). We can plot the standardized mean differences in a Love plot for a clean, visual display of balance across the sample:
#Plot balance plot(summary(m.out))
Although much has been written about matching theory, most of the theory relied upon in MatchIt
is described well in Ho, Imai, King, and Stuart (2007), Stuart (2010), and Greifer and Stuart (2021). The Journal of Statistical Software article for MatchIt
can be accessed here, though note that some options have changed, so the MatchIt
reference pages and included vignettes should be used for understanding the functions and methods available. Further references for individual methods are present in their respective help pages. The MatchIt
website provides access to vignettes and documentation files.
MatchIt
Please cite MatchIt
when using it for analysis presented in publications, which you can do by citing the Journal of Statistical Software article below:
Ho, D. E., Imai, K., King, G., & Stuart, E. A. (2011). MatchIt: Nonparametric Preprocessing for Parametric Causal Inference. Journal of Statistical Software, 42(8). doi:10.18637/jss.v042.i08
This citation can also be accessed using citation("MatchIt")
in R. For reproducibility purposes, it is also important to include the version number for the version used.
To download and install the latest stable version of MatchIt
from CRAN, run the following:
install.packages("MatchIt")
To install a development version, which may have a bug fixed or a new feature, run the following:
install.packages("pak") #If not yet installed pak::pkg_install("ngreifer/MatchIt")
This will require R to compile C++ code, which might require additional software to be installed on your computer. If you need the development version but can't compile the package, ask the maintainer for a binary version of the package.
If you have questions about MatchIt
or want to submit a bug report, please do so using the GitHub issues page for the package. Suggestions for new features and kind words of support are encouraged as well. Remember this software is free and developed by volunteers.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.