# remove first column, F_lu3, F_lu5, F_lu6, F_lu10
training_SA_To_2_reduced = training_SA_To_2_reduced[-which(training_SA_To_2_reduced$lc2010==6),
                                                    -c(1,25,27,28,32)]
testing_SA_To_2_reduced = testing_SA_To_2_reduced[-which(testing_SA_To_2_reduced$lc2010==6),
                                                  -c(1,25,27,28,32)]

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

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

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

tic("model fitting")
# 1st fold
set.seed(123)
SA_To_2_obj1_reduced = Surv(training_SA_To_2_reduced[SA_To_2_reduced_folds[[1]],]$time, 
                            training_SA_To_2_reduced[SA_To_2_reduced_folds[[1]],]$status)
Cox_SA_To_2_fld1_reduced = coxph(SA_To_2_obj1_reduced ~ ., 
                                 data=training_SA_To_2_reduced[SA_To_2_reduced_folds[[1]],-c(29,30)])
# summary(Cox_SA_To_2_fld1_reduced)
# Rsquare= 0.555
S0_SA_To_2_fld1_reduced = summary(survfit(Cox_SA_To_2_fld1_reduced, 
                                          newdata=training_SA_To_2_reduced[-SA_To_2_reduced_folds[[1]],-c(29,30)]), 
                                  time=5)$surv
lp_SA_To_2_fld1_reduced = predict(Cox_SA_To_2_fld1_reduced, 
                                  newdata = training_SA_To_2_reduced[-SA_To_2_reduced_folds[[1]],-c(29,30)], type = "lp")
