Learn Support Vector Machine (SVM) from Scratch in R

Learn Support Vector Machine (SVM) from Scratch in R

# Setup for cross validationset.seed(123)ctrl <- trainControl(method=”cv”,                     number = 2,                     summaryFunction=twoClassSummary,                     classProbs=TRUE)# Grid search to fine tune SVMgrid <- expand.grid(sigma = c(.01, .015, 0.2),                    C = c(0.75, 0.9, 1, 1.1, 1.25))#Train SVMsvm.tune <- train(x=trainX,                  y= svm.train$Class,                  method = “svmRadial”,                  metric=”ROC”,                  tuneGrid = grid,                  trControl=ctrl)svm.tune# Predict Target LabelvalX <-svm.validate[,4:61]pred <- predict(svm.tune, valX, type=”prob”)[2]# Model Performance Statisticspred_val <-prediction(pred[,2], svm.validate$Class)# Calculating Area under Curveperf_val <- performance(pred_val,”auc”)perf_val# Calculating True Positive and False Positive Rateperf_val <- performance(pred_val, “tpr”, “fpr”)# Plot the ROC curveplot(perf_val, col = “green”, lwd = 1.5)#Calculating KS statisticsks <- max(attr(perf_val, “y.values”)[[1]] – (attr(perf_val, “x.values”)[[1]]))ks
1. AUC on validation =  0.9168
2. KS on validation = 0.68

Link: Learn Support Vector Machine (SVM) from Scratch in R