gpt4 book ai didi

r - R 中的精确匹配和 GenMatch

转载 作者:行者123 更新时间:2023-12-04 08:24:08 25 4
gpt4 key购买 nike

因此,请遵循 Matching 包中的示例,尤其是 GenMatch 示例
Link to pdf here

按照这里的例子

library(Matching)
data(lalonde)
attach(lalonde)

X = cbind(age, educ, black, hisp, married, nodegr, u74, u75, re75, re74)

BalanceMat <- cbind(age, educ, black, hisp, married, nodegr, u74, u75, re75, re74,
I(re74*re75))

genout <- GenMatch(Tr=treat, X=X, BalanceMatrix=BalanceMat, estimand="ATE", M=1,
pop.size=16, max.generations=10, wait.generations=1)

Y=re78/1000

mout <- Match(Y=Y, Tr=treat, X=X, Weight.matrix=genout)
summary(mout)

我们看到所有的治疗案例都与对照案例相匹配。现在假设我们想要完全匹配已婚状态(或任何其他变量)。但是我们还是想使用之前创建的 GenMatch 矩阵。

引用链接

Exact = ..... 如果提供了逻辑向量,则应该为 X 中的每个协变量提供一个逻辑值。使用逻辑向量允许用户为某些变量而不是其他变量指定精确匹配。当未找到完全匹配时,将丢弃观察结果。

所以下面的说法正确吗??
mout2 <- Match(Y=Y, Tr=treat, X=X, exact=c(0,0,0,0,1,0,0,0,0,0), Weight.matrix=genout)
summary(mout2)

我会说这是不正确的,好像你比较
summary(mout$weights)
summary(mout2$weights)

你得到相同的值

最佳答案

我应该首先说我以前从未使用过这些包和函数,我的回答纯粹是基于玩你的代码和函数文档。
Weight.matrix 似乎有一个记录不充分、未警告的优先级。在 exactMatch()功能。它的帮助页面中有一个提示( ?Match ):

Weight.matrix: ...

This code changes the weights implied by the inverse of the variances by multiplying the first variable by a 1000 so that it is highly weighted. In order to enforce exact matching see the exact and caliper options.



当它说你应该使用 exact为了强制执行精确匹配(而不是给出手动计算或从 GenMatch() 计算的权重),在我看来,它是说你应该使用一个或另一个。然而,行为是 exact当您向 Weight.matrix 提供参数时,似乎会被忽略.从函数中删除它,你会得到不同的结果:
> mout2 <- Match(Y=Y, Tr=treat, X=X, exact=c(0,0,0,0,1,0,0,0,0,0))
> summary(mout2)

Estimate... 1.7605
AI SE...... 0.86408
T-stat..... 2.0374
p.val...... 0.041606

我无法详细说明这种变化的含义,因为我不熟悉其背后的理论。

我查了 Match()的来源,但除了调用一个名为 RmatchLoop() 的函数之外,没有任何用处。 ,我在任何地方都找不到(我猜它是内部包,需要其他一些伏都教才能看到它)。

基于此,我认为您的判断应该是天气与否,使用这两个论点都有意义,而从我读到的内容来看,事实并非如此。如果您实际上只想匹配其中一个,则没有理由为每个协变量赋予不同的权重。

顺便说一句,您的代码可以使用一些改进,例如:
  • 避免使用 attach ,如果您决定使用与数据列同名的变量,这是很危险的。
  • 而不是 cbind对数据帧的几乎所有列进行处理,只将不需要的列去掉:

  • 代码:
    X <- lalonde[,!(colnames(lalonde)=="re78" | colnames(lalonde) == "treat")]
    #or
    X <- subset(lalonde, select=-c(re78, treat)) #Subset is shorter in this case, but usually not recommended
    #instead of
    X = cbind(age, educ, black, hisp, married, nodegr, u74, u75, re75, re74)

    BalanceMat 也可以做同样的事情.另一个优点是您可以将数据保存为数据框。
  • 另外,对于 exact争论,更简洁的方法是:

  • 代码:
    exact = colnames(X)=="married"

    这样您就不太容易对列顺序等进行任何更改。

    关于r - R 中的精确匹配和 GenMatch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29985059/

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