chjackson / flexsurv

The flexsurv R package for flexible parametric survival and multi-state modelling
http://chjackson.github.io/flexsurv/
53 stars 28 forks source link

simulate.flexsurvreg appears to simulate survival times before left-truncation (start) times #192

Open mikesweeting opened 3 weeks ago

mikesweeting commented 3 weeks ago

Hi,

I'm trying to simulate survival times for a bunch of individuals conditional on them surviving up to their (individual-specific) censoring time. I'm using simulate.flexsurvreg() passing a vector of left-truncation times to the start argument. But some of the simulated times that are returned are before the left-truncation times.

Here is a reproducible example that shows the issue (I'm using version 2.3). Please let me know if I'm doing something wrong. Thanks!

library(flexsurv)
fit <- flexsurvreg(formula = Surv(futime, fustat) ~ rx, data = ovarian, dist="weibull")
nd <- ovarian
sim <- simulate(fit, seed=1002, newdata=nd, nsim = 2, start = nd$futime)

## Not all the simulated times are greater than the left trunction (start) times
sim$time_1 >= nd$futime
sim$time_2 >= nd$futime

## The same thing occurs with tidy = T
sim2 <- simulate(fit, seed=1002, newdata=nd, nsim = 2, start = nd$futime, tidy = T)
## Not all the simulated times are greater than the left trunction (start) times
sim2$time >= sim2$futime
chjackson commented 3 weeks ago

Thanks for spotting this - I think it is now fixed in https://github.com/chjackson/flexsurv/commit/353aad2497ef35db69425a9b2b06dcaeadf37df4 .