Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

caret - The tuning parameter grid should have columns mtry

Tags:

r

r-caret

I am using this code:

    mtry <- round(sqrt(18), 0)

gbmGrid <- expand.grid(
              interaction.depth = c(1, 2, 3, 4, 5, 6)
            , n.trees = seq(10, 10000, by = 100)
            , shrinkage = 0.01
            , n.minobsinnode = c(5, 10, 20, 30)
            , distribution = 'gaussian'
            , method = 'gbm'
            , mtry = mtry
    )

    fitControl <- trainControl(
                method = "repeatedcv"
                , number = 2
                , repeats = 3
        )

    gbmFit1 <- train(

                     Y ~

                      X1
                    + X2

                    , data = Train

                    , trControl = fitControl
                    , tuneGrid = gbmGrid
                    , verbose = FALSE
        )

but get:

The tuning parameter grid should have columns mtry

I installed the latest package as some people suggested this and also tried using .mtry. Any ideas? (yes I googled and had a look at SO)

like image 302
cs0815 Avatar asked Oct 16 '18 10:10

cs0815


2 Answers

I have taken it back to basics (iris). This works - the non existing mtry for gbm was the issue:

library(datasets)
library(gbm)
library(caret)

grid <- expand.grid(
                n.trees = seq(10, 1000, by = 100)
            , interaction.depth = c(4)
            , shrinkage = c(0.01, 0.1)
            , n.minobsinnode = c(5, 10, 20, 30)        
    )

train_control <- trainControl(
                    method = "repeatedcv"
                    , number = 10
                    , repeats = 10
    )

model <- train(Petal.Width ~ Petal.Length
                        , method = 'gbm'
                        , distribution = 'gaussian'
                        , data = iris
                        , trControl = train_control
                        , tuneGrid = grid
                        , verbose = FALSE
    )

model

Sorry for wasting your time!

like image 144
cs0815 Avatar answered Sep 26 '22 13:09

cs0815


In version >= 6.0-81 of caret the error message for this type of case is more clear. As an example, considering one supplies an mtry in the tuning grid when mtry is not a parameter for the given method.

In caret < 6.0-81, the following error will occur:

# Error: The tuning parameter grid should have columns mtry

In caret >= 6.0-81, the following error will occur:

# Error: The tuning parameter grid should not have columns mtry

Reprex of original confusing error message

And here is a reproducible example demonstrating the improved error message.

caret < 6.0-81

library(caret)
getNamespaceVersion("caret")
## version 
## "6.0-80"

mtry <- round(sqrt(18), 0)
gbmGrid <- expand.grid(
    interaction.depth = c(1, 2, 3, 4, 5, 6)
    , n.trees = seq(10, 10000, by = 100)
    , shrinkage = 0.01
    , n.minobsinnode = c(5, 10, 20, 30)
    , distribution = 'gaussian'
    , method = 'gbm'
    , mtry = mtry
)
fitControl <- trainControl(
    method = "repeatedcv"
    , number = 2
    , repeats = 3
)
gbmFit1 <- train(
    Species ~ Sepal.Length + Sepal.Width
    , data = iris
    , trControl = fitControl
    , tuneGrid = gbmGrid
    , verbose = FALSE
)
# Error: The tuning parameter grid should have columns mtry

caret >= 6.0-81

library(caret)
getNamespaceVersion("caret")
## version 
## "6.0-81"

mtry <- round(sqrt(18), 0)
gbmGrid <- expand.grid(
    interaction.depth = c(1, 2, 3, 4, 5, 6)
    , n.trees = seq(10, 10000, by = 100)
    , shrinkage = 0.01
    , n.minobsinnode = c(5, 10, 20, 30)
    , distribution = 'gaussian'
    , method = 'gbm'
    , mtry = mtry
)
fitControl <- trainControl(
    method = "repeatedcv"
    , number = 2
    , repeats = 3
)
gbmFit1 <- train(
    Species ~ Sepal.Length + Sepal.Width
    , data = iris
    , trControl = fitControl
    , tuneGrid = gbmGrid
    , verbose = FALSE
)
# Error: The tuning parameter grid should not have columns mtry

For more information, refer to the GitHub issue that described and then fixed this behavior: https://github.com/topepo/caret/issues/955

like image 31
jmuhlenkamp Avatar answered Sep 22 '22 13:09

jmuhlenkamp