# Remove first column, F_lu3, F_lu5, F_lu6 and F_lu10. Remove parcel with lu2006=6 and lu2006=10.
training_SA_To_3_full = training_SA_To_3_full[-which(training_SA_To_3_full$lu2006==6 | training_SA_To_3_full$lu2006==10),
                                              -c(1,25,27,28,32)]
testing_SA_To_3_full = testing_SA_To_3_full[-which(testing_SA_To_3_full$lu2006==10),-c(1,25,27,28,32)]

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

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

dim(training_SA_To_3_full) # 1185   30
dim(testing_SA_To_3_full) # 509  30
########################################################################################
# 10-fold Cross-validation
########################################################################################
set.seed(123)
SA_To_3_full_folds <- createFolds(training_SA_To_3_full$status, k = 10, list = TRUE, returnTrain = TRUE)

tic("model fitting")
# 1st fold
set.seed(123)
SA_To_3_obj1 = Surv(training_SA_To_3_full[SA_To_3_full_folds[[1]],]$time, 
                    training_SA_To_3_full[SA_To_3_full_folds[[1]],]$status)
Cox_SA_To_3_fld1 = coxph(SA_To_3_obj1 ~ ., 
                         data=training_SA_To_3_full[SA_To_3_full_folds[[1]],-c(29,30)])
# summary(Cox_SA_To_3_fld1)
# Rsquare= 0.267
S0_SA_To_3_fld1 = summary(survfit(Cox_SA_To_3_fld1, 
                                  newdata=training_SA_To_3_full[-SA_To_3_full_folds[[1]],-c(29,30)]), 
                          time=5)$surv
lp_SA_To_3_fld1 = predict(Cox_SA_To_3_fld1, 
                          newdata = training_SA_To_3_full[-SA_To_3_full_folds[[1]],-c(29,30)], type = "lp")
St_SA_To_3_fld1 = S0_SA_To_3_fld1^exp(lp_SA_To_3_fld1)
Acc_SA_To_3_fld1 = confusionMatrix(factor(training_SA_To_3_full[-SA_To_3_full_folds[[1]],]$status),
                                   factor(ifelse(St_SA_To_3_fld1>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_3_fld1
# Accuracy : 0.7288

# >>>>>>>>>>>>>>>>>
# 2nd fold
set.seed(123)
SA_To_3_obj2 = Surv(training_SA_To_3_full[SA_To_3_full_folds[[2]],]$time, 
                    training_SA_To_3_full[SA_To_3_full_folds[[2]],]$status)
Cox_SA_To_3_fld2 = coxph(SA_To_3_obj2 ~ ., 
                         data=training_SA_To_3_full[SA_To_3_full_folds[[2]],-c(29,30)])
# summary(Cox_SA_To_3_fld2)
# Rsquare= 0.269
S0_SA_To_3_fld2 = summary(survfit(Cox_SA_To_3_fld2, 
                                  newdata=training_SA_To_3_full[-SA_To_3_full_folds[[2]],-c(29,30)]), 
                          time=5)$surv
lp_SA_To_3_fld2 = predict(Cox_SA_To_3_fld2, 
                          newdata = training_SA_To_3_full[-SA_To_3_full_folds[[2]],-c(29,30)], type = "lp")
St_SA_To_3_fld2 = S0_SA_To_3_fld2^exp(lp_SA_To_3_fld2)
Acc_SA_To_3_fld2 = confusionMatrix(factor(training_SA_To_3_full[-SA_To_3_full_folds[[2]],]$status),
                                   factor(ifelse(St_SA_To_3_fld2>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_3_fld2
# Accuracy : 0.7712

# >>>>>>>>>>>>>>>>>
# 3rd fold
set.seed(123)
SA_To_3_obj3 = Surv(training_SA_To_3_full[SA_To_3_full_folds[[3]],]$time, 
                    training_SA_To_3_full[SA_To_3_full_folds[[3]],]$status)
Cox_SA_To_3_fld3 = coxph(SA_To_3_obj3 ~ ., 
                         data=training_SA_To_3_full[SA_To_3_full_folds[[3]],-c(29,30)])
# summary(Cox_SA_To_3_fld3)
# Rsquare= 0.27
S0_SA_To_3_fld3 = summary(survfit(Cox_SA_To_3_fld3, 
                                  newdata=training_SA_To_3_full[-SA_To_3_full_folds[[3]],-c(29,30)]), 
                          time=5)$surv
lp_SA_To_3_fld3 = predict(Cox_SA_To_3_fld3, 
                          newdata = training_SA_To_3_full[-SA_To_3_full_folds[[3]],-c(29,30)], type = "lp")
St_SA_To_3_fld3 = S0_SA_To_3_fld3^exp(lp_SA_To_3_fld3)
Acc_SA_To_3_fld3 = confusionMatrix(factor(training_SA_To_3_full[-SA_To_3_full_folds[[3]],]$status),
                                   factor(ifelse(St_SA_To_3_fld3>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_3_fld3
# Accuracy : 0.7395

# >>>>>>>>>>>>>>>>>
# 4th fold
set.seed(123)
SA_To_3_obj4 = Surv(training_SA_To_3_full[SA_To_3_full_folds[[4]],]$time, 
                    training_SA_To_3_full[SA_To_3_full_folds[[4]],]$status)
Cox_SA_To_3_fld4 = coxph(SA_To_3_obj4 ~ ., 
                         data=training_SA_To_3_full[SA_To_3_full_folds[[4]],-c(29,30)])
# summary(Cox_SA_To_3_fld4)
# Rsquare= 0.276 
S0_SA_To_3_fld4 = summary(survfit(Cox_SA_To_3_fld4, 
                                  newdata=training_SA_To_3_full[-SA_To_3_full_folds[[4]],-c(29,30)]), 
                          time=5)$surv
lp_SA_To_3_fld4 = predict(Cox_SA_To_3_fld4, 
                          newdata = training_SA_To_3_full[-SA_To_3_full_folds[[4]],-c(29,30)], type = "lp")
St_SA_To_3_fld4 = S0_SA_To_3_fld4^exp(lp_SA_To_3_fld4)
Acc_SA_To_3_fld4 = confusionMatrix(factor(training_SA_To_3_full[-SA_To_3_full_folds[[4]],]$status),
                                   factor(ifelse(St_SA_To_3_fld4>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_3_fld4
# Accuracy : 0.6807

# >>>>>>>>>>>>>>>>>
# 5th fold
set.seed(123)
SA_To_3_obj5 = Surv(training_SA_To_3_full[SA_To_3_full_folds[[5]],]$time, 
                    training_SA_To_3_full[SA_To_3_full_folds[[5]],]$status)
Cox_SA_To_3_fld5 = coxph(SA_To_3_obj5 ~ ., 
                         data=training_SA_To_3_full[SA_To_3_full_folds[[5]],-c(29,30)])
# summary(Cox_SA_To_3_fld5)
# Rsquare= 0.275
S0_SA_To_3_fld5 = summary(survfit(Cox_SA_To_3_fld5, 
                                  newdata=training_SA_To_3_full[-SA_To_3_full_folds[[5]],-c(29,30)]), 
                          time=5)$surv
lp_SA_To_3_fld5 = predict(Cox_SA_To_3_fld5, 
                          newdata = training_SA_To_3_full[-SA_To_3_full_folds[[5]],-c(29,30)], type = "lp")
St_SA_To_3_fld5 = S0_SA_To_3_fld5^exp(lp_SA_To_3_fld5)
Acc_SA_To_3_fld5 = confusionMatrix(factor(training_SA_To_3_full[-SA_To_3_full_folds[[5]],]$status),
                                   factor(ifelse(St_SA_To_3_fld5>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_3_fld5
# Accuracy : 0.6780

# >>>>>>>>>>>>>>>>>
# 6th fold
set.seed(123)
SA_To_3_obj6 = Surv(training_SA_To_3_full[SA_To_3_full_folds[[6]],]$time, 
                    training_SA_To_3_full[SA_To_3_full_folds[[6]],]$status)
Cox_SA_To_3_fld6 = coxph(SA_To_3_obj6 ~ ., 
                         data=training_SA_To_3_full[SA_To_3_full_folds[[6]],-c(29,30)])
# summary(Cox_SA_To_3_fld6)
# Rsquare= 0.275
S0_SA_To_3_fld6 = summary(survfit(Cox_SA_To_3_fld6, 
                                  newdata=training_SA_To_3_full[-SA_To_3_full_folds[[6]],-c(29,30)]), 
                          time=5)$surv
lp_SA_To_3_fld6 = predict(Cox_SA_To_3_fld6, 
                          newdata = training_SA_To_3_full[-SA_To_3_full_folds[[6]],-c(29,30)], type = "lp")
St_SA_To_3_fld6 = S0_SA_To_3_fld6^exp(lp_SA_To_3_fld6)
Acc_SA_To_3_fld6 = confusionMatrix(factor(training_SA_To_3_full[-SA_To_3_full_folds[[6]],]$status),
                                   factor(ifelse(St_SA_To_3_fld6>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_3_fld6
# Accuracy : 0.6975

# >>>>>>>>>>>>>>>>>
# 7th fold
set.seed(123)
SA_To_3_obj7 = Surv(training_SA_To_3_full[SA_To_3_full_folds[[7]],]$time, 
                    training_SA_To_3_full[SA_To_3_full_folds[[7]],]$status)
Cox_SA_To_3_fld7 = coxph(SA_To_3_obj7 ~ ., 
                         data=training_SA_To_3_full[SA_To_3_full_folds[[7]],-c(29,30)])
# summary(Cox_SA_To_3_fld7)
# Rsquare= 0.274
S0_SA_To_3_fld7 = summary(survfit(Cox_SA_To_3_fld7, 
                                  newdata=training_SA_To_3_full[-SA_To_3_full_folds[[7]],-c(29,30)]), 
                          time=5)$surv
lp_SA_To_3_fld7 = predict(Cox_SA_To_3_fld7, 
                          newdata = training_SA_To_3_full[-SA_To_3_full_folds[[7]],-c(29,30)], type = "lp")
St_SA_To_3_fld7 = S0_SA_To_3_fld7^exp(lp_SA_To_3_fld7)
Acc_SA_To_3_fld7 = confusionMatrix(factor(training_SA_To_3_full[-SA_To_3_full_folds[[7]],]$status),
                                   factor(ifelse(St_SA_To_3_fld7>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_3_fld7
# Accuracy : 0.7143

# >>>>>>>>>>>>>>>>>
# 8th fold
set.seed(123)
SA_To_3_obj8 = Surv(training_SA_To_3_full[SA_To_3_full_folds[[8]],]$time, 
                    training_SA_To_3_full[SA_To_3_full_folds[[8]],]$status)
Cox_SA_To_3_fld8 = coxph(SA_To_3_obj8 ~ ., 
                         data=training_SA_To_3_full[SA_To_3_full_folds[[8]],-c(29,30)])
# summary(Cox_SA_To_3_fld8)
# Rsquare= 0.271
S0_SA_To_3_fld8 = summary(survfit(Cox_SA_To_3_fld8, 
                                  newdata=training_SA_To_3_full[-SA_To_3_full_folds[[8]],-c(29,30)]), 
                          time=5)$surv
lp_SA_To_3_fld8 = predict(Cox_SA_To_3_fld8, 
                          newdata = training_SA_To_3_full[-SA_To_3_full_folds[[8]],-c(29,30)], type = "lp")
St_SA_To_3_fld8 = S0_SA_To_3_fld8^exp(lp_SA_To_3_fld8)
Acc_SA_To_3_fld8 = confusionMatrix(factor(training_SA_To_3_full[-SA_To_3_full_folds[[8]],]$status),
                                   factor(ifelse(St_SA_To_3_fld8>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_3_fld8
# Accuracy : 0.7647

# >>>>>>>>>>>>>>>>>
# 9th fold
set.seed(123)
SA_To_3_obj9 = Surv(training_SA_To_3_full[SA_To_3_full_folds[[9]],]$time, 
                    training_SA_To_3_full[SA_To_3_full_folds[[9]],]$status)
Cox_SA_To_3_fld9 = coxph(SA_To_3_obj9 ~ ., 
                         data=training_SA_To_3_full[SA_To_3_full_folds[[9]],-c(29,30)])
# summary(Cox_SA_To_3_fld9)
# Rsquare= 0.282
S0_SA_To_3_fld9 = summary(survfit(Cox_SA_To_3_fld9, 
                                  newdata=training_SA_To_3_full[-SA_To_3_full_folds[[9]],-c(29,30)]), 
                          time=5)$surv
lp_SA_To_3_fld9 = predict(Cox_SA_To_3_fld9, 
                          newdata = training_SA_To_3_full[-SA_To_3_full_folds[[9]],-c(29,30)], type = "lp")
St_SA_To_3_fld9 = S0_SA_To_3_fld9^exp(lp_SA_To_3_fld9)
Acc_SA_To_3_fld9 = confusionMatrix(factor(training_SA_To_3_full[-SA_To_3_full_folds[[9]],]$status),
                                   factor(ifelse(St_SA_To_3_fld9>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_3_fld9
# Accuracy : 0.6780

# >>>>>>>>>>>>>>>>>
# 10th fold
set.seed(123)
SA_To_3_obj10 = Surv(training_SA_To_3_full[SA_To_3_full_folds[[10]],]$time, 
                     training_SA_To_3_full[SA_To_3_full_folds[[10]],]$status)
Cox_SA_To_3_fld10 = coxph(SA_To_3_obj10 ~ ., 
                          data=training_SA_To_3_full[SA_To_3_full_folds[[10]],-c(29,30)])
# summary(Cox_SA_To_3_fld10)
# Rsquare= 0.286
S0_SA_To_3_fld10 = summary(survfit(Cox_SA_To_3_fld10, 
                                   newdata=training_SA_To_3_full[-SA_To_3_full_folds[[10]],-c(29,30)]), 
                           time=5)$surv
lp_SA_To_3_fld10 = predict(Cox_SA_To_3_fld10, 
                           newdata = training_SA_To_3_full[-SA_To_3_full_folds[[10]],-c(29,30)], type = "lp")
St_SA_To_3_fld10 = S0_SA_To_3_fld10^exp(lp_SA_To_3_fld10)
Acc_SA_To_3_fld10 = confusionMatrix(factor(training_SA_To_3_full[-SA_To_3_full_folds[[10]],]$status),
                                    factor(ifelse(St_SA_To_3_fld10>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_3_fld10
# Accuracy : 0.7119

# >>>>>>>>>>>>>>>>>
# Mean Accuracy
Acc_SA_To_3 = c(Acc_SA_To_3_fld1[[1]], Acc_SA_To_3_fld2[[1]], 
                Acc_SA_To_3_fld3[[1]], Acc_SA_To_3_fld4[[1]], 
                Acc_SA_To_3_fld5[[1]], Acc_SA_To_3_fld6[[1]],
                Acc_SA_To_3_fld7[[1]], Acc_SA_To_3_fld8[[1]], 
                Acc_SA_To_3_fld9[[1]], Acc_SA_To_3_fld10[[1]])
(mean_Acc_SA_To_3 <- mean(Acc_SA_To_3))
# Accuracy : 0.7164

toc()


########################################################################################
# Final Model
########################################################################################
# find out which training models returns the highest accuracy
which(Acc_SA_To_3==max(Acc_SA_To_3)) # 2
# find out significant predictors
summary(Cox_SA_To_3_fld2)
# ParcelArea+Wood_dist+lu4_dist+lu2006+lc2010+DA_Popn_Density+Residential_Popn_Density+F_lu7

# construct final model
set.seed(123)
SA_To_3_full_final.obj = Surv(training_SA_To_3_full[SA_To_3_full_folds[[3]],]$time, training_SA_To_3_full[SA_To_3_full_folds[[3]],]$status)
Cox_SA_To_3_full_final = coxph(SA_To_3_full_final.obj ~ ParcelArea+Wood_dist+lu4_dist+lu2006+lc2010
                              +DA_Popn_Density+Residential_Popn_Density+F_lu7, 
                              data=training_SA_To_3_full[SA_To_3_full_folds[[3]],-c(29,30)])
# summary(Cox_SA_To_3_full_final)
# Rsquare= 0.248
S0_SA_To_3_full_final = summary(survfit(Cox_SA_To_3_full_final, 
                                   newdata=testing_SA_To_3_full[,-c(29,30)]), time=5)$surv
lp_SA_To_3_full_final = predict(Cox_SA_To_3_full_final, 
                           newdata = testing_SA_To_3_full[,-c(29,30)], type = "lp")
St_SA_To_3_full_final = S0_SA_To_3_full_final^exp(lp_SA_To_3_full_final)
Acc_SA_To_3_full_final = confusionMatrix(factor(testing_SA_To_3_full$status),
                                    factor(ifelse(St_SA_To_3_full_final>=0.5, 0 , 1)))$overall["Accuracy"]
Acc_SA_To_3_full_final
# Accuracy : 0.6935167
