########################################################################################
# Land use change from anything to industrial
########################################################################################
# using To_5_data

#########################################################################################
# Creating training and test datsets
#########################################################################################
library(caret)
GAM_To_5 = To_5_data

# create training dataset and test dataset
# training_GAM_To_5 = training_LR_To_5
# testing_GAM_To_5 = testing_LR_To_5

# write.csv(GAM_To_5,"GAM_To_5.csv")
# write.csv(training_GAM_To_5,"training_GAM_To_5.csv")
# write.csv(testing_GAM_To_5,"testing_GAM_To_5.csv")

training_GAM_To_5$Response=factor(training_GAM_To_5$Response)
training_GAM_To_5$lc2006=factor(training_GAM_To_5$lc2006)
training_GAM_To_5$lu2006=factor(training_GAM_To_5$lu2006)
training_GAM_To_5$lc2010=factor(training_GAM_To_5$lc2010)

testing_GAM_To_5$Response=factor(testing_GAM_To_5$Response)
testing_GAM_To_5$lc2006=factor(testing_GAM_To_5$lc2006)
testing_GAM_To_5$lu2006=factor(testing_GAM_To_5$lu2006)
testing_GAM_To_5$lc2010=factor(testing_GAM_To_5$lc2010)

############################################
# set up trainControl and grid
############################################
# using train() to conduct gam
ctrl = trainControl(method="none", savePredictions=TRUE)
grid = data.frame(method="GCV.Cp", select=FALSE)

########################################################################################
# 10-fold Cross-validation
########################################################################################
# library(caret)
# library(mgcv)
set.seed(123)
GAM_To_5_folds = createFolds(training_GAM_To_5$Response, k=10,list = TRUE, returnTrain = TRUE)

# fold 1
# GAM_To_5_fld1 = train(Response~., data=training_GAM_To_5[GAM_To_5_folds[[1]],-c(1,26,29,33)], 
#                       method="gam", family="binomial",
#                       trControl=ctrl, tuneGrid=grid, metric="Accuracy", maxit=5000)
# GAM_To_5_fld1_pred = predict(GAM_To_5_fld1, newdata=training_GAM_To_5[-GAM_To_5_folds[[1]],])
# GAM_To_5_fld1_CM = confusionMatrix(factor(GAM_To_5_fld1_pred), 
#                                    training_GAM_To_5[-GAM_To_5_folds[[1]],]$Response)
# (GAM_To_5_fld1_Acc = GAM_To_5_fld1_CM$overall[[1]]) 
# 0.9459

# fold 1
GAM_To_5_fld1 = train(Response~., data=training_To_5_data1.2[GAM_To_5_folds[[1]],], 
                      method="gam", family="binomial",
                      trControl=ctrl, tuneGrid=grid, metric="Accuracy")
GAM_To_5_fld1_pred = predict(GAM_To_5_fld1, 
                             newdata=training_To_5_data1.2[-GAM_To_5_folds[[1]],])
GAM_To_5_fld1_CM = confusionMatrix(factor(GAM_To_5_fld1_pred), 
                                   training_To_5_data1.2[-GAM_To_5_folds[[1]],]$Response)
(GAM_To_5_fld1_Acc = GAM_To_5_fld1_CM$overall[[1]]) 

# fold 2
GAM_To_5_fld2 = train(Response~., data=training_To_5_data1.2[GAM_To_5_folds[[2]],], 
                      method="gam", family="binomial",
                      trControl=ctrl, tuneGrid=grid, metric="Accuracy")
GAM_To_5_fld2_pred = predict(GAM_To_5_fld2, 
                             newdata=training_To_5_data1.2[-GAM_To_5_folds[[2]],])
GAM_To_5_fld2_CM = confusionMatrix(factor(GAM_To_5_fld2_pred), 
                                   training_To_5_data1.2[-GAM_To_5_folds[[2]],]$Response)
(GAM_To_5_fld2_Acc = GAM_To_5_fld2_CM$overall[[1]]) 
# 0.8889

# fold 3
GAM_To_5_fld3 = train(Response~., data=training_To_5_data1.2[GAM_To_5_folds[[3]],], 
                      method="gam", family="binomial",
                      trControl=ctrl, tuneGrid=grid, metric="Accuracy")
