Useful R Functions
This is a Coursera course offered by Imperial College London on Survival Analysis using R.
survival pkg commands
library(survival)
fit = survfit(Surv(fu_time, death) ~ gender)
plot(fit, xlab = "time", ylab = "survival probability")
survdiff(Surv(fu_time, death) ~ gender, rho = 0)
# define significant vars as vector
# use formula as input for coxph()
fmla <- as.formula(paste("Surv(fu_time, death) ~ ", paste(vars_sig, collapse=" + "), sep = ""))
cox_vars_sig <- coxph(fmla, data = g2)
summary(cox_vars_sig)
survminer pkg commands
- pretty graphs for KM plots and residual plots
library(survminer)
fit = coxph(...)
zph = cox.zph(fit, transform = "km")
plot(zph)
OR
ggcoxzph(zph) # time transformed Schoenfeld residuals (PH assumption)
# influential data points
ggcoxdiagnostics(fit, type = "dfbeta", linear.prediction = FALSE, ggtheme = theme_bw())
# residual vs covariates
ggcoxdiagnostics(fit, type = "deviance", linear.prediction = FALSE, ggtheme = theme_bw())
# Martingale residuals (linear relationship btw continuous vars and outcome)
ggcoxfunctional(Surv(fu_time, death) ~ age + log(age) + sqrt(age), data = g)