# ============================================================
# Kaplan-Meier Survival Curves
# Biostatistics in R — Rverse Analytics
# https://rverseanalytics.com/learn-biostatistics/km
# ============================================================

# Kaplan-Meier survival curves
library(survival)
# Survival by sex (lung cancer data)
fit <- survfit(Surv(time, status) ~ sex, data = lung)
fit
# The survival curve
plot(fit, col = c("#2f6fed", "#17a2b8"), lwd = 3, xlab = "Days", ylab = "Survival probability")
legend("topright", c("Male", "Female"), col = c("#2f6fed", "#17a2b8"), lwd = 3)
# Log-rank test: do the groups differ?
survdiff(Surv(time, status) ~ sex, data = lung)
