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

I have a model ( fit ) based on historical information until the last month. Now I would like to predict the use of my model in the current month. When I try to call the following code:

 predicted <- predict(fit, testData[-$Readmit]) 

I get the following error:

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

Notes:

  • A matching model was created using the train function from caret using a random forest algorithm
  • predict is a generic function that will call a specific prediction function based on the first input argument. In my case it will be:

    >fit$modelInfo$label

    [1] "Random Forest"

Therefore, the called forecasting method will look like this: predict.randomForest . For more information, see [Carriage Documentation] [3].

Here is the source code for generating the model and calling it:

 # Script-1: create a model: fit <- train(testData[-$Readmit], testData$Readmit) saveRDS(fit, modelFileName) # save the fit object into a file # Script-2: predict fit <- readRDS(modelFileName) # Load the model (generated previously) predicted <- predict(fit, testData[-$Readmit]) 

Note The lead time for creating the model is about 3 hours, so I save the object for reuse after that.

A dataset from the training model in the form of the following structure:

 > str(fit$trainingData) 'data.frame': 29955 obs. of 27 variables: $ Acuity : Factor w/ 3 levels "Elective ","Emergency ",..: 2 2 2 1 1 2 2 2 1 1 ... $ AgeGroup : Factor w/ 10 levels "100-105","65-70",..: 8 6 9 9 5 4 9 2 3 2 ... $ IsPriority : int 0 0 0 0 0 0 0 0 0 0 ... $ QNXTReferToId : int 115 1703712 115 3690 1948 115 109 512 481 1785596 ... $ QNXTReferFromId : int 1740397 1724801 1711465 1704170 1714272 1731911 1535 1712758 1740614 1760252 ... $ iscasemanagement : Factor w/ 2 levels "N","Y": 2 1 1 2 2 1 2 1 2 2 ... $ iseligible : Factor w/ 2 levels "N","Y": 2 2 2 2 2 2 2 2 2 2 ... $ referralservicecode : Factor w/ 11 levels "12345","278",..: 1 1 1 9 9 1 1 6 9 9 ... $ IsHighlight : Factor w/ 2 levels "N","Y": 1 1 1 1 1 1 1 1 1 1 ... $ admittingdiagnosiscode: num 439 786 785 786 428 ... $ dischargediagnosiscode: num 439 0 296 786 428 ... $ RealLengthOfStay : int 3 1 6 1 2 3 3 7 3 2 ... $ QNXTPCPId : int 1740397 1724801 1711465 1704170 1714272 1731911 1535 1712758 1740614 1760252 ... $ QNXTProgramId : Factor w/ 3 levels "QMXHPQ0839 ",..: 1 1 1 1 1 1 1 1 1 1 ... $ physicalzipcode : int 33054 33712 33010 33809 33010 33013 33142 33030 33161 33055 ... $ gender : Factor w/ 2 levels "F","M": 1 1 1 1 2 1 1 2 2 1 ... $ ethnicitycode : Factor w/ 4 levels "ETHN0001 ",..: 4 4 4 4 4 4 4 4 4 4 ... $ dx1 : num 439 786 296 786 428 ... $ dx2 : num 439 292 785 786 428 ... $ dx3 : num 402 0 250 0 0 ... $ svc1 : int 0 120 120 762 762 120 120 120 762 762 ... $ svc2 : int 120 0 0 0 0 0 0 0 0 0 ... $ svc3 : int 0 0 0 0 0 0 0 0 0 0 ... $ Disposition : Factor w/ 28 levels "0","APPEAL & GRIEVANCE REVIEW ",..: 11 11 16 11 11 11 11 11 11 11 ... $ AvgIncome : Factor w/ 10 levels "-1",">100k","0-25k",..: 3 6 3 8 3 4 3 5 4 4 ... $ CaseManagerNameID : int 124 1 1 19 20 1 16 1 43 20 ... $ .outcome : Factor w/ 2 levels "NO","YES": 1 2 2 1 1 1 2 2 1 1 ... 

