我有一个模型(fit),基于上个月的历史信息。现在我想用我的模型来预测这个月。当我试图调用以下代码时:
predicted <- predict(fit, testData[-$Readmit])我得到以下错误:
Error in UseMethod("predict") : no applicable method for 'predict'
applied to an object of class "train"备注:
train包中的卡雷特函数建立了拟合模型。predict是一个泛型函数,它将根据第一个输入参数调用特定的预测函数。就我而言,情况将是:
>fit$modelInfo$label
[1] "Random Forest"因此,调用的预测方法是:predict.randomForest。有关更多信息,请参阅插入符号文档。
在这里,生成模型并调用它的摘要源代码:
# 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])注意事项:生成模型的执行时间约为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 ...现在,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 ...变量结构是相同的,只是一些因素变量有不同的级别,因为有些变量有新的值。例如:模型中的Acuity有3级,在测试数据中有4级.
我没有办法预先知道所有变量的所有可能的水平。
有什么建议吗请..。
提前谢谢你,
大卫
发布于 2017-01-25 00:03:00
我想我找到了为什么这个happened...The predict是来自:stats包的泛型函数。我使用名称空间::-notation从caret包调用函数(这是创建用户包的建议),而来自caret包的等效predict函数是:predict.train,这是一个内部函数,外部应用程序无法调用。调用此函数的唯一方法是从predict包中使用泛型stats函数,然后根据第一个输入参数的类:它标识的特定predict函数将被调用。
对于这个特殊情况,这个函数的类是train,因此它实际上会调用函数:来自caret包的train.predict。该函数还根据所使用的算法(方法)处理用于预测的特定函数,例如:predict.gbm或predict.glm等。在插入符号文档部分详细解释:"5.7提取预测和类概率“。
因此,::-notation适用于包中的其他函数,例如:caret.train,但不适用于这个特定的函数:predict。在这种情况下,必须显式加载库,因此它可以在内部调用predict.train函数。
简而言之,解决方案只是在调用predict函数之前添加以下行:
library(caret)然后错误就消失了。
发布于 2019-01-22 19:25:07
基于@David的回答,我尝试在调用library(caret)函数之前加载它,但是没有帮助。
尝试了一下之后,我意识到我必须加载包含模型本身的库。在我的例子中,我不得不打电话给library(kenlab)以获得支持向量。
https://stackoverflow.com/questions/38623624
复制相似问题