# Generating data from KM curves

I have some survival data from different cohorts and can fit Kaplan Meier curves and cox proportional hazard models. I want to be able to simulate data that resembles the data found in say the KM curves. What I would like to know is there any way to simulate data from the KM curves or the cox proportional hazard model?

Certainly.

Remember that the KM curves are estimates of the survival function, ie $\hat S_{km}(t)$. As such, you can simulate draws from this distribution by using the inverse survival function, i.e.

$u_i \sim$ uniform(0,1)

$T_i = S_{km}^{-1}(u_i)$

Then $T_i$ will be distributed according the distribution in the KM curve. Note that the KM curve will be a discrete distribution, while the original data generating process may be continuous. Also, this does not take into account the uncertainty in the KM curves.

Here's how one could do this in R:

library(survival)
y <- rexp(100)
isCen <- y > 2
y[isCen] <- 2
fit <- survfit(Surv(y, !isCen) ~ 1 )
quantile(fit, probs = runif(1) )$quantile  Very technically speaking, a Cox-PH model does not necessarily include an estimated baseline survival distribution, which is required to sample from. However, this can be easily remedied by using the KM curves as the baseline survival estimate. Of course, the following method can be used for any proportional hazards model and baseline survival estimate. From here, we use the relation that if$\eta_i = X^T \beta$(i.e. the linear predictor for subject i), the estimated proportional hazards survival curve will be$S(t | \eta_i)$=$S_o(t)^{\exp(\eta_i)}$where$S_o(t)$is the baseline survival curve. Being extra clever, we can then see that$S^{-1}(u | \eta_i) = S_o^{-1}(u^{\exp(-\eta_i)})$So given$\hat S_o^{-1}$and$\eta_i$, we can take draws via$u_i \sim$uniform(0,1)$T_i = \hat S_o^{-1}(u^{\exp(-\eta_i)})$• Awesome, that makes sense. What about simulating from the cox model, is that doable? – RustyStatistician Mar 4 '16 at 2:52 • @RustyStatistician: updated it to include the cox model. – Cliff AB Mar 4 '16 at 5:04 • Could we talk in a private room or offline? I would like to pick your brain about this question some more for my actual application. – RustyStatistician Mar 4 '16 at 17:09 • @RustyStatistician:sure, except I'm not really sure how to the private chat room think on CV. Can you invite me? – Cliff AB Mar 4 '16 at 17:22 • How do you calculate$S^{-1}_{km}\$? – RustyStatistician Mar 4 '16 at 19:44