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

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

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

# fold 1
set.seed(123)
LR_To_1_fld1 <- glm(Response~., 
                    data=training_LR_To_1[To_1_folds[[1]],-c(1,26:29,33)],
                    family=binomial(link=logit))
LR_To_1_fld1_pred <- predict(LR_To_1_fld1, 
                             newdata=training_LR_To_1[-To_1_folds[[1]],-c(1,26:29,33)], 
                             type = "response")
LR_To_1_fld1_pred1.1 <- LR_To_1_fld1_pred
LR_To_1_fld1_pred1.1[which(LR_To_1_fld1_pred<0.5)] <- 0
LR_To_1_fld1_pred1.1[which(LR_To_1_fld1_pred>=0.5)] <- 1
LR_To_1_fld1_CM = confusionMatrix(factor(LR_To_1_fld1_pred1.1), 
                                  training_LR_To_1[-To_1_folds[[1]],-c(1,26:29,33)]$Response) 
(LR_To_1_fld1_Acc = LR_To_1_fld1_CM$overall[[1]]) # 0.704918

# fold 2
LR_To_1_fld2 <- glm(Response~., 
                    data=training_LR_To_1[To_1_folds[[2]],-c(1,26:29,33)],
                    family=binomial(link=logit))
LR_To_1_fld2_pred <- predict(LR_To_1_fld2, 
                             newdata=training_LR_To_1[-To_1_folds[[2]],-c(1,26:29,33)], 
                             type = "response")
LR_To_1_fld2_pred1.1 <- LR_To_1_fld2_pred
LR_To_1_fld2_pred1.1[which(LR_To_1_fld2_pred<0.5)] <- 0
LR_To_1_fld2_pred1.1[which(LR_To_1_fld2_pred>=0.5)] <- 1
LR_To_1_fld2_CM = confusionMatrix(factor(LR_To_1_fld2_pred1.1), 
                                  training_LR_To_1[-To_1_folds[[2]],-c(1,26:29,33)]$Response) 
(LR_To_1_fld2_Acc = LR_To_1_fld2_CM$overall[[1]]) # 0.63333

# fold 3
LR_To_1_fld3 <- glm(Response~., 
                    data=training_LR_To_1[To_1_folds[[3]],-c(1,26:29,33)],
                    family=binomial(link=logit))
LR_To_1_fld3_pred <- predict(LR_To_1_fld3, 
                             newdata=training_LR_To_1[-To_1_folds[[3]],-c(1,26:29,33)], 
                             type = "response")
LR_To_1_fld3_pred1.1 <- LR_To_1_fld3_pred
LR_To_1_fld3_pred1.1[which(LR_To_1_fld3_pred<0.5)] <- 0
LR_To_1_fld3_pred1.1[which(LR_To_1_fld3_pred>=0.5)] <- 1
LR_To_1_fld3_CM = confusionMatrix(factor(LR_To_1_fld3_pred1.1), 
                                  training_LR_To_1[-To_1_folds[[3]],-c(1,26:29,33)]$Response) 
(LR_To_1_fld3_Acc = LR_To_1_fld3_CM$overall[[1]]) # 0.6721311

# fold 4
LR_To_1_fld4 <- glm(Response~., 
                    data=training_LR_To_1[To_1_folds[[4]],-c(1,26:29,33)],
                    family=binomial(link=logit))
LR_To_1_fld4_pred <- predict(LR_To_1_fld4, 
                             newdata=training_LR_To_1[-To_1_folds[[4]],-c(1,26:29,33)], 
                             type = "response")
LR_To_1_fld4_pred1.1 <- LR_To_1_fld4_pred
LR_To_1_fld4_pred1.1[which(LR_To_1_fld4_pred<0.5)] <- 0
LR_To_1_fld4_pred1.1[which(LR_To_1_fld4_pred>=0.5)] <- 1
LR_To_1_fld4_CM = confusionMatrix(factor(LR_To_1_fld4_pred1.1), 
                                  training_LR_To_1[-To_1_folds[[4]],-c(1,26:29,33)]$Response) 
(LR_To_1_fld4_Acc = LR_To_1_fld4_CM$overall[[1]]) # 0.7377049

# fold 5
LR_To_1_fld5 <- glm(Response~., 
                    data=training_LR_To_1[To_1_folds[[5]],-c(1,26:29,33)],
                    family=binomial(link=logit))
LR_To_1_fld5_pred <- predict(LR_To_1_fld5, 
                             newdata=training_LR_To_1[-To_1_folds[[5]],-c(1,26:29,33)], 
                             type = "response")
