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!
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.