Integrate Kaplan-Meier estimator quickly in R

I am searching for R code for computing the following integral

$$\int\limits_t^{a}(x-t){\widehat S\left( x \right)dx}$$, where $$t$$ and $$b$$ are fixed constants and $$\widehat S\left( x \right)$$ is the Kaplan-Meier estimator for a survival function defined by $$\widehat S\left( t \right) = \prod\limits_{{X_j} \le t} {\left( {1 - \frac{{{d_j}}}{{{n_j}}}} \right)}$$.

Is there a faster way to compute this integral for a survival data, (2,3+,2,9,16,18+,7,17,5,5+): +symbol denotes the censored observations.

Using the "survival" library, you can compute the estimated survival curve with the function

ff = survfit(Surv(x, ind)~1)

Then by accessing the survival values and times in the ff object through \$surv and \$time, respectively, you can use these values in the function

approxfun

which can then be integrated numerically using the function

integrate

• @J McVittie Yes..!! Works perfectly. Many thanks. :) Feb 22 '20 at 13:28
• Consider upvoting answers that help you, as well as accepting them Feb 28 '20 at 21:44