R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

#Load necessary libraries library(survival)

library(survminer)

library(ggplot2)

#Load required dataset #Survival datasets

data()

data(veteran)

#PART 1

#Kaplan-Meier estimate

#Fit the Kaplan-Meier model

km_fit<-survfit(Surv(time,status)~trt,data=veteran) #creates a Kaplan-Meier survival curve,grouping patients by treatment(trt)

km_fit

summary(km_fit)

#Plot the Kaplan-Meier curve

plot(km_fit,col=“red”,xlab=“Time”,ylab=“Survival Probability”,main=“KM CURVE”)

#INTERPRETATION

#Explanation of the codes

#time:#time to event

#status:#Event indicator (1= event occurred, 0=censored)

#Results

#The two curves represent different treatment groups.If the curve separate widely,survival rates differ between groups

#PART 2

#Nelson-Aalen estimate

na_fit<-survfit(coxph(Surv(time,status)~trt,data=veteran),type=“aalen”)#fits a cox proportional hazard model using treatment(trt) as a covariate

na_fit

summary(na_fit)

#Plot the Nelson-Aaalen curve

plot(na_fit,col=“blue”,xlab=“Time”,ylab=“Survival Probability”,main=“NA CURVE”)

#INTERPRETATION

#Explanation of the code

#survfit(…, type=“aalen”)# converts the Cox model output into a Nelson-Aalen estimate

#Results

#The steeper the curve, the higher the hazard rate.If one group’s curve is consistently higher, that group experiences higher risk over time

#PART 3

#Log-Rank Test

log_rank_test<-survdiff(Surv(time,status)~trt,data=veteran) # compares the survival distributions of the two treatment groups.

log_rank_test

#INTERPRETATION

#Output: chi-square statistic - measures the difference in survival

#p-value- tests if survival differences are statistically significant

#Results

#p-value>0.05, hence no significant survival difference

#A lower chi-square value suggests a weaker difference

#PART 4

#Cox Proportional Hazards Model

cox_fit<-coxph(Surv(time,status)~trt+age+karno,data=veteran) # fits a Cox model with treatrment (trt),age(age) and Karnofsky score(karno) as predictors

cox_fit

#INTERPRETATION

#Explanation of the code

#summary(cox_fit)# displays likelihood ratio test and p-values for each variable

#Results

#p-value<0.05 #the predictor significantly affects survival