# Remove the index X1, F_lu3, F_lu4, F_lu5, F_lu6, F_lu7, F_lu9 and F_lu11.
# 
training_SA_To_10_full = training_SA_To_10_full[,-c(1,25:29,31,33)]
testing_SA_To_10_full = testing_SA_To_10_full[,-c(1,25:29,31,33)]

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

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

dim(training_SA_To_10_full) # 50 27
dim(testing_SA_To_10_full) # 22 27

########################################################################################
# Detec significant predictors
########################################################################################
set.seed(123)
SA_To_10_surv = Surv(training_SA_To_10_full$time, 
                     training_SA_To_10_full$status)
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# Geographical predictors
Cox_SA_To_10_surv1 = coxph(SA_To_10_surv ~ DA_Area+ParcelArea+MeanDEM+MeanSlope, 
                           data=training_SA_To_10_full[,-c(26,27)])
summary(Cox_SA_To_10_surv1) # Rsquare= 0.163
# Significant predictors: DA_Area
S0_SA_To_10_surv1 = summary(survfit(Cox_SA_To_10_surv1, 
                                  newdata=testing_SA_To_10_full[,-c(26,27)]), time=5)$surv
lp_SA_To_10_surv1 = predict(Cox_SA_To_10_surv1, 
                            newdata = testing_SA_To_10_full[,-c(26,27)], type = "lp")
