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

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

#  Remove F_lu3, F_lu5, F_lu6, F_lu10

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

set.seed(123)
LR_To_2_reduced_fld1 <- glm(Response~., 
                            data=training_LR_To_2_reduced[To_2_reduced_folds[[1]],-c(1,26,28,29,33)], 
                            family="binomial")
LR_To_2_reduced_fld1_pred <- predict(LR_To_2_reduced_fld1, 
                                  newdata=training_LR_To_2_reduced[-To_2_reduced_folds[[1]],-c(1,26,28,29,33)], 
                                  type = "response")
LR_To_2_reduced_fld1_pred1.1 <- LR_To_2_reduced_fld1_pred
LR_To_2_reduced_fld1_pred1.1[which(LR_To_2_reduced_fld1_pred<0.5)] <- 0
LR_To_2_reduced_fld1_pred1.1[which(LR_To_2_reduced_fld1_pred>=0.5)] <- 1
LR_To_2_reduced_fld1_CM = confusionMatrix(factor(LR_To_2_reduced_fld1_pred1.1), 
                                       training_LR_To_2_reduced[-To_2_reduced_folds[[1]],-c(1,26,28,29,33)]$Response) 
(LR_To_2_reduced_fld1_Acc = LR_To_2_reduced_fld1_CM$overall[[1]]) 

# fold 2
LR_To_2_reduced_fld2 <- glm(Response~., 
                         data=training_LR_To_2_reduced[To_2_reduced_folds[[2]],-c(1,26,28,29,33)], 
                         family="binomial")
LR_To_2_reduced_fld2_pred <- predict(LR_To_2_reduced_fld2, 
                                  newdata=training_LR_To_2_reduced[-To_2_reduced_folds[[2]],-c(1,26,28,29,33)], 
                                  type = "response")
LR_To_2_reduced_fld2_pred1.1 <- LR_To_2_reduced_fld2_pred
LR_To_2_reduced_fld2_pred1.1[which(LR_To_2_reduced_fld2_pred<0.5)] <- 0
LR_To_2_reduced_fld2_pred1.1[which(LR_To_2_reduced_fld2_pred>=0.5)] <- 1
LR_To_2_reduced_fld2_CM = confusionMatrix(factor(LR_To_2_reduced_fld2_pred1.1), 
                                       training_LR_To_2_reduced[-To_2_reduced_folds[[2]],-c(1,26,28,29,33)]$Response) 
(LR_To_2_reduced_fld2_Acc = LR_To_2_reduced_fld2_CM$overall[[1]]) 

# fold 3
LR_To_2_reduced_fld3 <- glm(Response~., 
                         data=training_LR_To_2_reduced[To_2_reduced_folds[[3]],-c(1,26,28,29,33)], 
                         family="binomial")
LR_To_2_reduced_fld3_pred <- predict(LR_To_2_reduced_fld3, 
                                  newdata=training_LR_To_2_reduced[-To_2_reduced_folds[[3]],-c(1,26,28,29,33)], 
                                  type = "response")
LR_To_2_reduced_fld3_pred1.1 <- LR_To_2_reduced_fld3_pred
LR_To_2_reduced_fld3_pred1.1[which(LR_To_2_reduced_fld3_pred<0.5)] <- 0
LR_To_2_reduced_fld3_pred1.1[which(LR_To_2_reduced_fld3_pred>=0.5)] <- 1
LR_To_2_reduced_fld3_CM = confusionMatrix(factor(LR_To_2_reduced_fld3_pred1.1), 
                                       training_LR_To_2_reduced[-To_2_reduced_folds[[3]],-c(1,26,28,29,33)]$Response) 
(LR_To_2_reduced_fld3_Acc = LR_To_2_reduced_fld3_CM$overall[[1]])

# fold 4
LR_To_2_reduced_fld4 <- glm(Response~., 
                         data=training_LR_To_2_reduced[To_2_reduced_folds[[4]],-c(1,26,28,29,33)], 
                         family="binomial")
LR_To_2_reduced_fld4_pred <- predict(LR_To_2_reduced_fld4, 
                                  newdata=training_LR_To_2_reduced[-To_2_reduced_folds[[4]],-c(1,26,28,29,33)], 
                                  type = "response")
