## Author: Eric Morenz ## Date: 2020/07/22 ## R basics for survival library(dplyr) ## If you get an error. Please run: install.packages("dplyr") library(readxl) ## To read .xls files library(survival) ## Classic and well known ## library(flexsurv) ## mostly an extension of survival package. Not used ## library(foreign) ## only used if we want to load in a Stata dataset (.dta format) setwd(here::here())## install.packages("here") ### Reading our data and ### Taking a small subset of the data and renaming some variables df <- read_excel("PS_Datasets/pbc.xls") %>% select(N, X, D, Z1) %>% rename(ID = N, Time = X, Death = D, Trt = Z1) %>% filter(ID < 200) #### Creating a survival object. This is one way to do it. surv_obj <- df %>% with(Surv(time=Time, event=Death, type="right")) #### time is the time to event #### event usually follows 1 = event/death, 0 = censoring ### Basic summary summary(surv_obj) ### Creating a Kaplan-Meier curve survfit_obj <- survfit(surv_obj ~ 1, data=df, conf.type = "log-log") ## ## Kaplan-Meier plot with pointwise confidence intervals plot(survfit_obj, col=c( 'red', 'blue', 'blue'), main="Kaplan-Meier Survival Estimate", ylab="Survival Probability", xlab="Time") ### There are ways to create KM plots with ggplot2, but they often require additonal packages