### Installing finetune Development Version (R) Source: https://github.com/tidymodels/finetune/blob/main/README.md This snippet shows how to install the development version of the `finetune` package from GitHub using the `pak` package. The `pak` package itself might need to be installed first if not already present in your R environment. ```R # install.packages("pak") pak::pak("tidymodels/finetune") ``` -------------------------------- ### Installing finetune from CRAN (R) Source: https://github.com/tidymodels/finetune/blob/main/README.md This snippet demonstrates how to install the stable version of the `finetune` package directly from CRAN. It is a prerequisite for using the package's functionalities and ensures you have the latest stable release. ```R install.packages("finetune") ``` -------------------------------- ### Tuning with tune_sim_anneal using formula and custom parameters in R Source: https://github.com/tidymodels/finetune/blob/main/tests/testthat/_snaps/sa-misc.md This snippet extends the previous example by providing custom parameter information (`rda_param`) to `tune_sim_anneal`. It uses a formula interface and runs for 3 iterations, optimizing `roc_auc` while controlling the parameter space with specific bounds or transformations. ```R set.seed(1) f_res_2 <- tune_sim_anneal(rda_spec, Class ~ ., rs, iter = 3, param_info = rda_param) ``` -------------------------------- ### Continuing Simulated Annealing Tuning with Previous Results in R Source: https://github.com/tidymodels/finetune/blob/main/tests/testthat/_snaps/sa-overall.md This code continues a simulated annealing tuning process, using the `res` object from a previous run as the `initial` starting point. It performs additional iterations (2 in this case) but with verbose output disabled. The `new_res` object will contain the updated tuning results. ```R set.seed(1) new_res <- tune_sim_anneal(var_wflow, cell_folds, iter = 2, initial = res, control = control_sim_anneal(verbose = FALSE)) ``` -------------------------------- ### Finetuning without Initial Results using tune_sim_anneal (R) Source: https://github.com/tidymodels/finetune/blob/main/tests/testthat/_snaps/sa-overall.md This R code shows how to initiate hyperparameter finetuning with simulated annealing (`tune_sim_anneal`) for a random forest workflow (`wf_rf`) without providing any initial tuning results. The optimization process starts from scratch, iteratively searching for better `roc_auc` values across the provided resamples (`bt`). ```R set.seed(40) rf_res_finetune <- tune_sim_anneal(wf_rf, resamples = bt) ``` -------------------------------- ### Setting Up Simulated Annealing Tuning for RDA Model (R) Source: https://github.com/tidymodels/finetune/blob/main/README.md This R code snippet initializes the environment for tuning a regularized discriminant analysis (RDA) model using `finetune`'s simulated annealing. It loads necessary tidymodels ecosystem libraries, prepares sample data, sets up resampling for robust evaluation, and defines the RDA model specification with tunable parameters. ```R library(tidymodels) library(finetune) # Syntax very similar to `tune_grid()` or `tune_bayes()`: ## ----------------------------------------------------------------------------- data(two_class_dat, package = "modeldata") set.seed(1) rs <- bootstraps(two_class_dat, times = 10) # more resamples usually needed # Optimize a regularized discriminant analysis model library(discrim) rda_spec <- discrim_regularized(frac_common_cov = tune(), frac_identity = tune()) |> set_engine("klaR") ``` -------------------------------- ### Loading Libraries and Preparing Data for SVM Tuning in R Source: https://github.com/tidymodels/finetune/blob/main/man/rmd/anova-benchmark.md This snippet loads essential R packages for statistical modeling and parallel computation, including `kernlab`, `tidymodels`, `finetune`, and `doParallel`. It then prepares the `cells` dataset by removing the `case` column and sets up 25 bootstrap resamples for robust model evaluation. ```R library(kernlab) library(tidymodels) library(finetune) library(doParallel) data(cells, package = "modeldata") cells <- cells |> select(-case) set.seed(6376) rs <- bootstraps(cells, times = 25) ``` -------------------------------- ### Tuning with tune_sim_anneal using a workflow interface in R Source: https://github.com/tidymodels/finetune/blob/main/tests/testthat/_snaps/sa-misc.md This snippet illustrates tuning a complete `workflow` object (`wflow`) using `tune_sim_anneal`. It performs simulated annealing for 3 iterations, showcasing how to tune an entire modeling pipeline including preprocessing steps and the model specification within a single workflow object. ```R set.seed(1) f_wflow_1 <- tune_sim_anneal(wflow, rs, iter = 3) ``` -------------------------------- ### Tuning with tune_sim_anneal using a recipe interface in R Source: https://github.com/tidymodels/finetune/blob/main/tests/testthat/_snaps/sa-misc.md This snippet shows how to use `tune_sim_anneal` with a `recipe` object (`rec`) instead of a formula. It tunes the `rda_spec` model for 3 iterations, demonstrating the flexibility of the `tune_sim_anneal` function with preprocessed data defined by a recipe. ```R set.seed(1) f_rec_1 <- tune_sim_anneal(rda_spec, rec, rs, iter = 3) ``` -------------------------------- ### Performing Initial Simulated Annealing Tuning in R Source: https://github.com/tidymodels/finetune/blob/main/tests/testthat/_snaps/sa-overall.md This snippet initializes a simulated annealing hyperparameter tuning process using `tune_sim_anneal` from the `tidymodels` ecosystem. It sets a random seed for reproducibility and configures the tuning to be verbose, showing iteration details. The `res` object will store the tuning results. ```R set.seed(1) res <- tune_sim_anneal(var_wflow, cell_folds, iter = 2, control = control_sim_anneal( verbose = TRUE, verbose_iter = TRUE)) ``` -------------------------------- ### Continuing Simulated Annealing Tuning from Grid Search Results in R Source: https://github.com/tidymodels/finetune/blob/main/tests/testthat/_snaps/sa-overall.md This snippet demonstrates continuing a simulated annealing tuning process, but instead of using previous simulated annealing results, it initializes the process with `grid_res`, likely results from a prior grid search. It runs for 2 iterations with verbose output disabled, storing the new results in `new_new_res`. ```R set.seed(1) new_new_res <- tune_sim_anneal(var_wflow, cell_folds, iter = 2, initial = grid_res, control = control_sim_anneal(verbose = FALSE)) ``` -------------------------------- ### Tuning with tune_sim_anneal using a formula interface in R Source: https://github.com/tidymodels/finetune/blob/main/tests/testthat/_snaps/sa-misc.md This snippet demonstrates tuning a model specification (`rda_spec`) using simulated annealing with a formula interface (`Class ~ .`). It sets a random seed for reproducibility and runs for 3 iterations, optimizing `roc_auc` to find the best hyperparameters. ```R set.seed(1) f_res_1 <- tune_sim_anneal(rda_spec, Class ~ ., rs, iter = 3) ``` -------------------------------- ### Configuring Simulated Annealing Control in R Source: https://github.com/tidymodels/finetune/blob/main/tests/testthat/_snaps/sa-control.md This snippet demonstrates how to initialize a simulated annealing control object using `control_sim_anneal()`. It sets `no_improve` to 2, indicating the search stops after 2 poor iterations, and `restart` to 6, which triggers a restart after 6 poor iterations. The message indicates a potential conflict where the restart is scheduled after the search would stop. ```R control_sim_anneal(no_improve = 2, restart = 6) ``` -------------------------------- ### Benchmarking Sequential Grid Search for SVM Tuning in R Source: https://github.com/tidymodels/finetune/blob/main/man/rmd/anova-benchmark.md This snippet measures the execution time of a traditional grid search for hyperparameter tuning using the `tune_grid` function. It applies the previously defined SVM workflow and tuning grid to the bootstrap resamples sequentially, providing a baseline for performance comparison against more advanced tuning methods. ```R system.time({ set.seed(2) svm_wflow |> tune_grid(resamples = rs, grid = svm_grid) }) ``` -------------------------------- ### Finetuning with Initial Results using tune_sim_anneal (R) Source: https://github.com/tidymodels/finetune/blob/main/tests/testthat/_snaps/sa-overall.md This R code snippet demonstrates how to perform hyperparameter finetuning using simulated annealing (`tune_sim_anneal`) with an existing set of initial tuning results (`rf_res`). The function optimizes the `roc_auc` metric for a random forest workflow (`wf_rf`) over specified resamples (`bt`), leveraging prior optimization efforts. ```R set.seed(40) rf_res_finetune <- tune_sim_anneal(wf_rf, resamples = bt, initial = rf_res) ``` -------------------------------- ### Defining SVM Model, Recipe, Workflow, and Tuning Grid in R Source: https://github.com/tidymodels/finetune/blob/main/man/rmd/anova-benchmark.md This code defines an SVM model specification with a radial basis function kernel, marking `cost` and `rbf_sigma` for tuning. It also creates a preprocessing recipe that applies YeoJohnson transformation and normalization to all predictors. These components are combined into a `tidymodels` workflow, and a Latin Hypercube sampling grid of 25 parameter combinations is generated for efficient hyperparameter search. ```R svm_spec <- svm_rbf(cost = tune(), rbf_sigma = tune()) |> set_engine("kernlab") |> set_mode("classification") svm_rec <- recipe(class ~ ., data = cells) |> step_YeoJohnson(all_predictors()) |> step_normalize(all_predictors()) svm_wflow <- workflow() |> add_model(svm_spec) |> add_recipe(svm_rec) set.seed(1) svm_grid <- svm_spec |> parameters() |> grid_latin_hypercube(size = 25) ``` -------------------------------- ### Tuning Model with Simulated Annealing in Tidymodels (R) Source: https://github.com/tidymodels/finetune/blob/main/README.md This snippet demonstrates how to use simulated annealing for hyperparameter tuning. It initializes a random search process, iteratively exploring the parameter space to find optimal settings for a model, aiming to maximize `roc_auc`. The `tune_sim_anneal` function is used with a specified number of iterations and an initial configuration. ```R set.seed(2) sa_res <- rda_spec |> tune_sim_anneal(Class ~ ., resamples = rs, iter = 20, initial = 4) show_best(sa_res, metric = "roc_auc", n = 2) ``` -------------------------------- ### Initializing ANOVA Racing Tune in R Source: https://github.com/tidymodels/finetune/blob/main/tests/testthat/_snaps/anova-filter.md This snippet initializes an ANOVA-based racing tune process using `tune_race_anova` from the `finetune` package. It sets a random seed for reproducibility and defines the model specification (`spec`), a formula (`mpg ~ .`), cross-validation folds (`folds`), and a hyperparameter grid (`grid`) for tuning. The result, containing the tuned model, is stored in `anova_mod`. ```R set.seed(129) anova_mod <- tune_race_anova(spec, mpg ~ ., folds, grid = grid) ``` -------------------------------- ### Racing Hyperparameter Tuning with ANOVA in Tidymodels (R) Source: https://github.com/tidymodels/finetune/blob/main/README.md This code illustrates the racing method for hyperparameter tuning using an ANOVA-type analysis. It first generates a parameter grid using `grid_max_entropy` (now deprecated, `grid_space_filling` is preferred) and then applies `tune_race_anova` to efficiently eliminate suboptimal parameter combinations based on statistical testing across resamples, maximizing `roc_auc`. ```R set.seed(3) grid <- rda_spec |> extract_parameter_set_dials() |> grid_max_entropy(size = 20) ctrl <- control_race(verbose_elim = TRUE) set.seed(4) grid_anova <- rda_spec |> tune_race_anova(Class ~ ., resamples = rs, grid = grid, control = ctrl) show_best(grid_anova, metric = "roc_auc", n = 2) ``` -------------------------------- ### Setting Up Parallel Processing in R Source: https://github.com/tidymodels/finetune/blob/main/man/rmd/anova-benchmark.md This snippet configures parallel processing in R to accelerate computational tasks. It detects the number of physical CPU cores available, creates a PSOCK cluster with that many cores, and registers it with `doParallel` to enable parallel execution for subsequent operations, such as hyperparameter tuning. ```R cores <- parallel::detectCores(logical = FALSE) cl <- makePSOCKcluster(cores) registerDoParallel(cl) ``` -------------------------------- ### Benchmarking Sequential ANOVA Racing for SVM Tuning in R Source: https://github.com/tidymodels/finetune/blob/main/man/rmd/anova-benchmark.md This code measures the execution time of hyperparameter tuning using the `tune_race_anova` function from the `finetune` package. This method employs ANOVA racing to efficiently identify optimal parameters by early stopping poor-performing candidates, offering a faster alternative to traditional grid search. ```R system.time({ set.seed(2) svm_wflow |> tune_race_anova(resamples = rs, grid = svm_grid) }) ``` -------------------------------- ### Casting Simulated Annealing Control to Grid Control in R Source: https://github.com/tidymodels/finetune/blob/main/tests/testthat/_snaps/sa-control.md This snippet shows how to convert a `control_sim_anneal()` object into a `control_grid()` object using `parsnip::condense_control()`. This is useful for unifying control parameters across different tuning methods within the tidymodels framework, resulting in a generic grid/resamples control object. ```R parsnip::condense_control(control_sim_anneal(), control_grid()) ``` -------------------------------- ### Handling Incompatible Parameter Ranges in tune_sim_anneal (R) Source: https://github.com/tidymodels/finetune/blob/main/tests/testthat/_snaps/sa-overall.md This R snippet illustrates an error that occurs when the `param_info` supplied to `tune_sim_anneal` defines a parameter range that is incompatible with the values present in the `initial` tuning results. The error message indicates that the `param_info` range must encompass all values from the initial grid to ensure a valid optimization search space. ```R res <- tune_sim_anneal(car_wflow, param_info = parameter_set_with_smaller_range, resamples = car_folds, initial = tune_res_with_bigger_range, iter = 2) ``` -------------------------------- ### Racing Hyperparameter Tuning with Win-Loss Statistics in Tidymodels (R) Source: https://github.com/tidymodels/finetune/blob/main/README.md This snippet demonstrates another racing method for hyperparameter tuning, `tune_race_win_loss`. It treats tuning parameters as competitors in a tournament, calculating win/loss statistics across resamples to identify the best performing combinations. This method efficiently prunes the search space, aiming to maximize `roc_auc`. ```R set.seed(4) grid_win_loss<- rda_spec |> tune_race_win_loss(Class ~ ., resamples = rs, grid = grid, control = ctrl) show_best(grid_win_loss, metric = "roc_auc", n = 2) ``` -------------------------------- ### Benchmarking Parallel Grid Search for SVM Tuning in R Source: https://github.com/tidymodels/finetune/blob/main/man/rmd/anova-benchmark.md This snippet measures the execution time of a grid search for SVM hyperparameter tuning, leveraging the previously configured parallel processing. By distributing the tuning tasks across multiple CPU cores, this approach aims to significantly reduce the overall computation time compared to its sequential counterpart. ```R system.time({ set.seed(2) svm_wflow |> tune_grid(resamples = rs, grid = svm_grid) }) ``` -------------------------------- ### Viewing Best Tuning Results in R Source: https://github.com/tidymodels/finetune/blob/main/tests/testthat/_snaps/win-loss-overall.md This snippet shows how to retrieve and display the best performing hyperparameter configurations from a set of `tuning_results` using the `show_best` function. If no specific metric is provided, it defaults to 'roc_auc' as indicated by the warning message. ```R best_res <- show_best(tuning_results) ``` -------------------------------- ### Benchmarking Parallel ANOVA Racing for SVM Tuning in R Source: https://github.com/tidymodels/finetune/blob/main/man/rmd/anova-benchmark.md This code measures the execution time of ANOVA racing for SVM hyperparameter tuning, utilizing parallel processing. Combining the efficiency of the racing algorithm with the speed-up from parallel execution, this method is expected to be the fastest approach demonstrated for finding optimal model parameters. ```R system.time({ set.seed(2) svm_wflow |> tune_race_anova(resamples = rs, grid = svm_grid) }) ``` -------------------------------- ### Applying Simulated Annealing for Hyperparameter Tuning in R Source: https://github.com/tidymodels/finetune/blob/main/tests/testthat/_snaps/sa-overall.md This R code snippet initiates a simulated annealing process for hyperparameter tuning using the `tune_sim_anneal` function from the `finetune` package. It sets a random seed for reproducibility, takes a workflow object (`f_wflow`) and resampling folds (`cell_folds`) as input, and runs for a specified number of iterations (`iter = 2`) with verbose output. The process aims to optimize a metric like `roc_auc` by iteratively exploring parameter space. ```R set.seed(1) res <- tune_sim_anneal(f_wflow, cell_folds, iter = 2, control = control_sim_anneal( verbose = TRUE)) ``` -------------------------------- ### Tuning Models with tune_race_anova in R Source: https://github.com/tidymodels/finetune/blob/main/tests/testthat/_snaps/anova-overall.md This snippet demonstrates how to use `tune_race_anova` from the `finetune` package to perform racing-based hyperparameter tuning. It initializes a random seed for reproducibility and then calls `tune_race_anova` with a workflow (`f_wflow`), cross-validation folds (`cell_folds`), a parameter grid (`grid_mod`), and control options for verbose elimination. This function efficiently eliminates poor-performing hyperparameter combinations early in the tuning process. ```R set.seed(1) res <- tune_race_anova(f_wflow, cell_folds, grid = grid_mod, control = control_race( verbose_elim = TRUE)) ``` -------------------------------- ### Logging Racing Process with Verbose Elimination in R Source: https://github.com/tidymodels/finetune/blob/main/tests/testthat/_snaps/anova-filter.md This snippet demonstrates how to log the progress of a racing tune process using `finetune:::log_racing`. It configures `control_race` to enable verbose elimination messages (`verbose_elim = TRUE`), passes the ANOVA racing results (`anova_res`), cross-validation splits (`ames_grid_search$splits`), the current fold number (10), and the performance metric ('rmse'). The accompanying message indicates the number of eliminated and remaining candidates for that specific fold. ```R finetune:::log_racing(control_race(verbose_elim = TRUE), anova_res, ames_grid_search$splits, 10, "rmse") ``` -------------------------------- ### Tuning Models with Formula Interface and Racing in R Source: https://github.com/tidymodels/finetune/blob/main/tests/testthat/_snaps/win-loss-overall.md This snippet demonstrates how to perform racing-based hyperparameter tuning using the `tune_race_win_loss` function from the `finetune` package. It uses a formula interface (`f_wflow`) and cross-validation folds (`cell_folds`) to optimize model parameters, with verbose output enabled for elimination progress. The `roc_auc` metric is implicitly maximized. ```R set.seed(1) res <- tune_race_win_loss(f_wflow, cell_folds, grid = 5, control = control_race( verbose_elim = TRUE)) ``` -------------------------------- ### Casting control_race to control_grid in R Source: https://github.com/tidymodels/finetune/blob/main/tests/testthat/_snaps/race-control.md This snippet demonstrates how to convert a `control_race` object into a `control_grid` object using the `parsnip::condense_control` function. This is useful for adapting racing methods' control settings to a grid-based resampling context, allowing for consistent control object structures across different resampling strategies. ```R parsnip::condense_control(control_race(), control_grid()) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.