分类图错误

时间:2018-08-16 21:19:28

标签: r ggplot2 plot classification svm

我正在尝试在R中绘制一个分类图,在该图中我得到找不到公式的错误。我尝试的代码:

# fit model
library(e1071)
svmfit <- svm(all~., data = sda, kernel = "radial", cost = 10, gamma = 1)
# plot results
plot(svmfit, sda)

> plot(svmfit, sda)# Error I am getting.
Error in plot.svm(svmfit, sda) : missing formula.

我期望的是这张图片Classification plot with one factor variable y and 2 other variables 中给出的内容 我的数据集“ sda”在第一列(“ all”)中包含五个因子,其余的还有其他9个数字列。 有人可以建议我如何绘制分类图。

我的数据格式是

  

sapply(sda,class)

>     all      Mean       Var      Skew       Kur       min       max 
 "factor" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" 
      IQR       med 
"numeric" "numeric"

1 个答案:

答案 0 :(得分:1)

如果数据sda具有两个以上的维,则必须通过公式告诉plot您要查看哪个维。例如,如果sda具有变量x和y,则可以编写

plot(svmfit, sda, y~x)

当然,您需要用数据中的实际变量名替换x和y。但是,如果仅指定x和y值,那么其他变量将使用什么?您还必须使用slice参数来指定。

帮助页面?plot.svm

上有一个很好的例子
m2 <- svm(Species~., data = iris)
plot(m2, iris, Petal.Width ~ Petal.Length,
     slice = list(Sepal.Width = 3, Sepal.Length = 4))

Iris plot