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.


1 Answer 1


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


which can then be integrated numerically using the function


  • $\begingroup$ @J McVittie Yes..!! Works perfectly. Many thanks. :) $\endgroup$
    – vip123
    Feb 22, 2020 at 13:28
  • $\begingroup$ Consider upvoting answers that help you, as well as accepting them $\endgroup$ Feb 28, 2020 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.