Support Vectir Machine (SVM)

Model Construction

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

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

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

svm.predictions <- predict(svm_model, newdata = test)

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

          Reference
Prediction   0   1
         0 923 169
         1  26  70
                                          
               Accuracy : 0.8359          
                 95% CI : (0.8135, 0.8565)
    No Information Rate : 0.7988          
    P-Value [Acc > NIR] : 0.0006432       
                                          
                  Kappa : 0.342           
                                          
 Mcnemar's Test P-Value : < 2.2e-16       
                                          
            Sensitivity : 0.9726          
            Specificity : 0.2929          
         Pos Pred Value : 0.8452          
         Neg Pred Value : 0.7292          
             Prevalence : 0.7988          
         Detection Rate : 0.7769          
   Detection Prevalence : 0.9192          
      Balanced Accuracy : 0.6327          
                                          
       'Positive' Class : 0               
                                          
Show/Hide Code
svm.predictions <- as.numeric(svm.predictions)
pred_obj <- prediction(svm.predictions, test$good)
auc_val <- performance(pred_obj, "auc")@y.values[[1]]
auc_val
[1] 0.6327449
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 = "SVM (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
svm.kfoldCV.ROC.plot <- recordPlot()

pander::pander(svm_model$results)
sigma C Accuracy Kappa AccuracySD KappaSD
0.08153 0.25 0.8103 0.2517 0.01142 0.05454
0.08153 0.5 0.815 0.2901 0.01191 0.05751
0.08153 1 0.8179 0.3203 0.01671 0.06094

Summary

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

Model Error Rate Sensitivity Specificity AUC
SVM 0.1641 0.9726 0.2929 0.6327449