1

I am trying to get a plot similar to the one obtained in this post Plot SVM linear model trained by caret package in R

This code works if I run it on my console, but if I do it with my data it does not work, so I am wondering if it is a problem of my data. Here it is the example that does not work for me:

library(wakefield)
X <- r_sample_factor(c("low", "high"), n=232)
MAMAMA<-r_sample_factor(c("C/C", "C/G", "G/G"), n=232)
MEMEME<-r_sample_factor(c("C/C", "C/T", "T/T"), n=232)
MIMIMI<-r_sample_factor(c("A/A", "A/T", "T/T"), n=232)

datos<-data.frame(X,MAMAMA,MEMEME, MIMIMI)

sv<-caret::train(X~., datos, method="svmRadial", trControl= trainControl(method='cv', number=5))

kernlab::plot(sv$finalModel)
GFA
  • 25
  • 4

1 Answers1

1

There are two reasons why the plot works in the link page but not work in your case:

(1) In the link page, X only has two predictors, so it can be visualized on a 2-dimensional plane. In your case you have three categorical predictors, and if you dummy them there would be more predictors, I have also searched online but to date I haven't found any solution for kernlab::plot to deal with more than 2 predictors.

(2) In the link page, the code is using linear kernel, therefore it's possible to get an explicit expression for the hyperplane, but in your case, you are using radial kernel which maps predictors into an infinite feature space, thus hyperplane cannot be expressed explicitly in finite-dimension plot.

Xiang
  • 314
  • 1
  • 9