rsvmr-caretkernlab

Error with tuning custom SVM model in caret


I'm having trouble with my custom training model in the caret package. I need to do a SVM regression and I want to find all the parameters of the SVM model - cost, sigma and epsilon. The built-in version has only cost and sigma. I have already found quite a helpful tip here and here but my model still does not work.

Error in models$grid(x = x, y = y, len = tuneLength, search = trControl$search) : unused argument (search = trControl$search)

This error is the one I am getting and my code is here.

SVMrbf <- list(type = "Regression", library = "kernlab", loop = NULL)
prmrbf <- data.frame(parameters = data.frame(parameter = c('sigma', 'C', 'epsilon'),
                                         class = c("numeric", "numeric", "numeric"),
                                         label = c('Sigma', "Cost", "epsilon")))
SVMrbf$parameters <- prmrbf
svmGridrbf <- function(x, y, len = NULL) {
                  library(kernlab)
                  sigmas <- sigest(as.matrix(x), na.action = na.omit, scaled = TRUE, frac = 1)
                  expand.grid(sigma = mean(sigmas[-2]), epsilon = 10^(-5:0),
          C = 2 ^(-5:len)) # len = tuneLength in train
}
SVMrbf$grid <- svmGridrbf
svmFitrbf <- function(x, y, wts, param, lev, last, weights, classProbs, ...) {
                   ksvm(x = as.matrix(x), y = y,
                         type = "eps-svr",
                         kernel = "rbfdot",
                         sigma = param$sigma,
                         C = param$C, epsilon = param$epsilon,
                         prob.model = classProbs,
                         ...)
}
SVMrbf$fit <- svmFitrbf
svmPredrbf <- function(modelFit, newdata, preProc = NULL, submodels = NULL)
  predict(modelFit, newdata)
SVMrbf$predict <- svmPredrbf
svmProb <- function(modelFit, newdata, preProc = NULL, submodels = NULL)
  predict(modelFit, newdata, type="probabilities")
SVMrbf$prob <- svmProb
svmSortrbf <- function(x) x[order(x$C), ]
SVMrbf$sort <- svmSortrbf


svmRbfFit <- train(x = train.predictors1, y = train.response1, method =       SVMrbf,
                 tuneLength = 10)
svmRbfFit

I could not find anyone, who had the same error and have no clue what is actually wrong. This code is pretty much just something I found online and slightly altered.

BTW this is my first post, so hopefully it's understandable, if not I can add additional info.


Solution

  • The solution is to include an argument search into your grid function, for example with

    svmGridrbf <- function(x, y, len = NULL, search = "grid") {
                      library(kernlab)
                      sigmas <- sigest(as.matrix(x), na.action = na.omit, scaled = TRUE, frac = 1)
                      expand.grid(sigma = mean(sigmas[-2]), epsilon = 10^(-5:0), C = 2 ^(-5:len)) # len = tuneLength in train
    }
    

    If you look at the caret documentation for custom functions carefully, you'll see that caret wants you to specify how to select default parameters in case the user wants to do grid search and in case she wants to do random search (see "the grid element").

    The error message tells you that caret passes an argument to the function which is not actually defined as an argument for that function.

    This is probably easier to see here:

    sd(x = c(1,2,3), a = 2)
    # Error in sd(x = c(1, 2, 3), a = 2) : unused argument (a = 2)