比率1:1使用Matchit软件包的精确匹配

发布于 2025-01-22 07:27:25 字数 359 浏览 1 评论 0原文

library(MatchIt)

df <- data.frame(lalonde)

m.out1 <- matchit(treat ~ age + race + educ, data = lalonde,
                  method = "exact")

m.data1<-match.data(m.out1)

我想知道如何在与Matchit软件包进行确切的匹配后,如何获得控制和治疗样本的相同尺寸。理想情况下,如果将经过处理的单元与多个控件匹配,我想随机选择一个控件。 我的真正数据集不是拉隆德。它实际上是一个非常大的。因此,我可能有许多与经过处理的单元相关的控件,我想为每个处理的单元随机绘制一个对照。

library(MatchIt)

df <- data.frame(lalonde)

m.out1 <- matchit(treat ~ age + race + educ, data = lalonde,
                  method = "exact")

m.data1<-match.data(m.out1)

I would like to know how I can get the same size for both the control and treatment samples after running an exact matching with MatchIt package. Ideally, I would like to randomly pick a control if a treated unit has been matched to more than one control.
My real dataset is not lalonde. It is actually an extremely large one. So I might have many controls associated with a treated unit and I want to draw one randomly for each treated unit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我是男神闪亮亮 2025-01-29 07:27:25

对于确切的匹配,您可以使用此代码。

library(Matching)
data("lalonde")

Y <- lalonde$re78
Tr <- lalonde$treat
X <- lalonde[setdiff(names(lalonde), c('re78', 'treat'))]

set.seed(42)  ## comment out for FIXING the ties
rmtch <- Match(Y=Y, Tr=Tr, X=X, exact=TRUE, ties=FALSE)
summary(rmtch)
# Estimate...  1678.6 
# SE.........  981 
# T-stat.....  1.7111 
# p.val......  0.087055 
# 
# Original number of observations..............  445 
# Original number of treated obs...............  185 
# Matched number of observations...............  55 
# Matched number of observations  (unweighted).  55 
# 
# Number of obs dropped by 'exact' or 'caliper'  130 

str(rmtch)  ## what is stored in Match object

rmtch$index.control  ## indices of control units
# [1] 261 254 188 279 288 317 323 280 186 311 305 234 337 302 219 345 234 328
# [19] 271 218 253 249 339 271 339 344 351 253 328 339 255 217 254 197 254 284
# [37] 266 252 253 280 208 226 209 354 204 282 350 296 202 247 219 330 347 280
# [55] 344

如果您重新运行代码,您会发现IDS略有变化,如果数据集更大,它们可能会更清楚地做到这一点。

要修复控制单元的随机化,您可以使用set.seed()。为了确定性地使用ties = false(请参阅?match help page)。

For exact matching you could use this code.

library(Matching)
data("lalonde")

Y <- lalonde$re78
Tr <- lalonde$treat
X <- lalonde[setdiff(names(lalonde), c('re78', 'treat'))]

set.seed(42)  ## comment out for FIXING the ties
rmtch <- Match(Y=Y, Tr=Tr, X=X, exact=TRUE, ties=FALSE)
summary(rmtch)
# Estimate...  1678.6 
# SE.........  981 
# T-stat.....  1.7111 
# p.val......  0.087055 
# 
# Original number of observations..............  445 
# Original number of treated obs...............  185 
# Matched number of observations...............  55 
# Matched number of observations  (unweighted).  55 
# 
# Number of obs dropped by 'exact' or 'caliper'  130 

str(rmtch)  ## what is stored in Match object

rmtch$index.control  ## indices of control units
# [1] 261 254 188 279 288 317 323 280 186 311 305 234 337 302 219 345 234 328
# [19] 271 218 253 249 339 271 339 344 351 253 328 339 255 217 254 197 254 284
# [37] 266 252 253 280 208 226 209 354 204 282 350 296 202 247 219 330 347 280
# [55] 344

If you re-run the code, you will see that the IDs change slightly, which they would probably do more clearly if the dataset was larger.

To fix the randomization of the control units you may use set.seed(). For handling ties deterministically use ties=FALSE (see ?Match help page).

慈悲佛祖 2025-01-29 07:27:25

最简单的方法是进行1:1最近的邻居匹配与精确匹配的约束:

m.out1 <- matchit(treat ~ age + race + educ, data = lalonde,
                  method = "nearest",
                  # Here the exact matching constraint.
                  exact = ~ age + race + educ)

如果您要进行精确匹配,则已经内置了一个选项,可以通过设置k2k = true

m.out1 <- matchit(treat ~ age + race + educ, data = lalonde,
                  method = "cem", k2k = TRUE,
                  cutpoints = 0)

设置cutpoints = 0请求确切的匹配(无粗略)。

The easiest way is to do 1:1 nearest neighbor matching with exact matching constraints:

m.out1 <- matchit(treat ~ age + race + educ, data = lalonde,
                  method = "nearest",
                  # Here the exact matching constraint.
                  exact = ~ age + race + educ)

If you are doing coarsened exact matching, there is an option already built in to request this which is by setting k2k = TRUE:

m.out1 <- matchit(treat ~ age + race + educ, data = lalonde,
                  method = "cem", k2k = TRUE,
                  cutpoints = 0)

Setting cutpoints = 0 requests exact matching (no coarsening).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文