Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Groupwise metrics not working with finetune #120

Open
Saarialho opened this issue Aug 30, 2024 · 1 comment
Open

Groupwise metrics not working with finetune #120

Saarialho opened this issue Aug 30, 2024 · 1 comment
Labels
bug an unexpected problem or unintended behavior

Comments

@Saarialho
Copy link

The problem

I'm having trouble with using groupwise metrics with finetune

Reproducible example

pacman::p_load(tidymodels, finetune)
data(ames)

diff_range <- function(x) {
  diff(range(x$.estimate))
}
demographic_parity_ <-
  new_groupwise_metric(
    fn = detection_prevalence,
    name = "demographic_parity",
    aggregate = diff_range
  )

m_set <- metric_set(demographic_parity_(Neighborhood))

set.seed(502)
ames_split <- initial_split(ames)
ames_train <- training(ames_split)
ames_folds <- vfold_cv(ames_train, v = 10)

rf_model <-
  rand_forest(trees = tune()) %>%
  set_engine("ranger") %>%
  set_mode("classification")

rf_wflow <-
  workflow() %>%
  add_formula(
    Lot_Shape ~ Year_Built + Bldg_Type + Latitude + Longitude
  ) %>%
  add_model(rf_model)

grid <- parameters(trees(c(10, 100))) %>%
  grid_max_entropy(size = 10)

tune_res_anova <- tune_race_anova(
  rf_wflow,
  ames_folds,
  grid = grid,
  metrics = m_set
)
#> Warning in max(best_config$B, na.rm = TRUE): no non-missing arguments to max;
#> returning -Inf
#> Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]): contrasts can be applied only to factors with 2 or more levels
#> Warning: package 'ranger' was built under R version 4.2.3
#> Warning: package 'rlang' was built under R version 4.2.3
#> Warning in max(best_config$B, na.rm = TRUE): no non-missing arguments to max;
#> returning -Inf
#> Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]): contrasts can be applied only to factors with 2 or more levels

