Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/tidymodels/finetune

Additional functions for model tuning
https://github.com/tidymodels/finetune

Last synced: 3 days ago
JSON representation

Additional functions for model tuning

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```

# finetune

[![R-CMD-check](https://github.com/tidymodels/finetune/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/tidymodels/finetune/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/tidymodels/finetune/branch/main/graph/badge.svg)](https://app.codecov.io/gh/tidymodels/finetune?branch=main)
[![Lifecycle](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html)

`finetune` contains some extra functions for model tuning that extend what is currently in the `tune` package. You can install the CRAN version of the package with the following code:

```{r, eval = FALSE}
install.packages("finetune")
```

To install the development version of the package, run:

```{r, eval = FALSE}
# install.packages("pak")
pak::pak("tidymodels/finetune")
```

There are two main sets of tools in the package: _simulated annealing_ and _racing_.

Tuning via _simulated annealing_ optimization is an iterative search tool for finding good values:

```{r load, include=FALSE}
library(tidymodels)
library(finetune)
library(discrim)
library(rlang)
library(MASS)
```
```{r sa}
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")

## -----------------------------------------------------------------------------

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)
```

The second set of methods are for _racing_. We start off by doing a small set of resamples for all of the grid points, then statistically testing to see which ones should be dropped or investigated more. The two methods here are based on those should in [Kuhn (2014)](https://arxiv.org/abs/1405.6974).

For example, using an ANOVA-type analysis to filter out parameter combinations:

```{r race}
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)
```

`tune_race_win_loss()` can also be used. It treats the tuning parameters as sports teams in a tournament and computed win/loss statistics.

```{r race-wl}
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)
```

## Contributing

This project is released with a [Contributor Code of Conduct](https://contributor-covenant.org/version/2/0/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.

- For questions and discussions about tidymodels packages, modeling, and machine learning, please [post on Posit Community](https://forum.posit.co/new-topic?category_id=15&tags=tidymodels,question).

- If you think you have encountered a bug, please [submit an issue](https://github.com/tidymodels/usemodels/issues).

- Either way, learn how to create and share a [reprex](https://reprex.tidyverse.org/articles/articles/learn-reprex.html) (a minimal, reproducible example), to clearly communicate about your code.

- Check out further details on [contributing guidelines for tidymodels packages](https://www.tidymodels.org/contribute/) and [how to get help](https://www.tidymodels.org/help/).