########################################################################################
# Land use change from anything to median-density residential
########################################################################################
training_LR_To_2_full$Response = factor(training_LR_To_2_full$Response)
training_LR_To_2_full$lu2006 = factor(training_LR_To_2_full$lu2006)
training_LR_To_2_full$lc2006 = factor(training_LR_To_2_full$lc2006)
training_LR_To_2_full$lc2010 = factor(training_LR_To_2_full$lc2010)

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

# Remove F_lu6. 

########################################################################################
# 10-fold Cross-validation
########################################################################################
set.seed(123)
To_2_full_folds <- createFolds(training_LR_To_2_full$Response, k = 10, list = TRUE, returnTrain = TRUE)

# fold 1
LR_To_2_full_fld1 <- glm(Response~., 
                         data=training_LR_To_2_full[To_2_full_folds[[1]],-c(1,29)], 
                         family="binomial")
LR_To_2_full_fld1_pred <- predict(LR_To_2_full_fld1, 
                                  newdata=training_LR_To_2_full[-To_2_full_folds[[1]],-c(1,29)], 
                                  type = "response")
LR_To_2_full_fld1_pred1.1 <- LR_To_2_full_fld1_pred
LR_To_2_full_fld1_pred1.1[which(LR_To_2_full_fld1_pred<0.5)] <- 0
LR_To_2_full_fld1_pred1.1[which(LR_To_2_full_fld1_pred>=0.5)] <- 1
LR_To_2_full_fld1_CM = confusionMatrix(factor(LR_To_2_full_fld1_pred1.1), 
                                       training_LR_To_2_full[-To_2_full_folds[[1]],-c(1,29)]$Response) 
(LR_To_2_full_fld1_Acc = LR_To_2_full_fld1_CM$overall[[1]]) # 0.8877147

# fold 2
LR_To_2_full_fld2 <- glm(Response~., 
                         data=training_LR_To_2_full[To_2_full_folds[[2]],-c(1,29)], 
                         family="binomial")
LR_To_2_full_fld2_pred <- predict(LR_To_2_full_fld2, 
                                  newdata=training_LR_To_2_full[-To_2_full_folds[[2]],-c(1,29)], 
                                  type = "response")
LR_To_2_full_fld2_pred1.1 <- LR_To_2_full_fld2_pred
LR_To_2_full_fld2_pred1.1[which(LR_To_2_full_fld2_pred<0.5)] <- 0
LR_To_2_full_fld2_pred1.1[which(LR_To_2_full_fld2_pred>=0.5)] <- 1
LR_To_2_full_fld2_CM = confusionMatrix(factor(LR_To_2_full_fld2_pred1.1), 
                                       training_LR_To_2_full[-To_2_full_folds[[2]],-c(1,29)]$Response) 
(LR_To_2_full_fld2_Acc = LR_To_2_full_fld2_CM$overall[[1]]) # 0.8982827

# fold 3
LR_To_2_full_fld3 <- glm(Response~., 
                         data=training_LR_To_2_full[To_2_full_folds[[3]],-c(1,29)], 
                         family="binomial")
LR_To_2_full_fld3_pred <- predict(LR_To_2_full_fld3, 
                                  newdata=training_LR_To_2_full[-To_2_full_folds[[3]],-c(1,29)], 
                                  type = "response")
LR_To_2_full_fld3_pred1.1 <- LR_To_2_full_fld3_pred
LR_To_2_full_fld3_pred1.1[which(LR_To_2_full_fld3_pred<0.5)] <- 0
LR_To_2_full_fld3_pred1.1[which(LR_To_2_full_fld3_pred>=0.5)] <- 1
LR_To_2_full_fld3_CM = confusionMatrix(factor(LR_To_2_full_fld3_pred1.1), 
                                       training_LR_To_2_full[-To_2_full_folds[[3]],-c(1,29)]$Response) 
(LR_To_2_full_fld3_Acc = LR_To_2_full_fld3_CM$overall[[1]]) # 0.8799472

# fold 4
LR_To_2_full_fld4 <- glm(Response~., 
                         data=training_LR_To_2_full[To_2_full_folds[[4]],-c(1,29)], 
                         family="binomial")
LR_To_2_full_fld4_pred <- predict(LR_To_2_full_fld4, 
                                  newdata=training_LR_To_2_full[-To_2_full_folds[[4]],-c(1,29)], 
                                  type = "response")
LR_To_2_full_fld4_pred1.1 <- LR_To_2_full_fld4_pred
LR_To_2_full_fld4_pred1.1[which(LR_To_2_full_fld4_pred<0.5)] <- 0
LR_To_2_full_fld4_pred1.1[which(LR_To_2_full_fld4_pred>=0.5)] <- 1
LR_To_2_full_fld4_CM = confusionMatrix(factor(LR_To_2_full_fld4_pred1.1), 
                                       training_LR_To_2_full[-To_2_full_folds[[4]],-c(1,29)]$Response) 
(LR_To_2_full_fld4_Acc = LR_To_2_full_fld4_CM$overall[[1]]) # 0.914248