now testData will have the following structure:

 > str(testData[-$Readmit]) 'data.frame': 610 obs. of 26 variables: $ Acuity : Factor w/ 4 levels "0","Elective ",..: 3 2 4 2 2 2 4 3 3 3 ... $ AgeGroup : Factor w/ 9 levels "100-105","65-70",..: 4 3 5 4 2 9 4 2 4 6 ... $ IsPriority : int 0 0 0 0 0 0 1 1 1 1 ... $ QNXTReferToId : int 2140 482 1703785 1941 114 1714905 1703785 98 109 109 ... $ QNXTReferFromId : int 1791383 1729375 1718532 1746336 1718267 1718267 1718532 98 109 109 ... $ iscasemanagement : Factor w/ 2 levels "N","Y": 2 2 2 2 2 2 1 2 2 1 ... $ iseligible : Factor w/ 2 levels "N","Y": 2 2 2 2 2 2 2 2 2 2 ... $ referralservicecode : Factor w/ 7 levels "12345","IPMAT ",..: 5 1 1 1 1 1 1 5 1 5 ... $ IsHighlight : Factor w/ 2 levels "N","Y": 1 1 1 1 1 1 1 1 1 1 ... $ admittingdiagnosiscode: num 11440 11317 11420 11317 1361 ... $ dischargediagnosiscode: num 11440 11317 11420 11317 1361 ... $ RealLengthOfStay : int 1 2 4 3 1 1 16 1 1 3 ... $ QNXTPCPId : int 3212 1713678 1738430 1713671 1720569 1791640 1725962 1148 1703290 1705009 ... $ QNXTProgramId : Factor w/ 2 levels "QMXHPQ0839 ",..: 1 1 1 1 1 1 1 1 1 1 ... $ physicalzipcode : int 34744 33175 33844 33178 33010 33010 33897 33126 33127 33125 ... $ gender : Factor w/ 2 levels "F","M": 2 1 2 1 2 2 2 1 1 2 ... $ ethnicitycode : Factor w/ 1 level "No Ethnicity ": 1 1 1 1 1 1 1 1 1 1 ... $ dx1 : num 11440 11317 11420 11317 1361 ... $ dx2 : num 11440 11317 11420 11317 1361 ... $ dx3 : num 0 1465 0 11326 0 ... $ svc1 : int 52648 27447 50040 27447 55866 55866 51595 0 99221 300616 ... $ svc2 : int 76872 120 50391 120 120 38571 120 762 120 0 ... $ svc3 : int 762 0 120 0 0 51999 0 0 0 762 ... $ Disposition : Factor w/ 14 levels "0","DENIED- Not Medically Necessary ",..: 3 5 3 4 3 3 5 3 3 5 ... $ AvgIncome : Factor w/ 10 levels "-1",">100k","0-25k",..: 6 7 5 9 3 3 6 4 3 4 ... $ CaseManagerNameID : int 1 2 3 4 5 6 7 8 9 7 ... 

The structure of the variables is the same, only some factor variables have different levels, because some variable has new values. For example: Acuity in the model has 3 levels and 4 levels of test data.

I have no direct way to find out all possible levels for all variables.

Any advice please ...

Thanks in advance,

David

+8
source share
2 answers

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

In this particular case, the class of this function is train , so it will actually name the function: train.predict from the caret package. This function also processes the specific function requested for forecasting based on the algorithm (method) used, for example: predict.gbm or predict.glm , etc. This is explained in detail in the carriage: "5.7 Extracting forecasts and class probabilities."

Therefore :: abstract is working well for other functions in the package, for example: caret.train for example, but not for this specific one: predict . In such cases, it is necessary to explicitly load the library, so it can internally call the predict.train function.

In short, the solution simply adds the following line before calling the predict function:

 library(caret) 

Then the error disappears.

+8
source

Based on @David Leal's answer, I tried to load library(caret) before calling the prediction function, but that didn't help.

After trying a little, I realized that I need to download the library that contains the model itself. In my case, I had to call library(kenlab) to support vectors.

0
source

All Articles