Plot survival and hazard function of survreg using curve()

user1252482 picture user1252482 · Apr 26, 2013 · Viewed 14.8k times · Source

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.

Answer

nograpes picture nograpes · Apr 26, 2013

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.