5

I wonder if there is a way to get all coefficients and p-values in svmLinear method from the e1071package. I tried summary(modelname) but that did not work. Below is the code for my svm model with 10-fold cross validation:

library("e1071")
library("caret")
load(df) ## my dataset
ctrl <- trainControl(method = "repeatedcv", number = 10, savePredictions = TRUE) ## 10 fold cross validation
fitsvm <- train(Attrition ~., data=df, method = "svmLinear", trControl = ctrl) ##train model

summary (fitsvm)

Length  Class   Mode 
 1      ksvm     S4 

I could get them with glm - logistic regression:

fit <- train(Attrition ~., data= df, method="glm", family="binomial", trControl= tc)
summary(fit)

                          Estimate   Std. Error  z value  Pr(>|z|)    
(Intercept)               3.424e+00  1.254e+00   2.731    0.006318 ** 

I'd be glad if someone can show me a way, thanks a lot!

Cettt
  • 11,460
  • 7
  • 35
  • 58
Cem Akyuz
  • 73
  • 2
  • 8
  • 3
    svmLinear in caret uses the kernlab package. To access ksvm class info you can use the fitsvm$finalmodel@coef for the coefficients. p-values are not calculated. – phiver Jan 15 '18 at 17:36
  • @phiver thanks a lot! I didn't know about kernlab. I tried the code you wrote but it gives me "Error: trying to get slot "coef" from an object of a basic class ("NULL") with no slots". – Cem Akyuz Jan 16 '18 at 10:44

1 Answers1

2

SVM does not assume a probabilistic model, so there are no Std-Errors or p. values.

You can get the coefficients, though. In the e1071 package, the alpha*y are stored in fit$coefs, and the Support-Vectors are stored in fit$SV. You have to be careful how to extract them. If you only have a binary classification, then the coefs of the separation plane b+w1*x1+w2*x2+...=0 are simply:

w = t(fit$SV) %*% fit$coefs
b = -fit$rho

If you only have 2d features, you can plot the separation-line using:

abline(-b/w[2], -w[1]/w[2])

It is a bit more tricky for multi-class. You can check my answer here for a detailed explanation how to extract w and b from the coefs and SV.

Maverick Meerkat
  • 5,737
  • 3
  • 47
  • 66