#loading packages
library(survival)
library(survminer)
## Loading required package: ggplot2
## Loading required package: ggpubr
##
## Attaching package: 'survminer'
## The following object is masked from 'package:survival':
##
## myeloma
#loading dataset
data(lung)
## Warning in data(lung): data set 'lung' not found
lung$surv_obj <- Surv(time = lung$time, event = lung$status == 2)
#kaplan meier
km_fit <- survfit(lung$surv_obj ~ 1)
ggsurvplot(km_fit, data = lung, conf.int = TRUE, pval = TRUE)
## Warning in .pvalue(fit, data = data, method = method, pval = pval, pval.coord = pval.coord, : There are no survival curves to be compared.
## This is a null model.

#it shows the overall probability overtime for the entire cohort
#nelson aalen
na_fit <- survfit(lung$surv_obj ~ 1, type = "fh")
ggsurvplot(na_fit, data = lung, fun = "cumhaz")

#it shows cumulative hazard function over time an represents the accumulated risk of an event which is death
#long rank test
lung$sex <- factor(lung$sex, labels = c("Male", "Female"))
log_rank_test <- survdiff(lung$surv_obj ~ lung$sex)
log_rank_test
## Call:
## survdiff(formula = lung$surv_obj ~ lung$sex)
##
## N Observed Expected (O-E)^2/E (O-E)^2/V
## lung$sex=Male 138 112 91.6 4.55 10.3
## lung$sex=Female 90 53 73.4 5.68 10.3
##
## Chisq= 10.3 on 1 degrees of freedom, p= 0.001
#it shows statistically test for differences in survival between the groups
#cox proportion hazard
cox_model <- coxph(lung$surv_obj ~ age + sex + ph.ecog, data = lung)
summary(cox_model)
## Call:
## coxph(formula = lung$surv_obj ~ age + sex + ph.ecog, data = lung)
##
## n= 227, number of events= 164
## (1 observation deleted due to missingness)
##
## coef exp(coef) se(coef) z Pr(>|z|)
## age 0.011067 1.011128 0.009267 1.194 0.232416
## sexFemale -0.552612 0.575445 0.167739 -3.294 0.000986 ***
## ph.ecog 0.463728 1.589991 0.113577 4.083 4.45e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## exp(coef) exp(-coef) lower .95 upper .95
## age 1.0111 0.9890 0.9929 1.0297
## sexFemale 0.5754 1.7378 0.4142 0.7994
## ph.ecog 1.5900 0.6289 1.2727 1.9864
##
## Concordance= 0.637 (se = 0.025 )
## Likelihood ratio test= 30.5 on 3 df, p=1e-06
## Wald test = 29.93 on 3 df, p=1e-06
## Score (logrank) test = 30.5 on 3 df, p=1e-06
#it estimates hazard ratios for predictors and tests their significance and also checks the proportional hazard assumption