Edneide Ramalho














Sign up
Course Notes: Survival Analysis in R
  • AI Chat
  • Code
  • Report
  • Beta
    Spinner

    Chapter 1: What is Survival Analysis?

    install.packages("TH.data")
    # Packages
    library(tidyverse)
    library(TH.data)
    Hidden output
    • We are interested in the time until the event occurs. For example:
      • Time until death
      • Time until the cab takes you after you call it
      • Time until you get a new job after you get unemployed, and so on
    # Datasets we are going to use
    data(GBSG2, package = "TH.data")

    Taking a look at the data

    # Check out the help page for this dataset
    help(GBSG2, package = "TH.data")
    
    # Load the data
    data(GBSG2, package = "TH.data")
    
    # Look at the summary of the dataset
    summary(GBSG2)

    Creating Surv objects

    time <- c(5, 6, 2, 4, 4)
    event <- c(1, 0, 0, 1, 1)
    
    library("survival")
    Surv(time, event)

    Digging into GBSG2 dataset

    # Our data set is called GBSG2 and is from package TH.data
    data("GBSG2", package = "TH.data")
    
    # Count censored and uncensored data
    num_cens <- table(GBSG2$cens)
    num_cens
    
    # Create barplot of censored and uncensored data
    barplot(num_cens)
    
    # Use help() to look at cens
    help(GBSG2, package = "TH.data")

    Using the Surv() function for GBSG2

    # Create Surv-Object
    sobj <- Surv(GBSG2$time, GBSG2$cens)
    
    # Look at 10 first elements
    sobj[1:10]
    
    # Look at summary
    summary(sobj)
    
    # Look at structure
    str(sobj)

    The UnempDur dataset

    • The UnempDur dataset contains information on how long people stay unemployed.