Skip to main content
v2026.11,986 entries · CC-BY 4.0

MatchIt in R: Install, matchit() Options, Balance Output

Install MatchIt from CRAN, run a worked matchit() example, map options like replace, mahvars and caliper to questions, and read balance output.

Written and maintained by CASRAI Editorial Board

Published · Last updated

On this page

MatchIt is an R package (CRAN, GPL-2 or GPL-3) that preprocesses observational data by matching, weighting or subclassifying treated and control units so that the covariates are balanced before you estimate a treatment effect. This page is for analysts who need to install it, run a first match, choose among the matchit() options, and read the balance output well enough to decide whether to keep matching or stop. It was checked against CRAN and the package documentation on 2026-11-07.

What MatchIt is used for in research

MatchIt implements the approach of Ho, Imai, King and Stuart (2007), "Matching as Nonparametric Preprocessing for Reducing Model Dependence in Parametric Causal Inference": match first, then fit your usual outcome model on the matched data. The CRAN page lists the maintainer as Noah Greifer and version 4.8.2 (published 2026-11-01), which depends on R 4.1.0 or later. Typical uses are cohort comparisons without randomisation, such as program evaluations or exposure studies, where treated and untreated groups differ on measured covariates. For the method behind it, see propensity score matching explained; for a Stata equivalent, see propensity score matching in Stata.

MatchIt is distributed as an R package. If you search for "matchit r", this is the package meant; the other R packages that do related work are compared in the alternatives table below.

Install or access (official source only)

MatchIt is on CRAN. From an R session with R 4.1.0 or later:

install.packages("MatchIt")
library("MatchIt")

The general mechanics of installing packages are covered in how to install R packages. The package’s Suggests field lists optmatch, Matching, cobalt, marginaleffects and sandwich, which support optimal matching, genetic matching, extra balance plots and effect estimation. The optional gurobi solver is not on CRAN. The package website with the reference pages and vignettes is kosukeimai.github.io/MatchIt.

Step-by-step task: match, check balance, estimate an effect

The steps follow the "Getting Started" vignette, which uses the lalonde job-training data shipped with the package (treatment treat, outcome re78).

  1. Check the starting imbalance without matching. Setting method = NULL estimates the propensity score but does not match:
    m.out0 <- matchit(treat ~ age + educ + race + married + nodegree + re74 + re75,
                      data = lalonde, method = NULL, distance = "glm")
    summary(m.out0)
  2. Run a match. Nearest-neighbor matching on a logistic propensity score is the default (method = "nearest", distance = "glm", link = "logit"):
    m.out1 <- matchit(treat ~ age + educ + race + married + nodegree + re74 + re75,
                      data = lalonde, method = "nearest", distance = "glm")
  3. Read the balance. summary(m.out1, un = FALSE) prints only the matched-sample table. Plot it with plot(summary(m.out1)), or draw distributions with plot(m.out1, type = "density", interactive = FALSE, which.xs = ~age + married + re75). For publication-style plots see Love plots and covariate balance.
  4. Iterate if needed. The vignette’s next match uses method = "full" with link = "probit". Change one option at a time and compare the summaries.
  5. Extract the matched data. m.data <- match_data(m.out1) returns the matched sample with three added columns: distance, weights and subclass.
  6. Estimate the effect. Fit the outcome model with the weights and use cluster-robust standard errors, for example with the marginaleffects package:
    library("marginaleffects")
    fit <- lm(re78 ~ treat * (age + educ + race + married + nodegree + re74 + re75),
              data = m.data, weights = weights)
    avg_comparisons(fit, variables = "treat", vcov = ~subclass,
                    newdata = subset(treat == 1))

The vignette states: "The outcome model coefficients and tests should not be interpreted or reported," and recommends cluster-robust standard errors for most analyses, with pair membership as the clustering variable. Report the estimated effect from the comparison step, not the raw regression coefficients.

Which matchit() option answers which question

The full signature in the reference is matchit(formula, data, method = "nearest", distance = "glm", link = "logit", distance.options, estimand = "ATT", exact, mahvars, antiexact, discard = "none", reestimate = FALSE, s.weights, replace = FALSE, m.order, caliper, std.caliper = TRUE, ratio = 1, verbose = FALSE, include.obj = FALSE, normalize = TRUE, ...). This table maps the options people search for to what each changes, as documented in the reference page.

