0

I want the survival probability estimated by Kaplan-Meier estimator for each individual of my dataframe. The survfit(Surv(.)) function calculates the survival probability for each unique time ordered by decreasing order.

What would be an elegant way of getting the survival probability for each individual from a survfit object?

library(survival)
data = data.frame(cbind(id = 1:10, 
            time = c(2,3,4,5,2,3,8,9,10,11), 
            status = c(1,0,0,1,0,1,0,1,0,1)))
survfit(Surv(data$time, data$status)~1)$surv
 # 0.9000 0.7875 0.7875 0.6300 0.6300 0.4200 0.4200 0.0000
survfit(Surv(data$time, data$status)~1)$time
 #  2  3  4  5  8  9 10 11

Thanks in advance!

Flora Grappelli
  • 659
  • 9
  • 26

1 Answers1

1

You can put the results in a data.frame and then just merge it back in

merge(data, 
   with(survfit(Surv(data$time, data$status)~1), data.frame(time, surv))
)

Here I just use with() to easily extract both columns from the survfit result.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • It's working. I had a problem at the beginning when the name of the variable in the survift were pasted from elsewhere and were not found in the data. I also tried to make a loop but it was less elegant than just doing a merge :-) – Flora Grappelli Apr 28 '20 at 16:29