简体   繁体   English

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

[英]How to plot the survival curve generated by survreg (package survival of R)?

I'm trying to fit and plot a Weibull model to a survival data.我正在尝试将 Weibull 模型拟合并绘制到生存数据中。 The data has just one covariate, cohort, which runs from 2006 to 2010. So, any ideas on what to add to the two lines of code that follows to plot the survival curve of the cohort of 2010?数据只有一个协变量,即队列,它从 2006 年到 2010 年运行。那么,对于绘制 2010 年队列生存曲线的两行代码添加什么内容有什么想法吗?

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

Accomplishing the same with the Cox PH model is rather straightforward, with the following lines.使用 Cox PH 模型完成相同的操作非常简单,如下几行。 The problem is that survfit() doesn't accept objects of type survreg.问题是 survfit() 不接受 survreg 类型的对象。

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

Using the data lung (from the survival package), here is what I'm trying to accomplish.使用数据肺(来自生存包),这就是我想要完成的。

#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')

Hope this helps and I haven't made some misleading mistake:希望这会有所帮助,我没有犯一些误导性的错误:

copied from above:从上面复制:

    #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')

for Weibull, use predict, re the comment from 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")

绘图输出

The trick here was reversing the quantile orders for plotting vs predicting.这里的技巧是颠倒绘制与预测的分位数顺序。 There is likely a better way to do this, but it works here.可能有更好的方法来做到这一点,但它在这里有效。 Good luck!祝你好运!

An alternative option is to make use of the package flexsurv .另一种选择是使用包flexsurv This offers some additional functionality over the survival package - including that the parametric regression function flexsurvreg() has a nice plot method which does what you ask.这提供了survival包的一些附加功能 - 包括参数回归函数flexsurvreg()有一个很好的绘图方法, flexsurvreg()您的要求。

Using lung as above;如上使用肺;

#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")

plot.flexsurvreg 的输出

You can plot on the cumulative hazard or hazard scale using the type argument, and add confidence intervals with the ci argument.您可以使用type参数绘制累积危险或危险量表,并使用ci参数添加置信区间。

This is just a note clarifying Tim Riffe's answer , which uses the following code:这只是澄清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")

The reason for the two mirror-image sequences, seq(.01,.99,by=.01) and seq(.99,.01,by=-.01) , is because the predict() method is giving quantiles for the event distribution f(t) - that is, values of the inverse CDF of f(t) - while a survival curve is plotting 1-(CDF of f) versus t.两个镜像序列seq(.01,.99,by=.01)seq(.99,.01,by=-.01)的原因是因为 predict() 方法给出了分位数事件分布 f(t) - 即 f(t) 的逆 CDF 值 - 而生存曲线绘制 1-(f 的 CDF)与 t 的关系。 In other words, if you plot p versus predict(p), you'll get the CDF, and if you plot 1-p versus predict(p) you'll get the survival curve, which is 1-CDF.换句话说,如果您绘制 p 与 predict(p) 的关系,您将获得 CDF,如果您绘制 1-p 与 predict(p) 的关系,您将获得生存曲线,即 1-CDF。 The following code is more transparent and generalizes to arbitrary vectors of p values:下面的代码更加透明并且可以推广到任意的 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")

In case someone wants to add a Weibull distribution to the Kaplan-Meyer curve in the ggplot2 ecosystem, we can do the following:如果有人想在ggplot2生态系统中向 Kaplan-Meyer 曲线添加威布尔分布,我们可以执行以下操作:

library(survminer)
library(tidyr)

s <- with(lung,Surv(time,status))
fKM <- survfit(s ~ sex,data=lung)
sWei <- survreg(s ~ as.factor(sex),dist='weibull',data=lung)

pred.sex1 = predict(sWei, newdata=list(sex=1),type="quantile",p=seq(.01,.99,by=.01))
pred.sex2 = predict(sWei, newdata=list(sex=2),type="quantile",p=seq(.01,.99,by=.01))

df = data.frame(y=seq(.99,.01,by=-.01), sex1=pred.sex1, sex2=pred.sex2)
df_long = gather(df, key= "sex", value="time", -y)

p = ggsurvplot(fKM, data = lung, risk.table = T)
p$plot = p$plot + geom_line(data=df_long, aes(x=time, y=y, group=sex))

在此处输入图片说明

In case you'd like to use the survival function itself S(t) (instead of the inverse survival function S^{-1}(p) used in other answers here) I've written a function to implement that for the case of the Weibull distribution (following the same inputs as the pec::predictSurvProb family of functions:如果您想使用生存函数本身S(t) (而不是此处其他答案中使用的逆生存函数S^{-1}(p) ),我已经编写了一个函数来实现该情况Weibull 分布(遵循与pec::predictSurvProb函数系列相同的输入:

survreg.predictSurvProb <- function(object, newdata, times){
  shape <- 1/object$scale # also equals 1/exp(fit$icoef[2])
  lps <- predict(object, newdata = newdata, type = "lp")
  surv <- t(sapply(lps, function(lp){
    sapply(times, function(t) 1 - pweibull(t, shape = shape, scale = exp(lp)))
  }))
  
  return(surv)
}

You can then do:然后你可以这样做:

sWei <- survreg(s ~ as.factor(sex),dist='weibull',data=lung)
times <- seq(min(lung$time), max(lung$time), length.out = 1000)
new_dat <- data.frame(sex = c(1,2))
surv <- survreg.predictSurvProb(sWei, newdata = new_dat, times = times)

lines(times, surv[1, ],col='red')
lines(times, surv[2, ],col='red')

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM