I am trying to manually replicate the predictions of kernlabs SVM (polynomial & radial kernel) using caret. Here is the code to fit the model:
fitControlSVM <- trainControl(
method = "repeatedcv",
number = 5,
repeats = 5,
summaryFunction = twoClassSummary,
classProbs = TRUE,
allowParallel = TRUE)
data("PimaIndiansDiabetes2", package = "mlbench")
pima.data <- na.omit(PimaIndiansDiabetes2)
set.seed(1)
svm_poly <- train(diabetes ~ .,
data = pima.data,
method = "svmPoly",
trControl = fitControlSVM,
metric = "Spec",
preProcess = c("center","scale"),
tuneLength = 3) # tunelength creates a grid
Given the fit, I can extract the coefficients & xmat of the "best" model:
coefs <- svm_poly$finalModel@coef[[1]]
xmat <- svm_poly$finalModel@xmatrix[[1]]
and quickly get the coefficient for each predictor:
(predictor <- coefs %*% xmat)
given the svm_poly$finalModel, I further get the hyperparameter:
SV type: C-svc (classification)
parameter : cost C = 0.25
Polynomial kernel function.
Hyperparameters : degree = 1 scale = 0.01 offset = 1
But I just don't know/understand how to calculate the prediction myself, as I am not that confident with SVM to be honest... So I am trying to replicate manually:
predict(svm_poly, pima.data[,-9], type ="prob")
Thanks