St_SA_To_10_surv1 = S0_SA_To_10_surv1^exp(lp_SA_To_10_surv1)
Acc_SA_To_10_surv1 = confusionMatrix(factor(testing_SA_To_10_full$status),
                                     factor(ifelse(St_SA_To_10_surv1>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_10_surv1 # 0.4545

# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# Distance predictors
Cox_SA_To_10_surv2 = coxph(SA_To_10_surv ~ Ramp_dist+River_dist+Water_dist+Wood_dist+LRoad_dist+MRoad_dist
                           +lu4_dist+lu5_dist+lu8_dist+lu9_dist, 
                           data=training_SA_To_10_full[,-c(26,27)])
summary(Cox_SA_To_10_surv2) # Rsquare= 0.624
# Significant predictors: Water_dist, lu5_dist
S0_SA_To_10_surv2 = summary(survfit(Cox_SA_To_10_surv2, 
                                    newdata=testing_SA_To_10_full[,-c(26,27)]), time=5)$surv
lp_SA_To_10_surv2 = predict(Cox_SA_To_10_surv2, 
                            newdata = testing_SA_To_10_full[,-c(26,27)], type = "lp")
St_SA_To_10_surv2 = S0_SA_To_10_surv2^exp(lp_SA_To_10_surv2)
Acc_SA_To_10_surv2 = confusionMatrix(factor(testing_SA_To_10_full$status),
                                     factor(ifelse(St_SA_To_10_surv2>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_10_surv2 # 1

# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# LU and LC predictors
Cox_SA_To_10_surv3 = coxph(SA_To_10_surv ~ lu2006+lc2006+lc2010, 
                           data=training_SA_To_10_full[,-c(26,27)])
summary(Cox_SA_To_10_surv3) # Rsquare= 0.562
# Significant predictors: lc2006
S0_SA_To_10_surv3 = summary(survfit(Cox_SA_To_10_surv3,                                     
                                    newdata=testing_SA_To_10_full[-which(testing_SA_To_10_full$lu2006==2 |
                                                                         testing_SA_To_10_full$lu2006==3 |
                                                                         testing_SA_To_10_full$lc2006==3),
                                                                  -c(26,27)]), time=5)$surv
lp_SA_To_10_surv3 = predict(Cox_SA_To_10_surv3, 
                            newdata = testing_SA_To_10_full[-which(testing_SA_To_10_full$lu2006==2 |
                                                                     testing_SA_To_10_full$lu2006==3 |
                                                                     testing_SA_To_10_full$lc2006==3),
                                                            -c(26,27)], type = "lp")
St_SA_To_10_surv3 = S0_SA_To_10_surv3^exp(lp_SA_To_10_surv3)
Acc_SA_To_10_surv3 = confusionMatrix(factor(testing_SA_To_10_full$status[-which(testing_SA_To_10_full$lu2006==2 |
                                                                                testing_SA_To_10_full$lu2006==3 |
                                                                                testing_SA_To_10_full$lc2006==3)]),
                                     factor(ifelse(St_SA_To_10_surv3>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_10_surv3 # 0.6842

# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# Demographical predictors
Cox_SA_To_10_surv4 = coxph(SA_To_10_surv ~ DA_Popn_Density+Residential_Popn_Density+Change_Popn+Change_AveIncome, 
                           data=training_SA_To_10_full[,-c(26,27)])
summary(Cox_SA_To_10_surv4) # Rsquare= 0.67
# Significant predictors: Residential_Popn_Density, Change_Popn
S0_SA_To_10_surv4 = summary(survfit(Cox_SA_To_10_surv4,                                     
                                    newdata=testing_SA_To_10_full[,-c(26,27)]), time=5)$surv
lp_SA_To_10_surv4 = predict(Cox_SA_To_10_surv4, 
                            newdata = testing_SA_To_10_full[,-c(26,27)], type = "lp")
St_SA_To_10_surv4 = S0_SA_To_10_surv4^exp(lp_SA_To_10_surv4)
Acc_SA_To_10_surv4 = confusionMatrix(factor(testing_SA_To_10_full$status),
                                     factor(ifelse(St_SA_To_10_surv4>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_10_surv4 # 0.6364

# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# Spatial predictors
Cox_SA_To_10_surv5 = coxph(SA_To_10_surv ~ F_lu1+F_lu2+F_lu8+F_lu10, 
                           data=training_SA_To_10_full[,-c(26,27)])
summary(Cox_SA_To_10_surv5) # Rsquare= 0.67
# Significant predictors: None
S0_SA_To_10_surv5 = summary(survfit(Cox_SA_To_10_surv5,                                     
                                    newdata=testing_SA_To_10_full[,-c(26,27)]), time=5)$surv
lp_SA_To_10_surv5 = predict(Cox_SA_To_10_surv5, 
                            newdata = testing_SA_To_10_full[,-c(26,27)], type = "lp")
St_SA_To_10_surv5 = S0_SA_To_10_surv5^exp(lp_SA_To_10_surv5)
Acc_SA_To_10_surv5 = confusionMatrix(factor(testing_SA_To_10_full$status),
                                     factor(ifelse(St_SA_To_10_surv5>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_10_surv5 # 0.5909

# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# combine all the significant predictors: DA_Area+Water_dist+lu5_dist+lc2006+Residential_Popn_Density+Change_Popn
Cox_SA_To_10_surv6 = coxph(SA_To_10_surv ~ DA_Area+Water_dist+lu5_dist+lc2006+Residential_Popn_Density+Change_Popn, 
                           data=training_SA_To_10_full[,-c(26,27)])
summary(Cox_SA_To_10_surv6) # Rsquare= 0.645
S0_SA_To_10_surv6 = summary(survfit(Cox_SA_To_10_surv6,                                     
                                    newdata=testing_SA_To_10_full[-which(testing_SA_To_10_full$lc2006==3),
                                                                  -c(26,27)]), time=5)$surv
lp_SA_To_10_surv6 = predict(Cox_SA_To_10_surv6, 
                            newdata = testing_SA_To_10_full[-which(testing_SA_To_10_full$lc2006==3),
                                                            -c(26,27)], type = "lp")
St_SA_To_10_surv6 = S0_SA_To_10_surv6^exp(lp_SA_To_10_surv6)
Acc_SA_To_10_surv6 = confusionMatrix(factor(testing_SA_To_10_full$status[-which(testing_SA_To_10_full$lc2006==3)]),
                                     factor(ifelse(St_SA_To_10_surv6>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_10_surv6 # 0.95

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

tic("model fitting")
# 1st fold
set.seed(123)
SA_To_10_obj1 = Surv(training_SA_To_10_full[SA_To_10_folds[[1]],]$time, 
                     training_SA_To_10_full[SA_To_10_folds[[1]],]$status)
Cox_SA_To_10_fld1 = coxph(SA_To_10_obj1 ~ DA_Area+Water_dist+lu5_dist+lc2006+Residential_Popn_Density+Change_Popn, 
                          data=training_SA_To_10_full[SA_To_10_folds[[1]],-c(26,27)])
# summary(Cox_SA_To_10_fld1)
# Rsquare= 0.701
S0_SA_To_10_fld1 = summary(survfit(Cox_SA_To_10_fld1, 
                                   newdata=training_SA_To_10_full[-SA_To_10_folds[[1]],-c(26,27)]), time=5)$surv
lp_SA_To_10_fld1 = predict(Cox_SA_To_10_fld1, 
                           newdata = training_SA_To_10_full[-SA_To_10_folds[[1]],-c(26,27)], type = "lp")
St_SA_To_10_fld1 = S0_SA_To_10_fld1^exp(lp_SA_To_10_fld1)
Acc_SA_To_10_fld1 = confusionMatrix(factor(training_SA_To_10_full[-SA_To_10_folds[[1]],]$status),
                                    factor(ifelse(St_SA_To_10_fld1>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_10_fld1
# Accuracy : 0.8

# >>>>>>>>>>>>>>>>>
# 2nd fold
set.seed(123)
SA_To_10_obj2 = Surv(training_SA_To_10_full[SA_To_10_folds[[2]],]$time, 
                    training_SA_To_10_full[SA_To_10_folds[[2]],]$status)
Cox_SA_To_10_fld2 = coxph(SA_To_10_obj2 ~ DA_Area+Water_dist+lu5_dist+lc2006+Residential_Popn_Density+Change_Popn, 
                          data=training_SA_To_10_full[SA_To_10_folds[[2]],-c(26,27)])
# summary(Cox_SA_To_10_fld2)
# Rsquare= 0.644
S0_SA_To_10_fld2 = summary(survfit(Cox_SA_To_10_fld2, 
                                   newdata=training_SA_To_10_full[-SA_To_10_folds[[2]],-c(26,27)]), time=5)$surv
lp_SA_To_10_fld2 = predict(Cox_SA_To_10_fld2, 
                           newdata = training_SA_To_10_full[-SA_To_10_folds[[2]],-c(26,27)], type = "lp")
St_SA_To_10_fld2 = S0_SA_To_10_fld2^exp(lp_SA_To_10_fld2)
Acc_SA_To_10_fld2 = confusionMatrix(factor(training_SA_To_10_full[-SA_To_10_folds[[2]],]$status),
                                    factor(ifelse(St_SA_To_10_fld2>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_10_fld2
# Accuracy : 0.6

# >>>>>>>>>>>>>>>>>
# 3rd fold
set.seed(123)
SA_To_10_obj3 = Surv(training_SA_To_10_full[SA_To_10_folds[[3]],]$time, 
                     training_SA_To_10_full[SA_To_10_folds[[3]],]$status)
Cox_SA_To_10_fld3 = coxph(SA_To_10_obj3 ~ DA_Area+Water_dist+lu5_dist+lc2006+Residential_Popn_Density+Change_Popn, 
                          data=training_SA_To_10_full[SA_To_10_folds[[3]],-c(26,27)])
# summary(Cox_SA_To_10_fld3)
# Rsquare= 0.67
S0_SA_To_10_fld3 = summary(survfit(Cox_SA_To_10_fld3, 
                                   newdata=training_SA_To_10_full[-SA_To_10_folds[[3]],-c(26,27)]), time=5)$surv
lp_SA_To_10_fld3 = predict(Cox_SA_To_10_fld3, 
                           newdata = training_SA_To_10_full[-SA_To_10_folds[[3]],-c(26,27)], type = "lp")
St_SA_To_10_fld3 = S0_SA_To_10_fld3^exp(lp_SA_To_10_fld3)
Acc_SA_To_10_fld3 = confusionMatrix(factor(training_SA_To_10_full[-SA_To_10_folds[[3]],]$status),
                                    factor(ifelse(St_SA_To_10_fld3>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_10_fld3
# Accuracy : 1

# >>>>>>>>>>>>>>>>>
# 4th fold
set.seed(123)
SA_To_10_obj4 = Surv(training_SA_To_10_full[SA_To_10_folds[[4]],]$time, 
                     training_SA_To_10_full[SA_To_10_folds[[4]],]$status)
Cox_SA_To_10_fld4 = coxph(SA_To_10_obj4 ~ DA_Area+Water_dist+lu5_dist+lc2006+Residential_Popn_Density+Change_Popn, 
                          data=training_SA_To_10_full[SA_To_10_folds[[4]],-c(26,27)])
# summary(Cox_SA_To_10_fld4)
# Rsquare= 0.632
S0_SA_To_10_fld4 = summary(survfit(Cox_SA_To_10_fld4, 
                                   newdata=training_SA_To_10_full[-SA_To_10_folds[[4]],-c(26,27)]), time=5)$surv
lp_SA_To_10_fld4 = predict(Cox_SA_To_10_fld4, 
                           newdata = training_SA_To_10_full[-SA_To_10_folds[[4]],-c(26,27)], type = "lp")
St_SA_To_10_fld4 = S0_SA_To_10_fld4^exp(lp_SA_To_10_fld4)
Acc_SA_To_10_fld4 = confusionMatrix(training_SA_To_10_full[-SA_To_10_folds[[4]],]$status,
                                    ifelse(St_SA_To_10_fld4>=0.5, 0 , 1))$overall["Accuracy"]
Acc_SA_To_10_fld4 = 3/5
# Accuracy : 0.6

# >>>>>>>>>>>>>>>>>
# 5th fold
set.seed(123)
SA_To_10_obj5 = Surv(training_SA_To_10_full[SA_To_10_folds[[5]],]$time, 
                     training_SA_To_10_full[SA_To_10_folds[[5]],]$status)
Cox_SA_To_10_fld5 = coxph(SA_To_10_obj5 ~ DA_Area+Water_dist+lu5_dist+lc2006+Residential_Popn_Density+Change_Popn, 
                          data=training_SA_To_10_full[SA_To_10_folds[[5]],-c(26,27)])
# summary(Cox_SA_To_10_fld5)
# Rsquare= 0.651
S0_SA_To_10_fld5 = summary(survfit(Cox_SA_To_10_fld5, 
                                   newdata=training_SA_To_10_full[-SA_To_10_folds[[5]],-c(26,27)]), time=5)$surv
lp_SA_To_10_fld5 = predict(Cox_SA_To_10_fld5, 
                           newdata = training_SA_To_10_full[-SA_To_10_folds[[5]],-c(26,27)], type = "lp")
St_SA_To_10_fld5 = S0_SA_To_10_fld5^exp(lp_SA_To_10_fld5)
Acc_SA_To_10_fld5 = confusionMatrix(factor(training_SA_To_10_full[-SA_To_10_folds[[5]],]$status),
                                    factor(ifelse(St_SA_To_10_fld5>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_10_fld5 = 4/5
# Accuracy : 0.8

# >>>>>>>>>>>>>>>>>
# 6th fold
set.seed(123)
SA_To_10_obj6 = Surv(training_SA_To_10_full[SA_To_10_folds[[6]],]$time, 
                     training_SA_To_10_full[SA_To_10_folds[[6]],]$status)
Cox_SA_To_10_fld6 = coxph(SA_To_10_obj6 ~ DA_Area+Water_dist+lu5_dist+lc2006+Residential_Popn_Density+Change_Popn, 
                          data=training_SA_To_10_full[SA_To_10_folds[[6]],-c(26,27)])
# summary(Cox_SA_To_10_fld6)
# Rsquare= 0.677
S0_SA_To_10_fld6 = summary(survfit(Cox_SA_To_10_fld6, 
                                   newdata=training_SA_To_10_full[-SA_To_10_folds[[6]],-c(26,27)]), time=5)$surv
lp_SA_To_10_fld6 = predict(Cox_SA_To_10_fld6, 
                           newdata = training_SA_To_10_full[-SA_To_10_folds[[6]],-c(26,27)], type = "lp")
St_SA_To_10_fld6 = S0_SA_To_10_fld6^exp(lp_SA_To_10_fld6)
Acc_SA_To_10_fld6 = confusionMatrix(factor(training_SA_To_10_full[-SA_To_10_folds[[6]],]$status),
                                    factor(ifelse(St_SA_To_10_fld6>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_10_fld6
# Accuracy : 0.8

# >>>>>>>>>>>>>>>>>
# 7th fold
set.seed(123)
SA_To_10_obj7 = Surv(training_SA_To_10_full[SA_To_10_folds[[7]],]$time, 
                     training_SA_To_10_full[SA_To_10_folds[[7]],]$status)
Cox_SA_To_10_fld7 = coxph(SA_To_10_obj7 ~ DA_Area+Water_dist+lu5_dist+lc2006+Residential_Popn_Density+Change_Popn, 
                          data=training_SA_To_10_full[SA_To_10_folds[[7]],-c(26,27)])
# summary(Cox_SA_To_10_fld7)
# Rsquare= 0.644
S0_SA_To_10_fld7 = summary(survfit(Cox_SA_To_10_fld7, 
                                   newdata=training_SA_To_10_full[-SA_To_10_folds[[7]],-c(26,27)]), time=5)$surv
lp_SA_To_10_fld7 = predict(Cox_SA_To_10_fld7, 
                           newdata = training_SA_To_10_full[-SA_To_10_folds[[7]],-c(26,27)], type = "lp")
St_SA_To_10_fld7 = S0_SA_To_10_fld7^exp(lp_SA_To_10_fld7)
Acc_SA_To_10_fld7 = confusionMatrix(factor(training_SA_To_10_full[-SA_To_10_folds[[7]],]$status),
                                    factor(ifelse(St_SA_To_10_fld7>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_10_fld7
# Accuracy : 1

# >>>>>>>>>>>>>>>>>
# 8th fold
set.seed(123)
SA_To_10_obj8 = Surv(training_SA_To_10_full[SA_To_10_folds[[8]],]$time, 
                     training_SA_To_10_full[SA_To_10_folds[[8]],]$status)
Cox_SA_To_10_fld8 = coxph(SA_To_10_obj8 ~ DA_Area+Water_dist+lu5_dist+lc2006+Residential_Popn_Density+Change_Popn, 
                          data=training_SA_To_10_full[SA_To_10_folds[[8]],-c(26,27)])
# summary(Cox_SA_To_10_fld8)
# Rsquare= 0.669
S0_SA_To_10_fld8 = summary(survfit(Cox_SA_To_10_fld8, 
                                   newdata=training_SA_To_10_full[-SA_To_10_folds[[8]],-c(26,27)]), time=5)$surv
lp_SA_To_10_fld8 = predict(Cox_SA_To_10_fld8, 
                           newdata = training_SA_To_10_full[-SA_To_10_folds[[8]],-c(26,27)], type = "lp")
St_SA_To_10_fld8 = S0_SA_To_10_fld8^exp(lp_SA_To_10_fld8)
Acc_SA_To_10_fld8 = confusionMatrix(factor(training_SA_To_10_full[-SA_To_10_folds[[8]],]$status),
                                    factor(ifelse(St_SA_To_10_fld8>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_10_fld8
# Accuracy : 1

# >>>>>>>>>>>>>>>>>
# 9th fold
set.seed(123)
SA_To_10_obj9 = Surv(training_SA_To_10_full[SA_To_10_folds[[9]],]$time, 
                    training_SA_To_10_full[SA_To_10_folds[[9]],]$status)
Cox_SA_To_10_fld9 = coxph(SA_To_10_obj9 ~ DA_Area+Water_dist+lu5_dist+lc2006+Residential_Popn_Density+Change_Popn, 
                          data=training_SA_To_10_full[SA_To_10_folds[[9]],-c(26,27)])
# summary(Cox_SA_To_10_fld9)
# Rsquare= 0.643
S0_SA_To_10_fld9 = summary(survfit(Cox_SA_To_10_fld9, 
                                   newdata=training_SA_To_10_full[-SA_To_10_folds[[9]],-c(26,27)]), time=5)$surv
lp_SA_To_10_fld9 = predict(Cox_SA_To_10_fld9, 
                           newdata = training_SA_To_10_full[-SA_To_10_folds[[9]],-c(26,27)], type = "lp")
St_SA_To_10_fld9 = S0_SA_To_10_fld9^exp(lp_SA_To_10_fld9)
Acc_SA_To_10_fld9 = confusionMatrix(factor(training_SA_To_10_full[-SA_To_10_folds[[9]],]$status),
                                    factor(ifelse(St_SA_To_10_fld9>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_10_fld9
# Accuracy : 0.8

# >>>>>>>>>>>>>>>>>
# 10th fold
set.seed(123)
SA_To_10_obj10 = Surv(training_SA_To_10_full[SA_To_10_folds[[10]],]$time, 
                      training_SA_To_10_full[SA_To_10_folds[[10]],]$status)
Cox_SA_To_10_fld10 = coxph(SA_To_10_obj10 ~ DA_Area+Water_dist+lu5_dist+lc2006+Residential_Popn_Density+Change_Popn, 
                           data=training_SA_To_10_full[SA_To_10_folds[[10]],-c(26,27)])
# summary(Cox_SA_To_10_fld10)
# Rsquare= 0.634
S0_SA_To_10_fld10 = summary(survfit(Cox_SA_To_10_fld10, 
                                    newdata=training_SA_To_10_full[-SA_To_10_folds[[10]],-c(26,27)]), time=5)$surv
lp_SA_To_10_fld10 = predict(Cox_SA_To_10_fld10, 
                            newdata = training_SA_To_10_full[-SA_To_10_folds[[10]],-c(26,27)], type = "lp")
St_SA_To_10_fld10 = S0_SA_To_10_fld10^exp(lp_SA_To_10_fld10)
Acc_SA_To_10_fld10 = confusionMatrix(factor(training_SA_To_10_full[-SA_To_10_folds[[10]],]$status),
                                     factor(ifelse(St_SA_To_10_fld10>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_10_fld10
# Accuracy : 1

# >>>>>>>>>>>>>>>>>
# Mean Accuracy
Acc_SA_To_10 = c(Acc_SA_To_10_fld1[[1]], Acc_SA_To_10_fld2[[1]], 
                 Acc_SA_To_10_fld3[[1]], Acc_SA_To_10_fld4[[1]], 
                 Acc_SA_To_10_fld5[[1]], Acc_SA_To_10_fld6[[1]],
                 Acc_SA_To_10_fld7[[1]], Acc_SA_To_10_fld8[[1]],
                 Acc_SA_To_10_fld9[[1]], Acc_SA_To_10_fld10[[1]])
(mean_Acc_SA_To_10 <- mean(Acc_SA_To_10))
# Accuracy : 0.84

toc() # 0.79


########################################################################################
# Final Model
########################################################################################
# find out which training models returns the highest accuracy
which(Acc_SA_To_10==max(Acc_SA_To_10)) # 3, 7, 8, 10
# find out significant predictors
summary(Cox_SA_To_10_fld8)
# DA_Area+Water_dist+lu5_dist+lc2006

# construct final model
set.seed(123)
SA_To_10_final.obj = Surv(training_SA_To_10_full[SA_To_10_folds[[8]],]$time, training_SA_To_10_full[SA_To_10_folds[[8]],]$status)
Cox_SA_To_10_final = coxph(SA_To_10_final.obj ~ DA_Area+Water_dist+lu5_dist+lc2006, 
                           data=training_SA_To_10_full[SA_To_10_folds[[8]],-c(26,27)])
# summary(Cox_SA_To_10_final)
# Rsquare= 0.628
S0_SA_To_10_final = summary(survfit(Cox_SA_To_10_final, 
                                    newdata=testing_SA_To_10_full[-which(testing_SA_To_10_full$lc2006==3),
                                                                  -c(26,27)]), time=5)$surv
lp_SA_To_10_final = predict(Cox_SA_To_10_final, 
                            newdata = testing_SA_To_10_full[-which(testing_SA_To_10_full$lc2006==3),
                                                            -c(26,27)], type = "lp")
St_SA_To_10_final = S0_SA_To_10_final^exp(lp_SA_To_10_final)
Acc_SA_To_10_final = confusionMatrix(factor(testing_SA_To_10_full$status[-which(testing_SA_To_10_full$lc2006==3)]),
                                     factor(ifelse(St_SA_To_10_final>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_10_final
# Accuracy : 1

