2016-09-17 70 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)