首页 文章

如何绘制survreg生成的生存曲线(R的包存活率)?

提问于
浏览
26

我正在尝试将Weibull模型拟合并绘制成生存数据 . 该数据只有一个协变量,同期,从2006年到2010年 . 所以,任何关于如何添加到两行代码的想法,以绘制2010年队列的生存曲线?

library(survival)
s <- Surv(subSetCdm$dur,subSetCdm$event)
sWei <- survreg(s ~ cohort,dist='weibull',data=subSetCdm)

使用Cox PH模型完成相同操作非常简单,具有以下几行 . 问题是survfit()不接受类型为幸存的对象 .

sCox <- coxph(s ~ cohort,data=subSetCdm)
cohort <- factor(c(2010),levels=2006:2010)
sfCox <- survfit(sCox,newdata=data.frame(cohort))
plot(sfCox,col='green')

使用数据肺(来自生存包),这是我想要完成的 .

#create a Surv object
s <- with(lung,Surv(time,status))

#plot kaplan-meier estimate, per sex
fKM <- survfit(s ~ sex,data=lung)
plot(fKM)

#plot Cox PH survival curves, per sex
sCox <- coxph(s ~ as.factor(sex),data=lung)
lines(survfit(sCox,newdata=data.frame(sex=1)),col='green')
lines(survfit(sCox,newdata=data.frame(sex=2)),col='green')

#plot weibull survival curves, per sex, DOES NOT RUN
sWei <- survreg(s ~ as.factor(sex),dist='weibull',data=lung)
lines(survfit(sWei,newdata=data.frame(sex=1)),col='red')
lines(survfit(sWei,newdata=data.frame(sex=2)),col='red')

3 回答

  • 7

    希望这有帮助,我没有犯一些误导性的错误:

    从上面复制:

    #create a Surv object
        s <- with(lung,Surv(time,status))
    
        #plot kaplan-meier estimate, per sex
        fKM <- survfit(s ~ sex,data=lung)
        plot(fKM)
    
        #plot Cox PH survival curves, per sex
        sCox <- coxph(s ~ as.factor(sex),data=lung)
        lines(survfit(sCox,newdata=data.frame(sex=1)),col='green')
        lines(survfit(sCox,newdata=data.frame(sex=2)),col='green')
    

    对于Weibull,使用预测,来自Vincent的评论:

    #plot weibull survival curves, per sex,
        sWei <- survreg(s ~ as.factor(sex),dist='weibull',data=lung)
    
        lines(predict(sWei, newdata=list(sex=1),type="quantile",p=seq(.01,.99,by=.01)),seq(.99,.01,by=-.01),col="red")
        lines(predict(sWei, newdata=list(sex=2),type="quantile",p=seq(.01,.99,by=.01)),seq(.99,.01,by=-.01),col="red")
    

    plot output

    这里的技巧是逆转绘图与预测的分位数顺序 . 可能有更好的方法来做到这一点,但它在这里工作 . 祝好运!

  • 14

    另一种选择是使用包 flexsurv . 这提供了一些比 survival 包更多的功能 - 包括参数回归函数 flexsurvreg() 有一个很好的绘图方法,可以满足您的要求 .

    如上使用肺;

    #create a Surv object
    s <- with(lung,Surv(time,status))
    
    require(flexsurv)
    sWei  <- flexsurvreg(s ~ as.factor(sex),dist='weibull',data=lung)
    sLno  <- flexsurvreg(s ~ as.factor(sex),dist='lnorm',data=lung)   
    
    plot(sWei)
    lines(sLno, col="blue")
    

    output from plot.flexsurvreg

    您可以使用 type 参数绘制累积危险或危险等级,并使用 ci 参数添加置信区间 .

  • 22

    这只是一个澄清Tim Riffe's answer的注释,它使用以下代码:

    lines(predict(sWei, newdata=list(sex=1),type="quantile",p=seq(.01,.99,by=.01)),seq(.99,.01,by=-.01),col="red")
    lines(predict(sWei, newdata=list(sex=2),type="quantile",p=seq(.01,.99,by=.01)),seq(.99,.01,by=-.01),col="red")
    

    两个镜像序列 seq(.01,.99,by=.01)seq(.99,.01,by=-.01) 的原因是因为predict()方法给出了事件分布f(t)的分位数 - 即f(t)的逆CDF的值 - 而生存曲线绘制1-(CD的f)对t . 换句话说,如果你绘制p与预测(p),你得到生存曲线,即1-CDF . 以下代码更透明,并推广到p值的任意向量:

    pct <- seq(.01,.99,by=.01)
    lines(predict(sWei, newdata=list(sex=1),type="quantile",p=pct),1-pct,col="red")
    lines(predict(sWei, newdata=list(sex=2),type="quantile",p=pct),1-pct,col="red")
    

相关问题