问题描述
我需要在MatchIt
输出中添加一列,以定义匹配的对(即,序列号为10的观察值与序列号为23等),然后将其称为1和1,然后再将其称为另一对(例如观察值12观察27.etc)。
我想将所需的列添加到匹配的数据中,并使用.csv
将其导出到write.csv(nn.match,file ="matched.data.csv")
中,以便我可以在匹配的队列中以McNemar test
的形式进行进一步的测试。
我看到了这个(https://journals.sfu.ca/jmde/index.php/jmde_1/article/view/431/414),但直到现在我都还不清楚。
有什么办法吗?感谢任何宝贵的投入。
假定这是示例代码:
library(MatchIt);library(Matching); data(lalonde)
lalonde$Serial.number <- seq.int(nrow(lalonde))
lalonde.formu <- treat~age + educ + black + hisp + married + nodegr + re74 + re75
### Get matched Data using PSM nearest neighbor
m.nn<-matchit(lalonde.formu,data = lalonde,caliper=0.1,method ="nearest")
nn.match<-match.data(m.nn)
write.csv(nn.match,file ="matched.data.csv")
#============================
#---Outcome analysis using paired t-test
# this command saves the data matched
matches <- data.frame(m.nn$match.matrix)
#these commands find the matches. one for group 1 one for group 2
group1 <- match(row.names(matches),row.names(nn.match))
group2 <- match(matches$X1,row.names(nn.match))
# these commands extract the outcome value for the matches
yT <- nn.match$treat[group1]
yC <- nn.match$treat[group2]
# binding
matched.cases <- cbind(matches,yT,yC)
#Paired t-test
t.test(matched.cases$yT,matched.cases$yC,paired = TRUE)```
解决方法
如果我了解您想要什么:
# install.packages("tidyverse")
library(tidyverse)
tibble(pair.t= as.numeric(row.names(m.nn$match.matrix)),pair.x=as.vector(m.nn$match.matrix)) %>%
inner_join(nn.match %>% mutate(pair.t=row_number()))
#> pair.t pair.x age educ black hisp married nodegr re74 re75 re78 u74 u75 treat Serial.number distance weights
#> <dbl> <chr> <int> <int> <int> <int> <int> <int> <dbl> <dbl> <dbl> <int> <int> <int> <int> <dbl> <dbl>
#> 1 1 404 37 11 1 0 1 1 0 0 9930. 1 1 1 1 0.402 1
#> 2 2 355 22 9 0 1 0 1 0 0 3596. 1 1 1 2 0.247 1
#> 3 3 266 30 12 1 0 0 0 0 0 24910. 1 1 1 3 0.559 1
#> 4 4 380 27 11 1 0 0 1 0 0 7506. 1 1 1 4 0.353 1
#> 5 5 207 33 8 1 0 0 1 0 0 290. 1 1 1 5 0.410 1
#> 6 6 357 22 9 1 0 0 1 0 0 4056. 1 1 1 6 0.380 1
#> 7 7 256 23 12 1 0 0 0 0 0 0 1 1 1 7 0.551 1
#> 8 8 245 32 11 1 0 0 1 0 0 8472. 1 1 1 8 0.358 1
#> 9 9 NA 19 9 1 0 0 1 0 0 8174. 1 1 1 11 0.377 1
#> 10 10 NA 21 13 1 0 0 0 0 0 17095. 1 1 1 12 0.531 1
#> # … with 175 more rows
# if you want only the 160 matches
tibble(pair.t= as.numeric(row.names(m.nn$match.matrix)),pair.x=as.vector(m.nn$match.matrix)) %>%
inner_join(nn.match %>% mutate(pair.t=row_number())) %>% na.omit
#> pair.t pair.x age educ black hisp married nodegr re74 re75 re78 u74 u75 treat Serial.number distance weights
#> <dbl> <chr> <int> <int> <int> <int> <int> <int> <dbl> <dbl> <dbl> <int> <int> <int> <int> <dbl> <dbl>
#> 1 1 404 37 11 1 0 1 1 0 0 9930. 1 1 1 1 0.402 1
#> 2 2 355 22 9 0 1 0 1 0 0 3596. 1 1 1 2 0.247 1
#> 3 3 266 30 12 1 0 0 0 0 0 24910. 1 1 1 3 0.559 1
#> 4 4 380 27 11 1 0 0 1 0 0 7506. 1 1 1 4 0.353 1
#> 5 5 207 33 8 1 0 0 1 0 0 290. 1 1 1 5 0.410 1
#> 6 6 357 22 9 1 0 0 1 0 0 4056. 1 1 1 6 0.380 1
#> 7 7 256 23 12 1 0 0 0 0 0 0 1 1 1 7 0.551 1
#> 8 8 245 32 11 1 0 0 1 0 0 8472. 1 1 1 8 0.358 1
#> 9 11 281 18 8 1 0 0 1 0 0 0 1 1 1 13 0.393 1
#> 10 12 263 27 10 1 0 1 1 0 0 18740. 1 1 1 14 0.408 1
#> # … with 150 more rows
说明:
-
tibble(pair.t= as.numeric(row.names(m.nn$match.matrix)),pair.x=as.vector(m.nn$match.matrix))
创建一个tibble
,类似于包含匹配项的data.frame。 - 在添加包含处理ID的pair.t列之后,下一行内部将其与nn.match data.frame连接起来。他们加入了平等的
pair.t