在R中预测生存曲线-参数法

4

我正在尝试使用R语言为survreg或flexsurvreg创建预测生存图。但是,当我在survreg中使用多个预测因子时,绘图时出现错误。我想尝试使用flexsurvreg或survreg进行绘图。对于肺癌数据集,我使用以下代码拟合模型。

require(survival)
s <- with(lung,Surv(time,status))

sWei  <- survreg(s ~ as.factor(sex)+age+ph.ecog+wt.loss+ph.karno,dist='weibull',data=lung)

fitKM <- survfit(s ~ sex,data=lung)
plot(fitKM)

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

当我使用上述命令进行绘图时,出现了错误。请告诉我在绘制预测生存曲线时我做错了什么。

> lines(predict(sWei, newdata=list(sex=1),type="quantile",p=seq(.01,.99,by=.01)),seq(.99,.01,by=-.01),col="red")
Error in eval(expr, envir, enclos) : object 'age' not found

你的模型中使用了许多变量。在创建“newdata”时,您需要为模型中的所有变量创建值。 - Roman Luštrik
谢谢Roman。在我的模型中,我必须使用许多变量,是否有另一种方法在调整了许多变量后创建生存曲线。 - NiroshaR
1个回答

6
我们需要为您的模型中的每个变量赋值,以便它能够绘制曲线。在此过程中需要使用列表中的数值,请勿删除HTML标记。list 代表列表。
require(survival)
s <- with(lung,Surv(time,status))

sWei  <- survreg(s ~ as.factor(sex)+age+ph.ecog+wt.loss+ph.karno,dist='weibull',data=lung)

fitKM <- survfit(s ~ sex,data=lung)
plot(fitKM)

lines(predict(sWei, newdata=list(sex=1, 
                                 age = 1, 
                                 ph.ecog = 1, 
                                 ph.karno = 90,
                                 wt.loss = 2),
                                 type="quantile",
                                 p=seq(.01,.99,by=.01)),
                                 seq(.99,.01,by=-.01),
                                 col="blue")
lines(predict(sWei, newdata=list(sex=2, 
                                 age = 1, 
                                 ph.ecog = 1, 
                                 ph.karno = 90,
                                 wt.loss = 2),
                                 type="quantile",
                                 p=seq(.01,.99,by=.01)),
                                 seq(.99,.01,by=-.01),
                                 col="red")

enter image description here


非常感谢。你刚刚是给年龄赋值为1,还是有其他的原因? - NiroshaR
@Mani 非常欢迎。 age 和其他变量必须有一个值,但我选择这些值没有特别的原因。 我认为你想看到 sex 的影响,所以我只是任意设置了其他值,并且在蓝色和红色曲线之间保持了除 sex 之外的所有内容相同。 - Hack-R

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接