Option Question it answers What the reference says
method Which matching algorithm? nearest, optimal, full, quick, genetic, cem, exact, cardinality, subclass; NULL skips matching but still allows propensity score estimation and common-support checks.
distance and link How is closeness measured? Default is a propensity score from a generalized linear model ("glm") with a logit link; distance = "mahalanobis" matches on covariates directly (see Mahalanobis distance).
replace Can one control serve several treated units? Default FALSE; TRUE allows matching with replacement.
mahvars Can I match on Mahalanobis distance within a propensity-score caliper? Specifies variables for Mahalanobis matching, typically used together with a caliper on the propensity score; give a character vector or a one-sided formula such as ~ age + educ.
caliper and std.caliper How far apart may a pair be? An unnamed value applies to the distance measure; std.caliper = TRUE reads it in standard-deviation units. Named values set per-covariate calipers.
exact Must pairs agree on a variable? Used for exact matching on the variables you name, for example exact = ~ married + race in the reference examples.
ratio How many controls per treated unit? Default 1.
estimand Which population does the effect describe? ATT (default), ATC or ATE. The reference cautions that setting ATT does not by itself guarantee that the average treatment effect in the treated is what you end up estimating.
discard Drop units outside common support? Default "none".
s.weights Do I have survey or sampling weights? Pass the sampling weights here; the package has a vignette on matching with sampling weights.

The reference page’s own examples combine these, for instance Mahalanobis distance with replacement and exact matching on married and race, or a probit propensity score with mahvars = ~ age + educ + re74 + re75, caliper = .1 and ratio = 2.

Output and how to read it

Printing the object shows the call and sample sizes. summary() gives a balance table with one row per covariate and these columns:

  • Means Treated / Means Control: covariate means in each group.
  • Std. Mean Diff.: the standardized mean difference; values near zero indicate good balance.
  • Var. Ratio: the ratio of treated to control variances; values near one indicate good balance.
  • eCDF Mean / eCDF Max: differences between the empirical distribution functions of the two groups.
  • Std. Pair Dist.: the standardized distance within matched pairs.

A sample-size table reports All, Matched, Unmatched and Discarded counts; full matching adds an effective sample size (ESS) row. A practical way to use these columns: compare the matched rows with the unmatched rows (summary(m.out0)), confirm the standardized differences moved toward zero for the covariates that matter to the outcome, and check that the matched sample size is still adequate. If balance is poor, change the method, distance or calipers and rerun; if it is good, move to effect estimation. The returned object also carries match.matrix, weights, subclass, distance, discarded and estimand (subclassification returns no match.matrix). A related weighting approach is inverse probability weighting.

Troubleshooting

  • Method needs another package. Optimal and full matching rely on optmatch and genetic matching on Matching; both are listed in Suggests on CRAN. Install them with install.packages() if a method asks for one.
  • Install fails on an older R. The current CRAN release depends on R 4.1.0 or later; update R first.
  • Balance still poor. Try a different method, add a caliper, add exact on a key categorical covariate, or revise the propensity score model.
  • Too few matched units. Matching without replacement and tight calipers shrink the sample; check the sample-size table and consider replace = TRUE or a looser caliper.
  • Standard errors look too small. Use cluster-robust errors with pair membership (subclass) as the clustering variable, as in step 6.

Free alternatives and companions

These R packages are open-source on CRAN; versions are as listed on their CRAN pages on 2026-11-07.

Package What CRAN says it does CRAN version, license
MatchIt Nonparametric preprocessing for parametric causal inference via matching, weighting and subclassification. 4.8.2, GPL-2 or GPL-3
Matching Multivariate and propensity score matching with balance optimization using a genetic search algorithm. 4.10-15, GPL-3
optmatch Distance-based bipartite matching using minimum cost flow, for treatment and control groups in observational studies. 0.10.8, MIT + file LICENSE
cobalt Covariate balance tables and plots after matching, weighting or subclassification; integrates with MatchIt, WeightIt, Matching, optmatch and others. 5.0.0, GPL-2 or GPL-3
WeightIt Balancing weights for causal effect estimation in observational studies with binary, multi-category or continuous treatments. 2.1.0, GPL-2 or GPL-3

FAQ

What does MatchIt do?

It matches, weights or subclassifies treated and control units on observed covariates so that the groups are comparable before you estimate a treatment effect with an ordinary outcome model.

Is there a MatchIt example I can run?

Yes. The six steps above follow the package’s own Getting Started workflow on the bundled lalonde data, and the reference page ships further examples.

What is the MatchIt formula?

The first argument is a formula with the treatment indicator on the left and covariates on the right, for example treat ~ age + educ + race. It is used to estimate the propensity score (or, with Mahalanobis distance, to define the covariates matched on).

What does mahvars do?

It names variables for Mahalanobis-distance matching, typically inside a caliper on the propensity score, so pairs are close on the score and on those covariates.

How do I match with replacement?

Set replace = TRUE. The default is FALSE, where each control is used at most once.

Where is the MatchIt documentation and source?

The package website at kosukeimai.github.io/MatchIt hosts the reference pages and vignettes; the released package is on CRAN.

Sources (official documentation, read 2026-11-07)

Cite the software itself following how to cite software, code and R packages.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about MatchIt in R: Install, matchit() Options, Balance Output

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →