Search code examples
rmachine-learningsvmr-caret

Least Square Support Vector Machine in Caret fail


I try to fit a Least Square Support Vector Machine vie the caret package in R, but I am unable to get it to work. Even for extreme simple examples like this it fails:

library(caret)
library(tidyverse)
data("iris")


#to make this example a binary classification task

iris <- iris %>% filter(Species %in% c("setosa", "versicolor")) %>%
    mutate(Species = droplevels(Species))

svmls <- train(Species ~ .,
               iris,
               method = "lssvmLinear",
               preProc = c("center", "scale")
               )

with a couple of warnings like this:

In eval(xpr, envir = envir) :
  model fit failed for Resample09: tau=0.0625 Error in if (truegain[k] < tol) break : 
  missing value where TRUE/FALSE needed

While calling the lssmv function from kernlab directly succeed:

library(kernlab)
svmls2 <- lssvm(Species~.,data=iris)
svmls2

I would really appreciate any guess on what might be wrong.


Solution

  • I knew the question already old, but here is some answer

    I also got the same error, when Look depth in it, The Caret Default of LSSVM Linear is using Polygonal Kernel, that looks like this:

    getModelInfo()$lssvmLinear$fit
    function(x, y, wts, param, lev, last, classProbs, ...) {
                        kernlab::lssvm(x = as.matrix(x), y = y,
                                       tau = param$tau,
                                       kernel = kernlab::polydot(degree = 1,
                                                                 scale = 1,
                                                                 offset = 1), ...)    
                      }
    

    hence I edited it to use only the default kernel, this way it can behave like what expected:

    newlssvm <- getModelInfo()$lssvmLinear
    newlssvm$fit <- function(x, y, wts, param, lev, last, classProbs, ...) {
      kernlab::lssvm(x = as.matrix(x), y = y,
                     tau = param$tau)    
    }
    
    svmls <- train(Species ~ .,
                   iris,
                   method = newlssvm,
                   preProc = c("center", "scale")
                   )
    

    I reclaimed that this issue was at kernlab, since this:

    lssvm(Species~.,data= iris, kernel = kernlab::polydot(degree = 2,
                                                          scale = 0.01, offset=1))
    

    give similiar errors like this:

    Error in if (truegain[k] < tol) break : 
      missing value where TRUE/FALSE needed
    In addition: Warning message:
    In sqrt(G[kadv, kadv]) : NaNs produced