R倾向评分与其他标准匹配

lkaoscv7  于 2023-04-18  发布在  其他
关注(0)|答案(1)|浏览(100)

我有一个队列,看起来像这样:

id exposure sex age time
 1        0   F  50  300
 2        0   F  55  320
 3        0   F  60  365
 4        0   M  60  335
 5        0   M  65  350
 6        0   M  70  365
 7        1   F  50  310
 8        1   F  50  325
 9        1   M  65  340
10        1   M  70  345

我需要基于exposure(因变量)创建单独的匹配组,匹配协变量agesex(自变量)。但我还必须强制要求,对于每个匹配,“暴露”(exposure == 1)观察值的time大于“未暴露”(exposure == 0)匹配值。
我计划使用MatchIt命令来匹配我的倾向分数,但我认为没有办法添加标准,要求暴露与未暴露的time更大。
我很感激任何建议!

eiee3dmh

eiee3dmh1#

MatchIt中没有内置的方法来做到这一点,但是matchit()允许你提供任何你想要的距离矩阵,禁止的匹配用Inf标记。所以我们可以做的是创建一个距离矩阵,然后对于每个暴露的单元,如果处理单元的time小于或等于控制单元的time,则将到每个控制单元的距离设置为Inf

# Estimate a propensity score
df$ps <- glm(exposure ~ sex * age * time, data = df,
          family = binomial)$fitted

# Create distance matrix of ps, with treated units
# on rows and control units on columns
ps_dist <- MatchIt::euclidean_dist(exposure ~ ps, data = df)

# Set to `Inf` any forbidden matches
exposed_times <- df$time[[df$exposure == 1]]
unexposed_times <- df$time[[df$exposure == 0]]
ps_dist[outer(exposed_times, unexposed_times, "<=")] <- Inf

# Supply the distance matrix to `matchit()`
m <- MatchIt::matchit(exposure ~ sex + age + time, data = df,
                      distance = ps_dist)

相关问题