# fold 5
LR_To_2_full_fld5 <- glm(Response~., 
                         data=training_LR_To_2_full[To_2_full_folds[[5]],-c(1,29)], 
                         family="binomial")
LR_To_2_full_fld5_pred <- predict(LR_To_2_full_fld5, 
                                  newdata=training_LR_To_2_full[-To_2_full_folds[[5]],-c(1,29)], 
                                  type = "response")
LR_To_2_full_fld5_pred1.1 <- LR_To_2_full_fld5_pred
LR_To_2_full_fld5_pred1.1[which(LR_To_2_full_fld5_pred<0.5)] <- 0
LR_To_2_full_fld5_pred1.1[which(LR_To_2_full_fld5_pred>=0.5)] <- 1
LR_To_2_full_fld5_CM = confusionMatrix(factor(LR_To_2_full_fld5_pred1.1), 
                                       training_LR_To_2_full[-To_2_full_folds[[5]],-c(1,29)]$Response) 
(LR_To_2_full_fld5_Acc = LR_To_2_full_fld5_CM$overall[[1]]) # 0.8981481

# fold 6
LR_To_2_full_fld6 <- glm(Response~., 
                         data=training_LR_To_2_full[To_2_full_folds[[6]],-c(1,29)], 
                         family="binomial")
LR_To_2_full_fld6_pred <- predict(LR_To_2_full_fld6, 
                                  newdata=training_LR_To_2_full[-To_2_full_folds[[6]],-c(1,29)], 
                                  type = "response")
LR_To_2_full_fld6_pred1.1 <- LR_To_2_full_fld6_pred
LR_To_2_full_fld6_pred1.1[which(LR_To_2_full_fld6_pred<0.5)] <- 0
LR_To_2_full_fld6_pred1.1[which(LR_To_2_full_fld6_pred>=0.5)] <- 1
LR_To_2_full_fld6_CM = confusionMatrix(factor(LR_To_2_full_fld6_pred1.1), 
                                       training_LR_To_2_full[-To_2_full_folds[[6]],-c(1,29)]$Response) 
(LR_To_2_full_fld6_Acc = LR_To_2_full_fld6_CM$overall[[1]]) # 0.8850727

# fold 7
LR_To_2_full_fld7 <- glm(Response~., 
                         data=training_LR_To_2_full[To_2_full_folds[[7]],-c(1,29)], 
                         family="binomial")
LR_To_2_full_fld7_pred <- predict(LR_To_2_full_fld7, 
                                  newdata=training_LR_To_2_full[-c(To_2_full_folds[[7]],
                                                                   which(training_LR_To_2_full$lu2006==6)),-c(1,29)], 
                                  type = "response")
LR_To_2_full_fld7_pred1.1 <- LR_To_2_full_fld7_pred
LR_To_2_full_fld7_pred1.1[which(LR_To_2_full_fld7_pred<0.5)] <- 0
LR_To_2_full_fld7_pred1.1[which(LR_To_2_full_fld7_pred>=0.5)] <- 1
LR_To_2_full_fld7_CM = confusionMatrix(factor(LR_To_2_full_fld7_pred1.1), 
                                       training_LR_To_2_full[-c(To_2_full_folds[[7]],
                                                                which(training_LR_To_2_full$lu2006==6)),-c(1,29)]$Response) 
(LR_To_2_full_fld7_Acc = LR_To_2_full_fld7_CM$overall[[1]]) # 0.8941799

# fold 8
LR_To_2_full_fld8 <- glm(Response~., 
                         data=training_LR_To_2_full[To_2_full_folds[[8]],-c(1,29)], 
                         family="binomial")
LR_To_2_full_fld8_pred <- predict(LR_To_2_full_fld8, 
                                  newdata=training_LR_To_2_full[-To_2_full_folds[[8]],-c(1,29)], 
                                  type = "response")
LR_To_2_full_fld8_pred1.1 <- LR_To_2_full_fld8_pred
LR_To_2_full_fld8_pred1.1[which(LR_To_2_full_fld8_pred<0.5)] <- 0
LR_To_2_full_fld8_pred1.1[which(LR_To_2_full_fld8_pred>=0.5)] <- 1
LR_To_2_full_fld8_CM = confusionMatrix(factor(LR_To_2_full_fld8_pred1.1), 
                                       training_LR_To_2_full[-To_2_full_folds[[8]],-c(1,29)]$Response) 
(LR_To_2_full_fld8_Acc = LR_To_2_full_fld8_CM$overall[[1]]) # 0.9129288

# fold 9
LR_To_2_full_fld9 <- glm(Response~., 
                         data=training_LR_To_2_full[To_2_full_folds[[9]],-c(1,29)], 
                         family="binomial")
LR_To_2_full_fld9_pred <- predict(LR_To_2_full_fld9, 
                                  newdata=training_LR_To_2_full[-To_2_full_folds[[9]],-c(1,29)], 
                                  type = "response")
LR_To_2_full_fld9_pred1.1 <- LR_To_2_full_fld9_pred
LR_To_2_full_fld9_pred1.1[which(LR_To_2_full_fld9_pred<0.5)] <- 0
LR_To_2_full_fld9_pred1.1[which(LR_To_2_full_fld9_pred>=0.5)] <- 1
LR_To_2_full_fld9_CM = confusionMatrix(factor(LR_To_2_full_fld9_pred1.1), 
                                       training_LR_To_2_full[-To_2_full_folds[[9]],-c(1,29)]$Response) 
(LR_To_2_full_fld9_Acc = LR_To_2_full_fld9_CM$overall[[1]]) # 0.910171

# fold 6
LR_To_2_full_fld10 <- glm(Response~., 
                          data=training_LR_To_2_full[To_2_full_folds[[10]],-c(1,29)], 
                          family="binomial")
LR_To_2_full_fld10_pred <- predict(LR_To_2_full_fld10, 
                                   newdata=training_LR_To_2_full[-To_2_full_folds[[10]],-c(1,29)], 
                                   type = "response")
LR_To_2_full_fld10_pred1.1 <- LR_To_2_full_fld10_pred
LR_To_2_full_fld10_pred1.1[which(LR_To_2_full_fld10_pred<0.5)] <- 0
LR_To_2_full_fld10_pred1.1[which(LR_To_2_full_fld10_pred>=0.5)] <- 1
LR_To_2_full_fld10_CM = confusionMatrix(factor(LR_To_2_full_fld10_pred1.1), 
                                        training_LR_To_2_full[-To_2_full_folds[[10]],-c(1,29)]$Response) 
(LR_To_2_full_fld10_Acc = LR_To_2_full_fld10_CM$overall[[1]]) # 0.8809524



LR_To_2_full_Acc = c(LR_To_2_full_fld1_Acc, LR_To_2_full_fld2_Acc, LR_To_2_full_fld3_Acc, 
                   LR_To_2_full_fld4_Acc, LR_To_2_full_fld5_Acc, LR_To_2_full_fld6_Acc,
                   LR_To_2_full_fld7_Acc, LR_To_2_full_fld8_Acc, LR_To_2_full_fld9_Acc, LR_To_2_full_fld10_Acc)
(LR_To_2_full_meanAcc = mean(LR_To_2_full_Acc))
# 0.8961646
which(LR_To_2_full_Acc == max(LR_To_2_full_Acc)) # 4
summary(LR_To_2_full_fld4)
# significant predictors:
# MeanDEM + Water_dist + LRoad_dist + lu4_dist + lu8_dist
# + DA_Popn_Density + Residential_Popn_Density + F_lu1 + F_lu2
# + F_lu3 + F_lu7 + F_lu1 + lu2006 + lu2006 + lc2010 

########################################################################################
# Final Model
########################################################################################
# Construct final model
set.seed(123)
LR_To_2_full_Final <- glm(Response~MeanDEM+Water_dist+Wood_dist+LRoad_dist+lu4_dist+lu5_dist+lu8_dist
                          +DA_Popn_Density+Residential_Popn_Density+Change_Popn+F_lu1+F_lu3+F_lu7+F_lu11
                          +lu2006+lc2006+lc2010, 
                          data=training_LR_To_2_full[To_2_full_folds[[4]],-c(1,29)], family=binomial(link=logit))
summary(LR_To_2_full_Final)
LR_To_2_full_Final_pred <- predict(LR_To_2_full_Final, newdata=testing_LR_To_2_full[,-c(1,2)], type="response")
LR_To_2_full_Final_pred2 <- LR_To_2_full_Final_pred
LR_To_2_full_Final_pred2[which(LR_To_2_full_Final_pred<0.5)] <- 0
LR_To_2_full_Final_pred2[which(LR_To_2_full_Final_pred>=0.5)] <- 1
confusionMatrix(factor(LR_To_2_full_Final_pred2),factor(testing_LR_To_2_full$Response)) # 0.8986

# Remove lu5_dist, Change_Popn, F_lu3
LR_To_2_full_Final2 <- glm(Response~MeanDEM+Water_dist+Wood_dist+LRoad_dist+lu4_dist+lu8_dist
                           +DA_Popn_Density+Residential_Popn_Density+F_lu1+F_lu7+F_lu11
                           +lu2006+lc2006+lc2010, 
                           data=training_LR_To_2_full[To_2_full_folds[[4]],-c(1,29)], family=binomial(link=logit))
summary(LR_To_2_full_Final2)
LR_To_2_full_Final2_pred <- predict(LR_To_2_full_Final2, newdata=testing_LR_To_2_full[,-c(1,2)], type="response")
LR_To_2_full_Final2_pred2 <- LR_To_2_full_Final2_pred
LR_To_2_full_Final2_pred2[which(LR_To_2_full_Final2_pred<0.5)] <- 0
LR_To_2_full_Final2_pred2[which(LR_To_2_full_Final2_pred>=0.5)] <- 1
confusionMatrix(factor(LR_To_2_full_Final2_pred2),factor(testing_LR_To_2_full$Response)) # 0.8983


anova(LR_To_2_full_Final,LR_To_2_full_Final2,test="Chisq") # p=0.2067, prefer final2