LR_To_1_fld5_pred1.1 <- LR_To_1_fld5_pred
LR_To_1_fld5_pred1.1[which(LR_To_1_fld5_pred<0.5)] <- 0
LR_To_1_fld5_pred1.1[which(LR_To_1_fld5_pred>=0.5)] <- 1
LR_To_1_fld5_CM = confusionMatrix(factor(LR_To_1_fld5_pred1.1), 
                                  training_LR_To_1[-To_1_folds[[5]],-c(1,26:29,33)]$Response) 
(LR_To_1_fld5_Acc = LR_To_1_fld5_CM$overall[[1]]) # 0.8196721

# fold 6
LR_To_1_fld6 <- glm(Response~., 
                    data=training_LR_To_1[To_1_folds[[6]],-c(1,26:29,33)],
                    family=binomial(link=logit))
LR_To_1_fld6_pred <- predict(LR_To_1_fld6, 
                             newdata=training_LR_To_1[-To_1_folds[[6]],-c(1,26:29,33)], 
                             type = "response")
LR_To_1_fld6_pred1.1 <- LR_To_1_fld6_pred
LR_To_1_fld6_pred1.1[which(LR_To_1_fld6_pred<0.5)] <- 0
LR_To_1_fld6_pred1.1[which(LR_To_1_fld6_pred>=0.5)] <- 1
LR_To_1_fld6_CM = confusionMatrix(factor(LR_To_1_fld6_pred1.1), 
                                  training_LR_To_1[-To_1_folds[[6]],-c(1,26:29,33)]$Response) 
(LR_To_1_fld6_Acc = LR_To_1_fld6_CM$overall[[1]]) # 0.6393443

# fold 7
LR_To_1_fld7 <- glm(Response~., 
                    data=training_LR_To_1[To_1_folds[[7]],-c(1,26:29,33)],
                    family=binomial(link=logit))
LR_To_1_fld7_pred <- predict(LR_To_1_fld7, 
                             newdata=training_LR_To_1[-To_1_folds[[7]],-c(1,26:29,33)], 
                             type = "response")
LR_To_1_fld7_pred1.1 <- LR_To_1_fld7_pred
LR_To_1_fld7_pred1.1[which(LR_To_1_fld7_pred<0.5)] <- 0
LR_To_1_fld7_pred1.1[which(LR_To_1_fld7_pred>=0.5)] <- 1
LR_To_1_fld7_CM = confusionMatrix(factor(LR_To_1_fld7_pred1.1), 
                                  training_LR_To_1[-To_1_folds[[7]],-c(1,26:29,33)]$Response) 
(LR_To_1_fld7_Acc = LR_To_1_fld7_CM$overall[[1]]) # 0.7258065

# fold 8
LR_To_1_fld8 <- glm(Response~., 
                    data=training_LR_To_1[To_1_folds[[8]],-c(1,26:29,33)],
                    family=binomial(link=logit))
LR_To_1_fld8_pred <- predict(LR_To_1_fld8, 
                             newdata=training_LR_To_1[-To_1_folds[[8]],-c(1,26:29,33)], 
                             type = "response")
LR_To_1_fld8_pred1.1 <- LR_To_1_fld8_pred
LR_To_1_fld8_pred1.1[which(LR_To_1_fld8_pred<0.5)] <- 0
LR_To_1_fld8_pred1.1[which(LR_To_1_fld8_pred>=0.5)] <- 1
LR_To_1_fld8_CM = confusionMatrix(factor(LR_To_1_fld8_pred1.1), 
                                  training_LR_To_1[-To_1_folds[[8]],-c(1,26:29,33)]$Response) 
(LR_To_1_fld8_Acc = LR_To_1_fld8_CM$overall[[1]]) # 0.5409836

# fold 9
LR_To_1_fld9 <- glm(Response~., 
                    data=training_LR_To_1[To_1_folds[[9]],-c(1,26:29,33)],
                    family=binomial(link=logit))
LR_To_1_fld9_pred <- predict(LR_To_1_fld9, 
                             newdata=training_LR_To_1[-To_1_folds[[9]],-c(1,26:29,33)], 
                             type = "response")
LR_To_1_fld9_pred1.1 <- LR_To_1_fld9_pred
LR_To_1_fld9_pred1.1[which(LR_To_1_fld9_pred<0.5)] <- 0
LR_To_1_fld9_pred1.1[which(LR_To_1_fld9_pred>=0.5)] <- 1
LR_To_1_fld9_CM = confusionMatrix(factor(LR_To_1_fld9_pred1.1), 
                                  training_LR_To_1[-To_1_folds[[9]],-c(1,26:29,33)]$Response) 
(LR_To_1_fld9_Acc = LR_To_1_fld9_CM$overall[[1]]) # 0.65

