MatchIt Package: Combining Nearest Neighbor and Exact Matching

I am trying to do a PSM analysis with a package MatchItin R using the "exact match" for some variables and the "nearest neighbor" method for other variables in the same dataset

For the purposes of this question I will use an approximate data set lalonde.

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

I expected this code to do an exact match for the variable married(binary variable with 0and 1), and then do the “closest” match for all the other variables in the model.

However, I received the following warning message:

Warning message: exact variables not contained in the data. An exact match has not been made.

When considering the summary of the output MatchIt, only the “closest” method was used. I don’t know where the error is, since only the “exact” method is used, the function identifies exact matches, but not in combination with another matching method.

Do you know any method of combining the exact and nearest neighbor matches in the same dataset, or do you know where my error is?

+4
source share
1 answer

What happens is that you click this loop in the package's nearest neighbor file:

  ## 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])
    }
   }
  }

I believe this is due to the way you pointed out married.

The next version will not cause an error:

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

Source: https://habr.com/ru/post/1654923/


All Articles