0

I'm using model and predict of library e1071, it seems to be working fine with training and test data, but not with a new data I'm providing. Code:

data2<-read.table('checked.bed')
data3<-read.csv('result_20_02.csv', header = FALSE)
index<-1:nrow(data2)
testindex<-sample(index,trunc(length(index)/3))
testset<-data2[testindex,]
trainset<-data2[-testindex,]
model1<-tune.svm(V3 ~ .,data=trainset,gamma=10^(-6:1),cost=10^(1:3))
bestGamma<-model1$best.parameters[[1]]
bestC<-model1$best.parameters[[2]]
svm.model<-svm(V3 ~ .,data=trainset,method='C-classification', kernel='radial',
           cost=bestC,gamma=bestGamma,verbose=TRUE,cross=10)
svm.pred<-predict(svm.model,testset[,-3],decision.values = TRUE)
svm.pred1<-predict(svm.model,data3[,-3],decision.values = TRUE)

svm.pred is working fine, but not svm.pred1. Error:

Error in scale.default(newdata[, object$scaled, drop = FALSE], center = object$x.scale$"scaled:center",  : length of 'center' must equal the number of columns of 'x'

I've checked both the tables (data3 and testset) have 5 columns and same type of data

Chhavi
  • 1
  • I can't really make out your code without sample data so I'm not sure if this is your issue, but I know that having the DV in your prediction dataset throws off a lot of `predict` functions. The dataset you predict on for an svm object must have only the independent variables from your training dataset - the `.` in `V3 ~ .`. – ila Jul 22 '15 at 14:20
  • You should include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data so we can run the code to see why you are getting the error. See the link for tips on including data in your question. – MrFlick Jul 22 '15 at 14:20
  • @Dr.MantisTobaggan Do you have examples where having the response variable in the predict data frame throws things off? I've never seen a predict() function have a problem with that before. – MrFlick Jul 22 '15 at 14:21
  • @MrFlick It's only an issue when using `predict()` on certain model objects. With `lm()` for example it doesn't cause an error, but I have run into various errors stemming from this when using caret model objects with methods of `"knn"`, `"svmLinear"`, and a few others. Edit: And I'm pretty sure caret's "svmLinear" object is borrowed from the package e1071 (which @Chhavi used), so the same thing could apply here. – ila Jul 22 '15 at 14:24

0 Answers0