St_SA_To_2_fld1_reduced = S0_SA_To_2_fld1_reduced^exp(lp_SA_To_2_fld1_reduced)
Acc_SA_To_2_fld1_reduced = confusionMatrix(factor(training_SA_To_2_reduced[-SA_To_2_reduced_folds[[1]],]$status),
                                           factor(ifelse(St_SA_To_2_fld1_reduced>0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_2_fld1_reduced
# Accuracy : 0.8806

# >>>>>>>>>>>>>>>>>
# 2nd fold
set.seed(123)
SA_To_2_obj2_reduced = Surv(training_SA_To_2_reduced[SA_To_2_reduced_folds[[2]],]$time, 
                            training_SA_To_2_reduced[SA_To_2_reduced_folds[[2]],]$status)
Cox_SA_To_2_fld2_reduced = coxph(SA_To_2_obj2_reduced ~ ., 
                                 data=training_SA_To_2_reduced[SA_To_2_reduced_folds[[2]],-c(29,30)])
# summary(Cox_SA_To_2_fld2_reduced)
# Rsquare= 0.551
S0_SA_To_2_fld2_reduced = summary(survfit(Cox_SA_To_2_fld2_reduced, 
                                          newdata=training_SA_To_2_reduced[-SA_To_2_reduced_folds[[2]],-c(29,30)]), 
                                  time=5)$surv
lp_SA_To_2_fld2_reduced = predict(Cox_SA_To_2_fld2_reduced, 
                                  newdata = training_SA_To_2_reduced[-SA_To_2_reduced_folds[[2]],-c(29,30)], type = "lp")
St_SA_To_2_fld2_reduced = S0_SA_To_2_fld2_reduced^exp(lp_SA_To_2_fld2_reduced)
Acc_SA_To_2_fld2_reduced = confusionMatrix(factor(training_SA_To_2_reduced[-SA_To_2_reduced_folds[[2]],]$status),
                                                  factor(ifelse(St_SA_To_2_fld2_reduced>0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_2_fld2_reduced
# Accuracy : 0.9242

# >>>>>>>>>>>>>>>>>
# 3rd fold
set.seed(123)
SA_To_2_obj3_reduced = Surv(training_SA_To_2_reduced[SA_To_2_reduced_folds[[3]],]$time, 
                            training_SA_To_2_reduced[SA_To_2_reduced_folds[[3]],]$status)
Cox_SA_To_2_fld3_reduced = coxph(SA_To_2_obj3_reduced ~ ., 
                                 data=training_SA_To_2_reduced[SA_To_2_reduced_folds[[3]],-c(29,30)])
# summary(Cox_SA_To_2_fld3_reduced)
# Rsquare= 0.555
S0_SA_To_2_fld3_reduced = summary(survfit(Cox_SA_To_2_fld3_reduced, 
                                          newdata=training_SA_To_2_reduced[-SA_To_2_reduced_folds[[3]],-c(29,30)]), 
                                  time=5)$surv
lp_SA_To_2_fld3_reduced = predict(Cox_SA_To_2_fld3_reduced, 
                                  newdata = training_SA_To_2_reduced[-SA_To_2_reduced_folds[[3]],-c(29,30)], type = "lp")
St_SA_To_2_fld3_reduced = S0_SA_To_2_fld3_reduced^exp(lp_SA_To_2_fld3_reduced)
Acc_SA_To_2_fld3_reduced = confusionMatrix(factor(training_SA_To_2_reduced[-SA_To_2_reduced_folds[[3]],]$status),
                                           factor(ifelse(St_SA_To_2_fld3_reduced>0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_2_fld3_reduced
# Accuracy : 0.8507

# >>>>>>>>>>>>>>>>>
# 4th fold
set.seed(123)
SA_To_2_obj4_reduced = Surv(training_SA_To_2_reduced[SA_To_2_reduced_folds[[4]],]$time, 
                            training_SA_To_2_reduced[SA_To_2_reduced_folds[[4]],]$status)
Cox_SA_To_2_fld4_reduced = coxph(SA_To_2_obj4_reduced ~ ., 
                                 data=training_SA_To_2_reduced[SA_To_2_reduced_folds[[4]],-c(29,30)])
# summary(Cox_SA_To_2_fld4_reduced)
# Rsquare= 0.554
S0_SA_To_2_fld4_reduced = summary(survfit(Cox_SA_To_2_fld4_reduced, 
                                          newdata=training_SA_To_2_reduced[-SA_To_2_reduced_folds[[4]],-c(29,30)]), 
                                  time=5)$surv
lp_SA_To_2_fld4_reduced = predict(Cox_SA_To_2_fld4_reduced, 
                                  newdata = training_SA_To_2_reduced[-SA_To_2_reduced_folds[[4]],-c(29,30)], type = "lp")
St_SA_To_2_fld4_reduced = S0_SA_To_2_fld4_reduced^exp(lp_SA_To_2_fld4_reduced)
Acc_SA_To_2_fld4_reduced = confusionMatrix(factor(training_SA_To_2_reduced[-SA_To_2_reduced_folds[[4]],]$status),
                                          factor(ifelse(St_SA_To_2_fld4_reduced>0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_2_fld4_reduced
# Accuracy : 0.9403

# >>>>>>>>>>>>>>>>>
# 5th fold
set.seed(123)
SA_To_2_obj5_reduced = Surv(training_SA_To_2_reduced[SA_To_2_reduced_folds[[5]],]$time, 
                            training_SA_To_2_reduced[SA_To_2_reduced_folds[[5]],]$status)
Cox_SA_To_2_fld5_reduced = coxph(SA_To_2_obj5_reduced ~ ., 
                                 data=training_SA_To_2_reduced[SA_To_2_reduced_folds[[5]],-c(29,30)])
# summary(Cox_SA_To_2_fld5_reduced)
# Rsquare= 0.549
S0_SA_To_2_fld5_reduced = summary(survfit(Cox_SA_To_2_fld5_reduced, 
                                  newdata=training_SA_To_2_reduced[-SA_To_2_reduced_folds[[5]],-c(29,30)]), 
                                  time=5)$surv
lp_SA_To_2_fld5_reduced = predict(Cox_SA_To_2_fld5_reduced, 
                          newdata = training_SA_To_2_reduced[-SA_To_2_reduced_folds[[5]],-c(29,30)], type = "lp")
St_SA_To_2_fld5_reduced = S0_SA_To_2_fld5_reduced^exp(lp_SA_To_2_fld5_reduced)
Acc_SA_To_2_fld5_reduced = confusionMatrix(factor(training_SA_To_2_reduced[-SA_To_2_reduced_folds[[5]],]$status),
                                           factor(ifelse(St_SA_To_2_fld5_reduced>0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_2_fld5_reduced
# Accuracy : 0.8788

# >>>>>>>>>>>>>>>>>
# 6th fold
set.seed(123)
SA_To_2_obj6_reduced = Surv(training_SA_To_2_reduced[SA_To_2_reduced_folds[[6]],]$time, 
                            training_SA_To_2_reduced[SA_To_2_reduced_folds[[6]],]$status)
Cox_SA_To_2_fld6_reduced = coxph(SA_To_2_obj6_reduced ~ ., 
                                 data=training_SA_To_2_reduced[SA_To_2_reduced_folds[[6]],-c(29,30)])
# summary(Cox_SA_To_2_fld6_reduced)
# Rsquare= 0.561
S0_SA_To_2_fld6_reduced = summary(survfit(Cox_SA_To_2_fld6_reduced, 
                                          newdata=training_SA_To_2_reduced[-SA_To_2_reduced_folds[[6]],-c(29,30)]), 
                                  time=5)$surv
lp_SA_To_2_fld6_reduced = predict(Cox_SA_To_2_fld6_reduced, 
                                  newdata = training_SA_To_2_reduced[-SA_To_2_reduced_folds[[6]],-c(29,30)], type = "lp")
St_SA_To_2_fld6_reduced = S0_SA_To_2_fld6_reduced^exp(lp_SA_To_2_fld6_reduced)
Acc_SA_To_2_fld6_reduced = confusionMatrix(factor(training_SA_To_2_reduced[-SA_To_2_reduced_folds[[6]],]$status),
                                           factor(ifelse(St_SA_To_2_fld6_reduced>0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_2_fld6_reduced
# Accuracy : 0.8657

# >>>>>>>>>>>>>>>>>
# 7th fold
set.seed(123)
SA_To_2_obj7_reduced = Surv(training_SA_To_2_reduced[SA_To_2_reduced_folds[[7]],]$time, 
                            training_SA_To_2_reduced[SA_To_2_reduced_folds[[7]],]$status)
Cox_SA_To_2_fld7_reduced = coxph(SA_To_2_obj7_reduced ~ ., 
                                 data=training_SA_To_2_reduced[SA_To_2_reduced_folds[[7]],-c(29,30)])
# summary(Cox_SA_To_2_fld7_reduced)
# Rsquare= 0.562
S0_SA_To_2_fld7_reduced = summary(survfit(Cox_SA_To_2_fld7_reduced, 
                                          newdata=training_SA_To_2_reduced[-SA_To_2_reduced_folds[[7]],-c(29,30)]), time=5)$surv
lp_SA_To_2_fld7_reduced = predict(Cox_SA_To_2_fld7_reduced, 
                                  newdata = training_SA_To_2_reduced[-SA_To_2_reduced_folds[[7]],-c(29,30)], type = "lp")
St_SA_To_2_fld7_reduced = S0_SA_To_2_fld7_reduced^exp(lp_SA_To_2_fld7_reduced)
Acc_SA_To_2_fld7_reduced = confusionMatrix(factor(training_SA_To_2_reduced[-SA_To_2_reduced_folds[[7]],]$status),
                                           factor(ifelse(St_SA_To_2_fld7_reduced>0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_2_fld7_reduced
# Accuracy : 0.9851

# >>>>>>>>>>>>>>>>>
# 8th fold
set.seed(123)
SA_To_2_obj8_reduced = Surv(training_SA_To_2_reduced[SA_To_2_reduced_folds[[8]],]$time, 
                            training_SA_To_2_reduced[SA_To_2_reduced_folds[[8]],]$status)
Cox_SA_To_2_fld8_reduced = coxph(SA_To_2_obj8_reduced ~ ., 
                                 data=training_SA_To_2_reduced[SA_To_2_reduced_folds[[8]],-c(29,30)])
# summary(Cox_SA_To_2_fld8_reduced)
# Rsquare= 0.554
S0_SA_To_2_fld8_reduced = summary(survfit(Cox_SA_To_2_fld8_reduced, 
                                          newdata=training_SA_To_2_reduced[-SA_To_2_reduced_folds[[8]],-c(29,30)]), 
                                  time=5)$surv
lp_SA_To_2_fld8_reduced = predict(Cox_SA_To_2_fld8_reduced, 
                                  newdata = training_SA_To_2_reduced[-SA_To_2_reduced_folds[[8]],-c(29,30)], type = "lp")
St_SA_To_2_fld8_reduced = S0_SA_To_2_fld8_reduced^exp(lp_SA_To_2_fld8_reduced)
Acc_SA_To_2_fld8_reduced = confusionMatrix(factor(training_SA_To_2_reduced[-SA_To_2_reduced_folds[[8]],]$status),
                                           factor(ifelse(St_SA_To_2_fld8_reduced>0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_2_fld8_reduced
# Accuracy : 0.8657

# >>>>>>>>>>>>>>>>>
# 9th fold
set.seed(123)
SA_To_2_obj9_reduced = Surv(training_SA_To_2_reduced[SA_To_2_reduced_folds[[9]],]$time, 
                            training_SA_To_2_reduced[SA_To_2_reduced_folds[[9]],]$status)
Cox_SA_To_2_fld9_reduced = coxph(SA_To_2_obj9_reduced ~ ., 
                                 data=training_SA_To_2_reduced[SA_To_2_reduced_folds[[9]],-c(29,30)])
# summary(Cox_SA_To_2_fld9_reduced)
# Rsquare= 0.571
S0_SA_To_2_fld9_reduced = summary(survfit(Cox_SA_To_2_fld9_reduced, 
                                          newdata=training_SA_To_2_reduced[-SA_To_2_reduced_folds[[9]],-c(29,30)]), 
                                  time=5)$surv
lp_SA_To_2_fld9_reduced = predict(Cox_SA_To_2_fld9_reduced, 
                                  newdata = training_SA_To_2_reduced[-SA_To_2_reduced_folds[[9]],-c(29,30)], type = "lp")
St_SA_To_2_fld9_reduced = S0_SA_To_2_fld9_reduced^exp(lp_SA_To_2_fld9_reduced)
Acc_SA_To_2_fld9_reduced = confusionMatrix(factor(training_SA_To_2_reduced[-SA_To_2_reduced_folds[[9]],]$status),
                                           factor(ifelse(St_SA_To_2_fld9_reduced>0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_2_fld9_reduced
# Accuracy : 0.8485

# >>>>>>>>>>>>>>>>>
# 10th fold
set.seed(123)
SA_To_2_obj10_reduced = Surv(training_SA_To_2_reduced[SA_To_2_reduced_folds[[10]],]$time, 
                             training_SA_To_2_reduced[SA_To_2_reduced_folds[[10]],]$status)
Cox_SA_To_2_fld10_reduced = coxph(SA_To_2_obj10_reduced ~ ., 
                                  data=training_SA_To_2_reduced[SA_To_2_reduced_folds[[10]],-c(29,30)])
# summary(Cox_SA_To_2_fld10_reduced)
# Rsquare= 0.564
S0_SA_To_2_fld10_reduced = summary(survfit(Cox_SA_To_2_fld10_reduced, 
                                           newdata=training_SA_To_2_reduced[-SA_To_2_reduced_folds[[10]],-c(29,30)]), 
                                   time=5)$surv
lp_SA_To_2_fld10_reduced = predict(Cox_SA_To_2_fld10_reduced, 
                                   newdata = training_SA_To_2_reduced[-SA_To_2_reduced_folds[[10]],-c(29,30)], type = "lp")
St_SA_To_2_fld10_reduced = S0_SA_To_2_fld10_reduced^exp(lp_SA_To_2_fld10_reduced)
Acc_SA_To_2_fld10_reduced = confusionMatrix(factor(training_SA_To_2_reduced[-SA_To_2_reduced_folds[[10]],]$status),
                                            factor(ifelse(St_SA_To_2_fld10_reduced>0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_2_fld10_reduced
# Accuracy : 0.8182

# >>>>>>>>>>>>>>>>>
# Mean Accuracy
Acc_SA_To_2_reduced = c(Acc_SA_To_2_fld1_reduced[[1]], Acc_SA_To_2_fld2_reduced[[1]], Acc_SA_To_2_fld3_reduced[[1]], 
                        Acc_SA_To_2_fld4_reduced[[1]], Acc_SA_To_2_fld5_reduced[[1]], Acc_SA_To_2_fld6_reduced[[1]],
                        Acc_SA_To_2_fld7_reduced[[1]], Acc_SA_To_2_fld8_reduced[[1]], Acc_SA_To_2_fld9_reduced[[1]], 
                        Acc_SA_To_2_fld10_reduced[[1]])
mean_Acc_SA_To_2_reduced <- mean(Acc_SA_To_2_reduced)
# Accuracy : 0.8858

toc()

########################################################################################
# Estimate LUCC on test set
########################################################################################
# find out which training models returns the highest accuracy
which(Acc_SA_To_2_reduced==max(Acc_SA_To_2_reduced)) # 7

set.seed(123)
pred_S0_SA_To_2_reduced = summary(survfit(Cox_SA_To_2_fld7_reduced, 
                                          newdata=testing_SA_To_2_reduced[- which(testing_SA_To_2_reduced$lc2006==6),-c(29,30)]), 
                                  time=5)$surv
pred_lp_SA_To_2_reduced = predict(Cox_SA_To_2_fld7_reduced, 
                                  newdata = testing_SA_To_2_reduced[-which(testing_SA_To_2_reduced$lc2006==6),-c(29,30)], type = "lp")
pred_St_SA_To_2_reduced = pred_S0_SA_To_2_reduced^exp(pred_lp_SA_To_2_reduced)
pred_Acc_SA_To_2_reduced = confusionMatrix(factor(testing_SA_To_2_reduced$status[-which(testing_SA_To_2_reduced$lc2006==6)]),
                                           factor(ifelse(pred_St_SA_To_2_reduced>0.5, 0 , 1)))$overall["Accuracy"]
pred_Acc_SA_To_2_reduced
# Accuracy : 0.8815

########################################################################################
# Final Model
########################################################################################
# find out significant predictors
summary(Cox_SA_To_2_fld7_reduced)
# River_dist+lu4_dist+lu2006+lc2010+DA_Popn_Density 

# construct final model
set.seed(123)
SA_To_2_final.obj_reduced = Surv(training_SA_To_2_reduced$time, training_SA_To_2_reduced$status)
Cox_SA_To_2_final_reduced = coxph(SA_To_2_final.obj_reduced ~ River_dist+lu4_dist+lu2006+lc2010+DA_Popn_Density, 
                                  data=training_SA_To_2_reduced[,-c(29,30)])
# summary(Cox_SA_To_2_final_reduced)
# Rsquare= 0.537
S0_SA_To_2_final_reduced = summary(survfit(Cox_SA_To_2_final_reduced, 
                                           newdata=testing_SA_To_2_reduced[,-c(29,30)]), time=5)$surv
lp_SA_To_2_final_reduced = predict(Cox_SA_To_2_final_reduced, 
                                   newdata = testing_SA_To_2_reduced[,-c(29,30)], type = "lp")
St_SA_To_2_final_reduced = S0_SA_To_2_final_reduced^exp(lp_SA_To_2_final_reduced)
Acc_SA_To_2_final_reduced = confusionMatrix(factor(testing_SA_To_2_reduced$status),
                                            factor(ifelse(St_SA_To_2_final_reduced>0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_2_final_reduced
# Accuracy : 0.875

########################################################################################
# Checking Final Model with CV
########################################################################################
# 1st fold
Cox2_SA_To_2_fld1_reduced = coxph(SA_To_2_obj1_reduced ~ River_dist+lu4_dist+lu2006+lc2010+DA_Popn_Density, 
                                  data=training_SA_To_2_reduced[SA_To_2_reduced_folds[[1]],-c(29,30)])
# summary(Cox2_SA_To_2_fld1_reduced)
# Rsquare= 0.535
S0.2_SA_To_2_fld1_reduced = summary(survfit(Cox2_SA_To_2_fld1_reduced, 
                                            newdata=training_SA_To_2_reduced[-SA_To_2_reduced_folds[[1]],-c(29,30)]), time=5)$surv
lp2_SA_To_2_fld1_reduced = predict(Cox2_SA_To_2_fld1_reduced, 
                                   newdata = training_SA_To_2_reduced[-SA_To_2_reduced_folds[[1]],-c(29,30)], type = "lp")
St2_SA_To_2_fld1_reduced = S0.2_SA_To_2_fld1_reduced^exp(lp2_SA_To_2_fld1_reduced)
Acc2_SA_To_2_fld1_reduced = confusionMatrix(factor(training_SA_To_2_reduced[-SA_To_2_reduced_folds[[1]],]$status),
                                            factor(ifelse(St2_SA_To_2_fld1_reduced>0.5, 0 , 1)))$overall["Accuracy"]
Acc2_SA_To_2_fld1_reduced
# Accuracy : 0.8955

# >>>>>>>>>>>>>>>>>
Cox2_SA_To_2_fld2_reduced = coxph(SA_To_2_obj2_reduced ~ River_dist+lu4_dist+lu2006+lc2010+DA_Popn_Density, 
                                  data=training_SA_To_2_reduced[SA_To_2_reduced_folds[[2]],-c(29,30)])
# summary(Cox2_SA_To_2_fld2_reduced)
# Rsquare= 0.531
S0.2_SA_To_2_fld2_reduced = summary(survfit(Cox2_SA_To_2_fld2_reduced, 
                                            newdata=training_SA_To_2_reduced[-SA_To_2_reduced_folds[[2]],-c(29,30)]), time=5)$surv
lp2_SA_To_2_fld2_reduced = predict(Cox2_SA_To_2_fld2_reduced, 
                                   newdata = training_SA_To_2_reduced[-SA_To_2_reduced_folds[[2]],-c(29,30)], type = "lp")
St2_SA_To_2_fld2_reduced = S0.2_SA_To_2_fld2_reduced^exp(lp2_SA_To_2_fld2_reduced)
Acc2_SA_To_2_fld2_reduced = confusionMatrix(factor(training_SA_To_2_reduced[-SA_To_2_reduced_folds[[2]],]$status),
                                            factor(ifelse(St2_SA_To_2_fld2_reduced>0.5, 0 , 1)))$overall["Accuracy"]
Acc2_SA_To_2_fld2_reduced
# Accuracy : 0.9242

# >>>>>>>>>>>>>>>>>
# 3rd fold
Cox2_SA_To_2_fld3_reduced = coxph(SA_To_2_obj3_reduced ~ River_dist+lu4_dist+lu2006+lc2010+DA_Popn_Density, 
                                  data=training_SA_To_2_reduced[SA_To_2_reduced_folds[[3]],-c(29,30)])
# summary(Cox2_SA_To_2_fld3_reduced)
# Rsquare= 0.54
S0.2_SA_To_2_fld3_reduced = summary(survfit(Cox2_SA_To_2_fld3_reduced, 
                                            newdata=training_SA_To_2_reduced[-SA_To_2_reduced_folds[[3]],-c(29,30)]), time=5)$surv
lp2_SA_To_2_fld3_reduced = predict(Cox2_SA_To_2_fld3_reduced, 
                                   newdata = training_SA_To_2_reduced[-SA_To_2_reduced_folds[[3]],-c(29,30)], type = "lp")
St2_SA_To_2_fld3_reduced = S0.2_SA_To_2_fld3_reduced^exp(lp2_SA_To_2_fld3_reduced)
Acc2_SA_To_2_fld3_reduced = confusionMatrix(factor(training_SA_To_2_reduced[-SA_To_2_reduced_folds[[3]],]$status),
                                            factor(ifelse(St2_SA_To_2_fld3_reduced>0.5, 0 , 1)))$overall["Accuracy"]
Acc2_SA_To_2_fld3_reduced
# Accuracy : 0.8358

# >>>>>>>>>>>>>>>>>
# 4th fold
Cox2_SA_To_2_fld4_reduced = coxph(SA_To_2_obj4_reduced ~ River_dist+lu4_dist+lu2006+lc2010+DA_Popn_Density, 
                                  data=training_SA_To_2_reduced[SA_To_2_reduced_folds[[4]],-c(29,30)])
# summary(Cox2_SA_To_2_fld4_reduced)
# Rsquare= 0.538
S0.2_SA_To_2_fld4_reduced = summary(survfit(Cox2_SA_To_2_fld4_reduced, 
                                            newdata=training_SA_To_2_reduced[-SA_To_2_reduced_folds[[4]],-c(29,30)]), time=5)$surv
lp2_SA_To_2_fld4_reduced = predict(Cox2_SA_To_2_fld4_reduced, 
                                   newdata = training_SA_To_2_reduced[-SA_To_2_reduced_folds[[4]],-c(29,30)], type = "lp")
St2_SA_To_2_fld4_reduced = S0.2_SA_To_2_fld4_reduced^exp(lp2_SA_To_2_fld4_reduced)
Acc2_SA_To_2_fld4_reduced = confusionMatrix(factor(training_SA_To_2_reduced[-SA_To_2_reduced_folds[[4]],]$status),
                                            factor(ifelse(St2_SA_To_2_fld4_reduced>0.5, 0 , 1)))$overall["Accuracy"]
Acc2_SA_To_2_fld4_reduced
# Accuracy : 0.9403

# >>>>>>>>>>>>>>>>>
# 5th fold
Cox2_SA_To_2_fld5_reduced = coxph(SA_To_2_obj5_reduced ~ River_dist+lu4_dist+lu2006+lc2010+DA_Popn_Density, 
                                  data=training_SA_To_2_reduced[SA_To_2_reduced_folds[[5]],-c(29,30)])
# summary(Cox2_SA_To_2_fld5_reduced)
# Rsquare= 0.531
S0.2_SA_To_2_fld5_reduced = summary(survfit(Cox2_SA_To_2_fld5_reduced, 
                                            newdata=training_SA_To_2_reduced[-SA_To_2_reduced_folds[[5]],-c(29,30)]), time=5)$surv
lp2_SA_To_2_fld5_reduced = predict(Cox2_SA_To_2_fld5_reduced, 
                                   newdata = training_SA_To_2_reduced[-SA_To_2_reduced_folds[[5]],-c(29,30)], type = "lp")
St2_SA_To_2_fld5_reduced = S0.2_SA_To_2_fld5_reduced^exp(lp2_SA_To_2_fld5_reduced)
Acc2_SA_To_2_fld5_reduced = confusionMatrix(factor(training_SA_To_2_reduced[-SA_To_2_reduced_folds[[5]],]$status),
                                            factor(ifelse(St2_SA_To_2_fld5_reduced>0.5, 0 , 1)))$overall["Accuracy"]
Acc2_SA_To_2_fld5_reduced
# Accuracy : 0.9091

# >>>>>>>>>>>>>>>>>
# 6th fold
Cox2_SA_To_2_fld6_reduced = coxph(SA_To_2_obj6_reduced ~ River_dist+lu4_dist+lu2006+lc2010+DA_Popn_Density, 
                                  data=training_SA_To_2_reduced[SA_To_2_reduced_folds[[6]],-c(29,30)])
# summary(Cox2_SA_To_2_fld6_reduced)
# Rsquare= 0.539
S0.2_SA_To_2_fld6_reduced = summary(survfit(Cox2_SA_To_2_fld6_reduced, 
                                            newdata=training_SA_To_2_reduced[-SA_To_2_reduced_folds[[6]],-c(29,30)]), time=5)$surv
lp2_SA_To_2_fld6_reduced = predict(Cox2_SA_To_2_fld6_reduced, 
                                   newdata = training_SA_To_2_reduced[-SA_To_2_reduced_folds[[6]],-c(29,30)], type = "lp")
St2_SA_To_2_fld6_reduced = S0.2_SA_To_2_fld6_reduced^exp(lp2_SA_To_2_fld6_reduced)
Acc2_SA_To_2_fld6_reduced = confusionMatrix(factor(training_SA_To_2_reduced[-SA_To_2_reduced_folds[[6]],]$status),
                                            factor(ifelse(St2_SA_To_2_fld6_reduced>0.5, 0 , 1)))$overall["Accuracy"]
Acc2_SA_To_2_fld6_reduced
# Accuracy : 0.8657

# >>>>>>>>>>>>>>>>>
# 7th fold
Cox2_SA_To_2_fld7_reduced = coxph(SA_To_2_obj7_reduced ~ River_dist+lu4_dist+lu2006+lc2010+DA_Popn_Density, 
                                  data=training_SA_To_2_reduced[SA_To_2_reduced_folds[[7]],-c(29,30)])
# summary(Cox2_SA_To_2_fld7_reduced)
# Rsquare= 0.546
S0.2_SA_To_2_fld7_reduced = summary(survfit(Cox2_SA_To_2_fld7_reduced, 
                                            newdata=training_SA_To_2_reduced[-SA_To_2_reduced_folds[[7]],-c(29,30)]), time=5)$surv
lp2_SA_To_2_fld7_reduced = predict(Cox2_SA_To_2_fld7_reduced, 
                                   newdata = training_SA_To_2_reduced[-SA_To_2_reduced_folds[[7]],-c(29,30)], type = "lp")
St2_SA_To_2_fld7_reduced = S0.2_SA_To_2_fld7_reduced^exp(lp2_SA_To_2_fld7_reduced)
Acc2_SA_To_2_fld7_reduced = confusionMatrix(factor(training_SA_To_2_reduced[-SA_To_2_reduced_folds[[7]],]$status),
                                            factor(ifelse(St2_SA_To_2_fld7_reduced>0.5, 0 , 1)))$overall["Accuracy"]
Acc2_SA_To_2_fld7_reduced
# Accuracy : 0.9701

# >>>>>>>>>>>>>>>>>
# 8th fold
Cox2_SA_To_2_fld8_reduced = coxph(SA_To_2_obj8_reduced ~ River_dist+lu4_dist+lu2006+lc2010+DA_Popn_Density, 
                                  data=training_SA_To_2_reduced[SA_To_2_reduced_folds[[8]],-c(29,30)])
# summary(Cox2_SA_To_2_fld8_reduced)
# Rsquare= 0.531
S0.2_SA_To_2_fld8_reduced = summary(survfit(Cox2_SA_To_2_fld8_reduced, 
                                            newdata=training_SA_To_2_reduced[-SA_To_2_reduced_folds[[8]],-c(29,30)]), time=5)$surv
lp2_SA_To_2_fld8_reduced = predict(Cox2_SA_To_2_fld8_reduced, 
                                   newdata = training_SA_To_2_reduced[-SA_To_2_reduced_folds[[8]],-c(29,30)], type = "lp")
St2_SA_To_2_fld8_reduced = S0.2_SA_To_2_fld8_reduced^exp(lp2_SA_To_2_fld8_reduced)
Acc2_SA_To_2_fld8_reduced = confusionMatrix(factor(training_SA_To_2_reduced[-SA_To_2_reduced_folds[[8]],]$status),
                                            factor(ifelse(St2_SA_To_2_fld8_reduced>0.5, 0 , 1)))$overall["Accuracy"]
Acc2_SA_To_2_fld8_reduced
# Accuracy : 0.9104

# >>>>>>>>>>>>>>>>>
# 9th fold
Cox2_SA_To_2_fld9_reduced = coxph(SA_To_2_obj9_reduced ~ River_dist+lu4_dist+lu2006+lc2010+DA_Popn_Density, 
                                  data=training_SA_To_2_reduced[SA_To_2_reduced_folds[[9]],-c(29,30)])
# summary(Cox2_SA_To_2_fld9_reduced)
# Rsquare= 0.547
S0.2_SA_To_2_fld9_reduced = summary(survfit(Cox2_SA_To_2_fld9_reduced, 
                                            newdata=training_SA_To_2_reduced[-SA_To_2_reduced_folds[[9]],-c(29,30)]), time=5)$surv
lp2_SA_To_2_fld9_reduced = predict(Cox2_SA_To_2_fld9_reduced, 
                                   newdata = training_SA_To_2_reduced[-SA_To_2_reduced_folds[[9]],-c(29,30)], type = "lp")
St2_SA_To_2_fld9_reduced = S0.2_SA_To_2_fld9_reduced^exp(lp2_SA_To_2_fld9_reduced)
Acc2_SA_To_2_fld9_reduced = confusionMatrix(factor(training_SA_To_2_reduced[-SA_To_2_reduced_folds[[9]],]$status),
                                            factor(ifelse(St2_SA_To_2_fld9_reduced>0.5, 0 , 1)))$overall["Accuracy"]
Acc2_SA_To_2_fld9_reduced
# Accuracy : 0.8333

# >>>>>>>>>>>>>>>>>
# 10th fold
Cox2_SA_To_2_fld10_reduced = coxph(SA_To_2_obj10_reduced ~ River_dist+lu4_dist+lu2006+lc2010+DA_Popn_Density, 
                                   data=training_SA_To_2_reduced[SA_To_2_reduced_folds[[10]],-c(29,30)])
# summary(Cox2_SA_To_2_fld10_reduced)
# Rsquare= 0.547
S0.2_SA_To_2_fld10_reduced = summary(survfit(Cox2_SA_To_2_fld10_reduced, 
                                             newdata=training_SA_To_2_reduced[-SA_To_2_reduced_folds[[10]],-c(29,30)]), time=5)$surv
lp2_SA_To_2_fld10_reduced = predict(Cox2_SA_To_2_fld10_reduced, 
                                    newdata = training_SA_To_2_reduced[-SA_To_2_reduced_folds[[10]],-c(29,30)], type = "lp")
St2_SA_To_2_fld10_reduced = S0.2_SA_To_2_fld10_reduced^exp(lp2_SA_To_2_fld10_reduced)
Acc2_SA_To_2_fld10_reduced = confusionMatrix(factor(training_SA_To_2_reduced[-SA_To_2_reduced_folds[[10]],]$status),
                                             factor(ifelse(St2_SA_To_2_fld10_reduced>0.5, 0 , 1)))$overall["Accuracy"]
Acc2_SA_To_2_fld10_reduced
# Accuracy : 0.8333

# >>>>>>>>>>>>>>>>>
# Mean Accuracy
Acc2_SA_To_2_reduced = c(Acc2_SA_To_2_fld1_reduced[[1]], Acc2_SA_To_2_fld2_reduced[[1]], Acc2_SA_To_2_fld3_reduced[[1]], 
                         Acc2_SA_To_2_fld4_reduced[[1]], Acc2_SA_To_2_fld5_reduced[[1]], Acc2_SA_To_2_fld6_reduced[[1]],
                         Acc2_SA_To_2_fld7_reduced[[1]], Acc2_SA_To_2_fld8_reduced[[1]], Acc2_SA_To_2_fld9_reduced[[1]], 
                         Acc2_SA_To_2_fld10_reduced[[1]])
mean_Acc2_SA_To_2_reduced <- mean(Acc2_SA_To_2_reduced)
# Accuracy : 0.8918

########################################################################################
# Final Model
########################################################################################
# find out which training models returns the highest accuracy
which(Acc2_SA_To_2_reduced==max(Acc2_SA_To_2_reduced)) # 7
# find out significant predictors
summary(Cox_SA_To_2_fld7)
# MeanDEM+Water_dist+Wood_dist+LRoad_dist+MRoad_dist+lu4_dist+lu8_dist
# +DA_Popn_Density+Residential_Popn_Density+Change_Popn+Change_AveIncome
# +F_lu1+F_lu11++lu2006+lc2006+lc2010

# construct final model
set.seed(123)
SA_To_2_reduced_final.obj = Surv(training_SA_To_2_reduced[SA_To_2_reduced_folds[[7]],]$time, training_SA_To_2_reduced[SA_To_2_reduced_folds[[7]],]$status)
Cox_SA_To_2_reduced_final = coxph(SA_To_2_reduced_final.obj ~MeanDEM+Water_dist+Wood_dist+LRoad_dist+MRoad_dist+lu4_dist+lu8_dist
                                  +DA_Popn_Density+Residential_Popn_Density+Change_Popn+Change_AveIncome
                                  +F_lu1+F_lu11+lu2006+lc2006+lc2010, 
                                  data=training_SA_To_2_reduced[SA_To_2_reduced_folds[[7]],-c(29,30)])
summary(Cox_SA_To_2_reduced_final)
lp_SA_To_2_reduced_final = predict(Cox_SA_To_2_reduced_final, 
                                   newdata = testing_SA_To_2_reduced[-which(testing_SA_To_2_reduced$lc2006==6),-c(29,30)], type = "lp")
S0_SA_To_2_reduced_final = summary(survfit(Cox_SA_To_2_reduced_final, 
                                   newdata=testing_SA_To_2_reduced[-which(testing_SA_To_2_reduced$lc2006==6),-c(29,30)]), time=5)$surv
St_SA_To_2_reduced_final = S0_SA_To_2_reduced_final^exp(lp_SA_To_2_reduced_final)
Acc_SA_To_2_reduced_final = confusionMatrix(factor(testing_SA_To_2_reduced[-which(testing_SA_To_2_reduced$lc2006==6),]$status),
                                            factor(ifelse(St_SA_To_2_reduced_final>0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_2_reduced_final
# Accuracy : 0.8989547