Hello!
why cant i get tune_bayes
to work, when trying to tune my random forest model. please, what am i doing wrong?
The following is my code:
# ==============================================================================
# Specifying the RF model and setting up the workflow
# ==============================================================================
# defining the model specification
rf_spec <- rand_forest(trees = 4000,
mtry = tune(),
min_n = tune()) %>%
set_engine("ranger", importance = "impurity", splitrule = "extratrees") %>%
set_mode("regression")
# setting up the workflow
workflow_rf <- workflow() %>%
add_recipe(recipe_selected) %>%
add_model(rf_spec)
# ==============================================================================
# Exploring hyperparameter ranges for RF
# ==============================================================================
# checking where to set the hyperparameter limits
tune_res <- tune_grid(workflow_rf,
resamples = folds,
grid = 20)
# visualizing the perfprmance of the randomly chosen parameters
tune_res %>%
collect_metrics() %>%
filter(.metric == "rsq") %>%
select(mean, min_n, mtry) %>%
pivot_longer(min_n:mtry,
values_to = "value",
names_to = "parameter") %>%
ggplot(aes(value, mean, color = parameter)) +
geom_point(show.legend = FALSE) +
facet_wrap(~parameter, scales = "free_x") +
labs(x = NULL, y = "rsq")
# ==============================================================================
# Building the final model
# ==============================================================================
rf_grid <- grid_random(mtry(range = c(15, 30)),
min_n(range = c(15, 30)),
size = 10)
initial_tuning_results <- tune_grid(workflow_rf,
resamples = folds,
grid = rf_grid,
metrics = metric_set(rmse, rsq))
# running the tuning using bayesion tuning and block cross validation
(tuning_results_rf <- tune_bayes(workflow_rf,
resamples = folds,
param_info = rf_set,
iter = 30,
initial = 5,
control = control_bayes(save_pred = TRUE, verbose = TRUE),
metrics = metric_set(rmse, rsq)))