GAM_To_5_fld3_pred = predict(GAM_To_5_fld3, 
                             newdata=training_To_5_data1.2[-GAM_To_5_folds[[3]],])
GAM_To_5_fld3_CM = confusionMatrix(factor(GAM_To_5_fld3_pred), 
                                   training_To_5_data1.2[-GAM_To_5_folds[[3]],]$Response)
(GAM_To_5_fld3_Acc = GAM_To_5_fld3_CM$overall[[3]]) 
# 0.8180509

# fold 4
GAM_To_5_fld4 = train(Response~., data=training_To_5_data1.2[GAM_To_5_folds[[4]],], 
                      method="gam", family="binomial",
                      trControl=ctrl, tuneGrid=grid, metric="Accuracy")
GAM_To_5_fld4_pred = predict(GAM_To_5_fld4, 
                             newdata=training_To_5_data1.2[-GAM_To_5_folds[[4]],])
GAM_To_5_fld4_CM = confusionMatrix(factor(GAM_To_5_fld4_pred), 
                                   training_To_5_data1.2[-GAM_To_5_folds[[4]],]$Response)
(GAM_To_5_fld4_Acc = GAM_To_5_fld4_CM$overall[[1]]) 
# 0.8421

# fold 5
GAM_To_5_fld5 = train(Response~., data=training_To_5_data1.2[GAM_To_5_folds[[5]],], 
                      method="gam", family="binomial",
                      trControl=ctrl, tuneGrid=grid, metric="Accuracy")
GAM_To_5_fld5_pred = predict(GAM_To_5_fld5, 
                             newdata=training_To_5_data1.2[-GAM_To_5_folds[[5]],])
GAM_To_5_fld5_CM = confusionMatrix(factor(GAM_To_5_fld5_pred), 
                                   training_To_5_data1.2[-GAM_To_5_folds[[5]],]$Response)
(GAM_To_5_fld5_Acc = GAM_To_5_fld5_CM$overall[[1]]) 
# 0.9459

# fold 6
GAM_To_5_fld6 = train(Response~., data=training_To_5_data1.2[GAM_To_5_folds[[6]],], 
                      method="gam", family="binomial",
                      trControl=ctrl, tuneGrid=grid, metric="Accuracy")
GAM_To_5_fld6_pred = predict(GAM_To_5_fld6, 
                             newdata=training_To_5_data1.2[-GAM_To_5_folds[[6]],])
GAM_To_5_fld6_CM = confusionMatrix(factor(GAM_To_5_fld6_pred), 
                                   training_To_5_data1.2[-GAM_To_5_folds[[6]],]$Response)
(GAM_To_5_fld6_Acc = GAM_To_5_fld6_CM$overall[[1]]) 
# 0.9459

# fold 7
GAM_To_5_fld7 = train(Response~., data=training_To_5_data1.2[GAM_To_5_folds[[7]],], 
                      method="gam", family="binomial",
                      trControl=ctrl, tuneGrid=grid, metric="Accuracy")
GAM_To_5_fld7_pred = predict(GAM_To_5_fld7, 
                             newdata=training_To_5_data1.2[-GAM_To_5_folds[[7]],])
GAM_To_5_fld7_CM = confusionMatrix(factor(GAM_To_5_fld7_pred), 
                                   training_To_5_data1.2[-GAM_To_5_folds[[7]],]$Response)
(GAM_To_5_fld7_Acc = GAM_To_5_fld7_CM$overall[[1]]) 
# 0.9189

# fold 8
GAM_To_5_fld8 = train(Response~., data=training_To_5_data1.2[GAM_To_5_folds[[8]],], 
                      method="gam", family="binomial",
                      trControl=ctrl, tuneGrid=grid, metric="Accuracy")
GAM_To_5_fld8_pred = predict(GAM_To_5_fld8, 
                             newdata=training_To_5_data1.2[-GAM_To_5_folds[[8]],])
GAM_To_5_fld8_CM = confusionMatrix(factor(GAM_To_5_fld8_pred), 
                                   training_To_5_data1.2[-GAM_To_5_folds[[8]],]$Response)
(GAM_To_5_fld8_Acc = GAM_To_5_fld8_CM$overall[[1]]) 
# 0.9459

# fold 9
GAM_To_5_fld9 = train(Response~., data=training_To_5_data1.2[GAM_To_5_folds[[9]],], 
                      method="gam", family="binomial",
                      trControl=ctrl, tuneGrid=grid, metric="Accuracy",maxit=10000)
GAM_To_5_fld9_pred = predict(GAM_To_5_fld9, 
                             newdata=training_To_5_data1.2[-GAM_To_5_folds[[9]],])
GAM_To_5_fld9_CM = confusionMatrix(factor(GAM_To_5_fld9_pred), 
                                   training_To_5_data1.2[-GAM_To_5_folds[[9]],]$Response)
(GAM_To_5_fld9_Acc = GAM_To_5_fld9_CM$overall[[1]]) 

# fold 10
GAM_To_5_fld10 = train(Response~., data=training_To_5_data1.2[GAM_To_5_folds[[10]],], 
                      method="gam", family="binomial",
                      trControl=ctrl, tuneGrid=grid, metric="Accuracy")
GAM_To_5_fld10_pred = predict(GAM_To_5_fld10, 
                             newdata=training_To_5_data1.2[-GAM_To_5_folds[[10]],])
GAM_To_5_fld10_CM = confusionMatrix(factor(GAM_To_5_fld10_pred), 
                                   training_To_5_data1.2[-GAM_To_5_folds[[10]],]$Response)
(GAM_To_5_fld10_Acc = GAM_To_5_fld10_CM$overall[[1]]) 
# 0.9473684


GAM_To_5_Acc = c(GAM_To_5_fld1_Acc, GAM_To_5_fld2_Acc, GAM_To_5_fld3_Acc, 
                 GAM_To_5_fld4_Acc, GAM_To_5_fld5_Acc, GAM_To_5_fld6_Acc,
                 GAM_To_5_fld7_Acc, GAM_To_5_fld8_Acc, GAM_To_5_fld10_Acc) # GAMTo_5_fld9_Acc,
(GAM_To_5_meanAcc = mean(GAM_To_5_Acc))
# 0.8995

#######################################################
# find out which fold returns the highest accuracy
which(GAM_To_5_Acc == max(GAM_To_5_Acc)) # 10
# choose GAM model with fold 10
summary(GAM_To_5_fld10)
# significant: none

set.seed(123)
GAM_To_5_final_pred = predict(GAM_To_5_fld10, newdata=testing_To_5_data1.2)
confusionMatrix(factor(GAM_To_5_final_pred), testing_To_5_data1.2$Response)
# 0.9182



