#load neccesary libraries library(survival) library(survminer)
#picking a suitable data data<-lung
str(lung)
km_fit <- survfit(Surv(time, status) ~ 1, data = lung)
ggsurvplot(km_fit, data = lung, pval = TRUE, conf.int = TRUE, x lab = “Time in Days”, y lab = “Survival Probability”, title = “Kaplan-Meier Survival Curve”)
#comment on the curve ##The plot shows the probability of survival over time. ##The “status” variable indicates whether the patient died (status = 1) or was censored (status = 0). ##The p-value tests the null hypothesis that the survival functions are identical over time.
na_fit <- survfit(Surv(time, status) ~ 1, data = lung)
plot(na_fit, main = “Nelson-Aalen Estimator”, xlab = “Time in Days”, ylab = “Cumulative Hazard”)
##comment on the Nelson Aalen Estimator #The plot shows the cumulative hazard function over time. The hazard rate increases as time progresses, showing the cumulative risk of the event (death) occurring at any given time.
logrank_test <- survdiff(Surv(time, status) ~ sex, data = lung) logrank_test
##comment on the test #The log-rank test compares the survival curves between the two groups (male and female). #The null hypothesis is that the survival functions are the same. A low p-value (typically < 0.05) indicates that the survival curves for males and females are significantly different.
cox_fit <- coxph(Surv(time, status) ~ age + sex + ph.ecog, data = lung)
summary(cox_fit) ##Comment ##The Cox model provides estimates for the hazard ratios (HR) of each covariate. ##For example, the hazard ratio for “age” tells us how the risk of death changes for each unit increase in age. ##The p-values test the null hypothesis that each covariate has no effect on the survival time. A p-value < 0.05 typically indicates that the covariate is statistically significant