"RSNNS" 包不允许我使用 CARET 的训练功能

"RSNNS" package doesn't allow me to use CARET's train function

到目前为止,我正在使用带有 RandomForest 的 CARET 包进行训练。

我使用 CARET 的 train 函数进行交叉验证,一切正常。

直到我想尝试使用神经网络并上传 RSNNS 包。现在,每当我尝试使用 train(使用我的旧 rf 算法)时,我都会收到以下错误:

Error in UseMethod("train") : no applicable method for 'train' applied to an object of class "c('tbl_df', 'tbl', 'data.frame')"

这是错误吗?为什么 RSNNS 会导致这种情况?

问题是 RSNNS::train() 正在屏蔽 caret::train(),因为 RSNNS 版本是在插入符号之后加载的。通过使用 packageName::function() 语法调用 caret::train() 来解决问题。

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.
>