rmatchingexact-match

MatchIt Package: Combining "nearest neighbor" matching and "exact" matching


I'm trying to do a PSM Analysis with the MatchIt Package in R, using "exact matching" for some variables and the "nearest neighbor" method for other variables in the same dataset

For the purpose of this question, I will use the example dataset lalonde.

test = matchit(treat ~ age + educ + married, method = "nearest", 
                                       exact  = c(married), data = lalonde)

I expected that this code would perform exact matching for the variable married (binary variable with 0 and 1) and then do the "nearest" matching for all the other variables in the model.

However, I got following warning message:

Warning message: Exact variables not contained in data. Exact matching not done.

Looking at the summary of the matchit output, only the "nearest" method was used. I don't know where the mistake is since using just the "exact" method alone, the function identified exact matches, but not in combination with the other matching method.

Do you know any method how to combine "exact" matching and "nearest neighbor" matching in the same dataset or know where my mistake is?


Solution

  • What's happening is that you're hitting this loop in the nearest neighbor file of the package:

      ## Now for exact matching within nearest neighbor
      ## exact should not equal T for this type of matching--that would get sent to matchit2exact
      if (!is.null(exact)){
        if(!sum(exact%in%names(data))==length(exact)) {
            warning("Exact variables not contained in data. Exact matching not done.",call.=FALSE)
            exact=NULL
        }
        else {
        ww <- exact%in%dimnames(X)[[2]]
        nw <- length(exact)
        exact <- data[,exact,drop=F]
        if(sum(ww)!=nw){
          X <- cbind(X,exact[!ww])
        }
       }
      }
    

    Which I believe is due to the way that you specified married.

    The following version will not throw the error:

    test = matchit(treat ~ age + educ + married, method = "nearest", 
                   exact  = "married", data = lalonde)