snehita varma
snehita varma

Reputation: 21

Cannot plot graph for an SVM model in R

I'm using the plot function in R to visualize the results of support vector machine classification. But the function does not show an error and does not plot a graph either. Here is the code that I have been using.

library(e1071)
data = read.table(file.choose())
svm.model <- svm(MCQ160A~., data, cost=100, gamma=1)
plot(svm.model, data, MCQ160A~PAQ706+PAQ605)

Upvotes: 2

Views: 4322

Answers (2)

PleaseHelp
PleaseHelp

Reputation: 124

Adding an answer several years later in case someone stumbles upon this in the future. The reason that a plot isn't coming up is because you need to modify its formula. Your outcome cannot be part of the the equation and you can only have two independent variables.

Say you have an outcome y and multiple features x1, x2, ..., x10, the plot for svm represents a 2D image based off of 2 independent variables since your complex model is likely transformed on a multidimensional level.

See reproducible example below:

#Open iris dataset
attach(iris)

#create a model
svm_model <- svm(Species ~ ., data=iris)

summary(svm_model)

#Call:
svm(formula = Species ~ ., data = iris)

Parameters:
SVM-Type:  C-classification 
SVM-Kernel:  radial 
    cost:  1 
   gamma:  0.25 

Number of Support Vectors:  51
( 8 22 21 )
Number of Classes:  3 
Levels: 
setosa versicolor virginica

#plot svm using formula 
#plot(svm model, dataframe, independent variable 1 ~ independent variable 2)
plot(svm_model, iris, Sepal.Length~Petal.Length)

enter image description here

#another combo    
plot(svm_model, iris, Petal.Length~Sepal.Width)

enter image description here

You can use any two independent variables in your svm plot. Every plot should be different since they all map the outcome in different dimensions. Here's a good explanation for the multidimensional transformation of svm.

Upvotes: 2

IRTFM
IRTFM

Reputation: 263391

There is no function being applied to that model specification ... (yet).

Once you fix the missing code, there will probably still be the problem that you are specifying three variables in a formula that is only documented to handle two. You should look at the help page for plot.svm to see how to specify slices using third and (or fourth) variables.

Upvotes: 1

Related Questions