chjackson / flexsurv

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

adding time-dependent covariate in flexsurvspline #182

Closed quanhomie closed 3 months ago

quanhomie commented 6 months ago

Hi, thanks for giving us great package to use.

I'm trying to fitting model with time-dependent covariate using flexsurvspline function

flexsurvspline(Surv(start_t, end_t, status) ~ cov1, 
               data = data, k = 1)

but it gives me an error message saying,

Error in optim(method = "BFGS", par = c(gamma0 = 2.25366952832903, gamma1 = 0.605209668922837,  : 
  initial value in 'vmmin' is not finite`

The code run well if I use the Surv object not in counting process form, which means

flexsurvspline(Surv(obs_time, status) ~ cov1, 
               data = data, k = 1)

I'm wondering whether this error occurs because flexsurvspline function cannot be used for fitting model with time-dependent covariate or occurs because of the optimization problem.

The test data file is attached here test_data.csv

Thank you in advance.

chjackson commented 6 months ago

I wouldn't recommend using flexsurv with time-dependent covariates. The likelihood calculation does not generally apply (see this Github issue), and prediction from them isn't supported.

More technically, the error message was due to an unrelated issue - flexsurv uses a heuristic to choose the initial parameter values for optimisation, which sometimes fails. You can work around this by providing your own initial values (e.g. informed by parameter estimates from a simpler model that does work):

flexsurvspline(Surv(start_t, end_t, status) ~ cov1, data = data, k = 1, inits = c(-4, 0.7, 0))