library(tidymodels)
                    library(stacks)
                    library(finetune)
                    library(tidyverse)
                    
                    
                    data("Boston", package = "MASS")
                    boston_data <- Boston %>%
                      as_tibble() %>%
                      mutate(medv_log = log(medv)) %>%
                      select(-medv)
                    
                    
                    set.seed(123)
                    boston_split <- initial_split(boston_data, prop = 0.8, strata = medv_log)
                    boston_train <- training(boston_split)
                    boston_test <- testing(boston_split)
                    
                    
                    boston_folds <- vfold_cv(boston_train, v = 5, strata = medv_log)
                
             
            
                
                
                    
                    boston_recipe <- recipe(medv_log ~ ., data = boston_train) %>%
                      step_normalize(all_numeric_predictors()) %>%
                      step_corr(all_numeric_predictors(), threshold = 0.9) %>%
                      step_zv(all_predictors())
                    
                    
                    linear_spec <- linear_reg(penalty = tune(), mixture = tune()) %>%
                      set_engine("glmnet") %>%
                      set_mode("regression")
                    
                    
                    rf_spec <- rand_forest(
                      trees = 1000,
                      mtry = tune(),
                      min_n = tune()
                    ) %>%
                      set_engine("ranger") %>%
                      set_mode("regression")
                    
                    
                    xgb_spec <- boost_tree(
                      trees = tune(),
                      tree_depth = tune(),
                      learn_rate = tune()
                    ) %>%
                      set_engine("xgboost") %>%
                      set_mode("regression")
                
             
            
                
                
                    
                    control_stack <- control_stack_grid()
                    
                    
                    linear_res <- workflow() %>%
                      add_recipe(boston_recipe) %>%
                      add_model(linear_spec) %>%
                      tune_grid(
                        resamples = boston_folds,
                        grid = 15,
                        control = control_stack,
                        metrics = metric_set(rmse, mae, rsq)
                      )
                    
                    
                    rf_res <- workflow() %>%
                      add_recipe(boston_recipe) %>%
                      add_model(rf_spec) %>%
                      tune_grid(
                        resamples = boston_folds,
                        grid = 12,
                        control = control_stack,
                        metrics = metric_set(rmse, mae, rsq)
                      )
                    
                    
                    xgb_res <- workflow() %>%
                      add_recipe(boston_recipe) %>%
                      add_model(xgb_spec) %>%
                      tune_grid(
                        resamples = boston_folds,
                        grid = 15,
                        control = control_stack,
                        metrics = metric_set(rmse, mae, rsq)
                      )