% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/matchit2optimal.R
\name{method_optimal}
\alias{method_optimal}
\title{Optimal Pair Matching}
\arguments{
\item{formula}{a two-sided \link{formula} object containing the treatment and
covariates to be used in creating the distance measure used in the matching.
This formula will be supplied to the functions that estimate the distance
measure.}

\item{data}{a data frame containing the variables named in \code{formula}.
If not found in \code{data}, the variables will be sought in the
environment.}

\item{method}{set here to \code{"optimal"}.}

\item{distance}{the distance measure to be used. See \code{\link{distance}}
for allowable options. Can be supplied as a distance matrix.}

\item{link}{when \code{distance} is specified as a method of estimating
propensity scores, an additional argument controlling the link function used
in estimating the distance measure. See \code{\link{distance}} for allowable
options with each option.}

\item{distance.options}{a named list containing additional arguments
supplied to the function that estimates the distance measure as determined
by the argument to \code{distance}.}

\item{estimand}{a string containing the desired estimand. Allowable options
include \code{"ATT"} and \code{"ATC"}. See Details.}

\item{exact}{for which variables exact matching should take place.}

\item{mahvars}{for which variables Mahalanobis distance matching should take
place when \code{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.}

\item{antiexact}{for which variables anti-exact matching should take place.
Anti-exact matching is processed using \pkgfun{optmatch}{antiExactMatch}.}

\item{discard}{a string containing a method for discarding units outside a
region of common support. Only allowed when \code{distance} is not
\code{"mahalanobis"} and not a matrix.}

\item{reestimate}{if \code{discard} is not \code{"none"}, whether to
re-estimate the propensity score in the remaining sample prior to matching.}

\item{s.weights}{the variable containing sampling weights to be incorporated
into propensity score models and balance statistics.}

\item{ratio}{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.}

\item{min.controls, max.controls}{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.}

\item{verbose}{\code{logical}; whether information about the matching
process should be printed to the console. What is printed depends on the
matching method. Default is \code{FALSE} for no printing other than
warnings.}

\item{\dots}{additional arguments passed to \pkgfun{optmatch}{fullmatch}.
Allowed arguments include \code{tol} and \code{solver}. See the
\pkgfun{optmatch}{fullmatch} documentation for details. In general, \code{tol}
should be set to a low number (e.g., \code{1e-7}) to get a more precise
solution (default is \code{1e-3}).

The arguments \code{replace}, \code{caliper}, and \code{m.order} are ignored with a warning.}
}
\description{
In \code{\link[=matchit]{matchit()}}, setting \code{method = "optimal"} performs optimal pair
matching. The matching is optimal in the sense that that sum of the absolute
pairwise distances in the matched sample is as small as possible. The method
functionally relies on \pkgfun{optmatch}{fullmatch}.

Advantages of optimal pair matching include that the matching order is not
required to be specified and it is less likely that extreme within-pair
distances will be large, unlike with nearest neighbor matching. Generally,
however, as a subset selection method, optimal pair matching tends to
perform similarly to nearest neighbor matching in that similar subsets of
units will be selected to be matched.

This page details the allowable arguments with \code{method = "optmatch"}.
See \code{\link[=matchit]{matchit()}} for an explanation of what each argument means in a general
context and how it can be specified.

Below is how \code{matchit()} is used for optimal pair matching:
\preformatted{
matchit(formula,
        data = NULL,
        method = "optimal",
        distance = "glm",
        link = "logit",
        distance.options = list(),
        estimand = "ATT",
        exact = NULL,
        mahvars = NULL,
        antiexact = NULL,
        discard = "none",
        reestimate = FALSE,
        s.weights = NULL,
        ratio = 1,
        min.controls = NULL,
        max.controls = NULL,
        verbose = FALSE,
        ...) }
}
\details{
\subsection{Mahalanobis Distance Matching}{

Mahalanobis distance matching can be done one of two ways:
\enumerate{
\item{If no propensity score needs to be estimated, \code{distance} should be
set to \code{"mahalanobis"}, and Mahalanobis distance matching will occur
using all the variables in \code{formula}. Arguments to \code{discard} and
\code{mahvars} will be ignored. For example, to perform simple Mahalanobis
distance matching, the following could be run:

\preformatted{
matchit(treat ~ X1 + X2, method = "nearest",
        distance = "mahalanobis") }

With this code, the Mahalanobis distance is computed using \code{X1} and
\code{X2}, and matching occurs on this distance. The \code{distance}
component of the \code{matchit()} output will be empty.
}
\item{If a propensity score needs to be estimated for common support with
\code{discard}, \code{distance} should be whatever method is used to
estimate the propensity score or a vector of distance measures, i.e., it
should not be \code{"mahalanobis"}. Use \code{mahvars} to specify the
variables used to create the Mahalanobis distance. For example, to perform
Mahalanobis after discarding units outside the common support of the
propensity score in both groups, the following could be run:

\preformatted{
matchit(treat ~ X1 + X2 + X3, method = "nearest",
        distance = "glm", discard = "both",
        mahvars = ~ X1 + X2) }

With this code, \code{X1}, \code{X2}, and \code{X3} are used to estimate the
propensity score (using the \code{"glm"} method, which by default is
logistic regression), which is used to identify the common support. The
actual matching occurs on the Mahalanobis distance computed only using
\code{X1} and \code{X2}, which are supplied to \code{mahvars}. The estimated
propensity scores will be included in the \code{distance} component of the
\code{matchit()} output.
}
}
}

\subsection{Estimand}{

The \code{estimand} argument controls whether control units are selected to be matched with treated units
(\code{estimand = "ATT"}) or treated units are selected to be matched with
control units (\code{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. Setting \code{estimand = "ATC"} is equivalent to
swapping all treated and control labels for the treatment variable. When
\code{estimand = "ATC"}, the \code{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 \code{estimand = "ATT"}). Note that the argument supplied to \code{estimand} doesn't
necessarily correspond to the estimand actually targeted; it is merely a
switch to trigger which treatment group is considered "focal".
}

\subsection{Variable Ratio Matching}{

\code{matchit()} can perform variable
ratio matching, which involves matching a different number of control units
to each treated unit. When \code{ratio > 1}, rather than requiring all
treated units to receive \code{ratio} matches, the arguments to
\code{max.controls} and \code{min.controls} can be specified to control the
maximum and minimum number of matches each treated unit can have.
\code{ratio} controls how many total control units will be matched: \code{n1 * ratio} control
units will be matched, where \code{n1} is the number of
treated units, yielding the same total number of matched controls as fixed
ratio matching does.

Variable ratio matching can be used with any \code{distance} specification.
\code{ratio} does not have to be an integer but must be greater than 1 and
less than \code{n0/n1}, where \code{n0} and \code{n1} are the number of
control and treated units, respectively. Setting \code{ratio = n0/n1}
performs a restricted form of full matching where all control units are
matched. If \code{min.controls} is not specified, it is set to 1 by default.
\code{min.controls} must be less than \code{ratio}, and \code{max.controls}
must be greater than \code{ratio}. See the Examples section of
\code{\link[=method_nearest]{method_nearest()}} for an example of their use, which is the same
as it is with optimal matching.
}
}
\note{
Optimal pair matching is a restricted form of optimal full matching
where the number of treated units in each subclass is equal to 1, whereas in
unrestricted full matching, multiple treated units can be assigned to the
same subclass. \pkgfun{optmatch}{pairmatch} is simply a wrapper for
\pkgfun{optmatch}{fullmatch}, which performs optimal full matching and is the
workhorse for \code{\link{method_full}}. In the same way, \code{matchit()}
uses \code{optmatch::fullmatch()} under the hood, imposing the restrictions that
make optimal full matching function like optimal pair matching (which is
simply to set \code{min.controls >= 1} and to pass \code{ratio} to the
\code{mean.controls} argument). This distinction is not important for
regular use but may be of interest to those examining the source code.

The option \code{"optmatch_max_problem_size"} is automatically set to
\code{Inf} during the matching process, different from its default in
\emph{optmatch}. This enables matching problems of any size to be run, but
may also let huge, infeasible problems get through and potentially take a
long time or crash R. See \pkgfun{optmatch}{setMaxProblemSize} for more details.

A preprocessing algorithm describe by Sävje (2020; \doi{10.1214/19-STS739}) is used to improve the speed of the matching when 1:1 matching on a propensity score. It does so by adding an additional constraint that guarantees a solution as optimal as the solution that would have been found without the constraint, and that constraint often dramatically reduces the size of the matching problem at no cost. However, this may introduce differences between the results obtained by \emph{MatchIt} and by \emph{optmatch}, though such differences will shrink when smaller values of \code{tol} are used.
}
\section{Outputs}{


All outputs described in \code{\link[=matchit]{matchit()}} are returned with
\code{method = "optimal"}. When \code{include.obj = TRUE} in the call to
\code{matchit()}, the output of the call to \code{optmatch::fullmatch()} will be
included in the output. When \code{exact} is specified, this will be a list
of such objects, one for each stratum of the \code{exact} variables.
}

\examples{
\dontshow{if (requireNamespace("optmatch", quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf}
data("lalonde")

#1:1 optimal PS matching with exact matching on race
m.out1 <- matchit(treat ~ age + educ + race +
                    nodegree + married + re74 + re75,
                  data = lalonde,
                  method = "optimal",
                  exact = ~race)
m.out1
summary(m.out1)

#2:1 optimal matching on the scaled Euclidean distance
m.out2 <- matchit(treat ~ age + educ + race +
                    nodegree + married + re74 + re75,
                  data = lalonde,
                  method = "optimal",
                  ratio = 2,
                  distance = "scaled_euclidean")
m.out2
summary(m.out2, un = FALSE)
\dontshow{\}) # examplesIf}
}
\references{
In a manuscript, be sure to cite the following paper if using
\code{matchit()} with \code{method = "optimal"}:

Hansen, B. B., & Klopfer, S. O. (2006). Optimal Full Matching and Related
Designs via Network Flows. Journal of Computational and Graphical
Statistics, 15(3), 609–627. \doi{10.1198/106186006X137047}

For example, a sentence might read:

\emph{Optimal pair matching was performed using the MatchIt package (Ho,
Imai, King, & Stuart, 2011) in R, which calls functions from the optmatch
package (Hansen & Klopfer, 2006).}
}
\seealso{
\code{\link[=matchit]{matchit()}} for a detailed explanation of the inputs and outputs of
a call to \code{matchit()}.

\pkgfun{optmatch}{fullmatch}, which is the workhorse.

\code{\link{method_full}} for optimal full matching, of which optimal pair
matching is a special case, and which relies on similar machinery.
}
