title: “SURVIVAL ASSIGNMENT 3” author: “Everlyne Karanja” date: “2025-03-03” output: word_document
library(survival) library(survminer) data(“Orange”) head(“Orange”) set.seed(123) Orange\(event <- sample(c(0,1), size = nrow(Orange), replace = TRUE) surv_obj <- Surv(time = Orange\)age, event = Orange$event) summary(surv_obj)
km_fit <- survfit(surv_obj ~ 1, data = Orange) ggsurvplot(km_fit, conf.int = TRUE, title = “Kaplan-Meier Survival Curve for Orange Trees”, xlab = “Time (Days)”, ylab = “Survival Probability”)
na_fit <- survfit(surv_obj ~ 1, data = Orange, type = “fh”) ggsurvplot(na_fit, fun = “cumhaz”, title = “Nelson-Aalen Cumulative Hazard”, xlab = “Time (Days)”, ylab = “Cumulative Hazard”)
km_fit_group <- survfit(surv_obj ~ Tree, data = Orange) ggsurvplot(km_fit_group, pval = TRUE, title = “Kaplan-Meier by Tree Group”, xlab = “Time (Days)”, ylab = “Survival Probability”) survdiff(surv_obj ~ Tree, data = Orange)
cox_model <- coxph(surv_obj ~ circumference + Tree, data = Orange) summary(cox_model) ggforest(cox_model, data = Orange, main = “Cox Proportional Hazards Model”)