# fold 10
LR_To_1_fld10 <- glm(Response~., 
                    data=training_LR_To_1[To_1_folds[[10]],-c(1,26:29,33)],
                    family=binomial(link=logit))
LR_To_1_fld10_pred <- predict(LR_To_1_fld10, 
                             newdata=training_LR_To_1[-To_1_folds[[10]],-c(1,26:29,33)], 
                             type = "response")
LR_To_1_fld10_pred1.1 <- LR_To_1_fld10_pred
LR_To_1_fld10_pred1.1[which(LR_To_1_fld10_pred<0.5)] <- 0
LR_To_1_fld10_pred1.1[which(LR_To_1_fld10_pred>=0.5)] <- 1
LR_To_1_fld10_CM = confusionMatrix(factor(LR_To_1_fld10_pred1.1), 
                                  training_LR_To_1[-To_1_folds[[10]],-c(1,26:29,33)]$Response) 
(LR_To_1_fld10_Acc = LR_To_1_fld10_CM$overall[[1]]) # 0.7377049

LR_To_1_Acc = c(LR_To_1_fld1_Acc, LR_To_1_fld2_Acc, LR_To_1_fld3_Acc, 
                LR_To_1_fld4_Acc, LR_To_1_fld5_Acc, LR_To_1_fld6_Acc,
                LR_To_1_fld7_Acc, LR_To_1_fld8_Acc, LR_To_1_fld9_Acc, LR_To_1_fld10_Acc)
(LR_To_1_meanAcc = mean(LR_To_1_Acc))
# 0.6861599
which(LR_To_1_Acc == max(LR_To_1_Acc)) # 5
# significant predictors:
# MRoad_dist + lu4_dist + lu8_dist + lu2006 + lc2006 + lc2010

########################################################################################
# Final Model
########################################################################################
# Construct final model
set.seed(123)
LR_To_1_Final <- glm(Response~MRoad_dist+lu4_dist+lu8_dist+lu2006+lc2006+lc2010, 
                     data=training_LR_To_1[To_1_folds[[5]],-c(1,26:29,33)],
                     family=binomial(link=logit))

summary(mod_LR_To_1_Final)
LR_To_1_Final_pred <- predict(LR_To_1_Final, newdata=testing_LR_To_1[,-c(1,2,26:29,33)],type="response")
LR_To_1_Final_pred2 <- LR_To_1_Final_pred
LR_To_1_Final_pred2[which(LR_To_1_Final_pred<0.5)] <- 0
LR_To_1_Final_pred2[which(LR_To_1_Final_pred>=0.5)] <- 1
confusionMatrix(factor(LR_To_1_Final_pred2),factor(testing_LR_To_1$Response)) # 0.682

# remove lu4_dist
set.seed(123)
LR_To_1_Final2 <- glm(Response~MRoad_dist+lu8_dist+lu2006+lc2006+lc2010, 
                      data=training_LR_To_1[To_1_folds[[5]],-c(1,26:29,33)],
                      family=binomial(link=logit))

summary(LR_To_1_Final2)
LR_To_1_Final2_pred <- predict(LR_To_1_Final2, newdata=testing_LR_To_1[,-c(1,2,26:29,33)],type="response")
LR_To_1_Final2_pred2 <- LR_To_1_Final2_pred
LR_To_1_Final2_pred2[which(LR_To_1_Final2_pred<0.5)] <- 0
LR_To_1_Final2_pred2[which(LR_To_1_Final2_pred>=0.5)] <- 1
confusionMatrix(factor(LR_To_1_Final2_pred2),factor(testing_LR_To_1$Response)) # 0.6897


# remove MRoad_dist
set.seed(123)
LR_To_1_Final3 <- glm(Response~lu8_dist+lu2006+lc2006+lc2010, 
                      data=training_LR_To_1[To_1_folds[[5]],-c(1,26:29,33)],
                      family=binomial(link=logit))

summary(LR_To_1_Final3)
LR_To_1_Final3_pred <- predict(LR_To_1_Final3, newdata=testing_LR_To_1[,-c(1,2,26:29,33)],type="response")
LR_To_1_Final3_pred2 <- LR_To_1_Final3_pred
LR_To_1_Final3_pred2[which(LR_To_1_Final3_pred<0.5)] <- 0
LR_To_1_Final3_pred2[which(LR_To_1_Final3_pred>=0.5)] <- 1
confusionMatrix(factor(LR_To_1_Final3_pred2),factor(testing_LR_To_1$Response)) # 0.6705

anova(LR_To_1_Final,LR_To_1_Final2, test="Chisq") 
anova(LR_To_1_Final2, LR_To_1_Final3, test="Chisq")
anova(LR_To_1_Final, LR_To_1_Final3, test="Chisq")

# final3 is better
