Using xBalance with MatchIt

August 1, 2010

(This article was first published on Mark M. Fredrickson, and kindly contributed to R-bloggers)

In a previous post, I demonstrated how to create a propensity score matching, test balance, and analyze the outcome variable using the optmatch and RItools packages. The same strategy can be used with other matching algorithms, for example the various methods included in the MatchIt package.

I’ll use the same basic question and data from my previous article. The MatchIt package wraps optmatch to provide its “full” and “optimal” matching methods, so I will the “full” option to maintain consistency with my previous article. The first step is loading the packages and the data:

> library(MatchIt)
> library(optmatch)
> library(RItools)
> data(nuclearplants)

The interface for MatchIt is similar to optmatch for propensity score matches, except that the matchit() function compresses the process into a single step of specifying the propensity formula and producing the match, while fullmatch() allows a user to specify any number of distance matrices. In the end, the interface is fairly similar. As with the previous article, I match on a subset of the covariates.

> example.formula &lt- formula(pr ~ t1 + t2 + cap)
> match.opt &lt- fullmatch(
                                data = nuclearplants, 
                                family = binomial())))

> &lt- matchit(example.formula, 
                          data = nuclearplants, 
                          method = "full")

The object contains (among other items) a vector indicating each object’s matched set. For compatibility, save it as a factor:

> &lt- as.factor($subclass)

Unsurprisingly, as MatchIt uses optmatch the two matches are identical.

> lapply(split(nuclearplants, match.opt), rownames)

[1] "N" "Z" "a"

[1] "I" "G"

[1] "A" "B" "D" "V" "F" "b"

[1] "U" "c"

 [1] "H" "K" "L" "M" "C" "P" "R" "Y" "e" "f"

[1] "J" "O" "Q" "S" "T" "E" "W" "X" "d"

> lapply(split(nuclearplants,, rownames)

[1] "N" "Z" "a"

[1] "I" "G"

[1] "A" "B" "D" "V" "F" "b"

[1] "U" "c"

 [1] "H" "K" "L" "M" "C" "P" "R" "Y" "e" "f"

[1] "J" "O" "Q" "S" "T" "E" "W" "X" "d"

Now that I have a factor listing the groups, I can run xBalance to assess the balance properties of the match:

> xBalance(pr ~ . - (cost + pr), 
              data = nuclearplants, 
              strata =, 
              report = "chisquare.test")

---Overall Test---
      chisquare df p.value
strat       5.1  9    0.82
Signif. codes:  0 ‘***’ 0.001 ‘** ’ 0.01 ‘*  ’ 0.05 ‘.  ’ 0.1 ‘   ’ 1 

With a reported p-value of 0.82, there is little evidence against the null of balance, so we would fail to reject it.

This walk through used the the “full” method for matchit(), but the same techniques will work with other matchit() methods, such as coarsened exact matching or nearest neighbor. If you are reasonably confident that you wish to use optimal matching, you should consider using the optmatch package directly, instead of using it through MatchIt. In future posts I will be demonstrating important techniques to speed up the matching process (which can be a great benefit to large datasets) and how you can create matches that incorporate more subject matter information than can be included in a simple logit model.

To leave a comment for the author, please follow the link and comment on their blog: Mark M. Fredrickson. offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.