Kaplan-Meier and Log-Rank Test: R Code

Arndt Regorz, Dipl. Kfm. & M.Sc. Psychology, 11/22/2023

Here is the R code for the Youtube tutorials about survival analysis:

# Kaplan-Meier Estimator
library(survival)
library(broom)

head(aml, 3)

KapM_C <- survfit(Surv(time, status) ~ x, data=aml)

# Results
KapM_C
print(tidy(KapM_C))

# Graph
plot(KapM_C, ylab="Überlebenswahrscheinlichkeit", xlab="Monate",
conf.int=T, col=c("red", "blue"))

# Log Rank Test

library(survival)

#Default Mantel-Haenszel-Test
survdiff(Surv(time, status) ~ x, data=aml)

# More weight for earlier time points (rho = 1, Peto&Peto)
survdiff(Surv(time, status) ~ x, data=aml, rho=1)