Plot survival and hazard function of survreg using curve()
Asked Answered
O

2

16

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.

I would like to use the curve() function. 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).

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.

Optimize answered 26/4, 2013 at 12:51 Comment(0)
M
11

Your parameters are:

scale=exp(Intercept+beta*x) in your example and lets say for age=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:

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

Weibull Functions

Muth answered 26/4, 2013 at 16:51 Comment(0)
H
11

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. 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. Also, from the documentation in survreg:

There are multiple ways to parameterize a Weibull distribution. 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.

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

Survival and hazard functions

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. Otherwise, you could simply write your own version of pweibull that uses that survreg's parameterization:

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:

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. Now just wrap those, slower, untested functions instead of pweibull and dweibull directly.

Herbal answered 26/4, 2013 at 15:7 Comment(3)
Thanks for your elaborate email but I DO NOT want to use any *weibull function either. Is it possible to express the hazard as a function of Intercept, age (+other covariates), and scale?Optimize
Hm, perhaps you missed my final paragraph, where I show you how to write a function that is simply a wrapper of pweibull. I don't know why you would want to rewrite pweibull, as it is coded in C, and is very fast and well-tested. Unless this is just homework. Anyway, I show you how to handcode pweibull and dweibull.Herbal
I think the OP misses the math connection between the R output and the hazard/survival functionMuth
M
11

Your parameters are:

scale=exp(Intercept+beta*x) in your example and lets say for age=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:

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

Weibull Functions

Muth answered 26/4, 2013 at 16:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.