BH <- read.csv("BostonHousing Long.csv")
BH_subset <- BH[, c("CHAS", "RAD", "CAT.MEDV")]
BH_subset[] <- lapply(BH_subset, factor)

set.seed(1234)
train_index <- sample(1:nrow(BH_subset), 0.6 * nrow(BH_subset))
train_data <- BH_subset[train_index, ]
test_data <- BH_subset[-train_index, ]
 


library(e1071)


Naive_Bayes <- naiveBayes(CAT.MEDV ~ ., data = train_data)


table(train_data$CHAS, train_data$CAT.MEDV)
##    
##       0   1
##   0 121 161
##   1   7  14
highCHAS <- sum(train_data$CHAS == 1 & train_data$CAT.MEDV == 1)


num_CHAS <- sum(train_data$CHAS == 1)


highCHAS <- highCHAS / num_CHAS

highCHAS
## [1] 0.6666667
value_not_CHAS <- sum(train_data$CHAS == 0 & train_data$CAT.MEDV == 1)


num_not_CHAS <- sum(train_data$CHAS == 0)


value_given_not_CHAS <- value_not_CHAS / num_not_CHAS


value_given_not_CHAS
## [1] 0.570922
predictions <- predict(Naive_Bayes, newdata = test_data)

predictions[10]
## [1] 1
## Levels: 0 1
test_data[10,]
##    CHAS RAD CAT.MEDV
## 16    0   4        0
library(caret)
## Loading required package: ggplot2
## Loading required package: lattice
conf_matrix <- confusionMatrix(data = as.factor(predictions), reference = as.factor(test_data$CAT.MEDV))

conf_matrix
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction   0   1
##          0  32  16
##          1  55 100
##                                           
##                Accuracy : 0.6502          
##                  95% CI : (0.5804, 0.7157)
##     No Information Rate : 0.5714          
##     P-Value [Acc > NIR] : 0.01337         
##                                           
##                   Kappa : 0.2435          
##                                           
##  Mcnemar's Test P-Value : 6.49e-06        
##                                           
##             Sensitivity : 0.3678          
##             Specificity : 0.8621          
##          Pos Pred Value : 0.6667          
##          Neg Pred Value : 0.6452          
##              Prevalence : 0.4286          
##          Detection Rate : 0.1576          
##    Detection Prevalence : 0.2365          
##       Balanced Accuracy : 0.6149          
##                                           
##        'Positive' Class : 0               
## 
error_rate <- round(1 - conf_matrix$overall['Accuracy'], 2)

error_rate
## Accuracy 
##     0.35
sensitivity <- round(conf_matrix$byClass['Sensitivity'], 2)

sensitivity
## Sensitivity 
##        0.37
specificity <- round(conf_matrix$byClass['Specificity'], 2)

specificity
## Specificity 
##        0.86
mode_value <- as.numeric(names(sort(table(train_data$CAT.MEDV), decreasing = TRUE)[1]))


mode_value
## [1] 1
library(pROC)
## Type 'citation("pROC")' for a citation.
## 
## Attaching package: 'pROC'
## The following objects are masked from 'package:stats':
## 
##     cov, smooth, var
roc_curve <- roc(response = test_data$CAT.MEDV, predictor = as.numeric(predictions))
## Setting levels: control = 0, case = 1
## Setting direction: controls < cases
auc <- auc(roc_curve)

auc
## Area under the curve: 0.6149
roc_curve <- roc(response = test_data$CAT.MEDV, predictor = as.numeric(predictions))
## Setting levels: control = 0, case = 1
## Setting direction: controls < cases
plot(roc_curve, main = "ROC Curve", col = "blue")

roc_curve <- roc(response = test_data$CAT.MEDV, predictor = as.numeric(predictions))
## Setting levels: control = 0, case = 1
## Setting direction: controls < cases
plot(roc_curve, main = "Lift Chart", col = "blue", print.auc = TRUE, print.auc.x = 0.5, print.auc.y = 0.5, lwd = 2)