在r中使用MatchIt导出匹配的对 [英] export matched pairs using MatchIt in r

查看:75
本文介绍了在r中使用MatchIt导出匹配的对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 MatchIt 输出中添加一列,以定义匹配的对(即,序列号为10的观测值与序列号为23等),然后分别命名为1和1,然后再命名为另一对(例如观察12和观察27等).我想将所需的列添加到匹配的数据,并使用 write.csv(nn.match,file ="matched.data.csv") .csv >,因此我可以在匹配的同类群组中做进一步的测试,如 McNemar test .

I need to add a column to MatchIt output that defines the matched pairs (i.e. observation with serial number 10 was matched to serial number 23 .etc) and call it 1 and 1 then another pair (eg observation 12 with observation 27.etc). I want to add the needed column to the matched data and export them in .csv using write.csv(nn.match, file ="matched.data.csv") so I can do further testing among the matched cohorts as McNemar test.

我看到了这个( https://journals.sfu.ca/jmde/index.php/jmde_1/article/view/431/414 ),但直到现在我仍无法弄清.

I saw this (https://journals.sfu.ca/jmde/index.php/jmde_1/article/view/431/414) but I could not figure it out till now.

有什么办法吗?感谢任何宝贵的投入.

Any way to do that? Appreciate any precious input.

假定这是示例代码:

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

推荐答案

如果我了解您想要什么:

If I understood what you want:

# 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))创建类似于包含匹配项的data.frame的 tibble .
  • 在添加包含处理ID的pair.t列之后,下一行内部将其与nn.match data.frame连接起来.他们在相等的 pair.t
  • 上加入

    explanations:

    • tibble(pair.t= as.numeric(row.names(m.nn$match.matrix)), pair.x=as.vector(m.nn$match.matrix)) creates a tibble similar to a data.frame that contains the matches.
    • the next line inner joins it with nn.match data.frame after adding a pair.t column that contains the treatment id. they're joined on equal pair.t
    • 这篇关于在r中使用MatchIt导出匹配的对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