Sal-laS
Sal-laS

Reputation: 11639

SVM:Need numeric dependent variable for regression

I have the following data

scorer<-function(points){
        points["scores"] <- as.vector((points$X-5)^2+(points$Y-5)^2-9)
        points["class"]<-(as.vector(  points$scores<0 ))
        points
}
dt<-scorer(data.frame(X=c(0,1,5,20,5,3,9,3,5,5),Y=c(0,9,9,0,-18,3,4,5,7,4)))

Then i am trying to predict the last column (class) using SVM

library(e1071)
model <- svm(class ~ . , dt)
predictedClass <- predict(model, dt)

but it complains with:

Error in svm.default(x, y, scale = scale, ..., na.action = na.action) : 
  Need numeric dependent variable for regression.

Upvotes: 3

Views: 10268

Answers (2)

lacine
lacine

Reputation: 11

With your dataset you can make classification using svm method.

But if you want absolutely to make regression, try to transform your variable "class" in numeric form which can take value 1 for negative score and 0 for positif score.

function(points) {

points["scores"] <- as.vector((points$X-5)^2+(points$Y-5)^2-9)
                   points["class"]<-as.vector(  ifelse(points$scores<0 ,1,0))
                   points
                 }
dt<-scorer(data.frame(X=c(0,1,`enter code here`5,20,5,3,9,3,5,5),Y=c(0,9,9,0,-18,3,4,5,7,4)))
svm(class~.,dt)

Upvotes: 1

Andrii
Andrii

Reputation: 3043

The advice from nya really works.

Please, have a look type parameter description

svm can be used as a classification machine, as a regression machine, or for novelty detection. Depending on whether y is a factor or not, the default setting for type is C-classification or eps-regression ... page 50

Upvotes: 4

Related Questions