Error in Plot, Formula Missing When Using Svm

Error in plot, formula missing when using svm

The problem is that in in your model, you have multiple covariates. The plot() will only run automatically if your data= argument has exactly three columns (one of which is a response). For example, in the ?plot.svm help page, you can call

data(cats, package = "MASS")
m1 <- svm(Sex~., data = cats)
plot(m1, cats)

So since you can only show two dimensions on a plot, you need to specify what you want to use for x and y when you have more than one to choose from

cplus<-cats
cplus$Oth<-rnorm(nrow(cplus))
m2 <- svm(Sex~., data = cplus)
plot(m2, cplus) #error
plot(m2, cplus, Bwt~Hwt) #Ok
plot(m2, cplus, Hwt~Oth) #Ok

So that's why you're getting the "Missing Formula" error.

There is another catch as well. The plot.svm will only plot continuous variables along the x and y axes. The contact-lenses data.frame has only categorical variables. The plot.svm function simply does not support this as far as I can tell. You'll have to decide how you want to summarize that information in your own visualization.

Plotting SVM Linear Separator in R

It appears that although svm() allows you to specify your input using either the default or formula method, plot.svm() only allows a formula method. Also, by only giving x to plot.svm(), you are not giving it all the info it needs. It also needs y.

Try this:

library(e1071)

x <- prcomp(iris[,1:4])$x[,1:2]
y <- iris[,5]

df <- data.frame(cbind(x[],y[]))

machine <- svm(y ~ PC1 + PC2, data=df)
plot(machine, data=df)

svmplot

SVM doesn't plot in R

You didn't get result because plot(x,y) requires that x and y are data points, whereas you use svmfit which is not data points.

Plotting svm() results can be done in several ways depending on your purpose. You can plot the fitted values against the actual values, both in training data. In another case, you can plot the predicted values from the svm model (that you built by using the training data) against the test data.

What I understand from your example is that you built a model to predict stroke variable, and you split the data into training and test, but in svmfit you used all data, not only the training data. So, I assume you want to plot the fitted stroke values from svmfit against the actual stroke values. If that's true, you can use, for example:

plot(svmfit$fitted, col = "red", pch = 17)
points(stroke$stroke, col = "blue", pch = 19)

This plot will show the fitted values of stroke resulted from svm in red points and the actual stroke values in blue points.
You can also use, for example:

plot(svmfit$fitted,stroke$stroke, col = c("red", "blue"), pch = c(17, 19))

You can use the same logic when you want to plot the predicted stroke data and the actual data from the test dataset.

Here is a simple example by using iris data:

svmfit.iris <- svm(Sepal.Length ~ Petal.Length + Petal.Width + Species, 
data = iris, kernel = "linear", cost = 10, scale = FALSE)
plot(svmfit.iris$fitted, iris$Sepal.Length, col = c("red", "blue"), pch = c(17,19))

Sample Image

Unable to draw svm plot. Error in terms.default(x) : no terms component nor attribute

from ?formula

The models fit by, e.g., the lm and glm functions are specified in a compact symbolic
form. The ~ operator is basic in the formation of such models. An expression of the form y
~ model is interpreted as a specification that the response y is modelled by a linear
predictor specified symbolically by model. Such a model consists of a series of terms separated by + operators. The terms themselves consist of variable and factor names.

Using formula with variable names fixs it...a fake example follows

library(e1071)
fulldata <- data.frame("label" = gl(2,150),
"V1" = rnorm(300),
"V2" = rnorm(300),
"V3" = rnorm(300),
"V4" = rnorm(300),
"V5" = rnorm(300),
"V6" = rnorm(300),
"V7" = rnorm(300),
"V8" = rnorm(300),
"V9" = rnorm(300),
"V10" = rnorm(300))

str(fulldata)

my.svm <- svm(label ~ .,
probability=TRUE,
na.rm=TRUE,
kernel="linear",
data = fulldata)
my.svm
plot(my.svm,
data = fulldata,
formula = V1 ~ V2)

Error in plotting SVM classification graph

Without being sure what exactly causes the problem, I would try to transform the Class column to a factor (so defining the type as C-classification will no longer be necessary) using something like this:

data$Class <- as.factor(data$Class)

or in one step:

model <- svm(as.factor(Class)~.,data, kernel = "linear")


Related Topics



Leave a reply



Submit