到目前为止,我正在使用带有RandomForest的CARET包进行训练。
我使用CARET的train
函数进行交叉验证,一切运行良好。
直到我想尝试使用神经网络并上传RSNNS包。现在,每当我尝试使用火车(使用我的旧rf算法)时,我都会收到以下错误:
UseMethod出错(" train"): 没有适用的方法来训练'应用于课程" c的对象(' tbl_df',' tbl',' data.frame')"
那是错误吗?为什么RSNNS会导致这种情况?
答案 0 :(得分:3)
问题是RSNNS::train()
屏蔽了caret::train()
,因为RSNNS版本是在插入符号后加载的。通过使用caret::train()
语法调用packageName::function()
来解决此问题。
library(caret)
library(RSNNS)
library(mlbench)
data(Sonar)
inTraining <- createDataPartition(Sonar$Class, p = .75, list=FALSE)
training <- Sonar[inTraining,]
testing <- Sonar[-inTraining,]
fitControl <- trainControl(method = "cv",
number = 3)
# error because RSNNS::train does not work like caret::train()
system.time(fit <- train(Class ~ ., method="rf",data=Sonar,trControl = fitControl))
# correct by calling caret::train()
system.time(fit <- caret::train(Class ~ ., method="rf",data=Sonar,trControl = fitControl))
fit
...和输出:
> system.time(fit <- train(Cx=Sonar[,-61],y=Sonar[,61], method="rf",data=Sonar,trControl = fitControl))
Error in UseMethod("train") :
no applicable method for 'train' applied to an object of class "data.frame"
Timing stopped at: 0.033 0 0.034
> # correct by calling caret::train()
> system.time(fit <- caret::train(x=Sonar[,-61],y=Sonar[,61], method="rf",data=Sonar,trControl = fitControl))
user system elapsed
3.888 0.069 3.981
> fit
Random Forest
208 samples
60 predictor
2 classes: 'M', 'R'
No pre-processing
Resampling: Cross-Validated (3 fold)
Summary of sample sizes: 139, 138, 139
Resampling results across tuning parameters:
mtry Accuracy Kappa
2 0.8175983 0.6292393
31 0.7645963 0.5249374
60 0.7694272 0.5336925
Accuracy was used to select the optimal model using the largest value.
The final value used for the model was mtry = 2.
>