Estimating a survival probability in R

Based on a sample of n survival times, I would like to estimate the probability of surviving time t, for some specific t, using the Kaplan-Meier estimator. Is it possible to do this in R? Please, note that t is not necessarily an event time.

Answer

You can use the output of the survfit function from the survival package and give that to stepfun.

km <- survfit(Surv(time, status)~1, data=veteran)
survest <- stepfun(km$time, c(1, km$surv))

Now survest is a function that can be evaluated at any time.

> survest(0:100)
  [1] 1.0000000 0.9854015 0.9781022 0.9708029 0.9635036 0.9635036 0.9635036
  [8] 0.9416058 0.9124088 0.9124088 0.8978102 0.8905109 0.8759124 0.8613139
 [15] 0.8613139 0.8467153 0.8394161 0.8394161 0.8175182 0.8029197 0.7883212
 [22] 0.7737226 0.7664234 0.7664234 0.7518248 0.7299270 0.7299270 0.7225540
 [29] 0.7225540 0.7151810 0.7004350 0.6856890 0.6856890 0.6783160 0.6783160
 [36] 0.6709430 0.6635700 0.6635700 0.6635700 0.6635700 0.6635700 0.6635700
 [43] 0.6561970 0.6488240 0.6414510 0.6340780 0.6340780 0.6340780 0.6267050
 [50] 0.6193320 0.6193320 0.5972130 0.5750940 0.5677210 0.5529750 0.5529750
 [57] 0.5456020 0.5456020 0.5456020 0.5382290 0.5382290 0.5308560 0.5308560
 [64] 0.5234830 0.5234830 0.5234830 0.5234830 0.5234830 0.5234830 0.5234830
 [71] 0.5234830 0.5234830 0.5161100 0.5087370 0.5087370 0.5087370 0.5087370
 [78] 0.5087370 0.5087370 0.5087370 0.4939910 0.4939910 0.4866180 0.4866180
 [85] 0.4791316 0.4791316 0.4791316 0.4716451 0.4716451 0.4716451 0.4640380
 [92] 0.4640380 0.4564308 0.4564308 0.4564308 0.4412164 0.4412164 0.4412164
 [99] 0.4412164 0.4257351 0.4179945

Attribution
Source : Link , Question Author : user7064 , Answer Author : Brian Diggs

Leave a Comment