I want to create a kaplan meier plot. Most tutorials focus on a binary survival (death/survival), but in my case, I would like to plot the time to an event (1,2,3 times switch in medication). Is this possible?
Here is some example data to illustrate my point.
library (data.table)
data <- data.table( ID = c(1,1,2,3,4,5,6,6,7,7,7,8,9,10,11,11,11,12,12,13,14,14,14,15,15,16),
episode = c(1,2,1,1,1,1,1,2,1,2,3,1,1,1,1,2,3,1,2,1,1,2,3,1,2,1),
time_till_next_ep = c(0,5,0,0,0,0,0,8,0,4,14,0,0,0,0,6,17,0,9,0,0,4,9,0,8,0))
First, we have a variable indicating the participant ID
.
Then we have a variable indicating the medication episode
. This means if a participant takes a certain type of medication within a certain timeframe. Most participants only take the medication once (episode=1), but several take the medication again a second (episode = 2) or third time (episode = 3).
The time between the episodes is calculated in days in the time_till_next_ep
variable. Since there is no time between episodes for the 1st case, there are a lot of zeroes in the data.
I want to create a Kaplan-Meier-Graph: on the x-axis the time until the next episode and on the y-axis the number of episodes. With every new episode beginning for a patient, the graph would go one step down.
I've tried using the survival package to create this, but it does not seem to be working
library(survival)
S_input <- Surv(time = data[,as.numeric(time_till_next_ep)], event = data[,episode])
km_fit <- survfit(S_input ~ 1,
data = data,
type = "kaplan-meier")
km_fig <- ggsurvplot(km_fit,
data,
tables.theme = theme_cleantable())
If I check the input S_input
data, it seems to have added questionmarks next to my time data
> S_input
[1] 0 5? 0 0 0 0 0 8? 0 4? 14? 0 0 0 0 6? 17? 0 9? 0 0 4? 9? 0 8? 0
And the summary data is not really informative:
> summary(km_fit)
Call: survfit(formula = S_input ~ 1, data = data, type = "kaplan-meier")
10 observations deleted due to missingness
time n.risk n.event survival std.err lower 95% CI upper 95% CI
0 16 16 0 NaN NA NA
The message says that 10 observations are deleted due to missingness, these seem to be my 10 switches in medication.
Lastly, I get the following error when plotting: Error in f(...) : Aesthetics can not vary with a ribbon
Am I even using the survival analysis correctly in my case or is this really reserved for binary data?