创建非随机匹配对

时间:2017-11-20 16:42:43

标签: r statistics methodology

我正在寻找一个R套餐,它允许我将治疗组中的每个受试者匹配到具有相似特征(年龄,性别等)的一般人群中的受试者。

1 个答案:

答案 0 :(得分:1)

我使用MatchIt包来做这种事情。您可能会收到使用倾向得分匹配的建议,但这种广泛使用的方法存在局限性(参见:PS Not

library(MatchIt)   # use for matching
library(tidyverse) # The overall package.  It will load lots of dependencies

set.seed(950)
n.size <- 1000

# This creates a tibble (an easier to use version of a data frame)
myData <- tibble(
a = lubridate::now() + runif(n.size) * 86400,
b = lubridate::today() + runif(n.size) * 30,
ID = 1:n.size,
#   d = runif(1000),
ivFactor = sample(c("Level 1", "Level 2", "Level 3", "Level 4" ), n.size, replace = TRUE),
age = round(rnorm(n = n.size, mean = 52, sd = 10),2),
outContinuous = rnorm(n = n.size, mean = 100, sd = 10),
tmt = sample(c(1,0), size = n.size, prob = c(.3, .7), replace = TRUE)
)

# Using matching methods suggestions found in Ho, Imai, King and Stuart 
myData.balance <- matchit(tmt~age + ivFactor, data = myData, method = "nearest", distance = "logit")

# Check to see if the matching improved balance between treatment and control group
summary(myData.balance)

 # Extract the matched data.  Now we can use this in subsequent analyses
 myData.matched <- match.data(myData.balance)