Plot survival and hazard function of survreg using curve()

前端 未结 2 1083
北荒
北荒 2021-02-05 21:20

I have the following survreg model:

Call:
survreg(formula = Surv(time = (ev.time), event = ev) ~ age, 
    data = my.data, dist = \"weib\")
             Value S         


        
2条回答
  •  一生所求
    2021-02-05 21:33

    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.

提交回复
热议问题