標題
PSM: 平衡和矯正混雜因素
image.png
image.png
- 慎重選擇:可能會造成選擇性偏倚。因為匹配后,貧血組和非貧血組都刪掉部分人。那些刪掉的人可能就是有心血管事件的人。
- 比較靠譜的方法是:想在原始數據上做個cox回歸,這是托底的方法。然后做psm方法,比較二者的差別。
盡可能保證所有的實驗組都被匹配上。
收入re78是否同接受職業培訓有關
有兩種方法可以實現PSM
- 一個MatchIt包對照組和實驗組數量是1:1的。nonrandom包可以實現1:2,1:3甚至是1:4.
- 注意:能做PSM的一定要做COX回歸。在回歸的基礎上做PSM。
下面是案例1的數據
案例1
第1種R包
## 1) MatchIt包
library(MatchIt)#基于logistic回歸建模的
data(lalonde)#自帶數據
#View(lalonde)
head(lalonde)
#因變量~協變量1+協變量2
f=matchit(treat~re74+re75+educ+black+hispan+age+married+nodegree,data=lalonde,
method="nearest")#鄰近匹配,其不同于卡前值方式,很多指標不均衡。
#設定卡前值caliper=0.05就是你能接受的最大誤差,該方法更嚴苛
#f1=matchit(treat~re74+re75+educ+black+hispan+age+married+nodegree,data=lalonde,method="nearest",caliper=0.05)
summary(f)#match的數據結果
matchdata=match.data(f)#提取match的數據
matchdata
library(foreign)
matchdata$id<-1:nrow(matchdata)#這就是PSM方法挑出來的數據集,實驗組和對照組的數據實現均衡,可以通過卡方檢驗
write.dta(matchdata,"matchdata.dta")
write.csv(matchdata,"matchdata.csv")
第2種R包
案例2
## pscore {nonrandom}
install.packages("nonrandom")
#下載鏈接https://cran.r-project.org/src/contrib/Archive/nonrandom/
library(nonrandom)
## STU1
data(stu1)#自帶數據集
#View(stu1)
stu1.ps <- pscore(data = stu1,
formula = therapie~tgr+age)#therapie分組變量,其余是因素
stu1.match <- ps.match(object = stu1.ps,
ratio = 2,#1:2匹配,最多為1:1
caliper = 0.05,#
givenTmatchingC = FALSE,
matched.by = "pscore",
setseed = 38902,#每次設定一樣
combine.output=TRUE)
matchdata<- stu1.match$data.matched
matchdata#匹配完的數據
library(foreign)
matchdata$id<-1:nrow(matchdata)
write.dta(matchdata,"stu1matchdata.dta")#保存
write.csv(matchdata,"stu1matchdata.csv")
## STU1
data(stu1)
stu1.ps <- pscore(data = stu1,
formula = therapie~tgr+age)
plot.pscore(x = stu1.ps,
main = "PS distribution",
xlab = "",
par.1=list(col="red"),
par.0=list(lwd=2),
par.dens=list(kernel="gaussian"))
##pscore {nonrandom}
#這里同樣只是挑出有關數據集,后續計算可以自己設計