Skip to content

Instantly share code, notes, and snippets.

@lundquist-ecology-lab
Last active January 27, 2023 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lundquist-ecology-lab/00236e2a4a78112251284c9cb0a33bb7 to your computer and use it in GitHub Desktop.
Save lundquist-ecology-lab/00236e2a4a78112251284c9cb0a33bb7 to your computer and use it in GitHub Desktop.
Survival analysis and log-rank comparison in R
# Survival analysis in R
# Load library (install first if needed)
library(survival)
library(survMisc)
# create a sample data set
data <- lung
# fit a survival model
fit <- survfit(Surv(time, status) ~ sex, data = data)
# print the summary of the model
summary(fit)
# plot the survival curve
autoplot(fit,
timeTicks = "custom", times = seq(1, 1000, 100),
plotTable = TRUE,
CI = TRUE,
bands = TRUE)
# compare survival curves
result <- survdiff(Surv(time, status) ~ sex, data = lung)
# print the result of the log-rank test
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment