I'd like to add an answer with a code example for further clarity.
What we're essentially after is taking the survreg
output model and derive from it the survival function. To avoid the common notation confusion I'll actually go ahead and show the code that does that:
fit <- survreg(Surv(time,status) ~ age, data=stanford2) # this is the survreg output model
survreg.lp <- predict(fit, type = "lp")
survreg.scale <- fit$scale
# this is the survival function!
S_t <- function(t, survreg.scale, survreg.lp){
shape <- 1/survreg.scale
scale <- exp(survreg.lp)
ans <- 1 - pweibull(t, shape = shape, scale = scale)
}
As mentioned by vkehayas R's pweibull
parameterisation is:
$$F(x) = 1-exp(-\left(\frac{x}{b}\right)^a$$
where a
is the weibull distribution shape and b
is the scale.
We then get that a = 1/fit$scale
and b = exp(predict(fit, type = "lp"))
We can verify below that the derived survival function
# next let's verify it's correct:
fit <- survreg(Surv(time,status) ~ age, data=stanford2) # this is the survreg output model
# this is the survival function!
S_t <- function(t, survreg.scale, survreg.lp){
shape <- 1/survreg.scale
scale <- exp(survreg.lp)
ans <- 1 - pweibull(t, shape = shape, scale = scale)
}
new_dat <- data.frame(age = c(0, seq(min(stanford2$age), max(stanford2$age), length.out = 10)))
pct <- seq(0.01, 0.99, 0.01)
surv_curves <- sapply(pct,
function(x) predict(fit, type = "quantile", p = 1 - x,
newdata = new_dat))
matplot(y = pct, t(surv_curves), type = "l")
# you can vary the below subject_i variable to see it works for all of them
subject_i <- 1
single_curve <- surv_curves[subject_i, ]
plot(single_curve, pct, type = "l") # this is we know to be true
times <- round(seq(1, max(single_curve), length.out = 100))
lp <- predict(fit, newdata = new_dat, type = "lp")[subject_i]
surv <- sapply(times, function(t) S_t(t, survreg.scale = fit$scale, survreg.lp = lp))
lines(times, surv, col = "red", lty = 2) # this is the new S_t function
# They match!
So, to summarize:
a = 1/fit$scale
and b = exp(predict(fit, type = "lp"))
Hope this helps. I know I pulled a few hairs before figuring this out.