Search code examples
rneural-networkr-carettraining-data

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


So far I was using the CARET package with RandomForest for my training.

I use CARET's train function with cross validation and all is working well.

That is until I wanted to try using neural network and uploaded the RSNNS package. Now, whenever I'm trying to use train (with my old rf algorithms) I get the following error:

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

Is that bug? Why RSNNS causes that?


Solution

  • The problem is that RSNNS::train() is masking caret::train() because the RSNNS version was loaded after caret. Fix the problem by calling caret::train() with the packageName::function() syntax.

    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
    

    ...and the output:

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