########################################################################################
# Land use change from anything to low-density residential (i.e., code change as 1 and 0 otherwise)
########################################################################################
training_LR_To_5$Response = factor(training_LR_To_5$Response)
training_LR_To_5$lu2006 = factor(training_LR_To_5$lu2006)
training_LR_To_5$lc2006 = factor(training_LR_To_5$lc2006)
training_LR_To_5$lc2010 = factor(training_LR_To_5$lc2010)

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

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

# fold 1
set.seed(123)
LR_To_5_fld1 <- glm(Response~., 
                    data=training_LR_To_5[To_5_folds[[1]],-c(1,26,29,33)],
                    family=binomial(link=logit))
LR_To_5_fld1_pred <- predict(LR_To_5_fld1, 
                             newdata=training_LR_To_5[-To_5_folds[[1]],-c(1,26,29,33)], 
                             type = "response")
LR_To_5_fld1_pred1.1 <- LR_To_5_fld1_pred
LR_To_5_fld1_pred1.1[which(LR_To_5_fld1_pred<0.5)] <- 0
LR_To_5_fld1_pred1.1[which(LR_To_5_fld1_pred>=0.5)] <- 1
LR_To_5_fld1_CM = confusionMatrix(factor(LR_To_5_fld1_pred1.1), 
                                  training_LR_To_5[-To_5_folds[[1]],-c(1,26,29,33)]$Response) 
(LR_To_5_fld1_Acc = LR_To_5_fld1_CM$overall[[1]]) 

# fold 2
LR_To_5_fld2 <- glm(Response~., 
                    data=training_LR_To_5[To_5_folds[[2]],-c(1,26,29,33)],
                    family=binomial(link=logit))
LR_To_5_fld2_pred <- predict(LR_To_5_fld2, 
                             newdata=training_LR_To_5[-c(To_5_folds[[2]],
                                                         which(training_LR_To_5$lu2006==2)),-c(1,26,29,33)], 
                             type = "response")
LR_To_5_fld2_pred1.1 <- LR_To_5_fld2_pred
LR_To_5_fld2_pred1.1[which(LR_To_5_fld2_pred<0.5)] <- 0
LR_To_5_fld2_pred1.1[which(LR_To_5_fld2_pred>=0.5)] <- 1
LR_To_5_fld2_CM = confusionMatrix(factor(LR_To_5_fld2_pred1.1), 
                                  training_LR_To_5[-c(To_5_folds[[2]],
                                                      which(training_LR_To_5$lu2006==2)),-c(1,26,29,33)]$Response) 
(LR_To_5_fld2_Acc = LR_To_5_fld2_CM$overall[[1]]) 

# fold 3
LR_To_5_fld3 <- glm(Response~., 
                    data=training_LR_To_5[To_5_folds[[3]],-c(1,26,29,33)],
                    family=binomial(link=logit))
LR_To_5_fld3_pred <- predict(LR_To_5_fld3, 
                             newdata=training_LR_To_5[-To_5_folds[[3]],-c(1,26,29,33)], 
                             type = "response")
LR_To_5_fld3_pred1.1 <- LR_To_5_fld3_pred
LR_To_5_fld3_pred1.1[which(LR_To_5_fld3_pred<0.5)] <- 0
LR_To_5_fld3_pred1.1[which(LR_To_5_fld3_pred>=0.5)] <- 1
LR_To_5_fld3_CM = confusionMatrix(factor(LR_To_5_fld3_pred1.1), 
                                  training_LR_To_5[-To_5_folds[[3]],-c(1,26,29,33)]$Response) 
(LR_To_5_fld3_Acc = LR_To_5_fld3_CM$overall[[1]]) 

# fold 4
LR_To_5_fld4 <- glm(Response~., 
                    data=training_LR_To_5[To_5_folds[[4]],-c(1,26,29,33)],
                    family=binomial(link=logit))
LR_To_5_fld4_pred <- predict(LR_To_5_fld4, 
                             newdata=training_LR_To_5[-To_5_folds[[4]],-c(1,26,29,33)], 
                             type = "response")
