使用R

时间:2016-03-10 06:38:55

标签: r predict survival-analysis

我在R

中有以下模型
library(flexsurv)

data(ovarian)

model = flexsurvreg(Surv(futime, fustat) ~ ecog.ps + rx, data = ovarian, dist='weibull')

model

predict(model,data = ovarian, type = 'response')

模型摘要如下所示flexsurvreg model output

我试图使用R中的预测函数预测生存时间并获得以下错误

error while trying to predict

如何使用此flexsurvreg模型预测预期寿命?

据我所知,文档中提到了 totlos.fs 函数,但此数据似乎没有 totlos.fs trans 变量>需要提供输出。

如果 totlos.fs 没有其他替代方法,我如何在此数据中创建 trans 变量并将其与现有协变量一起处理?

请告知。

2 个答案:

答案 0 :(得分:1)

{{# names:index}} {{ . }}{{ #if index < names.length - 1 }}, {{ /if }} {{ / }} 文档的supplementary examples doc的第3部分有一个示例,其中使用模型方程直接计算预测值。当您使用Weibull分布(n = 2个参数)时,我认为这应该有效:

flexsurv

干杯

答案 1 :(得分:0)

Nik,

我知道你的问题很旧,但请看下面我是如何破解的。它涉及从您的测试数据拟合中检索形状和速率参数,然后使用flexsurv中的qgompertz()而不是预测。请原谅我自己封装的示例代码的使用,但您应该可以继续使用。

# generate the training data "lung1" from data(lung) in survival package
# hacked way for truncating the lung data to 2 years of follow up
require(survival)
lung$yrs <- lung$time/365
lung1 <- lung[c("status", "yrs")]
lung1$status[ lung1$yrs >2] <- 1
lung1$yrs[ lung1$yrs >2]  <- 2

# from the training data build KM to obtain survival %s
s <- Surv(time=lung1$yrs, event=lung1$status)
km.lung <- survfit(s ~ 1, data=lung1)
plot(km.lung)

# generate dataframe to use later for plotting 
cut.length <- sum((km.lung$time <= 2)) # so I can create example test data
test.data <- data.frame(yrs = km.lung$time[1:cut.length] , surv=round(km.lung$surv[1:cut.length], 3))


##
##  doing the same as above with gompertz
##
require(flexsurv) #needed to run gompertz model
s <- Surv(time=lung1$yrs, event=lung1$status)
gomp <- flexsurvreg(s ~ 1, data=lung1, dist="gompertz") # run this to get shape and rate estimates for gompertz
gomp # notice the shape and rate values 

# create variables for these values
g.shape <- 0.5866
g.rate <- 0.5816


##
##  plot data and vizualize the gomperts
##
# vars for plotting
df1 <- test.data 
xvar <- "yrs"
yvar <- "surv"

extendedtime <- 3 # 
ylim1 <- c(0,1)
xlim1 <- c(0, extendedtime)

# plot the survival % for training data
plot(df1[,yvar]~df1[,xvar], type="S", ylab="", xlab="", lwd=3, xlim=xlim1, ylim=ylim1)
# Nik--here is where the magic happens... pay special attention to: qgompertz(seq(.01,.99,by=.01), shape=0.58656, rate = .5816) 
lines (qgompertz(seq(.01,.99,by=.01), shape=0.58656, rate = .5816) ,  seq(.99,.01,by=-.01) , col="red", lwd=2, lty=2  )

# generate a km curve from the testing data
s <- Surv(time=lung$yrs, event=lung$status)
km.lung <- survfit(s ~ 1, data=lung)
par(new=T)
# now draw remaining survival curve from the testing section
plot(km.lung$surv[(cut.length+1):length(km.lung$time)]~km.lung$time[(cut.length+1):length(km.lung$time)], type="S", col="blue", ylab="", xlab="", lwd=3, xlim=xlim1, ylim=ylim1)