Last active
January 27, 2023 20:29
-
-
Save lundquist-ecology-lab/00236e2a4a78112251284c9cb0a33bb7 to your computer and use it in GitHub Desktop.
Survival analysis and log-rank comparison in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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