Random Forest tune_grid - Error

Hi,

I try to tune a random forest with tidymodels, but end up with this error message (for all folds):

Bootstrap01: preprocessor 1/1, model 1/27: Error in y.mat[, 2]: subscript out of bounds

This is my code:

# Random Forest -----------------------------------------------------------

set.seed(123)
split <- initial_split(image, strata = "nps_brand")
train <- training(split)
test <- testing(split)

set.seed(234)
folds <- bootstraps(train, strata = "nps_brand")


# Define Recipe -----------------------------------------------------------

rf_recipe <- recipe(nps_brand ~ ., data = train) %>% 
  step_dummy(nps_brand)

# Define Model Spec -------------------------------------------------------

rf_model <- rand_forest(
  mtry = tune(),
  trees = tune(),
  min_n = tune()) %>%
  set_mode("classification") %>%
  set_engine("ranger")

# Define Workflow ---------------------------------------------------------

rf_workflow <- workflow() %>%
  add_recipe(rf_recipe) %>%
  add_model(rf_model)

# Define Grid -------------------------------------------------------------

rf_grid <- grid_regular(min_n(), 
                        trees(), 
                        mtry(range(1,floor(sqrt(ncol(train)-1)))))

# Tuning ------------------------------------------------------------------

set.seed(123)
rf_trained <- tune_grid(rf_workflow,
                        resamples = folds,
                        grid = rf_grid,
                        metrics = metric_set(accuracy, roc_auc),
                        control = control_grid(verbose = TRUE))

I tried to find out, what might be wrong but can't find a solution and would appreciate help.

Best Chris

I were able to solve the issue...

The target variable is binary (0, 1) and the problem was the "step_dummy" function.
I removed the line and now it works properly.

Chris

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.