R(29), Survival Analysis

Published: by Creative Commons Licence

Facing survival analysis, we use R package named "survival" which contain functions like Surv(), survfit() and so on.

install.packages("survival")

Functions:

Surv(time,event)
survfit(formula)
  • time: for right censored data, this is the follow up time. For interval data, the first argument is the starting time for the interval.
  • event: The status indicator, normally 0=alive, 1=dead. Other choices are TRUE/FALSE (TRUE = death) or 1/2 (2=death). For interval censored data, the status indicator is 0=right censored, 1=event at time, 2=left censored, 3=interval censored. For multiple endpoint data the event variable will be a factor, whose first level is treated as censoring. Although unusual, the event indicator can be omitted, in which case all subjects are assumed to have an event.
  • formula: either a formula or a previously fitted mode
# Load the library.
library("survival")

# Create the survival object. 
survfit(Surv(pbc$time,pbc$status == 2)~1)

# Give the chart file a name.
png(file = "survival.png")

# Plot the graph. 
plot(survfit(Surv(pbc$time,pbc$status == 2)~1))

# Save the file.
dev.off()