Search code examples
rr-caret

caret - The tuning parameter grid should have columns mtry


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)


Solution

  • 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!