rsvmpredictkernlab

Looking into the predict function in R


I am trying to understand how the SVM predict function works when using command ksvm from R package kernlab.

I tried the look into the predict function using the following commands:

methods(class="ksvm")
getAnywhere(ksvm:::predict)

However, I get the following output and not the complete predict function:

A single object matching ‘:::’ ‘ksvm’ ‘predict’ was found
It was found in the following places
  package:base
  namespace:base
with value

function (pkg, name) 
{
    pkg <- as.character(substitute(pkg))
    name <- as.character(substitute(name))
    get(name, envir = asNamespace(pkg), inherits = FALSE)
}
<bytecode: 0x00000000088be4f8>
<environment: namespace:base>
Warning message:
In find(x, numeric = TRUE) :
  elements of 'what' after the first will be ignored

Can someone help with how to obtain the complete predict function?

Update 1:

Suggestion from misspelled worked fine on predict function for ksvm in kernlab package but doesn't seem to work on svm in e1071 package.

It throws the following error:

> getMethod("predict", "svm")
Error in getMethod("predict", "svm") : 
  no generic function found for 'predict'

In general, how to know which get method to use?


Solution

  • You were close. I was able to get the function code with getMethod("predict", "ksvm"). This answer describing S4 method dispatch was helpful. View source code for function

    Per your updated question, I can get the source code for predict.svm using the ::: function. Specifically with e1071:::predict.svm. The link above also describes this in the section on S3 method dispatch.

    There are at least a couple of things going on here. First is that in the former case you are dealing with S4 objects and S3 objects in the latter. The two systems have different method dispatches and different ways to view the source code. Another wrinkle is that the predict.svm function is an invisible function and can only be viewed either with ::: or getAnywhere().