0
votes

I have tried to create a SVM Model with Linear Kernel in R

Here is the code:

library(e1071)

svm.narrow.margin <- svm(Diagnosis~., 
                 data = biomed,
                 type = "C-classification",
                 cost = 1.0,
                 kernel = "linear")

However it returns this error message:

Error in if (any(as.integer(y) != y)) stop("dependent variable has to be of factor or integer type for classification mode.") : missing value where TRUE/FALSE needed In addition: Warning message: In svm.default(x, y, scale = scale, ..., na.action = na.action) : NAs introduced by coercion

I ran the same set of codes on R Studio Cloud and it works fine which is confusing.

1

1 Answers

0
votes

Let's try to recreate the problem and walk through the solution.

This works:

 svm_works <- svm(Species~., data = iris, type = "C-classification", cost = 1.0, 
             kernel = "linear")

> svm_works

Call:
svm(formula = Species ~ ., data = iris, type = "C-classification", cost = 1, 
    kernel = "linear")


Parameters:
   SVM-Type:  C-classification 
 SVM-Kernel:  linear 
       cost:  1 

Number of Support Vectors:  29

The outcome for SVM must be a classifier, or a factor in R terms, like Species.

> str(iris)
'data.frame':	150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

Let's see what happens when we change the predictor to a non-factor variable. This will produce your error.

#Change predictor to non-factor, like Sepal.Length

> svm_not_work <- svm(Sepal.Length~., data = iris, type = "C-classification", 
 cost = 1.0, kernel = "linear")
            
Error in svm.default(x, y, scale = scale, ..., na.action = na.action) : 
  dependent variable has to be of factor or integer type for classification mode.

So likely your classifier, or predictor, or your y in the formula (y~., data=data) (all of those are synonyms) has a problem.