shakthydoss
shakthydoss

Reputation: 2631

SVM Classification using R - Variable Length Differ Error

I am currently working in SVM Classification problem with help of packages available in R.

Example code given in this website work fine. http://en.wikibooks.org/wiki/Data_Mining_Algorithms_In_R/Classification/SVM

But when trying the same program with different data set I get variable lengths differ error. Here is my code.

library(MASS)
library(e1071)
data <- ChickWeight
data <- data[-3]  # removing unwanted column  
tune.svm(data$Diet~., data = data , gamma = 10^(-6:-1) , cost=10^(-1:1))

Error.

 Error in model.frame.default(formula, data) : 
 variable lengths differ (found for 'weight')

I tried googling about the error but I could find the proper fix or why this error is getting produced.

Please let know what is going wrong.

Upvotes: 3

Views: 3939

Answers (2)

Mo_Fahad
Mo_Fahad

Reputation: 1

You probably removed the:

'Diet'(target)

column before passing it to svm.

Upvotes: 0

Andrie
Andrie

Reputation: 179428

Your formula should include the columns only, without the data frame (and the $ operator). Try this:

library(MASS)
library(e1071)
tune.svm(Diet~., data = ChickWeight[-3] , gamma = 10^(-6:-1) , cost=10^(-1:1))

The results:

Parameter tuning of ‘svm’:

- sampling method: 10-fold cross validation 

- best parameters:
 gamma cost
   0.1   10

- best performance: 0.5641561 

Upvotes: 5

Related Questions