library(e1071), tune 变量长度不同

library(e1071), tune Variable lengths differ

我一直在尝试使用 iris 数据集,虽然我已经 svme1071 库中工作,但我一直收到 'variable lengths differ' 错误当我尝试让 tune 工作时:

library(e1071)  

data <- data.frame(iris$Sepal.Width,iris$Petal.Length,iris$Species)
svm_tr <- data[sample(nrow(datasvm), 100), ] #sample 100 random rows

tuned <- tune(svm, svm_tr$iris.Species~.,
              data = svm_tr[1:2],
              kernel = "linear",
              ranges = list(cost=c(.001,.01,.1,1,10,100)))

我检查了 svm_tr[1:2] 中每一列的长度,它们的长度相同。我知道该函数不会直接获取数据框,但也许我遗漏了什么?

我可以使用它:

tune(svm, iris.Species ~ ., data = svm_tr[1:3],
     kernel = "linear", ranges = list(cost=c(.001,.01,.1,1,10,100)))

如果它是一个公式接口,您不应该使用 $ 来引用变量,因为所有必需的变量都来自 data= 参数指定的对象。请注意,我还制作了 data=svm_tr[1:3] 而不是 1:2,以便包含 iris.Species 列。