LR_To_5_fld4_pred1.1 <- LR_To_5_fld4_pred
LR_To_5_fld4_pred1.1[which(LR_To_5_fld4_pred<0.5)] <- 0
LR_To_5_fld4_pred1.1[which(LR_To_5_fld4_pred>=0.5)] <- 1
LR_To_5_fld4_CM = confusionMatrix(factor(LR_To_5_fld4_pred1.1), 
                                  training_LR_To_5[-To_5_folds[[4]],-c(1,26,29,33)]$Response) 
(LR_To_5_fld4_Acc = LR_To_5_fld4_CM$overall[[1]]) 

# fold 5
LR_To_5_fld5 <- glm(Response~., 
                    data=training_LR_To_5[To_5_folds[[5]],-c(1,26,29,33)],
                    family=binomial(link=logit))
LR_To_5_fld5_pred <- predict(LR_To_5_fld5, 
                             newdata=training_LR_To_5[-To_5_folds[[5]],-c(1,26,29,33)], 
                             type = "response")
LR_To_5_fld5_pred1.1 <- LR_To_5_fld5_pred
LR_To_5_fld5_pred1.1[which(LR_To_5_fld5_pred<0.5)] <- 0
LR_To_5_fld5_pred1.1[which(LR_To_5_fld5_pred>=0.5)] <- 1
LR_To_5_fld5_CM = confusionMatrix(factor(LR_To_5_fld5_pred1.1), 
                                  training_LR_To_5[-To_5_folds[[5]],-c(1,26,29,33)]$Response) 
(LR_To_5_fld5_Acc = LR_To_5_fld5_CM$overall[[1]]) 

# fold 6
LR_To_5_fld6 <- glm(Response~., 
                    data=training_LR_To_5[To_5_folds[[6]],-c(1,26,29,33)],
                    family=binomial(link=logit))
LR_To_5_fld6_pred <- predict(LR_To_5_fld6, 
                             newdata=training_LR_To_5[-c(To_5_folds[[6]],
                                                         which(training_LR_To_5$lu2006==6)),-c(1,26,29,33)], 
                             type = "response")
LR_To_5_fld6_pred1.1 <- LR_To_5_fld6_pred
LR_To_5_fld6_pred1.1[which(LR_To_5_fld6_pred<0.5)] <- 0
LR_To_5_fld6_pred1.1[which(LR_To_5_fld6_pred>=0.5)] <- 1
LR_To_5_fld6_CM = confusionMatrix(factor(LR_To_5_fld6_pred1.1), 
                                  training_LR_To_5[-c(To_5_folds[[6]],
                                                      which(training_LR_To_5$lu2006==6)),-c(1,26,29,33)]$Response) 
(LR_To_5_fld6_Acc = LR_To_5_fld6_CM$overall[[1]]) 

# fold 7
LR_To_5_fld7 <- glm(Response~., 
                    data=training_LR_To_5[To_5_folds[[7]],-c(1,26,29,33)],
                    family=binomial(link=logit))
LR_To_5_fld7_pred <- predict(LR_To_5_fld7, 
                             newdata=training_LR_To_5[-To_5_folds[[7]],-c(1,26,29,33)], 
                             type = "response")
LR_To_5_fld7_pred1.1 <- LR_To_5_fld7_pred
LR_To_5_fld7_pred1.1[which(LR_To_5_fld7_pred<0.5)] <- 0
LR_To_5_fld7_pred1.1[which(LR_To_5_fld7_pred>=0.5)] <- 1
LR_To_5_fld7_CM = confusionMatrix(factor(LR_To_5_fld7_pred1.1), 
                                  training_LR_To_5[-To_5_folds[[7]],-c(1,26,29,33)]$Response) 
(LR_To_5_fld7_Acc = LR_To_5_fld7_CM$overall[[1]]) # 

# fold 8
LR_To_5_fld8 <- glm(Response~., 
                    data=training_LR_To_5[To_5_folds[[8]],-c(1,26,29,33)],
                    family=binomial(link=logit))
LR_To_5_fld8_pred <- predict(LR_To_5_fld8, 
                             newdata=training_LR_To_5[-To_5_folds[[8]],-c(1,26,29,33)], 
                             type = "response")
LR_To_5_fld8_pred1.1 <- LR_To_5_fld8_pred
LR_To_5_fld8_pred1.1[which(LR_To_5_fld8_pred<0.5)] <- 0
LR_To_5_fld8_pred1.1[which(LR_To_5_fld8_pred>=0.5)] <- 1
LR_To_5_fld8_CM = confusionMatrix(factor(LR_To_5_fld8_pred1.1), 
                                  training_LR_To_5[-To_5_folds[[8]],-c(1,26,29,33)]$Response) 
