# Survival analysis in time series

I have a dataset of patients who're prescribed a medication over the period of 3 years. The drug was supplied to each patient in different time intervals. I want to do a survival analysis to monitor recurrent events (i.e., events occurring multiple times).

The problem with the data is that, for some patients, the events do not occur in those time intervals where the drug is prescribed (e.g., an event may occur before/after the drug supply period). If I don't adjust this problem, using conventional survival analysis methods like Cox proportional hazard model may under/over-estimate the risk of events happening (because an event can happen when the drug supply is 0). So, how can I resolve this issue?

In R, the simplest approach would be to format the data with columns for (patientID, startTime, stopTime, event, drugStatus, otherCovariateValues...) with a separate row starting with each change of drug (or of other time-dependent covariates) or ending with each event. See this vignette for ways to format data this way. Including an ID for each patient allows correction for the correlations of results within individuals, e.g. by including a term of cluster(patientID) as a predictor in the model. This approach would be appropriate if the occurrence of an event is simply a function of the covariates and drug status at the time of the event, and if subsequent recurrent events were independent of prior events (given the present values of drug/covariates).
Frailty is another way that let you process your recurrent data.In survival package in R, simply use frailty(patient ID) when you use your model. Fit <- coxph(surv(start,stop,event)~ predictor1+ predictor2+.....+ frailty( patient ID), data)