Model Construction

Show/Hide Code
#--------------------#
#----Naive Bayes-----#
#--------------------#
set.seed(1234)
train_control <- trainControl(method = "cv", number = 10)

set.seed(1234)
nb_model <- train(good ~ ., 
               data = train, 
               method = "naive_bayes", 
               trControl = train_control)

save(nb_model, file = "dataset\\model\\nb.model_kfoldCV.Rdata")

K-fold CV

Show/Hide Code
# Data Import
load("dataset\\wine.data_cleaned.Rdata")
load("dataset\\train.Rdata")
load("dataset\\test.Rdata")

# Function Import
load("dataset\\function\\accu.kappa.plot.Rdata")

# Model import
load("dataset\\model\\nb.model_kfoldCV.Rdata")

nb.predictions <- predict(nb_model, newdata = test)

confusionMatrix(nb.predictions, test$good)
Confusion Matrix and Statistics

          Reference
Prediction   0   1
         0 743  87
         1 206 152
                                          
               Accuracy : 0.7534          
                 95% CI : (0.7278, 0.7776)
    No Information Rate : 0.7988          
    P-Value [Acc > NIR] : 0.9999          
                                          
                  Kappa : 0.3531          
                                          
 Mcnemar's Test P-Value : 5.438e-12       
                                          
            Sensitivity : 0.7829          
            Specificity : 0.6360          
         Pos Pred Value : 0.8952          
         Neg Pred Value : 0.4246          
             Prevalence : 0.7988          
         Detection Rate : 0.6254          
   Detection Prevalence : 0.6987          
      Balanced Accuracy : 0.7095          
                                          
       'Positive' Class : 0               
                                          
Show/Hide Code
nb.predictions <- as.numeric(nb.predictions)
pred_obj <- prediction(nb.predictions, test$good)
auc_val <- performance(pred_obj, "auc")@y.values[[1]]
auc_val
[1] 0.7094563
Show/Hide Code
roc_obj <- performance(pred_obj, "tpr", "fpr")
plot(roc_obj, colorize = TRUE, lwd = 2,
     xlab = "False Positive Rate", 
     ylab = "True Positive Rate",
     main = "Naive Bayes (10-fold CV)")
abline(a = 0, b = 1)
x_values <- as.numeric(unlist(roc_obj@x.values))
y_values <- as.numeric(unlist(roc_obj@y.values))
polygon(x = x_values, y = y_values, 
        col = rgb(0.3803922, 0.6862745, 0.9372549, alpha = 0.3),
        border = NA)
polygon(x = c(0, 1, 1), y = c(0, 0, 1), 
        col = rgb(0.3803922, 0.6862745, 0.9372549, alpha = 0.3),
        border = NA)
text(0.6, 0.4, paste("AUC =", round(auc_val, 4)))
Show/Hide Code
nb.kfoldCV.ROC.plot <- recordPlot()

pander::pander(nb_model$results)
usekernel laplace adjust Accuracy Kappa AccuracySD KappaSD
FALSE 0 1 0.7238 0.3583 0.02457 0.05065
TRUE 0 1 0.757 0.3707 0.03159 0.0819

Summary

Show/Hide Code
cowplot::plot_grid(nb.kfoldCV.ROC.plot)

Model Error Rate Sensitivity Specificity AUC
Naive Bayes 0.2466 0.7829 0.6360 0.7094563