(LR_To_5_fld8_Acc = LR_To_5_fld8_CM$overall[[1]]) 

# fold 9
LR_To_5_fld9 <- glm(Response~., 
                    data=training_LR_To_5[To_5_folds[[9]],-c(1,26,29,33)],
                    family=binomial(link=logit))
LR_To_5_fld9_pred <- predict(LR_To_5_fld9, 
                             newdata=training_LR_To_5[-To_5_folds[[9]],-c(1,26,29,33)], 
                             type = "response")
LR_To_5_fld9_pred1.1 <- LR_To_5_fld9_pred
LR_To_5_fld9_pred1.1[which(LR_To_5_fld9_pred<0.5)] <- 0
LR_To_5_fld9_pred1.1[which(LR_To_5_fld9_pred>=0.5)] <- 1
LR_To_5_fld9_CM = confusionMatrix(factor(LR_To_5_fld9_pred1.1), 
                                  training_LR_To_5[-To_5_folds[[9]],-c(1,26,29,33)]$Response) 
(LR_To_5_fld9_Acc = LR_To_5_fld9_CM$overall[[1]]) 

# fold 10
LR_To_5_fld10 <- glm(Response~., 
                     data=training_LR_To_5[To_5_folds[[10]],-c(1,26,29,33)],
                     family=binomial(link=logit))
LR_To_5_fld10_pred <- predict(LR_To_5_fld10, 
                              newdata=training_LR_To_5[-To_5_folds[[10]], -c(1,26,29,33)], 
                              type = "response")
LR_To_5_fld10_pred1.1 <- LR_To_5_fld10_pred
LR_To_5_fld10_pred1.1[which(LR_To_5_fld10_pred<0.5)] <- 0
LR_To_5_fld10_pred1.1[which(LR_To_5_fld10_pred>=0.5)] <- 1
LR_To_5_fld10_CM = confusionMatrix(factor(LR_To_5_fld10_pred1.1), 
                                   training_LR_To_5[-To_5_folds[[10]],-c(1,26,29,33)]$Response) 
(LR_To_5_fld10_Acc = LR_To_5_fld10_CM$overall[[1]]) 

###
LR_To_5_Acc = c(LR_To_5_fld1_Acc, LR_To_5_fld2_Acc, LR_To_5_fld3_Acc, 
                LR_To_5_fld4_Acc, LR_To_5_fld5_Acc, LR_To_5_fld6_Acc,
                LR_To_5_fld7_Acc, LR_To_5_fld8_Acc, LR_To_5_fld9_Acc, LR_To_5_fld10_Acc)
(LR_To_5_meanAcc = mean(LR_To_5_Acc))
# 0.9005

##################################################
which(LR_To_5_Acc == max(LR_To_5_Acc)) # 5 
summary(LR_To_5_fld5)
# significant predictors: all
summary(LR_To_5_fld10) # lu5_dist, lu8_dist

########################################################################################
# Final Model
########################################################################################
# Construct final model
set.seed(123)
LR_To_5_final = glm(Response~lu5_dist+lu8_dist, 
                    data=training_LR_To_5[To_5_folds[[5]],-c(1,26,29,33)],
                    family=binomial(link=logit))
summary(LR_To_5_final)
# remove lu8_dist
LR_To_5_final2 = glm(Response~lu5_dist, 
                     data=training_LR_To_5[To_5_folds[[5]],-c(1,26,29,33)],
                     family=binomial(link=logit))
summary(LR_To_5_final2)
anova(LR_To_5_final, LR_To_5_final2, test="Chisq") # prefer final2

LR_To_5_final2_pred <- predict(LR_To_5_final2, newdata=testing_LR_To_5[,-c(1,26,29,33)],type="response")
LR_To_5_final2_pred2 <- LR_To_5_final2_pred
LR_To_5_final2_pred2[which(LR_To_5_final2_pred<0.5)] <- 0
LR_To_5_final2_pred2[which(LR_To_5_final2_pred>=0.5)] <- 1
confusionMatrix(factor(LR_To_5_final2_pred2),factor(testing_LR_To_5$Response)) # 0.9119

