I'm using the survival
library. After computing the Kaplan-Meier estimator of a survival function:
km = survfit(Surv(time, flag) ~ 1)
I know how to compute percentiles:
quantile(km, probs = c(0.05,0.25,0.5,0.75,0.95))
But, how do I compute the mean survival time?
Calculate Mean Survival Time
The mean survival time will in general depend on what value is chosen for the maximum survival time. You can get the restricted mean survival time with
print(km, print.rmean=TRUE)
. By default, this assumes that the longest survival time is equal to the longest survival time in the data. You can set this to a different value by adding anrmean
argument (e.g.,print(km, print.rmean=TRUE, rmean=250)
).Extract Value of Mean Survival Time and Store in an Object
In response to your comment: I initially figured one could extract the mean survival time by looking at the object returned by
print(km, print.rmean=TRUE)
, but it turns out thatprint.survfit
doesn't return a list object but just returns text to the console.Instead, I looked through the code of
print.survfit
(you can see the code by typinggetAnywhere(print.survfit)
in the console) to see where the mean survival time is calculated. It turns out that a function calledsurvmean
takes care of this, but it's not an exported function, meaning R won't recognize the function when you try to run it like a "normal" function. So, to access the function, you need to run the code below (where you need to setrmean
explicitly):You'll see that the function returns a list where the first element is a matrix with several named values, including the mean and the standard error of the mean. So, to extract, for example, the mean survival time, you would do:
Details on How the Mean Survival Time is Calculated
The help for
print.survfit
provides details on the options and how the restricted mean is calculated: