UseMethod("predict"):没有适用于"预测"的方法应用于类"train"的对象

Dav*_*eal 11 r machine-learning

我有一个模型(fit),根据历史信息,直到上个月.现在我想预测当前月份使用我的模型.当我尝试调用以下代码时:

predicted <- predict(fit, testData[-$Readmit])
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

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

笔记:

  1. 拟合模型通过以下方式创建:train来自插入符号包的函数,使用随机森林算法
  2. predict是一个泛型函数,它将根据第一个输入参数调用特定的预测函数.在我的情况下,它将是:

    >fit$modelInfo$label

    [1] "Random Forest"

因此,调用的预测方法将是:predict.randomForest.有关详细信息,请参阅[插入文档] [3].

这里是生成模型并调用它的摘要源代码:

# 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])
Run Code Online (Sandbox Code Playgroud)

注意:生成模型的执行时间大约为3小时,这就是为什么我保存对象以便在此之后重用.

训练模型中的数据集如下结构:

> 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    ...
Run Code Online (Sandbox Code Playgroud)

现在testData将具有以下结构:

> 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 ...
Run Code Online (Sandbox Code Playgroud)

变量结构是相同的,只是某些因子变量具有不同的级别,因为某些变量具有新值.例如:Acuity在模型中有3个级别,在测试数据中有4个级别.

我没有从前期了解所有变量的所有可能级别的方法.

请给我任何建议......

提前致谢,

大卫

Dav*_*eal 12

我想我发现了为什么会发生这种情况......这predict是一个通用函数:statspackage.我使用namespace ::-notation来调用caret包中的函数(这是创建用户包的建议),而package中的等效predict函数caret是:predict.train,这是一个内部函数,不能被外部应用程序调用.调用此函数的唯一方法是使用package中的泛型predict函数stats,然后根据第一个输入参数的类:predicted <- predict(fit, testData[-$Readmit])它标识predict将调用的特定函数.

对于这种特殊情况,这个函数的类是train,所以它实际上会调用函数:train.predictfrom caretpackage.该函数还基于所使用的算法(方法)处理请求预测的特定功能,例如:predict.gbmpredict.glm等.在插入符号文档部分中详细解释:"5.7提取预测和类概率".

因此,::-notation适用于包中的其他功能,例如:caret.train但不适用于此特定的功能:predict.在这种情况下,有必要显式加载库,因此它在内部可以调用predict.train函数.

简而言之,解决方案是在调用predict函数之前添加以下行:

library(caret)
Run Code Online (Sandbox Code Playgroud)

然后错误消失.


Nik*_*pta 6

根据@David Leal 的回答,我library(caret)在调用 predict 函数之前尝试加载,但没有帮助。

尝试了一下之后,我意识到我必须加载包含模型本身的库。就我而言,我不得不要求library(kenlab)支持向量。