In matchit()
, setting method = "nearest"
performs greedy nearest
neighbor matching. A distance is computed between each treated unit and each
control unit, and, one by one, each treated unit is assigned a control unit
as a match. The matching is "greedy" in the sense that there is no action
taken to optimize an overall criterion; each match is selected without
considering the other matches that may occur subsequently.
This page details the allowable arguments with method = "nearest"
.
See matchit()
for an explanation of what each argument means in a general
context and how it can be specified.
Below is how matchit()
is used for nearest neighbor matching:
matchit(formula,
data = NULL,
method = "nearest",
distance = "glm",
link = "logit",
distance.options = list(),
estimand = "ATT",
exact = NULL,
mahvars = NULL,
antiexact = NULL,
discard = "none",
reestimate = FALSE,
s.weights = NULL,
replace = TRUE,
m.order = NULL,
caliper = NULL,
ratio = 1,
min.controls = NULL,
max.controls = NULL,
verbose = FALSE,
...)
a two-sided formula object containing the treatment and covariates to be used in creating the distance measure used in the matching.
a data frame containing the variables named in formula
.
If not found in data
, the variables will be sought in the
environment.
set here to "nearest"
.
the distance measure to be used. See distance
for allowable options. Can be supplied as a distance matrix.
when distance
is specified as a method of estimating
propensity scores, an additional argument controlling the link function used
in estimating the distance measure. See distance
for allowable
options with each option.
a named list containing additional arguments
supplied to the function that estimates the distance measure as determined
by the argument to distance
.
a string containing the desired estimand. Allowable options
include "ATT"
and "ATC"
. See Details.
for which variables exact matching should take place.
for which variables Mahalanobis distance matching should take
place when distance
corresponds to a propensity score (e.g., for
caliper matching or to discard units for common support). If specified, the
distance measure will not be used in matching.
for which variables ant-exact matching should take place.
a string containing a method for discarding units outside a
region of common support. Only allowed when distance
corresponds to a
propensity score.
if discard
is not "none"
, whether to
re-estimate the propensity score in the remaining sample prior to matching.
the variable containing sampling weights to be incorporated into propensity score models and balance statistics.
whether matching should be done with replacement.
the order that the matching takes place. The default is
"largest"
when distance
corresponds to a propensity score and
"data"
otherwise. See matchit()
for allowable options.
the width(s) of the caliper(s) used for caliper matching. See Details and Examples.
logical
; when calipers are specified, whether they
are in standard deviation units (TRUE
) or raw units (FALSE
).
how many control units should be matched to each treated unit for k:1 matching. For variable ratio matching, see section "Variable Ratio Matching" in Details below.
for variable ratio matching, the minimum and maximum number of controls units to be matched to each treated unit. See section "Variable Ratio Matching" in Details below.
logical
; whether information about the matching
process should be printed to the console. When TRUE
, a progress bar
implemented using RcppProgress will be displayed.
additional arguments that control the matching specification:
reuse.max
numeric
; the maximum number of
times each control can be used as a match. Setting reuse.max = 1
corresponds to matching without replacement (i.e., replace = FALSE
),
and setting reuse.max = Inf
corresponds to traditional matching with
replacement (i.e., replace = TRUE
) with no limit on the number of
times each control unit can be matched. Other values restrict the number of
times each control can be matched when matching with replacement.
replace
is ignored when reuse.max
is specified.
unit.id
one or more variables containing a unit ID for each
observation, i.e., in case multiple observations correspond to the same
unit. Once a control observation has been matched, no other observation with
the same unit ID can be used as matches. This ensures each control unit is
used only once even if it has multiple observations associated with it.
Omitting this argument is the same as giving each observation a unique ID.
Ignored when replace = TRUE
.
Mahalanobis distance matching can be done one of two ways:
If no propensity score needs to be estimated, distance
should be
set to "mahalanobis"
, and Mahalanobis distance matching will occur
using all the variables in formula
. Arguments to discard
and
mahvars
will be ignored, and a caliper can only be placed on named
variables. For example, to perform simple Mahalanobis distance matching, the
following could be run:
matchit(treat ~ X1 + X2, method = "nearest",
distance = "mahalanobis")
X1
and
X2
, and matching occurs on this distance. The distance
component of the matchit()
output will be empty.If a propensity score needs to be estimated for any reason, e.g., for
common support with discard
or for creating a caliper,
distance
should be whatever method is used to estimate the propensity
score or a vector of distance measures. Use mahvars
to specify the
variables used to create the Mahalanobis distance. For example, to perform
Mahalanobis within a propensity score caliper, the following could be run:
matchit(treat ~ X1 + X2 + X3, method = "nearest",
distance = "glm", caliper = .25,
mahvars = ~ X1 + X2)
X1
, X2
, and X3
are used to estimate the
propensity score (using the "glm"
method, which by default is
logistic regression), which is used to create a matching caliper. The actual
matching occurs on the Mahalanobis distance computed only using X1
and X2
, which are supplied to mahvars
. Units whose propensity
score difference is larger than the caliper will not be paired, and some
treated units may therefore not receive a match. The estimated propensity
scores will be included in the distance
component of the
matchit()
output. See Examples.The estimand
argument controls whether control units are selected to be
matched with treated units (estimand = "ATT"
) or treated units are
selected to be matched with control units (estimand = "ATC"
). The
"focal" group (e.g., the treated units for the ATT) is typically made to be
the smaller treatment group, and a warning will be thrown if it is not set
that way unless replace = TRUE
. Setting estimand = "ATC"
is
equivalent to swapping all treated and control labels for the treatment
variable. When estimand = "ATC"
, the default m.order
is
"smallest"
, and the match.matrix
component of the output will
have the names of the control units as the rownames and be filled with the
names of the matched treated units (opposite to when estimand = "ATT"
). Note that the argument supplied to estimand
doesn't
necessarily correspond to the estimand actually targeted; it is merely a
switch to trigger which treatment group is considered "focal".
matchit()
can perform variable
ratio "extremal" matching as described by Ming and Rosenbaum (2000). This
method tends to result in better balance than fixed ratio matching at the
expense of some precision. When ratio > 1
, rather than requiring all
treated units to receive ratio
matches, each treated unit is assigned
a value that corresponds to the number of control units they will be matched
to. These values are controlled by the arguments min.controls
and
max.controls
, which correspond to \(\alpha\) and \(\beta\),
respectively, in Ming and Rosenbaum (2000), and trigger variable ratio
matching to occur. Some treated units will receive min.controls
matches and others will receive max.controls
matches (and one unit
may have an intermediate number of matches); how many units are assigned
each number of matches is determined by the algorithm described in Ming and
Rosenbaum (2000, p119). ratio
controls how many total control units
will be matched: n1 * ratio
control units will be matched, where
n1
is the number of treated units, yielding the same total number of
matched controls as fixed ratio matching does.
Variable ratio matching cannot be used with Mahalanobis distance matching or
when distance
is supplied as a matrix. The calculations of the
numbers of control units each treated unit will be matched to occurs without
consideration of caliper
or discard
. ratio
does not
have to be an integer but must be greater than 1 and less than n0/n1
,
where n0
and n1
are the number of control and treated units,
respectively. Setting ratio = n0/n1
performs a crude form of full
matching where all control units are matched. If min.controls
is not
specified, it is set to 1 by default. min.controls
must be less than
ratio
, and max.controls
must be greater than ratio
. See
Examples below for an example of their use.
Sometimes an error will be produced by Rcpp along the lines of
"function 'Rcpp_precious_remove' not provided by package 'Rcpp'"
. It
is not immediately clear why this happens, though
this
thread appears to provide some insight. In a fresh session, run
remove.packages(c("MatchIt", "Rcpp")); install.packages("MatchIt")
.
This should sync MatchIt and Rcpp and ensure they work
correctly.
All outputs described in matchit()
are returned with
method = "nearest"
. When replace = TRUE
, the subclass
component is omitted. include.obj
is ignored.
In a manuscript, you don't need to cite another package when
using method = "nearest"
because the matching is performed completely
within MatchIt. For example, a sentence might read:
Nearest neighbor matching was performed using the MatchIt package (Ho, Imai, King, & Stuart, 2011) in R.
matchit()
for a detailed explanation of the inputs and outputs of
a call to matchit()
.
method_optimal()
for optimal pair matching, which is similar to
nearest neighbor matching except that an overall distance criterion is
minimized.
data("lalonde")
# 1:1 greedy NN matching on the PS
m.out1 <- matchit(treat ~ age + educ + race + nodegree +
married + re74 + re75, data = lalonde,
method = "nearest")
m.out1
#> A matchit object
#> - method: 1:1 nearest neighbor matching without replacement
#> - distance: Propensity score
#> - estimated with logistic regression
#> - number of obs.: 614 (original), 370 (matched)
#> - target estimand: ATT
#> - covariates: age, educ, race, nodegree, married, re74, re75
summary(m.out1)
#>
#> Call:
#> matchit(formula = treat ~ age + educ + race + nodegree + married +
#> re74 + re75, data = lalonde, method = "nearest")
#>
#> Summary of Balance for All Data:
#> Means Treated Means Control Std. Mean Diff. Var. Ratio eCDF Mean
#> distance 0.5774 0.1822 1.7941 0.9211 0.3774
#> age 25.8162 28.0303 -0.3094 0.4400 0.0813
#> educ 10.3459 10.2354 0.0550 0.4959 0.0347
#> raceblack 0.8432 0.2028 1.7615 . 0.6404
#> racehispan 0.0595 0.1422 -0.3498 . 0.0827
#> racewhite 0.0973 0.6550 -1.8819 . 0.5577
#> nodegree 0.7081 0.5967 0.2450 . 0.1114
#> married 0.1892 0.5128 -0.8263 . 0.3236
#> re74 2095.5737 5619.2365 -0.7211 0.5181 0.2248
#> re75 1532.0553 2466.4844 -0.2903 0.9563 0.1342
#> eCDF Max
#> distance 0.6444
#> age 0.1577
#> educ 0.1114
#> raceblack 0.6404
#> racehispan 0.0827
#> racewhite 0.5577
#> nodegree 0.1114
#> married 0.3236
#> re74 0.4470
#> re75 0.2876
#>
#> Summary of Balance for Matched Data:
#> Means Treated Means Control Std. Mean Diff. Var. Ratio eCDF Mean
#> distance 0.5774 0.3629 0.9740 0.7564 0.1321
#> age 25.8162 25.2054 0.0854 0.4633 0.0839
#> educ 10.3459 10.5838 -0.1183 0.5862 0.0222
#> raceblack 0.8432 0.4703 1.0259 . 0.3730
#> racehispan 0.0595 0.2108 -0.6400 . 0.1514
#> racewhite 0.0973 0.3189 -0.7478 . 0.2216
#> nodegree 0.7081 0.6486 0.1308 . 0.0595
#> married 0.1892 0.2000 -0.0276 . 0.0108
#> re74 2095.5737 2476.9475 -0.0780 1.2629 0.0546
#> re75 1532.0553 1669.7419 -0.0428 1.4712 0.0508
#> eCDF Max Std. Pair Dist.
#> distance 0.4216 0.9740
#> age 0.2541 1.3893
#> educ 0.0649 1.2205
#> raceblack 0.3730 1.0259
#> racehispan 0.1514 1.0514
#> racewhite 0.2216 0.8208
#> nodegree 0.0595 0.9868
#> married 0.0108 0.8005
#> re74 0.2865 0.8160
#> re75 0.2108 0.7552
#>
#> Sample Sizes:
#> Control Treated
#> All 429 185
#> Matched 185 185
#> Unmatched 244 0
#> Discarded 0 0
#>
# 3:1 NN Mahalanobis distance matching with
# replacement within a PS caliper
m.out2 <- matchit(treat ~ age + educ + race + nodegree +
married + re74 + re75, data = lalonde,
method = "nearest", replace = TRUE,
mahvars = ~ age + educ + re74 + re75,
ratio = 3, caliper = .02)
m.out2
#> A matchit object
#> - method: 3:1 nearest neighbor matching with replacement
#> - distance: Mahalanobis [matching]
#> Propensity score [caliper]
#> - estimated with logistic regression
#> - caliper: <distance> (0.006)
#> - number of obs.: 614 (original), 299 (matched)
#> - target estimand: ATT
#> - covariates: age, educ, race, nodegree, married, re74, re75
summary(m.out2, un = FALSE)
#>
#> Call:
#> matchit(formula = treat ~ age + educ + race + nodegree + married +
#> re74 + re75, data = lalonde, method = "nearest", mahvars = ~age +
#> educ + re74 + re75, replace = TRUE, caliper = 0.02, ratio = 3)
#>
#> Summary of Balance for Matched Data:
#> Means Treated Means Control Std. Mean Diff. Var. Ratio eCDF Mean
#> distance 0.5660 0.5660 0.0003 0.9880 0.0037
#> age 25.5000 24.0010 0.2095 0.5178 0.0835
#> educ 10.3086 10.3930 -0.0420 0.7356 0.0115
#> raceblack 0.8210 0.8169 0.0113 . 0.0041
#> racehispan 0.0679 0.0576 0.0435 . 0.0103
#> racewhite 0.1111 0.1255 -0.0486 . 0.0144
#> nodegree 0.7037 0.6852 0.0407 . 0.0185
#> married 0.1914 0.1132 0.1996 . 0.0782
#> re74 1831.9348 2119.9268 -0.0589 1.2783 0.0528
#> re75 1400.4354 1386.7669 0.0042 2.2735 0.0623
#> eCDF Max Std. Pair Dist.
#> distance 0.0432 0.0128
#> age 0.3508 0.9787
#> educ 0.0381 0.7836
#> raceblack 0.0041 0.0358
#> racehispan 0.0103 0.2643
#> racewhite 0.0144 0.1845
#> nodegree 0.0185 0.5556
#> married 0.0782 0.4588
#> re74 0.2984 0.4634
#> re75 0.2428 0.5274
#>
#> Sample Sizes:
#> Control Treated
#> All 429. 185
#> Matched (ESS) 64.19 162
#> Matched 137. 162
#> Unmatched 292. 23
#> Discarded 0. 0
#>
# 1:1 NN Mahalanobis distance matching within calipers
# on re74 and re75 and exact matching on married and race
m.out3 <- matchit(treat ~ age + educ + re74 + re75, data = lalonde,
method = "nearest", distance = "mahalanobis",
exact = ~ married + race,
caliper = c(re74 = .2, re75 = .15))
#> Warning: Fewer control units than treated units in some `exact` strata; not all treated units will get a match.
m.out3
#> A matchit object
#> - method: 1:1 nearest neighbor matching without replacement
#> - distance: Mahalanobis
#> - caliper: re74 (1295.593), re75 (494.352)
#> - number of obs.: 614 (original), 166 (matched)
#> - target estimand: ATT
#> - covariates: age, educ, re74, re75, married, race
summary(m.out3, un = FALSE)
#>
#> Call:
#> matchit(formula = treat ~ age + educ + re74 + re75, data = lalonde,
#> method = "nearest", distance = "mahalanobis", exact = ~married +
#> race, caliper = c(re74 = 0.2, re75 = 0.15))
#>
#> Summary of Balance for Matched Data:
#> Means Treated Means Control Std. Mean Diff. Var. Ratio eCDF Mean
#> age 24.7711 24.6024 0.0236 0.4804 0.0753
#> educ 10.3976 10.0482 0.1738 0.4610 0.0361
#> re74 705.7326 864.0131 -0.0324 0.9038 0.0270
#> re75 592.6384 640.9355 -0.0150 1.0194 0.0150
#> married 0.1687 0.1687 0.0000 . 0.0000
#> raceblack 0.7349 0.7349 0.0000 . 0.0000
#> racehispan 0.0723 0.0723 0.0000 . 0.0000
#> racewhite 0.1928 0.1928 0.0000 . 0.0000
#> eCDF Max Std. Pair Dist.
#> age 0.2410 0.7544
#> educ 0.0964 0.7131
#> re74 0.2410 0.0537
#> re75 0.1084 0.0358
#> married 0.0000 0.0000
#> raceblack 0.0000 0.0000
#> racehispan 0.0000 0.0000
#> racewhite 0.0000 0.0000
#>
#> Sample Sizes:
#> Control Treated
#> All 429 185
#> Matched 83 83
#> Unmatched 346 102
#> Discarded 0 0
#>
# 2:1 variable ratio NN matching on the PS
m.out4 <- matchit(treat ~ age + educ + race + nodegree +
married + re74 + re75, data = lalonde,
method = "nearest", ratio = 2,
min.controls = 1, max.controls = 12)
m.out4
#> A matchit object
#> - method: Variable ratio 2:1 nearest neighbor matching without replacement
#> - distance: Propensity score
#> - estimated with logistic regression
#> - number of obs.: 614 (original), 555 (matched)
#> - target estimand: ATT
#> - covariates: age, educ, race, nodegree, married, re74, re75
summary(m.out4, un = FALSE)
#>
#> Call:
#> matchit(formula = treat ~ age + educ + race + nodegree + married +
#> re74 + re75, data = lalonde, method = "nearest", ratio = 2,
#> min.controls = 1, max.controls = 12)
#>
#> Summary of Balance for Matched Data:
#> Means Treated Means Control Std. Mean Diff. Var. Ratio eCDF Mean
#> distance 0.5774 0.3608 0.9835 0.7425 0.1414
#> age 25.8162 25.4555 0.0504 0.4556 0.0830
#> educ 10.3459 10.4836 -0.0685 0.5655 0.0236
#> raceblack 0.8432 0.4703 1.0259 . 0.3730
#> racehispan 0.0595 0.2110 -0.6408 . 0.1515
#> racewhite 0.0973 0.3187 -0.7472 . 0.2214
#> nodegree 0.7081 0.6523 0.1229 . 0.0559
#> married 0.1892 0.2217 -0.0830 . 0.0325
#> re74 2095.5737 2613.7257 -0.1060 1.1678 0.0612
#> re75 1532.0553 1709.9255 -0.0553 1.3601 0.0496
#> eCDF Max Std. Pair Dist.
#> distance 0.4216 0.5599
#> age 0.2453 1.4056
#> educ 0.0653 1.3160
#> raceblack 0.3730 0.5129
#> racehispan 0.1515 1.0400
#> racewhite 0.2214 0.8208
#> nodegree 0.0559 1.0701
#> married 0.0325 1.0006
#> re74 0.2916 0.9103
#> re75 0.2015 0.8431
#>
#> Sample Sizes:
#> Control Treated
#> All 429 185
#> Matched (ESS) 202 185
#> Matched 370 185
#> Unmatched 59 0
#> Discarded 0 0
#>
# Some units received 1 match and some received 12
table(table(m.out4$subclass[m.out4$treat == 0]))
#>
#> 1 10 12
#> 168 1 16