Model Construction

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

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

save(bag_model, file = "dataset\\model\\bag.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\\bag.model_kfoldCV.Rdata")

bag.predictions <- predict(bag_model, newdata = test)

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

          Reference
Prediction   0   1
         0 920  30
         1  29 209
                                         
               Accuracy : 0.9503         
                 95% CI : (0.9364, 0.962)
    No Information Rate : 0.7988         
    P-Value [Acc > NIR] : <2e-16         
                                         
                  Kappa : 0.8452         
                                         
 Mcnemar's Test P-Value : 1              
                                         
            Sensitivity : 0.9694         
            Specificity : 0.8745         
         Pos Pred Value : 0.9684         
         Neg Pred Value : 0.8782         
             Prevalence : 0.7988         
         Detection Rate : 0.7744         
   Detection Prevalence : 0.7997         
      Balanced Accuracy : 0.9220         
                                         
       'Positive' Class : 0              
                                         
Show/Hide Code
bag.predictions <- as.numeric(bag.predictions)

pred_obj <- prediction(bag.predictions, test$good)
auc_val <- performance(pred_obj, "auc")@y.values[[1]]
auc_val
[1] 0.9219593
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 = "Bagging (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
bag.kfoldCV.ROC.plot <- recordPlot()

pander::pander(bag_model$results)
parameter Accuracy Kappa AccuracySD KappaSD
none 0.8085 0.3562 0.02525 0.09035

Summary

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

Model Error Rate Sensitivity Specificity AUC
Bagging 0.0497 0.9694 0.8745 0.9219593