# How to specify subject ID when using flexsurvreg to fit data with time-varying covariates

We are trying to use the R function flexsurv::flexsurvreg to fit data with time-varying covariates. The survival::coxph function has an 'id' argument that provides a means for specifying what the subject IDs are that link together multiple rows for the same subject. It does not appear that flexsurvreg has a comparable 'id' argument, so how do you specify the IDs with flexsurvreg?

I don't think that you can, but maybe you don't have to. From the vignette, page 3:

flexsurv does not currently support shared frailty, clustered or random effects models.

That might not, however, be necessary: if you only have at most one event per individual and a parametric proportional hazards (PH) model. See the discussion about handling time-dependent covariates on page 4 of the R survival package vignette on time dependence in Cox PH models. You don't need to keep track of individuals across rows because "The likelihood equations at any time point use only one copy of any subject, the program picks out the correct row of data at each time." An exception, however, is "When subjects have multiple events, then the rows for the events are correlated within subject and a cluster variance is needed."

For parametric modeling, the flexsurv vignette specifies in Section 3.1 that the package handles time-dependent covariates via left truncation in a counting-process format, as done with coxph() models:

Time-dependent covariates can be represented in “counting process” form — as a series of left-truncated survival times, which may also be right-censored. For each individual there would be multiple records, each corresponding to an interval where the covariate is assumed to be constant.

That works simply for a parametric proportional-hazard model, as Hougaard says in Section 4.2 of a brief Fundamentals of Survival Data review:

Time-dependent covariates are easily introduced in parametric proportional hazards models. For the likelihood, we need the hazard at the time of event and the integrated hazard over the observation period, which is easily found. (Emphasis added.)

So, provided that there is no more than 1 event per individual in a parametric PH model, you don't need to keep track of the IDs across rows to handle time-dependent covariates.

The problem is more difficult with parametric models other than PH models. For an accelerated failure time (AFT) model, Hougaard explains in the above-cited review that you need the entire history of a time-dependent covariate, which isn't available for a left-truncated observation. The eha package deals with this "by assuming that $$z(s)=z(t_0),0 \le s < t_0$$," where $$z(s)$$ is the set of covariate values over time $$s$$ with a left truncation at $$t_0$$, as explained in its vignette on parametric models. It might be that flexsurv makes a similar assumption silently; you could explore the code. Yet even the eha package, which does allow parametric modeling with specification of ID values for individuals, thus has some difficulties with time-dependent covariates in AFT models.

If the issue is multiple events per individual, you might consider the multi-state modeling that is now handled by flexsurv, allowing for multiple transitions involving an individual. I don't have experience with that, but it seems to turn the multi-state model into a set of single-transition models, which then can be combined.

Another approach might be to deal with the intra-subject correlation via bootstrapping. When lack of independence is dealt with via cluster variances (instead of by frailty/random-effect modeling), point estimates are the same but coefficient (co)variance estimates are appropriately adjusted for the lack of independence. Bootstrapping by individual might give you something workable in terms of bias and more reliable error estimates of the coefficients for your parametric model, although again I don't have experience with that in this type of parametric modeling with time-dependent coefficients.

Finally, if your client wishes to use this type of model for predictions on future cases, consider the epistemological objection that led the author of the Python lifelines package to make such predictions unavailable even for Cox PH models.

• Thanks much EdM. The same vignette also says that flexsurvreg can handle time-varying covariates on page 6. Our client is therefore convinced the package is capable of this, but we haven't been able to get it to work - it doesn't seem to be properly linking together the multiple rows for each subject. We have used eha but the client is interested in distributions that are supported by flexsurvreg but not supported by eha.
– Kari
Oct 25, 2021 at 16:15
• @Kari I expanded the answer to discuss further when such ID values might not be necessary, and to suggest alternate approaches for dealing with intra-individual correlations when they are.
– EdM
Oct 25, 2021 at 19:18