Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVM Classification using R - Variable Length Differ Error

Tags:

r

svm

data-mining

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.

like image 978
shakthydoss Avatar asked May 30 '26 20:05

shakthydoss


1 Answers

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 
like image 112
Andrie Avatar answered Jun 01 '26 20:06

Andrie