R language Access Menu

Title Text Both  

Survival analysis

The survival package is most useful for analyzing survival in datasets. It has many functions including those for Kaplan-Meier survival curve, Cox proportional hazard model etc. 

Code:

> library(survival)
> data(lung)
> head(lung)

 inst time status age sex ph.ecog ph.karno pat.karno meal.cal wt.loss
1    3  306      2  74   1       1       90       100     1175      NA
2    3  455      2  68   1       0       90        90     1225      15
3    3 1010      1  56   1       0       90        90       NA      15
4    5  210      2  57   1       1       90        60     1150      11
5    1  883      2  60   1       0      100        90       NA       0
6   12 1022      1  74   1       1       50        80      513       0

> survobj <- with(lung, Surv(time,status))
> fit = survfit(survobj ~ 1)
> fit
Call: survfit(formula = survobj ~ 1)

      n  events  median 0.95LCL 0.95UCL 
    228     165     310     285     363 
> plot(fit)

Output graph:

                     

For Cox proportional hazard model: 

Code:

> library(survival)
> head(heart)
  start stop event        age      year surgery transplant id
1     0   50     1 -17.155373 0.1232033       0          0  1
2     0    6     1   3.835729 0.2546201       0          0  2
3     0    1     0   6.297057 0.2655715       0          0  3
4     1   16     1   6.297057 0.2655715       0          1  3
5     0   36     0  -7.737166 0.4900753       0          0  4
6    36   39     1  -7.737166 0.4900753       0          1  4

> survobj = with(heart, Surv(start, stop, event))
> fit = coxph(survobj~age+year+surgery, data=heart)
> summary(fit)
Call:
coxph(formula = survobj ~ age + year + surgery, data = heart)

  n= 172, number of events= 75 

            coef exp(coef) se(coef)      z Pr(>|z|)  
age      0.02707   1.02744  0.01337  2.025   0.0429 *
year    -0.14627   0.86392  0.07044 -2.077   0.0378 *
surgery -0.63759   0.52856  0.36705 -1.737   0.0824 .
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

        exp(coef) exp(-coef) lower .95 upper .95
age        1.0274     0.9733    1.0009    1.0547
year       0.8639     1.1575    0.7525    0.9918
surgery    0.5286     1.8919    0.2574    1.0852

Concordance= 0.637  (se = 0.037 )
Rsquare= 0.084   (max possible= 0.969 )
Likelihood ratio test= 15.11  on 3 df,   p=0.001725
Wald test            = 14.5  on 3 df,   p=0.002303
Score (logrank) test = 15.03  on 3 df,   p=0.00179


The output shows that age and year are significant predictors of survival but not surgery, which shows only a trend.

References:
Therneau T (2015). _A Package for Survival Analysis in S_. version 2.38, <URL: http://CRAN.R-project.org/package=survival>.
 


    Comments & Feedback