Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract values from survfit object

I've created this model:

model <- survfit(Surv(time,status)~c$sex)
model

and the output is:

Call: survfit(formula = Surv(time, status) ~ c$sex)

             records n.max n.start events median 0.95LCL 0.95UCL
c$sex=female      15    15      15      8    720     517      NA
c$sex=male        28    28      28     23    234     145     712    

So, I want to extract the median for males and the same for females, but have no idea how to do it. Here are my attempts to do it:

>model$median
NULL

>summary(model)$table[, "median"]
c$sex=female c$sex=male 
       720.0            234.5 

I want each one of the values alone ("720" and "234.5"), can somebody help me?

Thanks in advance

like image 278
JMarcelino Avatar asked Oct 23 '13 17:10

JMarcelino


2 Answers

You've already got it. All you are seeing printed to screen are the names attributes of the length 2 vector.

fit <- survfit(Surv(time, status) ~ x, data = aml)
summary(fit)$table
#                records n.max n.start events median 0.95LCL 0.95UCL
#x=Maintained         11    11      11      7     31      18      NA
#x=Nonmaintained      12    12      12     11     23       8      NA

#  Access first value like any other vector
summary(fit)$table[,'median'][1]
#x=Maintained 
#          31

To print without names use unname()...

unname(summary(fit)$table[,'median'])
# [1] 31 23

But you do not need to unname() them to use them as a numeric value, that is just an aesthetic desire...

sum( summary(fit)$table[,'median'] )
[1] 54

For further proof (!) that it is a vector use str()...

str(summary(fit)$table[,'median'])
# Named num [1:2] 31 23
# - attr(*, "names")= chr [1:2] "x=Maintained" "x=Nonmaintained"
like image 120
Simon O'Hanlon Avatar answered Oct 10 '22 21:10

Simon O'Hanlon


This also works:

> library(survMisc)
> fit <- survfit(Surv(time, status) ~ x, data = aml)
> median(fit)
                median
x=Maintained        31
x=Nonmaintained     23

And without the names (i.e. remove the structure of a data.frame):

> unname(unlist(median(fit)))
[1] 31 23

It's nice if you also want the confidence interval (default is 'log'):

> median(fit, CI=TRUE)
                median lower upper
x=Maintained        31    13    NA
x=Nonmaintained     23     5    43
like image 20
dardisco Avatar answered Oct 10 '22 19:10

dardisco