# Remove first column, F_lu3, F_lu5, F_lu6, F_lu10 and remove rows with lc2006=1 and lc2006=6
training_SA_To_7_reduced = training_SA_To_7_reduced[-which(training_SA_To_7_reduced$lc2006==1 | training_SA_To_7_reduced$lc2006==6),
                                                    -c(1,25,27,28,32)]
testing_SA_To_7_reduced = testing_SA_To_7_reduced[-which(testing_SA_To_7_reduced$lc2006==1),-c(1,25,27,28,32)]

# make LU and LC factor
training_SA_To_7_reduced$lc2006 = factor(training_SA_To_7_reduced$lc2006)
training_SA_To_7_reduced$lu2006 = factor(training_SA_To_7_reduced$lu2006)
training_SA_To_7_reduced$lc2010 = factor(training_SA_To_7_reduced$lc2010)

testing_SA_To_7_reduced$lc2006 = factor(testing_SA_To_7_reduced$lc2006)
testing_SA_To_7_reduced$lu2006 = factor(testing_SA_To_7_reduced$lu2006)
testing_SA_To_7_reduced$lc2010 = factor(testing_SA_To_7_reduced$lc2010)

dim(training_SA_To_7_reduced) # 695  30
dim(testing_SA_To_7_reduced) # 299  30

########################################################################################
# 10-fold Cross-validation
########################################################################################
set.seed(123)
SA_To_7_reduced_folds <- createFolds(training_SA_To_7_reduced$status, k = 10, list = TRUE, returnTrain = TRUE)

tic("model fitting")
# 1st fold
set.seed(123)
SA_To_7_obj1_reduced = Surv(training_SA_To_7_reduced[SA_To_7_reduced_folds[[1]],]$time, 
                            training_SA_To_7_reduced[SA_To_7_reduced_folds[[1]],]$status)
Cox_SA_To_7_fld1_reduced = coxph(SA_To_7_obj1_reduced ~ ., 
                                 data=training_SA_To_7_reduced[SA_To_7_reduced_folds[[1]],-c(29,30)])
# summary(Cox_SA_To_7_fld1_reduced)
# Rsquare= 0.4
S0_SA_To_7_fld1_reduced = summary(survfit(Cox_SA_To_7_fld1_reduced, 
                                          newdata=training_SA_To_7_reduced[-SA_To_7_reduced_folds[[1]],-c(29,30)]), 
                                  time=5)$surv
lp_SA_To_7_fld1_reduced = predict(Cox_SA_To_7_fld1_reduced, 
                                  newdata = training_SA_To_7_reduced[-SA_To_7_reduced_folds[[1]],-c(29,30)], type = "lp")
