gpt4 book ai didi

r - 匹配包 : Combining "nearest neighbor" matching and "exact" matching

转载 作者:行者123 更新时间:2023-12-04 09:38:33 26 4
gpt4 key购买 nike

我正在尝试使用 MatchIt 进行 PSM 分析在 R 中打包,对某些变量使用“精确匹配”,对同一数据集中的其他变量使用“最近邻”方法

出于这个问题的目的,我将使用示例数据集 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输出,仅使用“最近”方法。我不知道错误在哪里,因为仅使用“精确”方法,该函数识别出完全匹配,但未与其他匹配方法结合使用。

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

最佳答案

发生的事情是您在包的最近邻居文件中遇到了这个循环:

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

关于r - 匹配包 : Combining "nearest neighbor" matching and "exact" matching,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39549935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com