简体   繁体   English

使用曲线()绘制幸存的生存和危险函数

[英]Plot survival and hazard function of survreg using curve()

I have the following survreg model: 我有以下幸存模型:

Call:
survreg(formula = Surv(time = (ev.time), event = ev) ~ age, 
    data = my.data, dist = "weib")
             Value Std. Error    z        p
(Intercept) 4.0961     0.5566 7.36 1.86e-13
age         0.0388     0.0133 2.91 3.60e-03
Log(scale)  0.1421     0.1208 1.18 2.39e-01
Scale= 1.15 

Weibull distribution

I would like to plot the hazard function and the survival function based on the above estimates. 我想根据上述估计绘制危险函数和生存函数。
I don't want to use predict() or pweibull() (as presented here Parametric Survival or here SO question . 我不想使用predict()pweibull() (如此处所示的Parametric Survival或这里的SO问题

I would like to use the curve() function. 我想使用curve()函数。 Any ideas how I can accomplish this? 我有什么想法可以做到这一点? It seems the Weibull function of the survreg uses other definitions of scale and shape than the usual (and different that for example rweibull). 似乎幸存者的Weibull函数使用了比平常更多的尺度和形状定义(并且与例如rweibull不同)。

UPDATE: I guess what I really require it to express hazard / survival as a function of the estimates Intercept , age (+ other potential covariates) , Scale without using any ready made *weilbull function. 更新:我想我真的需要它来表达危害/存活的估计的函数Interceptage (+ other potential covariates)Scale不使用任何现成的*weilbull功能。

The first link you provided actually has a clear explanation on the theory of how this works, along with a lovely example. 您提供第一个链接实际上对其工作原理有一个清晰的解释,以及一个可爱的例子。 (Thank you for this, it is a nice resource I will use in my own work.) (谢谢你,这是我将在我自己的工作中使用的一个很好的资源。)

To use the curve function, you will need to pass some function as an argument. 要使用curve函数,您需要将一些函数作为参数传递。 It is true that the *weibull family of functions use a different parameterization for the Weibull than survreg , but it can be easily transformed, as explained your first link. 确实, *weibull weibull函数系列对Weibull使用不同于survreg参数survreg ,但它可以很容易地转换,如第一个链接所述。 Also, from the documentation in survreg : 另外,从survreg的文档:

There are multiple ways to parameterize a Weibull distribution. 有多种方法可以参数化Weibull分布。 The survreg function imbeds it in a general location-scale familiy, which is a different parameterization than the rweibull function, and often leads to confusion. 幸存函数将其嵌入到一般位置规模的家族中,这是与rweibull函数不同的参数化,并且经常导致混淆。

  survreg's scale = 1/(rweibull shape) survreg's intercept = log(rweibull scale) 

Here is an implementation of that simple transformation: 以下是该简单转换的实现:

# The parameters
intercept<-4.0961
scale<-1.15

par(mfrow=c(1,2),mar=c(5.1,5.1,4.1,2.1)) # Make room for the hat.
# S(t), the survival function
curve(pweibull(x, scale=exp(intercept), shape=1/scale, lower.tail=FALSE), 
      from=0, to=100, col='red', lwd=2, ylab=expression(hat(S)(t)), xlab='t',bty='n',ylim=c(0,1))
# h(t), the hazard function
curve(dweibull(x, scale=exp(intercept), shape=1/scale)
      /pweibull(x, scale=exp(intercept), shape=1/scale, lower.tail=FALSE), 
      from=0, to=100, col='blue', lwd=2, ylab=expression(hat(h)(t)), xlab='t',bty='n')
par(mfrow=c(1,1),mar=c(5.1,4.1,4.1,2.1))

生存和危险功能

I understand that you mentioned in your answer that you did not want to use the pweibull function, but I am guessing that you did not want to use it because it uses a different parameterization. 我知道你在答案中提到你不想使用pweibull函数,但我猜你不想使用它,因为它使用了不同的参数化。 Otherwise, you could simply write your own version of pweibull that uses that survreg 's parameterization: 否则,您可以简单地编写自己的pweibull版本,该版本使用了survreg的参数化:

my.weibull.surv<-function(x,intercept,scale) pweibull(x,scale=exp(intercept),shape=1/scale,lower.tail=FALSE)
my.weibull.haz<-function(x,intercept,scale) dweibull(x, scale=exp(intercept), shape=1/scale) / pweibull(x,scale=exp(intercept),shape=1/scale,lower.tail=FALSE)

curve(my.weibull.surv(x,intercept,scale),1,100,lwd=2,col='red',ylim=c(0,1),bty='n')
curve(my.weibull.haz(x,intercept,scale),1,100,lwd=2,col='blue',bty='n')

As I mentioned in the comments, I don't know why you would do this (unless this is homework), but you could handcode pweibull and dweibull if you like: 正如我在评论中提到的,我不知道为什么你会这样做(除非这是家庭作业),但你可以手动pweibulldweibull如果你喜欢:

my.dweibull <- function(x,shape,scale) (shape/scale) * (x/scale)^(shape-1) * exp(- (x/scale)^shape)
my.pweibull <- function(x,shape,scale) exp(- (x/scale)^shape)

Those definitions come straight out of ?dweibull . 这些定义直接来自?dweibull Now just wrap those, slower, untested functions instead of pweibull and dweibull directly. 现在直接包装那些较慢的,未经测试的函数而不是pweibulldweibull

Your parameters are: 你的参数是:

scale=exp(Intercept+beta*x) in your example and lets say for age=40 在您的示例中, scale=exp(Intercept+beta*x) ,让我们说年龄= 40

scale=283.7

your shape parameter is the reciprocal of the scale that the model outputs 您的形状参数是模型输出的比例的倒数

shape=1/1.15

Then the hazard is: 然后危险是:

curve((shape/scale)*(x/scale)^(shape-1), from=0,to=12,ylab=expression(hat(h)(t)), col="darkblue",xlab="t", lwd=5)

The cumulative hazard function is: 累积危险函数是:

curve((x/scale)^(shape), from=0,to=12,ylab=expression(hat(F)(t)), col="darkgreen",xlab="t", lwd=5)

The Survival function is 1-the cumulative hazard function, so: 生存函数是1-累积危险函数,因此:

curve(1-((x/scale)^(shape)), from=0,to=12,ylab=expression(hat(S)(t)), col="darkred",xlab="t", lwd=5, ylim=c(0,1))

Also check out the eha package, and the function hweibull and Hweibull 还可以查看eha包,以及hweibullHweibull功能

威布尔函数

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

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