我知道这是一个旧帖子,但我以为我会分享
鉴于直接给出变量“年龄”相匹配的下面的例子(5年以内)和“结婚”的配套包示例数据拉隆德内:
(这是为人民的利益更复杂的例子像我这样谁没有发现类似的例子)
#install.packages("Matching")
library(Matching)
data(lalonde)
X <- cbind(lalonde$age,lalonde$married)
colnames(X)<-c("age","married")
Tr <- lalonde$treat
#Define caliper for age within 5 years (see package documentation for caliper)
5/sd(lalonde$age) #=0.7041973
# 5-to-one matching with replacement (the "M=5" option), with ties set to false to limit multiple matches
# Exact matching on "married" (the "exact=c(0,1)" option), corresponding to 0 (false) for age, 1 (true) for "married"
rr <- Match(Tr=Tr, X=X, M=2,ties=F,exact=c(0,1),caliper=0.7041973) #caliper set to match age within 5 years
summary(rr)
#Put results (rownames of treated and controls) into a dataframe
rr<-data.frame(rr$index.treated,rr$index.control)
colnames(rr)<-c("cases","controls")
lalonde$rowID<-as.numeric(rownames(lalonde))
要加入你的病例和对照原始特征的匹配你可以:(1)做一个简单的匹配或者(2)使用sqldf包的结果,有势必会有更多的选择
#Option 1 to join case control table to original characteristics
rr$CaseMarried <- lalonde$married[match(rr$cases, lalonde$rowID)]
rr$ControlMarried<-lalonde$married[match(rr$controls, lalonde$rowID)]
rr$CaseAge <- lalonde$age[match(rr$cases, lalonde$rowID)]
rr$ControlAge<-lalonde$age[match(rr$controls, lalonde$rowID)]
rr
#Option 2 to join case control table to original characterisitcs using sqldf
#install.packages("sqldf")
library(sqldf)
sqldf1<-sqldf(
"SELECT a.cases, b.age AS CaseAge, b.married AS CaseMarried,
a.controls, c.age AS ControlAge, c.married AS ControlMarried
FROM a
INNER JOIN lalonde b
ON a.cases=b.rowID
INNER JOIN lalonde c
ON a.controls=c.rowID")