# Simulating a Weibull conditional on time-to-event is greater than a given time

**Realizations in Biostatistics**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Recently, I had to simulate a time-to-event of subjects who have been on a study, are still ongoing at the time of a data cut, but who are still at risk of an event (e.g. progressive disease, cardiac event, death). This requires the simulation of a conditional Weibull. To do this, I created the following function:

# simulate conditional Weibull conditional on survival > T —————

# reliability function is exp{-(T+t/b)^a} / exp{-(T/b)^a} = 1-F(t)

# n = number of points to return

# shape = shape parm of weibull

# scale = scale parm of weibull (default 1)

# t is minimum (default is 0, which makes the function act like rweibull)

my_rweibull <- function(n,shape,scale=1,t=0) {

if (length(t)!=1 && length(t)!=n) {

stop(“length(t) is not 1 or n”)

}

return(shape*(-log(runif(n))+(t/shape)^scale)^(1/scale))

}

You use this function just like rweibull, with the exception that you pass in another vector t of minimum times or a scalar representing the minimum time of all simulated values. The idea is that the probability that the random variable will be at least T is given by exp{-(T+t/b)^a} / exp{-(T/b)^a}, so you can simulate this with a uniform random variate. I use the inversion method on the reliability function (just like using the inversion method on the distribution function, with the insight that if U is uniform(0,1), so is 1-U).

Truth be told, I ought to buckle down and learn how to do packages in R, but for now I’ll just pass on some code on my blog if I think it will be helpful (or if I need to find it while doing a Google search later).

**leave a comment**for the author, please follow the link and comment on their blog:

**Realizations in Biostatistics**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.