PSM倾向性评分

作者: 医学小白学生信 | 来源:发表于2021-07-26 18:04 被阅读0次

    倾向性评分中的结局变量不用管,其实没有用到,根据因变量调整所有的协变量就可以了。卡钳值用来再次对没有匹配的指标进行进一步的调整。

    library(MatchIt)
    library(tableone)
    rt1=read.table("genesymbol.txt",sep="\t",header=T,check.names=F)
    data(lalonde)
    head(lalonde,4)
    str(lalonde)
    
    dput(names(lalonde))
    preBL <- CreateTableOne(vars=c("treat","age","educ","black","hispan","married","nodegree","re74","re75","re78"),
                            strata="treat",data=lalonde,
                            factorVars=c("treat","black","hispan","married","nodegree"))
    # treat是感兴趣变量,re78为结局变量
    print(preBL,showAllLevels = TRUE)
    
    f=matchit(treat~re74+re75+educ+age+married+nodegree,data=lalonde,method="nearest",ratio = 1)
    # treat是感兴趣变量,re78为结局变量
    
    summary(f)
    
    
    matchdata=match.data(f)
    mBL <- CreateTableOne(vars=c("treat","age","educ","black","hispan","married","nodegree","re74","re75","re78"),
                          strata="treat",data=matchdata,
                          factorVars=c("treat","black","hispan","married","nodegree"))
    print(mBL,showAllLevels = TRUE)
    
    plot(f, type = 'jitter', interactive = FALSE)
    
    
    
    # hispan不平衡,需要卡钳值
    f1=matchit(treat~re74+re75+educ+black+hispan+age+married+nodegree,data=lalonde,method="nearest",caliper=0.05)
    summary(f1)
    
    matchdata1=match.data(f1)
    mBL1 <- CreateTableOne(vars=c("treat","age","educ","black","hispan","married","nodegree","re74","re75","re78"),
                           strata="treat",data=matchdata1,
                           factorVars=c("treat","black","hispan","married","nodegree"))
    print(mBL1,showAllLevels = TRUE)
    plot(f1, type = 'jitter', interactive = FALSE)
    
    #导出数据
    library(foreign)
    matchdata$id<-1:nrow(matchdata)
    write.csv(matchdata1,"matchdata.csv")
    
    

    相关文章

      网友评论

        本文标题:PSM倾向性评分

        本文链接:https://www.haomeiwen.com/subject/kbygmltx.html