I'm trying to generate simulation paths across time in R for survival and hazard functions using the Weibull distribution. Here are the steps I'm taking so far. I need direction because I'm not able to generate simulation paths with the below:

First: Here's an example of a single basic hazard curve generated in R which works fine (code followed by image):

# function for hazard:
weibHaz <- function(x, shape, scale) dweibull(x, shape=shape,
              scale=scale)/pweibull(x, shape=shape, scale=scale,lower.tail=F)

# plot hazard:
curve(weibHaz(x, shape=1.5, scale=1/0.03), from=0, to=80, ylab='Hazard', xlab='Time', col='red')

enter image description here

Second: Generate random numbers from Weibull distribution and store them in simulated_data, using same parameters as in the above hazard curve:

set.seed(123) # set a seed for reproducibility
simulated_data <- rweibull(n = 20, shape=1.5, scale=1/0.03) 

Third: plot the simulation results (code followed by image):

# ts() creates a time series object from the simulated data
# frequency = 1 indicates the time series data is not periodic
ts_data <- ts(simulated_data, start = 1, frequency = 1)
plot(ts_data, main = "Simulated Time Series Data", xlab = "Time", ylab = "Data")

enter image description here

The third step above obviously shows incorrect results. I need to see 20 curves over 80 time periods. The below image shows the type of simulation paths I've generated before using other data and using autoregressive time-series models. This is the form of output data and plot I'm looking for for the hazard curve generated with the Weibull distribution. Any recommendations for doing this correctly?

enter image description here


1 Answer 1


Your simulated_data are just 20 draws from a single fixed Weibull distribution having the specified shape and scale values. They are just samples of individual survival times drawn from that single distribution. This shows the associated survival function:

 plot(1:70,1-pweibull(1:70,shape=1.5, scale=1/0.03),type="l",bty="n",xlab="Time",ylab="Survival probability")

If the Weibull distribution is fixed, then the survival function is also fixed and there would be no variability in survival curves.

If you want to evaluate uncertainty in survival-curve estimates based on modeled data, then you want to draw from distributions of the shape and scale values consistent with the variance-covariance matrix of their modeled estimates, for example with the mvrnorm() function of the R MASS package, and display the set of survival curves in the way indicated above.

  • $\begingroup$ I know comments aren't to be used for "thank you", but you are quite the educator and supportive so anyhow THANK YOU. Now I can adjust my path. $\endgroup$ Apr 18, 2023 at 5:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.