LR_To_2_reduced_fld4_pred1.1 <- LR_To_2_reduced_fld4_pred
LR_To_2_reduced_fld4_pred1.1[which(LR_To_2_reduced_fld4_pred<0.5)] <- 0
LR_To_2_reduced_fld4_pred1.1[which(LR_To_2_reduced_fld4_pred>=0.5)] <- 1
LR_To_2_reduced_fld4_CM = confusionMatrix(factor(LR_To_2_reduced_fld4_pred1.1), 
                                       training_LR_To_2_reduced[-To_2_reduced_folds[[4]],-c(1,26,28,29,33)]$Response) 
(LR_To_2_reduced_fld4_Acc = LR_To_2_reduced_fld4_CM$overall[[1]]) 

# fold 5
LR_To_2_reduced_fld5 <- glm(Response~., 
                         data=training_LR_To_2_reduced[To_2_reduced_folds[[5]],-c(1,26,28,29,33)], 
                         family="binomial")
LR_To_2_reduced_fld5_pred <- predict(LR_To_2_reduced_fld5, 
                                  newdata=training_LR_To_2_reduced[-To_2_reduced_folds[[5]],-c(1,26,28,29,33)], 
                                  type = "response")
LR_To_2_reduced_fld5_pred1.1 <- LR_To_2_reduced_fld5_pred
LR_To_2_reduced_fld5_pred1.1[which(LR_To_2_reduced_fld5_pred<0.5)] <- 0
LR_To_2_reduced_fld5_pred1.1[which(LR_To_2_reduced_fld5_pred>=0.5)] <- 1
LR_To_2_reduced_fld5_CM = confusionMatrix(factor(LR_To_2_reduced_fld5_pred1.1), 
                                       training_LR_To_2_reduced[-To_2_reduced_folds[[5]],-c(1,26,28,29,33)]$Response) 
(LR_To_2_reduced_fld5_Acc = LR_To_2_reduced_fld5_CM$overall[[1]])

# fold 6
LR_To_2_reduced_fld6 <- glm(Response~., 
                         data=training_LR_To_2_reduced[To_2_reduced_folds[[6]],-c(1,26,28,29,33)], 
                         family="binomial")
LR_To_2_reduced_fld6_pred <- predict(LR_To_2_reduced_fld6, 
                                  newdata=training_LR_To_2_reduced[-To_2_reduced_folds[[6]],-c(1,26,28,29,33)], 
                                  type = "response")
LR_To_2_reduced_fld6_pred1.1 <- LR_To_2_reduced_fld6_pred
LR_To_2_reduced_fld6_pred1.1[which(LR_To_2_reduced_fld6_pred<0.5)] <- 0
LR_To_2_reduced_fld6_pred1.1[which(LR_To_2_reduced_fld6_pred>=0.5)] <- 1
LR_To_2_reduced_fld6_CM = confusionMatrix(factor(LR_To_2_reduced_fld6_pred1.1), 
                                       training_LR_To_2_reduced[-To_2_reduced_folds[[6]],-c(1,26,28,29,33)]$Response) 
(LR_To_2_reduced_fld6_Acc = LR_To_2_reduced_fld6_CM$overall[[1]]) 

# fold 7
LR_To_2_reduced_fld7 <- glm(Response~., 
                         data=training_LR_To_2_reduced[To_2_reduced_folds[[7]],-c(1,26,28,29,33)], 
                         family="binomial")
LR_To_2_reduced_fld7_pred <- predict(LR_To_2_reduced_fld7, 
                                  newdata=training_LR_To_2_reduced[-To_2_reduced_folds[[7]],-c(1,26,28,29,33)], 
                                  type = "response")
LR_To_2_reduced_fld7_pred1.1 <- LR_To_2_reduced_fld7_pred
LR_To_2_reduced_fld7_pred1.1[which(LR_To_2_reduced_fld7_pred<0.5)] <- 0
LR_To_2_reduced_fld7_pred1.1[which(LR_To_2_reduced_fld7_pred>=0.5)] <- 1
LR_To_2_reduced_fld7_CM = confusionMatrix(factor(LR_To_2_reduced_fld7_pred1.1), 
                                       training_LR_To_2_reduced[-To_2_reduced_folds[[7]],-c(1,26,28,29,33)]$Response) 
(LR_To_2_reduced_fld7_Acc = LR_To_2_reduced_fld7_CM$overall[[1]]) 

# fold 8
LR_To_2_reduced_fld8 <- glm(Response~., 
                         data=training_LR_To_2_reduced[To_2_reduced_folds[[8]],-c(1,26,28,29,33)], 
                         family="binomial")
