我知道這是一箇舊帖子,但我以爲我會分享
鑑於直接給出變量「年齡」相匹配的下面的例子(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")