# How do you prepare longitudinal data for survival analysis?

I'm trying to plot a Kaplan-Meier curve of my data with R. Currently, the data is in the following format:

patient_id;number_of_days;survival
1         ;100           ;T
1         ;200           ;F
1         ;300           ;F
2         ;50            ;F
...


Survival in my context is not to be interpreted literally: it means progression of disease, which is only assessed at more or less equally spaced time-points.

I'm having trouble determining what the Surv function expects as input? How am I supposed to prepare the data?

I've tried feeding my data directly to Surv:

Surv(data$number_of_days,data$survival)


However this seems to count each patient multiple times (once per time-point). So my guess is that I must apply some kind of transformation to my data first. I'm fairly certain this problem of converting longitudinal data to "event data" must be fairly common, so how is this called? Is it already implemented somewhere?

Thanks!

• You might want to convert the data frame to "wide format". Then, note that the surv() function can take 3 arguments. The 1st argument is the starting time for the interval, the 2nd is the ending time of the interval, and the last one indicates whether the observation is an event or not. I cannot provide more details right now... – ocram May 21 '13 at 16:33
• Details appear on pp 76-78 of the manual. – whuber May 21 '13 at 18:37

Here is a quick example that shows how to arrange the data in a similar context.

Consider the following data.

> dataWide
id       time status
1  1 0.88820072      1
2  2 0.05562832      0
3  3 5.24113929      1
4  4 2.91370906      1


For example, individual 1 had an event at $t = 0.888$, and individual 3 had an event at $t = 5.241$.

For illustration, I take 3 time intervals: $[0, 1), [1, 2), [2, \infty)$.

In the long format, the same data set becomes:

> dataLong
id period tstart      tstop status
1  1      1      0 0.88820072      1
2  2      1      0 0.05562832      0
3  3      1      0 1.00000000      0
4  3      2      1 2.00000000      0
5  3      3      2 5.24113929      1
6  4      1      0 1.00000000      0
7  4      2      1 2.00000000      0
8  4      3      2 2.91370906      1


For individual 1, the first period starts at $t = 0$ and ends at $t = 0.888$ in which he had an event (status = 1). Individual 3 had an event in period 3. Therefore status = 0 for period 1 (from $0$ to $1$) and for period 2 (from $1$ to $2$), and status = 1 in period 3 (from $2$ to $5.241$).

Depending on the format, the Kaplan Meier curve can be obtained as follows,

library(survival)

plot(survfit(Surv(time, status) ~ 1, data=dataWide),
conf.int=FALSE, mark.time=FALSE)

plot(survfit(Surv(tstart, tstop, status) ~ 1, data=dataLong),
conf.int=FALSE, mark.time=FALSE)


• Thanks for your detailed answer! I don't have tbegin (the time of the previous visit), but I guess I can transform the data myself. I was kinda hoping this was a solved problem, but it doesn't seem too hard anyways. – static_rtti May 22 '13 at 8:13