UseMethod("predict") : no applicable method for 'predict' applied to an object of class "train"

I think I found why this happened...The predict is a generic function from: stats package. I use the namespace ::-notation for invoking the functions from the caret package (that is the recommendation for creating a user packages) and the equivalent predict function from caret package is: predict.train, that is an internal function, that cannot be invoked by an external application. The only way to invoke this function, is using the generic predict function from stats package, then based on the class of the first input argument: predicted <- predict(fit, testData[-$Readmit]) it identifies the particular predict function will be invoked.

For this particular case the class of this function is train, so it would call actually the function: train.predict from caret package. This function also handles the particular function requested for prediction based on the algorithm (method) used, for example: predict.gbm or predict.glm, etc. It is explained, in detail, in the caret documentation section: "5.7 Extracting Predictions and Class Probabilities".

Therefore the ::-notation works well for other functions in the package, such as: caret.train for example, but not for this particular one: predict. In such cases it is necessary to explicitly load the library, so it internally can invoke predict.train function.

In short, the solution is just adding the following line before invoking the predict function:

library(caret)

Then error disappears.


Based on the answer from @David Leal, I tried loading library(caret) before calling the predict function but it did not help.

After trying a bit, I realized that I had to load the library that contains the model itself. In my case, I had to call library(kenlab) for Support Vectors.