【问题标题】:Propensity Score Matching and subset the data by using a weighting factor in R倾向得分匹配并通过使用 R 中的加权因子对数据进行子集化
【发布时间】:2019-12-20 21:07:44
【问题描述】:

我正在进行倾向得分匹配,并希望通过使用权重对数据进行子集化处理和控制。有 5 个变量:ID、治疗(是/否)、结果(是/否)、年龄和“体重”。我试图在 R 中编写一个程序,但根据权重执行此操作时遇到问题。使用了survey 包。

dput(dat2):

structure(list(ID = c(1, 2, 3, 4, 6, 7),
    Weight = c(2.4740626, 2.4740626, 2.4740626, 2.4740626, 1.9548149, 1.9548149),
    Age = c("35-44", "<15-24", "25-34", "35-44", ">45", "25-34"),
    Treatment = c(1, 0, 0, 1, 0, 0),
    Outcome = c(1, 1, 1, 0, 1, 1)), 
    row.names = c(NA, -6L),
    class = c("tbl_df", "tbl", "data.frame")))

head(dat2):

data<-svydesign(ids = ~dat2$Id,
                weights = ~dat2$Weight,
                data = dat2)
treat<-subset(dat, dat2$treatment==1)
cont<-subset(dat, dat2$treatment==0)

我正在分享数据样本。我有 1587 行。当我找到没有重量的尺寸时,treat 和 cont 的尺寸分别为 877*5 和 710*5。但如果使用权重,它将是 803*5 和 784*5。

请帮帮我。

提前致谢。

【问题讨论】:

  • 请将您的数据复制粘贴为文本,而不是图像
  • 或者你可以dput()你的数据?

标签: r


【解决方案1】:

一种方法如下:

样本数据

dat2 <- structure(list(ID = c(1, 2, 3, 4, 6, 7),
               Weight = c(2.4740626, 2.4740626, 2.4740626, 2.4740626, 1.9548149, 1.9548149),
               Age = c("35-44", "<15-24", "25-34", "35-44", ">45", "25-34"),
               Treatment = c(1, 0, 0, 1, 0, 0),
               Outcome = c(1, 1, 1, 0, 1, 1)), 
          row.names = c(NA, -6L),
          class = c("tbl_df", "tbl", "data.frame"))

脚本

data<-svydesign(ids = ~dat2$ID,
                weights = ~dat2$Weight,
                data = dat2)

treat<-subset(data, Treatment==1)
cont<-subset(data, Treatment==0)

【讨论】:

  • 谢谢@Majid。但这与没有权重的结果相同
  • 请在帖子底部添加您想要的输出。
猜你喜欢
  • 1970-01-01
  • 2021-02-20
  • 2015-05-18
  • 1970-01-01
  • 2021-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
相关资源
最近更新 更多