LR_To_2_reduced_fld8_pred <- predict(LR_To_2_reduced_fld8, 
                                  newdata=training_LR_To_2_reduced[-To_2_reduced_folds[[8]],-c(1,26,28,29,33)], 
                                  type = "response")
LR_To_2_reduced_fld8_pred1.1 <- LR_To_2_reduced_fld8_pred
LR_To_2_reduced_fld8_pred1.1[which(LR_To_2_reduced_fld8_pred<0.5)] <- 0
LR_To_2_reduced_fld8_pred1.1[which(LR_To_2_reduced_fld8_pred>=0.5)] <- 1
LR_To_2_reduced_fld8_CM = confusionMatrix(factor(LR_To_2_reduced_fld8_pred1.1), 
                                       training_LR_To_2_reduced[-To_2_reduced_folds[[8]],-c(1,26,28,29,33)]$Response) 
(LR_To_2_reduced_fld8_Acc = LR_To_2_reduced_fld8_CM$overall[[1]]) 

# fold 9
LR_To_2_reduced_fld9 <- glm(Response~., 
                         data=training_LR_To_2_reduced[To_2_reduced_folds[[9]],-c(1,26,28,29,33)], 
                         family="binomial")
LR_To_2_reduced_fld9_pred <- predict(LR_To_2_reduced_fld9, 
                                  newdata=training_LR_To_2_reduced[-To_2_reduced_folds[[9]],-c(1,26,28,29,33)], 
                                  type = "response")
LR_To_2_reduced_fld9_pred1.1 <- LR_To_2_reduced_fld9_pred
LR_To_2_reduced_fld9_pred1.1[which(LR_To_2_reduced_fld9_pred<0.5)] <- 0
LR_To_2_reduced_fld9_pred1.1[which(LR_To_2_reduced_fld9_pred>=0.5)] <- 1
LR_To_2_reduced_fld9_CM = confusionMatrix(factor(LR_To_2_reduced_fld9_pred1.1), 
                                       training_LR_To_2_reduced[-To_2_reduced_folds[[9]],-c(1,26,28,29,33)]$Response) 
(LR_To_2_reduced_fld9_Acc = LR_To_2_reduced_fld9_CM$overall[[1]]) 

# fold 10
LR_To_2_reduced_fld10 <- glm(Response~., 
                          data=training_LR_To_2_reduced[To_2_reduced_folds[[10]],-c(1,26,28,29,33)], 
                          family="binomial")
LR_To_2_reduced_fld10_pred <- predict(LR_To_2_reduced_fld10, 
                                   newdata=training_LR_To_2_reduced[-To_2_reduced_folds[[10]],-c(1,26,28,29,33)], 
                                   type = "response")
LR_To_2_reduced_fld10_pred1.1 <- LR_To_2_reduced_fld10_pred
LR_To_2_reduced_fld10_pred1.1[which(LR_To_2_reduced_fld10_pred<0.5)] <- 0
LR_To_2_reduced_fld10_pred1.1[which(LR_To_2_reduced_fld10_pred>=0.5)] <- 1
LR_To_2_reduced_fld10_CM = confusionMatrix(factor(LR_To_2_reduced_fld10_pred1.1), 
                                        training_LR_To_2_reduced[-To_2_reduced_folds[[10]],-c(1,26,28,29,33)]$Response) 
(LR_To_2_reduced_fld10_Acc = LR_To_2_reduced_fld10_CM$overall[[1]])



LR_To_2_reduced_Acc = c(LR_To_2_reduced_fld1_Acc, LR_To_2_reduced_fld2_Acc, LR_To_2_reduced_fld3_Acc, 
                        LR_To_2_reduced_fld4_Acc, LR_To_2_reduced_fld5_Acc, LR_To_2_reduced_fld6_Acc,
                        LR_To_2_reduced_fld7_Acc, LR_To_2_reduced_fld8_Acc, LR_To_2_reduced_fld9_Acc, LR_To_2_reduced_fld10_Acc)
(LR_To_2_reduced_meanAcc = mean(LR_To_2_reduced_Acc)) # 0.8843627

which(LR_To_2_reduced_Acc == max(LR_To_2_reduced_Acc)) # 7
summary(LR_To_2_reduced_fld7)
# significant variables:
# MeanDEM+Ramp_dist+LRoad_dist+lu4_dist+Residential_Popn_Density+Change_Popn +F_lu2+F_lu7+lc2006+lc2010

