# Import functions
source(".../functions.R") # Change "..." to the location of "functions.R"
library(survival)

# Import the gastric carcinoma data from the clinical trial conducted by the Gastrointestinal Tumor Study Group (Stablein et al. (1981))
# The data can be downloaded from the website: https://www.mayo.edu/research/documents/gastrichtml/DOC-10027680
data_Gastric<- read.table("http://www.mayo.edu/research/documents/gastricdat/DOC-10027750/")

# Rename the variables
names(data_Gastric)<-c("time", "delta", "group")

# Rename the groups so that the group with radiation is group 1 and the group without radiation is group 2
data_Gastric$group<-3-data_Gastric$group

# Plot survival curves 
plot(survfit(Surv(time, delta) ~ group, data = data_Gastric), col=c(2,4), lwd=3.0,      
     xlab=("Survival Time in Days"),
     ylab=("Proportion Surviving"),
     cex=1.5,
     cex.lab=1.5,
     cex.axis=1.5)
legend("bottomleft", legend=c( "Radiation","No Radiation"), col=c(2,4),lwd=3.0,cex=1.5)

# Find the hazard ratio and its confidence interval
group_factor <-factor(data_Gastric$group)
group_factor = relevel(group_factor, ref = 2)
fit_coxph<-coxph(Surv(time, delta) ~ group_factor, data = data_Gastric)
fit_coxph
exp(confint(fit_coxph))

# Exam the PH assumption through the Grambsch-Therneau test
cox.zph(fit_coxph)


# Estimate effect sizes based on the weighted logrank tests
results_ES_WL<-ES_WL(data_Gastric)
EShat_L<-results_ES_WL[1]
EShat_G<-results_ES_WL[2]
EShat_P<-results_ES_WL[3]
cat("Effect size based on the logrank test statistic: ", round(EShat_L,2), "\n")
cat("Effect size based on the Gehan-Wilcoxon test statistic: ", round(EShat_G,2), "\n")
cat("Effect size based on the Prentice-Wilcoxon test statistic: ", round(EShat_P,2),  "\n")

# Choose tau* for tau1, tau2, and tau in EShat_MWE and EShat_MWC
tau_star<-minimax(data_Gastric)
cat("tau*: ", tau_star,  "\n")

# Estimate the effect size based on the Mann-Whitney parameter by adding exponential tails
# By default, tau_i is set to t_maxi
EShat_MWE<-ES_MWE(data_Gastric,tau1=tau_star,tau2=tau_star)
cat("Effect size based on the Mann-Whitney parameter (estimated by adding exponential tails): ", round(EShat_MWE,2),  "\n")

# Estimate the effect size based on the Mann-Whitney parameter through conditioning
# By default, tau is set to min(t_max1,t_max2)
EShat_MWC<-ES_MWC(data_Gastric,tau=tau_star)
cat("Effect size based on the Mann-Whitney parameter (estimated through conditioning): ", round(EShat_MWC,2),  "\n")

# Find observed censoring rate in group 1
CRhat_1<-table(data_Gastric[data_Gastric$group==1,]$delta)[1]/(sum(table(data_Gastric[data_Gastric$group==1,]$delta)))
cat("The observed cesoring rate in group 1 is", round(100*CRhat_1),  "%\n")

# Find observed censoring rate in group 2
CRhat_2<-table(data_Gastric[data_Gastric$group==2,]$delta)[1]/(sum(table(data_Gastric[data_Gastric$group==2,]$delta)))
cat("The observed cesoring rate in group 2 is", round(100*CRhat_2),  "%\n")


# Construct confidence intervals for the effect sizes
set.seed(123)

B=100000

ES_L_boot=numeric(B)
ES_G_boot=numeric(B)
ES_P_boot=numeric(B)

ES_MWE_boot=numeric(B)
ES_MWC_boot=numeric(B)

for ( i in 1:B ) {
  Boot_Sample<-data_Gastric[sample(nrow(data_Gastric), nrow(data_Gastric), replace=TRUE), ]
  
  results_ES_WL_B<-ES_WL(Boot_Sample)
  ES_L_boot[i]<-results_ES_WL_B[1]
  ES_G_boot[i]<-results_ES_WL_B[2]
  ES_P_boot[i]<-results_ES_WL_B[3]
  
  ES_MWE_boot[i]<-ES_MWE(Boot_Sample)
  ES_MWC_boot[i]<-ES_MWC(Boot_Sample)
  
  if(i%%10000==0) print(i)
}

delta_ES_L<-ES_L_boot-EShat_L
delta_ES_G<-ES_G_boot-EShat_G
delta_ES_P<-ES_P_boot-EShat_P

delta_ES_MWE<-ES_MWE_boot-EShat_MWE
delta_ES_MWC<-ES_MWC_boot-EShat_MWC

ES_L_ci = round(EShat_L - quantile(delta_ES_L, c(0.975, 0.025)),2)
cat("Confidence interval for the effect Size based on the logrank test: ",ES_L_ci, "\n")

ES_G_ci = round(EShat_G - quantile(delta_ES_G, c(0.975, 0.025)),2)
cat("Confidence interval for the effect Size based on the Gehan-Wilcoxon test: ", ES_G_ci, "\n")

ES_P_ci = round(EShat_P - quantile(delta_ES_P, c(0.975, 0.025)),2)
cat("Confidence interval for the effect Size based on the Peto-Wilcoxon test: ",ES_P_ci, "\n")

ES_MWE_ci = round(EShat_MWE - quantile(delta_ES_MWE, c(0.975, 0.025)),2)
cat("Confidence interval for the effect Size based on the Mann-Whitney parameter (estimated by adding exponential tails): ",ES_MWE_ci, "\n")

ES_MWC_ci = round(EShat_MWC - quantile(delta_ES_MWC, c(0.975, 0.025)),2)
cat("Confidence interval for the effect Size based on the Mann-Whitney parameter (estimated through conditioning): ",ES_MWC_ci, "\n")
