0

I am putting together a survival analysis that gets run through a shiny app, and as such the output will be changing frequently. I have code that is running the survival analysis, and then printing the summary at for every 26 months (randomly chosen number as of right now), and doing that 10 times.

km_fit <- survfit(Surv(Life_Time_Months, Is_Closed) ~ 1, data = My_Data)
summary(km_fit, times = c(25 * (1:10)))

The output of the summary looks approximately like this:

time n.risk n.event survival std.err lower 95% CI upper 95% CI
   26  99910   19897    0.841 0.00104        0.839        0.843
   52  72512   15084    0.704 0.00134        0.701        0.707
   78  52153   10037    0.598 0.00150        0.595        0.601
  104  39170    5106    0.534 0.00159        0.531        0.537
  130  30783    3394    0.484 0.00165        0.481        0.487
  156  24563    2404    0.444 0.00171        0.440        0.447
  182  19351    1625    0.412 0.00176        0.408        0.415
  208  15463    1154    0.385 0.00182        0.382        0.389
  234  11924     796    0.363 0.00187        0.359        0.367
  260   9179     512    0.346 0.00194        0.342        0.349

Instead of having 10 printed results at multiples of 26, I would really like just one result that has when the percentage of survival is at 0.5, or as close as possible.

How would I go about doing this?

1 Answers1

2

The time corresponding to the median of the percentage of survival could be obtain in the summary of the fit.

For example, if you use the dataset lung:

library(survival)
library(survminer)
data("lung")
fit <- survfit(Surv(time, status) ~ sex, data = lung)

And the output of fit is:

> fit
Call: survfit(formula = Surv(time, status) ~ sex, data = lung)

        n events median 0.95LCL 0.95UCL
sex=1 138    112    270     212     310
sex=2  90     53    426     348     550

As you see, the median is the time at which the percentage of survival is equal to 0.5 in each condition. To extract this value, you can sort a summary table:

> summary(fit)$table
      records n.max n.start events   *rmean *se(rmean) median 0.95LCL 0.95UCL
sex=1     138   138     138    112 325.0663   22.59845    270     212     310
sex=2      90    90      90     53 458.2757   33.78530    426     348     550

And extract only median by doing:

> summary(fit)$table[,"median"]
sex=1 sex=2 
  270   426 

Adapted to your code, you should try:

summary(km_fit)$table[,"median"]

Does it answer your question ?

If not, please consider to provide a reproducible example of your data (How to make a great R reproducible example)

dc37
  • 15,840
  • 4
  • 15
  • 32
  • I think this is as close as I am going to get to what I had wanted. the extract only the median as not working for me and kept returning the "incorrect number of dimensions", but I just needed the row of the table that had the median survival value, so just "summary(km_fit)$table works – Ironicallylaughing Dec 24 '19 at 19:30
  • Glad my answer statisfies you (in a way) – dc37 Dec 25 '19 at 00:10
  • to remove the error "incorrect number of dimensions" try "summary(km_fit)$table["median"]" instead of "summary(km_fit)$table[,"median"]" – Alex Braksator Feb 27 '23 at 01:15