Simulating Survival Times I am interested in learning about how to simulate survival times from a Survival Model (e.g. Cox-PH).
For example, suppose we fit a Cox-PH Model on some data in R:
library(survival)
# Fit the Cox model
fit <- coxph(Surv(time, status) ~ age + sex + ph.ecog, data = lung)

The results of the model look something like this:
Call:
coxph(formula = Surv(time, status) ~ age + sex + ph.ecog, data = lung)

             coef exp(coef)  se(coef)      z        p
age      0.011067  1.011128  0.009267  1.194 0.232416
sex     -0.552612  0.575445  0.167739 -3.294 0.000986
ph.ecog  0.463728  1.589991  0.113577  4.083 4.45e-05

Likelihood ratio test=30.5  on 3 df, p=1.083e-06
n= 227, number of events= 164 
   (1 observation deleted due to missingness)

I have seen R packages that are able to perform similar tasks (e.g. https://www.jstatsoft.org/article/view/v097i03) - but is it possible to write a step-by-step process in R that can simulate from such a model?
I would be interested in learning how to do this.
Thanks!

This answer shows the general principle, and illustrates with a Weibull baseline hazard that follows proportional hazards. You sample randomly from a uniform distribution on $(0,1)$, and find the time that corresponds to that quantile of the survival curve, given the covariate values. That works in general.
Under proportional hazards, the survival over time as a function of time-constant covariate values in a vector $X$ and corresponding regression coefficients $\beta$ is:
$$S(t|X)= S_0(t)^{\exp(\beta' X)}, $$
where $S_0(t)$ is the baseline survival function. So, given your randomly sampled survival fraction and your covariates $X$, you invert the survival function to get the corresponding survival time.
That poses problems with an empirical Cox model. The baseline survival function is a step function, constant between event times and taking sharp steps down at event times. So it's a good idea at least to smooth the empirical baseline survival function. Furthermore, survival data often have a right-censored value for the last observation time, so in a semi-parametric Cox model there is no information on survival beyond that last observation time. That's why you will more typically see simulations based on parametric survival functions.
