2016-09-17 147 views
4

我试图用R中的MatchIt包进行PSM分析,对某些变量使用“精确匹配”,对其他变量使用“最近邻居”方法变量在同一个数据集中MatchIt包:结合“最近邻”匹配和“精确”匹配

为了这个问题的目的,我将使用示例数据集lalonde

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

我预计这一代码将针对可变married(二元变量与01)进行精确匹配,然后做在模型中的所有其他变量“最近”匹配。

不过,我得到以下警告消息:

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

纵观matchit输出的摘要,仅使用了“最近”的方法。我不知道错误发生在哪里,因为单独使用“精确”方法,该功能确定了精确匹配,但不能与其他匹配方法结合使用。

您是否知道在同一数据集中如何结合“精确”匹配和“最近邻居”匹配的任何方法,或知道我的错误在哪里?

回答

2

发生了什么事是你打的这个循环中的包的近邻文件:

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

我相信这是由于您指定married的方式。

以下版本不会抛出错误:

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