St_SA_To_7_fld1_reduced = S0_SA_To_7_fld1_reduced^exp(lp_SA_To_7_fld1_reduced)
Acc_SA_To_7_fld1_reduced = confusionMatrix(factor(training_SA_To_7_reduced[-SA_To_7_reduced_folds[[1]],]$status),
                                           factor(ifelse(St_SA_To_7_fld1_reduced>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_7_fld1_reduced
# Accuracy : 0.8551

# >>>>>>>>>>>>>>>>>
# 2nd fold
set.seed(123)
SA_To_7_obj2_reduced = Surv(training_SA_To_7_reduced[SA_To_7_reduced_folds[[2]],]$time, 
                            training_SA_To_7_reduced[SA_To_7_reduced_folds[[2]],]$status)
Cox_SA_To_7_fld2_reduced = coxph(SA_To_7_obj2_reduced ~ ., 
                                 data=training_SA_To_7_reduced[SA_To_7_reduced_folds[[2]],-c(29,30)])
# summary(Cox_SA_To_7_fld2_reduced)
# Rsquare= 0.414
S0_SA_To_7_fld2_reduced = summary(survfit(Cox_SA_To_7_fld2_reduced, 
                                          newdata=training_SA_To_7_reduced[-SA_To_7_reduced_folds[[2]],-c(29,30)]), 
                                  time=5)$surv
lp_SA_To_7_fld2_reduced = predict(Cox_SA_To_7_fld2_reduced, 
                                  newdata = training_SA_To_7_reduced[-SA_To_7_reduced_folds[[2]],-c(29,30)], type = "lp")
St_SA_To_7_fld2_reduced = S0_SA_To_7_fld2_reduced^exp(lp_SA_To_7_fld2_reduced)
Acc_SA_To_7_fld2_reduced = confusionMatrix(factor(training_SA_To_7_reduced[-SA_To_7_reduced_folds[[2]],]$status),
                                           factor(ifelse(St_SA_To_7_fld2_reduced>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_7_fld2_reduced
# Accuracy : 0.7826

# >>>>>>>>>>>>>>>>>
# 3rd fold
set.seed(123)
SA_To_7_obj3_reduced = Surv(training_SA_To_7_reduced[SA_To_7_reduced_folds[[3]],]$time, 
                            training_SA_To_7_reduced[SA_To_7_reduced_folds[[3]],]$status)
Cox_SA_To_7_fld3_reduced = coxph(SA_To_7_obj3_reduced ~ ., 
                                 data=training_SA_To_7_reduced[SA_To_7_reduced_folds[[3]],-c(29,30)])
# summary(Cox_SA_To_7_fld3_reduced)
# Rsquare= 0.421
S0_SA_To_7_fld3_reduced = summary(survfit(Cox_SA_To_7_fld3_reduced, 
                                          newdata=training_SA_To_7_reduced[-SA_To_7_reduced_folds[[3]],-c(29,30)]), 
                                  time=5)$surv
lp_SA_To_7_fld3_reduced = predict(Cox_SA_To_7_fld3_reduced, 
                                  newdata = training_SA_To_7_reduced[-SA_To_7_reduced_folds[[3]],-c(29,30)], type = "lp")
St_SA_To_7_fld3_reduced = S0_SA_To_7_fld3_reduced^exp(lp_SA_To_7_fld3_reduced)
Acc_SA_To_7_fld3_reduced = confusionMatrix(factor(training_SA_To_7_reduced[-SA_To_7_reduced_folds[[3]],]$status),
                                           factor(ifelse(St_SA_To_7_fld3_reduced>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_7_fld3_reduced
# Accuracy : 0.7143

# >>>>>>>>>>>>>>>>>
# 4th fold
set.seed(123)
SA_To_7_obj4_reduced = Surv(training_SA_To_7_reduced[SA_To_7_reduced_folds[[4]],]$time, 
                            training_SA_To_7_reduced[SA_To_7_reduced_folds[[4]],]$status)
Cox_SA_To_7_fld4_reduced = coxph(SA_To_7_obj4_reduced ~ ., 
                                 data=training_SA_To_7_reduced[SA_To_7_reduced_folds[[4]],-c(29,30)])
# summary(Cox_SA_To_7_fld4_reduced)
# Rsquare= 0.427
S0_SA_To_7_fld4_reduced = summary(survfit(Cox_SA_To_7_fld4_reduced, 
                                          newdata=training_SA_To_7_reduced[-SA_To_7_reduced_folds[[4]],-c(29,30)]), 
                                  time=5)$surv
lp_SA_To_7_fld4_reduced = predict(Cox_SA_To_7_fld4_reduced, 
                                  newdata = training_SA_To_7_reduced[-SA_To_7_reduced_folds[[4]],-c(29,30)], type = "lp")
St_SA_To_7_fld4_reduced = S0_SA_To_7_fld4_reduced^exp(lp_SA_To_7_fld4_reduced)
Acc_SA_To_7_fld4_reduced = confusionMatrix(factor(training_SA_To_7_reduced[-SA_To_7_reduced_folds[[4]],]$status),
                                           factor(ifelse(St_SA_To_7_fld4_reduced>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_7_fld4_reduced
# Accuracy : 0.7429

# >>>>>>>>>>>>>>>>>
# 5th fold
set.seed(123)
SA_To_7_obj5_reduced = Surv(training_SA_To_7_reduced[SA_To_7_reduced_folds[[5]],]$time, 
                            training_SA_To_7_reduced[SA_To_7_reduced_folds[[5]],]$status)
Cox_SA_To_7_fld5_reduced = coxph(SA_To_7_obj5_reduced ~ ., 
                                 data=training_SA_To_7_reduced[SA_To_7_reduced_folds[[5]],-c(29,30)])
# summary(Cox_SA_To_7_fld5_reduced)
# Rsquare= 0.415
S0_SA_To_7_fld5_reduced = summary(survfit(Cox_SA_To_7_fld5_reduced, 
                                          newdata=training_SA_To_7_reduced[-SA_To_7_reduced_folds[[5]],-c(29,30)]), 
                                  time=5)$surv
lp_SA_To_7_fld5_reduced = predict(Cox_SA_To_7_fld5_reduced, 
                                  newdata = training_SA_To_7_reduced[-SA_To_7_reduced_folds[[5]],-c(29,30)], type = "lp")
St_SA_To_7_fld5_reduced = S0_SA_To_7_fld5_reduced^exp(lp_SA_To_7_fld5_reduced)
Acc_SA_To_7_fld5_reduced = confusionMatrix(factor(training_SA_To_7_reduced[-SA_To_7_reduced_folds[[5]],]$status),
                                           factor(ifelse(St_SA_To_7_fld5_reduced>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_7_fld5_reduced
# Accuracy : 0.8116

# >>>>>>>>>>>>>>>>>
# 6th fold
set.seed(123)
SA_To_7_obj6_reduced = Surv(training_SA_To_7_reduced[SA_To_7_reduced_folds[[6]],]$time, 
                            training_SA_To_7_reduced[SA_To_7_reduced_folds[[6]],]$status)
Cox_SA_To_7_fld6_reduced = coxph(SA_To_7_obj6_reduced ~ ., 
                                 data=training_SA_To_7_reduced[SA_To_7_reduced_folds[[6]],-c(29,30)])
# summary(Cox_SA_To_7_fld6_reduced)
# Rsquare= 0.419
S0_SA_To_7_fld6_reduced = summary(survfit(Cox_SA_To_7_fld6_reduced, 
                                          newdata=training_SA_To_7_reduced[-SA_To_7_reduced_folds[[6]],-c(29,30)]), 
                                  time=5)$surv
lp_SA_To_7_fld6_reduced = predict(Cox_SA_To_7_fld6_reduced, 
                                  newdata = training_SA_To_7_reduced[-SA_To_7_reduced_folds[[6]],-c(29,30)], type = "lp")
St_SA_To_7_fld6_reduced = S0_SA_To_7_fld6_reduced^exp(lp_SA_To_7_fld6_reduced)
Acc_SA_To_7_fld6_reduced = confusionMatrix(factor(training_SA_To_7_reduced[-SA_To_7_reduced_folds[[6]],]$status),
                                           factor(ifelse(St_SA_To_7_fld6_reduced>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_7_fld6_reduced
# Accuracy : 0.7571

# >>>>>>>>>>>>>>>>>
# 7th fold
set.seed(123)
SA_To_7_obj7_reduced = Surv(training_SA_To_7_reduced[SA_To_7_reduced_folds[[7]],]$time, 
                            training_SA_To_7_reduced[SA_To_7_reduced_folds[[7]],]$status)
Cox_SA_To_7_fld7_reduced = coxph(SA_To_7_obj7_reduced ~ ., 
                                 data=training_SA_To_7_reduced[SA_To_7_reduced_folds[[7]],-c(29,30)])
# summary(Cox_SA_To_7_fld7_reduced)
# Rsquare= 0.427
S0_SA_To_7_fld7_reduced = summary(survfit(Cox_SA_To_7_fld7_reduced, 
                                          newdata=training_SA_To_7_reduced[-SA_To_7_reduced_folds[[7]],-c(29,30)]), 
                                  time=5)$surv
lp_SA_To_7_fld7_reduced = predict(Cox_SA_To_7_fld7_reduced, 
                                  newdata = training_SA_To_7_reduced[-SA_To_7_reduced_folds[[7]],-c(29,30)], type = "lp")
St_SA_To_7_fld7_reduced = S0_SA_To_7_fld7_reduced^exp(lp_SA_To_7_fld7_reduced)
Acc_SA_To_7_fld7_reduced = confusionMatrix(factor(training_SA_To_7_reduced[-SA_To_7_reduced_folds[[7]],]$status),
                                           factor(ifelse(St_SA_To_7_fld7_reduced>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_7_fld7_reduced
# Accuracy : 0.6857

# >>>>>>>>>>>>>>>>>
# 8th fold
set.seed(123)
SA_To_7_obj8_reduced = Surv(training_SA_To_7_reduced[SA_To_7_reduced_folds[[8]],]$time, 
                            training_SA_To_7_reduced[SA_To_7_reduced_folds[[8]],]$status)
Cox_SA_To_7_fld8_reduced = coxph(SA_To_7_obj8_reduced ~ ., 
                                 data=training_SA_To_7_reduced[SA_To_7_reduced_folds[[8]],-c(29,30)])
# summary(Cox_SA_To_7_fld8_reduced)
# Rsquare= 0.414
S0_SA_To_7_fld8_reduced = summary(survfit(Cox_SA_To_7_fld8_reduced, 
                                          newdata=training_SA_To_7_reduced[-SA_To_7_reduced_folds[[8]],-c(29,30)]), 
                                  time=5)$surv
lp_SA_To_7_fld8_reduced = predict(Cox_SA_To_7_fld8_reduced, 
                                  newdata = training_SA_To_7_reduced[-SA_To_7_reduced_folds[[8]],-c(29,30)], type = "lp")
St_SA_To_7_fld8_reduced = S0_SA_To_7_fld8_reduced^exp(lp_SA_To_7_fld8_reduced)
Acc_SA_To_7_fld8_reduced = confusionMatrix(factor(training_SA_To_7_reduced[-SA_To_7_reduced_folds[[8]],]$status),
                                           factor(ifelse(St_SA_To_7_fld8_reduced>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_7_fld8_reduced
# Accuracy : 0.8286

# >>>>>>>>>>>>>>>>>
# 9th fold
set.seed(123)
SA_To_7_obj9_reduced = Surv(training_SA_To_7_reduced[SA_To_7_reduced_folds[[9]],]$time, 
                            training_SA_To_7_reduced[SA_To_7_reduced_folds[[9]],]$status)
Cox_SA_To_7_fld9_reduced = coxph(SA_To_7_obj9_reduced ~ ., 
                                 data=training_SA_To_7_reduced[SA_To_7_reduced_folds[[9]],-c(29,30)])
# summary(Cox_SA_To_7_fld9_reduced)
# Rsquare= 0.406
S0_SA_To_7_fld9_reduced = summary(survfit(Cox_SA_To_7_fld9_reduced, 
                                          newdata=training_SA_To_7_reduced[-SA_To_7_reduced_folds[[9]],-c(29,30)]), 
                                  time=5)$surv
lp_SA_To_7_fld9_reduced = predict(Cox_SA_To_7_fld9_reduced, 
                                  newdata = training_SA_To_7_reduced[-SA_To_7_reduced_folds[[9]],-c(29,30)], type = "lp")
St_SA_To_7_fld9_reduced = S0_SA_To_7_fld9_reduced^exp(lp_SA_To_7_fld9_reduced)
Acc_SA_To_7_fld9_reduced = confusionMatrix(factor(training_SA_To_7_reduced[-SA_To_7_reduced_folds[[9]],]$status),
                                           factor(ifelse(St_SA_To_7_fld9_reduced>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_7_fld9_reduced
# Accuracy : 0.7971

# >>>>>>>>>>>>>>>>>
# 10th fold
set.seed(123)
SA_To_7_obj10_reduced = Surv(training_SA_To_7_reduced[SA_To_7_reduced_folds[[10]],]$time, 
                             training_SA_To_7_reduced[SA_To_7_reduced_folds[[10]],]$status)
Cox_SA_To_7_fld10_reduced = coxph(SA_To_7_obj10_reduced ~ ., 
                                  data=training_SA_To_7_reduced[SA_To_7_reduced_folds[[10]],-c(29,30)])
# summary(Cox_SA_To_7_fld10_reduced)
# Rsquare= 0.438
S0_SA_To_7_fld10_reduced = summary(survfit(Cox_SA_To_7_fld10_reduced, 
                                           newdata=training_SA_To_7_reduced[-SA_To_7_reduced_folds[[10]],-c(29,30)]), 
                                   time=5)$surv
lp_SA_To_7_fld10_reduced = predict(Cox_SA_To_7_fld10_reduced, 
                                   newdata = training_SA_To_7_reduced[-SA_To_7_reduced_folds[[10]],-c(29,30)], type = "lp")
St_SA_To_7_fld10_reduced = S0_SA_To_7_fld10_reduced^exp(lp_SA_To_7_fld10_reduced)
Acc_SA_To_7_fld10_reduced = confusionMatrix(factor(training_SA_To_7_reduced[-SA_To_7_reduced_folds[[10]],]$status),
                                            factor(ifelse(St_SA_To_7_fld10_reduced>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_7_fld10_reduced
# Accuracy : 0.6957

# >>>>>>>>>>>>>>>>>
# Mean Accuracy
Acc_SA_To_7_reduced = c(Acc_SA_To_7_fld1_reduced[[1]], Acc_SA_To_7_fld2_reduced[[1]], 
                        Acc_SA_To_7_fld3_reduced[[1]], Acc_SA_To_7_fld4_reduced[[1]],
                        Acc_SA_To_7_fld5_reduced[[1]], Acc_SA_To_7_fld6_reduced[[1]],
                        Acc_SA_To_7_fld7_reduced[[1]], Acc_SA_To_7_fld8_reduced[[1]], 
                        Acc_SA_To_7_fld9_reduced[[1]], Acc_SA_To_7_fld10_reduced[[1]])
(mean_Acc_SA_To_7_reduced <- mean(Acc_SA_To_7_reduced))
# Accuracy : 0.7671

toc() # 1.25

########################################################################################
# Final Model
########################################################################################
# find out which training models returns the highest accuracy
which(Acc_SA_To_7_reduced==max(Acc_SA_To_7_reduced)) # 1
# find out significant predictors
summary(Cox_SA_To_7_fld1_reduced)
# MeanDEM+lu4_dist+lu8_dist+lu2006+lc2010+DA_Popn_Density

# construct final model
set.seed(123)
SA_To_7_final.obj_reduced = Surv(training_SA_To_7_reduced[SA_To_7_reduced_folds[[1]],]$time, training_SA_To_7_reduced[SA_To_7_reduced_folds[[1]],]$status)
Cox_SA_To_7_final_reduced = coxph(SA_To_7_final.obj_reduced ~ MeanDEM+lu4_dist+lu8_dist+lu2006+lc2010+DA_Popn_Density, 
                                  data=training_SA_To_7_reduced[SA_To_7_reduced_folds[[1]],-c(29,30)])
# summary(Cox_SA_To_7_final_reduced)
S0_SA_To_7_final_reduced = summary(survfit(Cox_SA_To_7_final_reduced, 
                                           newdata=testing_SA_To_7_reduced[,-c(29,30)]), time=5)$surv
lp_SA_To_7_final_reduced = predict(Cox_SA_To_7_final_reduced, 
                                   newdata = testing_SA_To_7_reduced[,-c(29,30)], type = "lp")
St_SA_To_7_final_reduced = S0_SA_To_7_final_reduced^exp(lp_SA_To_7_final_reduced)
Acc_SA_To_7_final_reduced = confusionMatrix(factor(testing_SA_To_7_reduced$status),
                                            factor(ifelse(St_SA_To_7_final_reduced>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_7_final_reduced
# Accuracy : 0.7525

# remove DA_Popn_Density
Cox_SA_To_7_final2_reduced = coxph(SA_To_7_final.obj_reduced ~ MeanDEM+lu4_dist+lu8_dist+lu2006+lc2010, 
                                   data=training_SA_To_7_reduced[SA_To_7_reduced_folds[[1]],-c(29,30)])
# summary(Cox_SA_To_7_final2_reduced)
S0_SA_To_7_final2_reduced = summary(survfit(Cox_SA_To_7_final2_reduced, 
                                            newdata=testing_SA_To_7_reduced[,-c(29,30)]), time=5)$surv
lp_SA_To_7_final2_reduced = predict(Cox_SA_To_7_final2_reduced, 
                                   newdata = testing_SA_To_7_reduced[,-c(29,30)], type = "lp")
St_SA_To_7_final2_reduced = S0_SA_To_7_final2_reduced^exp(lp_SA_To_7_final2_reduced)
Acc_SA_To_7_final2_reduced = confusionMatrix(factor(testing_SA_To_7_reduced$status),
                                            factor(ifelse(St_SA_To_7_final2_reduced>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_7_final2_reduced # 0.7659

anova(Cox_SA_To_7_final_reduced,Cox_SA_To_7_final2_reduced,test="Chisq")
# final2