########################################################################################
# Final Model
########################################################################################
# Construct final model
set.seed(123)
LR_To_2_reduced_Final <- glm(Response~MeanDEM+Ramp_dist+LRoad_dist+lu4_dist+Residential_Popn_Density+Change_Popn 
                             +F_lu2+F_lu7+lc2006+lc2010, 
                             data=training_LR_To_2_reduced[To_2_reduced_folds[[7]],-c(1,26,28,29,33)], family=binomial(link=logit))
summary(LR_To_2_reduced_Final)
set.seed(123)
LR_To_2_reduced_Final_pred <- predict(LR_To_2_reduced_Final, 
                                  newdata=testing_LR_To_2_reduced[-which(testing_LR_To_2_reduced$lc2006==6),-c(1,2,26,28,29,33)],
                                  type="response")
LR_To_2_reduced_Final_pred2 <- LR_To_2_reduced_Final_pred
LR_To_2_reduced_Final_pred2[which(LR_To_2_reduced_Final_pred<0.5)] <- 0
LR_To_2_reduced_Final_pred2[which(LR_To_2_reduced_Final_pred>=0.5)] <- 1
confusionMatrix(factor(LR_To_2_reduced_Final_pred2),
                testing_LR_To_2_reduced[-which(testing_LR_To_2_reduced$lc2006==6),]$Response) # 0.8662

# Remove Change_Popn
LR_To_2_reduced_Final2 <- glm(Response~MeanDEM+Ramp_dist+LRoad_dist+lu4_dist+Residential_Popn_Density
                              +F_lu2+F_lu7+lc2006+lc2010, 
                              data=training_LR_To_2_reduced[To_2_reduced_folds[[7]],-c(1,26,28,29,33)], family=binomial(link=logit))
summary(LR_To_2_reduced_Final2)
set.seed(123)
LR_To_2_reduced_Final2_pred <- predict(LR_To_2_reduced_Final2, 
                                       newdata=testing_LR_To_2_reduced[-which(testing_LR_To_2_reduced$lc2006==6),-c(1,2,26,28,29,33)],
                                       type="response")
LR_To_2_reduced_Final2_pred2 <- LR_To_2_reduced_Final2_pred
LR_To_2_reduced_Final2_pred2[which(LR_To_2_reduced_Final2_pred<0.5)] <- 0
LR_To_2_reduced_Final2_pred2[which(LR_To_2_reduced_Final2_pred>=0.5)] <- 1
confusionMatrix(factor(LR_To_2_reduced_Final2_pred2),
                testing_LR_To_2_reduced[-which(testing_LR_To_2_reduced$lc2006==6),]$Response) # 0.8763

# Remove MeanDEM
LR_To_2_reduced_Final3 <- glm(Response~Ramp_dist+LRoad_dist+lu4_dist+Residential_Popn_Density
                              +F_lu2+F_lu7+lc2006+lc2010, 
                              data=training_LR_To_2_reduced[To_2_reduced_folds[[7]],-c(1,26,28,29,33)], family=binomial(link=logit))
summary(LR_To_2_reduced_Final3)
set.seed(123)
LR_To_2_reduced_Final3_pred <- predict(LR_To_2_reduced_Final3, 
                                       newdata=testing_LR_To_2_reduced[-which(testing_LR_To_2_reduced$lc2006==6),-c(1,2,26,28,29,33)],
                                       type="response")
LR_To_2_reduced_Final3_pred2 <- LR_To_2_reduced_Final3_pred
LR_To_2_reduced_Final3_pred2[which(LR_To_2_reduced_Final3_pred<0.5)] <- 0
LR_To_2_reduced_Final3_pred2[which(LR_To_2_reduced_Final3_pred>=0.5)] <- 1
confusionMatrix(factor(LR_To_2_reduced_Final3_pred2),
                testing_LR_To_2_reduced[-which(testing_LR_To_2_reduced$lc2006==6),]$Response) # 0.8763

anova(LR_To_2_reduced_Final, LR_To_2_reduced_Final2, test="Chisq") # p-value=0.1876, prefer final2
anova(LR_To_2_reduced_Final2, LR_To_2_reduced_Final3, test="Chisq") # p-value=0.1139, prefer final3


