Skip to content

Conversation

@simonpcouch
Copy link
Contributor

@simonpcouch simonpcouch commented Apr 5, 2023

A first step for infrastructure for fairness metrics! This function is closely analogous to collect_metrics(), but allows for computing new metrics after tuning without needing to re-fit and re-predict from resampled models.🏄

# load needed packages:
library(tune)
library(parsnip)
library(rsample)
library(yardstick)

# evaluate a linear regression against resamples.
# note that we pass `save_pred = TRUE`:
res <-
  fit_resamples(
    linear_reg(),
    mpg ~ cyl + hp,
    bootstraps(mtcars, 5),
    control = control_grid(save_pred = TRUE)
  )

# to return the metrics supplied to `fit_resamples()`:
collect_metrics(res)
#> # A tibble: 2 × 6
#>   .metric .estimator  mean     n std_err .config             
#>   <chr>   <chr>      <dbl> <int>   <dbl> <chr>               
#> 1 rmse    standard   3.62      5  0.172  Preprocessor1_Model1
#> 2 rsq     standard   0.746     5  0.0129 Preprocessor1_Model1

# to compute new metrics:
compute_metrics(res, metric_set(mae))
#> # A tibble: 1 × 6
#>   .metric .estimator  mean     n std_err .config             
#>   <chr>   <chr>      <dbl> <int>   <dbl> <chr>               
#> 1 mae     standard    2.89     5   0.151 Preprocessor1_Model1

# if `metrics` is the same as that passed to `fit_resamples()`,
# then `collect_metrics()` and `compute_metrics()` give the same
# output, though `compute_metrics()` is quite a bit slower:
all.equal(
  collect_metrics(res),
  compute_metrics(res, metric_set(rmse, rsq))
)
#> [1] TRUE

Created on 2023-04-05 with reprex v2.0.2

There is likely some additional work to do for accommodating iterative tuning, so marking as a draft for now!

@simonpcouch simonpcouch marked this pull request as ready for review April 11, 2023 21:14
Copy link
Member

@DavisVaughan DavisVaughan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lots of minor comments, but nothing super blocking so I will approve too

Copy link
Member

@topepo topepo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@simonpcouch simonpcouch merged commit 4dc4586 into main Apr 19, 2023
@simonpcouch simonpcouch deleted the compute-metrics branch April 19, 2023 16:31
@github-actions
Copy link
Contributor

github-actions bot commented May 4, 2023

This pull request has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.

@github-actions github-actions bot locked and limited conversation to collaborators May 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants