0

I have built a SVM model using the R package e1071 and the iris data set. I have split up the iris data into a training and test data and built the SVM from the training data. Now I would like to plot the test data into the same plot that show the training data and the SVM boundaries using the points function. When I do this, however, quite a few of the test data points end up outside to the right of the plot, which shouldn't be possible considering the plot scale and the actual values of the test data. Here is the code I used:

# Import e1071 package and iris data set
library(e1071)
data("iris")

# Split the data into training and test data
index <- c(1:35, 51:85, 101:135)
iris_training <- iris[index, ]
iris_test <- iris[-index, ]

# Compute SVM model with linear kernel
svm_model <- svm(Species ~ Petal.Length + Petal.Width, 
                 data=iris_training,
                 kernel='linear')

# Plot the data and decision boundaries
plot(svm_model, 
     data = iris_training[ , c(3, 4, 5)],
     xlim = c(0.05, 2.55),
     ylim = c(0.9, 7), 
     svSymbol = 1, 
     dataSymbol = 1, 
     symbolPalette = c("black", "blue", "red"))

# Plot the test data into the same plot
points(iris_test$Petal.Width, 
       iris_test$Petal.Length,
       pch="x")

Any ideas how to fix this or how to work around this problem? Help greatly appreciated.

oli
  • 23
  • 6

1 Answers1

0

Ok so this kind of stuff is right up my alley, but I have been looking at this every way possible and have nothing real to explain this. BUT, I figured out a way to maybe get the figure LOOKING correct if that's what you need. (Still doesn't explain why this error is happening.) I did: iris_test$Petal.Width[31:45] <- iris_test$Petal.Width[31:45] - 0.3 and plotted. And it gets you the figure you need. I'm really not sure why this is necessary. I adjusted everything I could think of and got nothing.

jwells
  • 92
  • 2
  • 11
  • 1
    Thanks a lot @jwells. I need the plot for a presentation, so the fix you suggested will solve my problem. BTW, I had to subtract 0.7 instead of 0.3 to make the plot look right. Not sure if this provides any new clues to understand this plotting behaviour. But it seems that this could be an issue that someone of the R developers might want to have a look at. Thanks again for the help. – oli Mar 26 '17 at 19:10