# # only lc and lu variables
# set.seed(123)
# tic("model fitting")
# mod2_GAM_To_5 <- train(Response~lc2006+lu2006+lc2010, 
#                       data=training_GAM_To_5[,-c(1,26,29,33)], method="gam", family="binomial",
#                       trControl=ctrl, tuneGrid=grid, metric="Accuracy", maxit=5000)
# toc() # 0.92
# summary(mod2_GAM_To_5)
# # lu2006 and lc2010
# 
# # only demographic variables
# set.seed(123)
# tic("model fitting")
# mod3_GAM_To_5 <- train(Response~DA_Area+ParcelArea+MeanDEM+MeanSlope, 
#                        data=training_GAM_To_5[,-c(1,26,29,33)], method="gam", family="binomial",
#                        trControl=ctrl, tuneGrid=grid, metric="Accuracy", maxit=5000)
# toc() # 2.4
# summary(mod3_GAM_To_5)
# # s(ParcelArea), s(MeanDEM)
# 
# # only distance variables
# set.seed(123)
# tic("model fitting")
# mod4_GAM_To_5 <- train(Response~Ramp_dist+River_dist+Water_dist+Wood_dist+LRoad_dist+MRoad_dist
#                        +lu4_dist+lu5_dist+lu8_dist+lu9_dist, 
#                        data=training_GAM_To_5[,-c(1,26,29,33)], method="gam", family="binomial",
#                        trControl=ctrl, tuneGrid=grid, metric="Accuracy", maxit=5000)
# toc() # 32.5
# summary(mod4_GAM_To_5)
# # None
# 
# # only demographic variables
# set.seed(123)
# tic("model fitting")
# mod5_GAM_To_5 <- train(Response~DA_Popn_Density+Residential_Popn_Density+Change_Popn+Change_AveIncome, 
#                        data=training_GAM_To_5[,-c(1,26,29,33)], method="gam", family="binomial",
#                        trControl=ctrl, tuneGrid=grid, metric="Accuracy", maxit=5000)
# toc() # 2.89
# summary(mod5_GAM_To_5)
# # s(DA_Popn_Density), s(Change_Popn)
# 
# # only spatial variables
# set.seed(123)
# tic("model fitting")
# mod6_GAM_To_5 <- train(Response~F_lu1+F_lu2+F_lu4+F_lu5+F_lu7+F_lu8+F_lu9+F_lu11, 
#                        data=training_GAM_To_5[,-c(1,26,29,33)], method="gam", family="binomial",
#                        trControl=ctrl, tuneGrid=grid, metric="Accuracy", maxit=5000)
# toc() # 1
# summary(mod6_GAM_To_5)
# # s(F_lu2)
# 
# # Aggregate significant predictors
# set.seed(123)
# tic("model fitting")
# mod7_GAM_To_5 <- train(Response~lu2006+lc2010+ParcelArea+MeanDEM+DA_Popn_Density+Change_Popn+F_lu2, 
#                        data=training_GAM_To_5[,-c(1,26,29,33)], method="gam", family="binomial",
#                        trControl=ctrl, tuneGrid=grid, metric="Accuracy", maxit=5000)
# toc() # 5
# summary(mod7_GAM_To_5)
# # lu2006, lc2010, s(MeanDEM)
# 
# ########################################################################################
# # Final Model
# ########################################################################################
# # Construct final model
# set.seed(123)
# mod_GAM_To_5_Final <- gam(Response~lu2006+lc2010+s(ParcelArea)+s(MeanDEM)
#                           +s(DA_Popn_Density)+s(Change_Popn+F_lu2),
#                           data=training_GAM_To_5,
#                           family=binomial(link=logit),
#                           method="GCV.Cp")
# summary(mod_GAM_To_5_Final)
# set.seed(123)
# mod_GAM_To_5_Final_pred <- predict(mod_GAM_To_5_Final, newdata=testing_GAM_To_5,type="response")
# mod_GAM_To_5_Final_pred2 <- mod_GAM_To_5_Final_pred
# mod_GAM_To_5_Final_pred2[which(mod_GAM_To_5_Final_pred<0.5)] <- 0
# mod_GAM_To_5_Final_pred2[which(mod_GAM_To_5_Final_pred>=0.5)] <- 1
# confusionMatrix(factor(mod_GAM_To_5_Final_pred2),factor(testing_GAM_To_5$Response)) # 0.7421
# 
# # Remove
# set.seed(123)
# mod2_GAM_To_5_Final <- gam(Response~lu2006+lc2010+s(MeanDEM),
#                            data=training_GAM_To_5,
#                            family=binomial(link=logit),
#                            method="GCV.Cp")
# summary(mod2_GAM_To_5_Final)
# set.seed(123)
# mod2_GAM_To_5_Final_pred <- predict(mod2_GAM_To_5_Final, newdata=testing_GAM_To_5,type="response")
# mod2_GAM_To_5_Final_pred2 <- mod2_GAM_To_5_Final_pred
# mod2_GAM_To_5_Final_pred2[which(mod2_GAM_To_5_Final_pred<0.5)] <- 0
# mod2_GAM_To_5_Final_pred2[which(mod2_GAM_To_5_Final_pred>=0.5)] <- 1
# confusionMatrix(factor(mod2_GAM_To_5_Final_pred2),factor(testing_GAM_To_5$Response)) # 0.7799
# 
# rocplot(mod_GAM_To_5_Final) # AUC=0.8993
# rocplot(mod2_GAM_To_5_Final) # AUC=0.8744
# 
# anova(mod2_GAM_To_5_Final,mod_GAM_To_5_Final,test="Chisq") # p=0.00645, prefer mod1
# # Conclusion: choose mod1 as final model
# 
