Hi all,
My post is more of a preamble to before I need to use Stan. I’m currently trying to simulate some time to event data from a Weibull distribution. The trick is that I want to simulate the data when the treatment has some proposed effect on the hazard after a lag time L. I could really use a second pair of eyes to tell me if this makes any sense at all.
Some brief theory when survival time has a Weibull distribution
Suppose that under the placebo condition, survival time follows a Weibull distribution T_0 \sim \textrm{Weibull}(k, \, \lambda) where k>0 and \lambda > 0 are the shape and scale parameters respectively. Let t be the time from randomisation.
Let h_0(t) be the hazard function under the placebo condition. Then for t \geq 0,
Let S_0(t) be the survival function under the placebo condition. Then for t \geq 0,
Let H_0(t) be the cumulative hazard function under the placebo condition. Then for t \geq 0,
Delayed treatment effect
Suppose that the treatment exerts its effect by scaling the hazard function by \theta < 1 (on the hazard ratio scale) after a lag period L from randomisation.
The survival function under treatment S_1(t) is then:
Proposed idea
With the closed form equation to S_1(t), I can generate a random survival time by generating a random uniform random variable u from a \textrm{Unif}(0, 1). Solve for the particular value of t^{*} that satisfies:
i.e. the method of inverse transform sampling.
# u is a random variate from the uniform U(0,1)
# shape is the weibull shape parameter
# scale is the weibull scale parameter
# lag is the lag period after which the hazard will be scaled. Defaults to 0
# hr is the scaling factor on the hazard function. Defaults to 1
rweibull.delayed <- function(u, shape, scale, lag=0, hr=1) {
# u could be a vector
# First check if F^-1 (u) = t < lag
# If it is, then return this t
t = qweibull(u, scale=scale, shape=shape, lower.tail = T)
# If more than lag, return a t from the part of the survival function that has been modified by the scaled hazard
t = ifelse(t > lag,
(uniroot(function(x) 1 - (pweibull(x, shape=shape, scale=scale, lower.tail = F)^hr) * (pweibull(lag, shape=shape, scale=scale, lower.tail = F)^(1-hr)) - u,
c(0, 1e9), tol = 0.00001))$root,
t)
return(t)
}