rkernlab

Predict function for ksvm is not found?


I'm trying to use the function predict.ksvm from library kernlab in R. I have been reading the documentation at the following link: https://www.rdocumentation.org/packages/kernlab/versions/0.9-27/topics/predict.ksvm

The function ksvm is working, so it's just the predict function that is currently not working.

The code:

library(kernlab)
mySvm<-ksvm(x=as.matrix(train[,-4703]),y=train[,4703],kernel="vanilladot")
predSvm<-predict.ksvm(mySvm,newdata=test[,-4703])

The error:

Error in predict.ksvm(mySvm, newdata = test[, -4703]) : 
could not find function "predict.ksvm"

Solution

  • Try simply

    predSvm <- predict(mySvm, newdata = test[,-4703])
    

    It should work because mySvm is an object of class ksvm and an appropriate function method will be automatically chosen for it.

    When you write

    predSvm <- predict.ksvm(mySvm, newdata = test[,-4703])
    

    it doesn't work because the predict method for the ksvm class is somewhat hidden from you, pretends not to exist. If it were an S3 function, you could write kernlab:::predict.ksvm, but in this case it is an S4 function, so you need getMethod("predict", "ksvm") as to see the function.