jmb277
jmb277

Reputation: 568

library(e1071), tune Variable lengths differ

I have been attempting to utilize the iris dataset and although I've gotten svm to work from the e1071 library, I keep getting a 'variable lengths differ' error when I attempt to make tune work:

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)))

I have checked the lengths of each of the columns in svm_tr[1:2] and they are the same length. I know the function doesn't take a dataframe directly but maybe I'm missing something?

Upvotes: 1

Views: 846

Answers (1)

thelatemail
thelatemail

Reputation: 93813

I can get it to work with:

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

If it's a formula interface you shouldn't be referring to a variable by using $ as all the required variables are sourced from the object specified by the data= argument. Note that I've also made data=svm_tr[1:3] instead of 1:2 so that the iris.Species column is included.

Upvotes: 2

Related Questions