Search code examples
rdataframesurvival-analysis

How to extract the dataframe that includes the median and its 95% confidence interval in the survfit function?


Let's say I applied survival analysis on the following df

# Load necessary packages
library(survival)

# Generate example survival data (time to event and event status)
set.seed(123)
n <- 100
time <- rexp(n, rate = 0.1)  # Exponential survival times (you can replace this with your own dataset)
status <- sample(0:1, n, replace = TRUE)  # Censoring status (0: censored, 1: event)

# Create a survival object
surv_object <- Surv(time, status)

# Fit Kaplan-Meier survival curve
km_fit <- survfit(surv_object ~ 1)

the output of km_fit is as follows


Call: survfit(formula = surv_object ~ 1)

       n events median 0.95LCL 0.95UCL
[1,] 100     52   14.6    10.7    17.3

However the data frame that includes the median and its confidence interval, I am not able to extract it.


Solution

  • Under the hood, the print method for objects of class survfit calls the survmean() function (which is not exported from the survival package namespace). You can call it directly with survival:::survmean(). In this case, if you step through the survival:::print.survfit() function using debug, you'll find that it sets the rmean argument to "none". Here's how you could get the matrix you're looking for:

    library(survival)
    
    # Generate example survival data (time to event and event status)
    set.seed(123)
    n <- 100
    time <- rexp(n, rate = 0.1)  # Exponential survival times (you can replace this with your own dataset)
    status <- sample(0:1, n, replace = TRUE)  # Censoring status (0: censored, 1: event)
    
    # Create a survival object
    surv_object <- Surv(time, status)
    
    # Fit Kaplan-Meier survival curve
    km_fit <- survfit(surv_object ~ 1)
    
    km_fit
    #> Call: survfit(formula = surv_object ~ 1)
    #> 
    #>        n events median 0.95LCL 0.95UCL
    #> [1,] 100     52   14.6    10.7    17.3
    
    survival:::survmean(km_fit, rmean="none")$matrix
    #>   records     n.max   n.start    events    median   0.95LCL   0.95UCL 
    #> 100.00000 100.00000 100.00000  52.00000  14.63301  10.67213  17.31154
    

    Created on 2023-09-21 with reprex v2.0.2