Created on 2024-08-30 with reprex v2.0.2

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.2.1 (2022-06-23 ucrt)
#>  os       Windows 10 x64 (build 19045)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  Finnish_Finland.utf8
#>  ctype    Finnish_Finland.utf8
#>  tz       Europe/Helsinki
#>  date     2024-08-30
#>  pandoc   2.18 @ C:/Program Files/RStudio/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package      * version     date (UTC) lib source
#>  backports      1.4.1       2021-12-13 [1] CRAN (R 4.2.0)
#>  boot           1.3-28      2021-05-03 [1] CRAN (R 4.2.1)
#>  broom        * 1.0.5       2023-06-09 [1] CRAN (R 4.2.1)
#>  class          7.3-20      2022-01-16 [1] CRAN (R 4.2.1)
#>  cli            3.6.1       2023-03-23 [1] CRAN (R 4.2.3)
#>  codetools      0.2-18      2020-11-04 [1] CRAN (R 4.2.1)
#>  colorspace     2.1-0       2023-01-23 [1] CRAN (R 4.2.3)
#>  data.table     1.14.2      2021-09-27 [1] CRAN (R 4.2.1)
#>  dials        * 1.2.0       2023-04-03 [1] CRAN (R 4.2.3)
#>  DiceDesign     1.9         2021-02-13 [1] CRAN (R 4.2.1)
#>  digest         0.6.30      2022-10-18 [1] CRAN (R 4.2.2)
#>  dplyr        * 1.1.4       2023-11-17 [1] CRAN (R 4.2.3)
#>  evaluate       0.23        2023-11-01 [1] CRAN (R 4.2.3)
#>  fansi          1.0.6       2023-12-08 [1] CRAN (R 4.2.3)
#>  fastmap        1.1.0       2021-01-25 [1] CRAN (R 4.2.1)
#>  finetune     * 1.1.0       2023-04-19 [1] CRAN (R 4.2.3)
#>  foreach        1.5.2       2022-02-02 [1] CRAN (R 4.2.1)
#>  fs             1.5.2       2021-12-08 [1] CRAN (R 4.2.1)
#>  furrr          0.3.1       2022-08-15 [1] CRAN (R 4.2.1)
#>  future         1.33.0      2023-07-01 [1] CRAN (R 4.2.3)
#>  future.apply   1.11.0      2023-05-21 [1] CRAN (R 4.2.3)
#>  generics       0.1.3       2022-07-05 [1] CRAN (R 4.2.1)
#>  ggplot2      * 3.5.0       2024-02-23 [1] CRAN (R 4.2.3)
#>  globals        0.16.2      2022-11-21 [1] CRAN (R 4.2.1)
#>  glue           1.6.2       2022-02-24 [1] CRAN (R 4.2.1)
#>  gower          1.0.1       2022-12-22 [1] CRAN (R 4.2.2)
#>  GPfit          1.0-8       2019-02-08 [1] CRAN (R 4.2.1)
#>  gtable         0.3.5       2024-04-22 [1] CRAN (R 4.2.1)
#>  hardhat        1.3.1.9000  2024-04-11 [1] Github (tidymodels/hardhat@ed8b032)
#>  htmltools      0.5.7       2023-11-03 [1] CRAN (R 4.2.3)
#>  infer        * 1.0.4       2022-12-02 [1] CRAN (R 4.2.3)
#>  ipred          0.9-14      2023-03-09 [1] CRAN (R 4.2.3)
#>  iterators      1.0.14      2022-02-05 [1] CRAN (R 4.2.1)
#>  knitr          1.45        2023-10-30 [1] CRAN (R 4.2.3)
#>  lattice        0.20-45     2021-09-22 [1] CRAN (R 4.2.1)
#>  lava           1.7.2.1     2023-02-27 [1] CRAN (R 4.2.3)
#>  lhs            1.1.5       2022-03-22 [1] CRAN (R 4.2.1)
#>  lifecycle      1.0.4       2023-11-07 [1] CRAN (R 4.2.3)
#>  listenv        0.9.0       2022-12-16 [1] CRAN (R 4.2.3)
#>  lme4           1.1-30      2022-07-08 [1] CRAN (R 4.2.1)
#>  lubridate      1.9.0       2022-11-06 [1] CRAN (R 4.2.2)
#>  magrittr       2.0.3       2022-03-30 [1] CRAN (R 4.2.1)
#>  MASS           7.3-58.1    2022-08-03 [1] CRAN (R 4.2.1)
#>  Matrix         1.5-3       2022-11-11 [1] CRAN (R 4.2.2)
#>  minqa          1.2.4       2014-10-09 [1] CRAN (R 4.2.1)
#>  modeldata    * 1.3.0       2024-01-21 [1] CRAN (R 4.2.3)
#>  modelenv       0.1.0       2022-10-17 [1] CRAN (R 4.2.2)
#>  munsell        0.5.1       2024-04-01 [1] CRAN (R 4.2.3)
#>  nlme           3.1-159     2022-08-09 [1] CRAN (R 4.2.1)
#>  nloptr         2.0.3       2022-05-26 [1] CRAN (R 4.2.1)
#>  nnet           7.3-17      2022-01-16 [1] CRAN (R 4.2.1)
#>  pacman         0.5.1       2019-03-11 [1] CRAN (R 4.2.1)
#>  parallelly     1.36.0      2023-05-26 [1] CRAN (R 4.2.3)
#>  parsnip      * 1.2.1.9001  2024-04-11 [1] Github (tidymodels/parsnip@853d814)
#>  pillar         1.9.0       2023-03-22 [1] CRAN (R 4.2.3)
#>  pkgconfig      2.0.3       2019-09-22 [1] CRAN (R 4.2.1)
#>  prodlim        2023.03.31  2023-04-02 [1] CRAN (R 4.2.3)
#>  purrr        * 1.0.2       2023-08-10 [1] CRAN (R 4.2.3)
#>  R.cache        0.16.0      2022-07-21 [1] CRAN (R 4.2.2)
#>  R.methodsS3    1.8.2       2022-06-13 [1] CRAN (R 4.2.0)
#>  R.oo           1.25.0      2022-06-12 [1] CRAN (R 4.2.0)
#>  R.utils        2.12.2      2022-11-11 [1] CRAN (R 4.2.2)
#>  R6             2.5.1       2021-08-19 [1] CRAN (R 4.2.1)
#>  ranger       * 0.15.1      2023-04-03 [1] CRAN (R 4.2.3)
#>  Rcpp           1.0.9       2022-07-08 [1] CRAN (R 4.2.1)
#>  recipes      * 1.0.10.9000 2024-04-11 [1] Github (tidymodels/recipes@63ced27)
#>  reprex         2.0.2       2022-08-17 [1] CRAN (R 4.2.1)
#>  rlang        * 1.1.3       2024-01-10 [1] CRAN (R 4.2.3)
#>  rmarkdown      2.25        2023-09-18 [1] CRAN (R 4.2.3)
#>  rpart          4.1.16      2022-01-24 [1] CRAN (R 4.2.1)
#>  rsample      * 1.2.0       2023-08-23 [1] CRAN (R 4.2.3)
#>  rstudioapi     0.15.0.9000 2024-02-29 [1] Github (rstudio/rstudioapi@5b639f9)
#>  scales       * 1.3.0       2023-11-28 [1] CRAN (R 4.2.3)
#>  sessioninfo    1.2.2       2021-12-06 [1] CRAN (R 4.2.1)
#>  styler         1.10.2      2023-08-29 [1] CRAN (R 4.2.3)
#>  survival       3.4-0       2022-08-09 [1] CRAN (R 4.2.1)
#>  tibble       * 3.2.1       2023-03-20 [1] CRAN (R 4.2.3)
#>  tidymodels   * 1.1.1       2023-08-24 [1] CRAN (R 4.2.3)
#>  tidyr        * 1.3.1       2024-01-24 [1] CRAN (R 4.2.3)
#>  tidyselect     1.2.1       2024-03-11 [1] CRAN (R 4.2.3)
#>  timechange     0.1.1       2022-11-04 [1] CRAN (R 4.2.2)
#>  timeDate       4022.108    2023-01-07 [1] CRAN (R 4.2.3)
#>  tune         * 1.2.1       2024-04-18 [1] CRAN (R 4.2.3)
#>  utf8           1.2.4       2023-10-22 [1] CRAN (R 4.2.3)
#>  vctrs          0.6.5       2023-12-01 [1] CRAN (R 4.2.3)
#>  withr          3.0.1       2024-07-31 [1] CRAN (R 4.2.1)
#>  workflows    * 1.1.4.9000  2024-04-22 [1] Github (tidymodels/workflows@89005f5)
#>  workflowsets * 1.0.1       2023-04-06 [1] CRAN (R 4.2.3)
#>  xfun           0.42        2024-02-08 [1] CRAN (R 4.2.3)
#>  yaml           2.3.8       2023-12-11 [1] CRAN (R 4.2.3)
#>  yardstick    * 1.3.0       2024-01-19 [1] CRAN (R 4.2.3)
#> 
#>  [1] C:/Users/saariea/R-4.2.1/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

@simonpcouch
Copy link
Contributor

Just noting that I can reproduce this and 1) don't see the failure with tune_grid() nor 2) with detection_prevalence on its own! Will revisit this when we're focused in on the package again—thanks for the reprex.

@EmilHvitfeldt EmilHvitfeldt added the bug an unexpected problem or unintended behavior label Sep 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

3 participants