如何调整 tidymodels 配方参数?

How to tune a tidymodels recipe parameter?

描述

我正在尝试调整配方中的自定义参数,但我很难做到。我的印象是,这只是在配方中调用 tune("variableID") 然后将 tune_grid() 与包含列 variableID.

的网格一起使用的问题

然而,这似乎不起作用,所以我想出了一个 reprex 来说明我的方法。请注意 tune_grid() 甚至没有检测到调整参数。我在这里做错了什么?

我也尝试研究其他人是如何做的,但我在这里能找到的唯一接近我的问题的问题是 并且 tune() 的使用与预期的一样。

代表

library(tidyverse)
library(tidymodels)
#> Registered S3 method overwritten by 'tune':
#>   method                   from   
#>   required_pkgs.model_spec parsnip

iris_splits <- vfold_cv(iris)

glmnet_recipe <- 
  recipe(formula = Species ~ Sepal.Length + Petal.Length, data = iris) %>% 
  step_mutate(Sepal.Length = round(Sepal.Length), digits = tune("digits")) %>% 
  step_zv(all_predictors()) %>% 
  step_normalize(all_predictors(), -all_nominal()) 

glmnet_spec <- 
  multinom_reg(penalty = 0, mixture = 0) %>% 
  set_mode("classification") %>% 
  set_engine("glmnet") 

glmnet_workflow <- 
  workflow() %>% 
  add_recipe(glmnet_recipe) %>% 
  add_model(glmnet_spec) 

glmnet_grid <- expand_grid(digits = c(0, 1))

glmnet_tune <- 
  tune_grid(glmnet_workflow, resamples = iris_splits, grid = glmnet_grid) 
#> Warning: No tuning parameters have been detected, performance will be evaluated
#> using the resamples with no tuning. Did you want to [tune()] parameters?
#> x Fold01: preprocessor 1/1: Error: Problem with `mutate()` column `digits`.
#> i `dig...
#> x Fold02: preprocessor 1/1: Error: Problem with `mutate()` column `digits`.
#> i `dig...
#> x Fold03: preprocessor 1/1: Error: Problem with `mutate()` column `digits`.
#> i `dig...
#> x Fold04: preprocessor 1/1: Error: Problem with `mutate()` column `digits`.
#> i `dig...
#> x Fold05: preprocessor 1/1: Error: Problem with `mutate()` column `digits`.
#> i `dig...
#> x Fold06: preprocessor 1/1: Error: Problem with `mutate()` column `digits`.
#> i `dig...
#> x Fold07: preprocessor 1/1: Error: Problem with `mutate()` column `digits`.
#> i `dig...
#> x Fold08: preprocessor 1/1: Error: Problem with `mutate()` column `digits`.
#> i `dig...
#> x Fold09: preprocessor 1/1: Error: Problem with `mutate()` column `digits`.
#> i `dig...
#> x Fold10: preprocessor 1/1: Error: Problem with `mutate()` column `digits`.
#> i `dig...
#> Warning: All models failed. See the `.notes` column.

glmnet_tune$.notes[[1]]$.notes
#> [1] "preprocessor 1/1: Error: Problem with `mutate()` column `digits`.\ni `digits = tune(\"digits\")`.\nx `digits` must be a vector, not a call."
show_best(glmnet_tune)
#> Warning: No value of `metric` was given; metric 'roc_auc' will be used.
#> Error: All of the models failed. See the .notes column.

reprex package (v2.0.0)

于 2021-06-15 创建 会话信息
sessioninfo::session_info()
#> - Session info ---------------------------------------------------------------
#>  setting  value                       
#>  version  R version 4.0.5 (2021-03-31)
#>  os       Windows 10 x64              
#>  system   x86_64, mingw32             
#>  ui       RTerm                       
#>  language (EN)                        
#>  collate  German_Germany.1252         
#>  ctype    German_Germany.1252         
#>  tz       Europe/Berlin               
#>  date     2021-06-15                  
#> 
#> - Packages -------------------------------------------------------------------
#>  package      * version     date       lib source                             
#>  assertthat     0.2.1       2019-03-21 [1] CRAN (R 4.0.3)                     
#>  backports      1.2.1       2020-12-09 [1] CRAN (R 4.0.3)                     
#>  broom        * 0.7.6       2021-04-05 [1] CRAN (R 4.0.5)                     
#>  cellranger     1.1.0       2016-07-27 [1] CRAN (R 4.0.3)                     
#>  class          7.3-18      2021-01-24 [2] CRAN (R 4.0.5)                     
#>  cli            2.5.0       2021-04-26 [1] CRAN (R 4.0.3)                     
#>  codetools      0.2-18      2020-11-04 [2] CRAN (R 4.0.5)                     
#>  colorspace     2.0-1       2021-05-04 [1] CRAN (R 4.0.5)                     
#>  crayon         1.4.1       2021-02-08 [1] CRAN (R 4.0.4)                     
#>  DBI            1.1.0       2019-12-15 [1] CRAN (R 4.0.3)                     
#>  dbplyr         2.1.1       2021-04-06 [1] CRAN (R 4.0.5)                     
#>  dials        * 0.0.9       2020-09-16 [1] CRAN (R 4.0.4)                     
#>  DiceDesign     1.9         2021-02-13 [1] CRAN (R 4.0.4)                     
#>  digest         0.6.27      2020-10-24 [1] CRAN (R 4.0.3)                     
#>  dplyr        * 1.0.6       2021-05-05 [1] CRAN (R 4.0.5)                     
#>  ellipsis       0.3.2       2021-04-29 [1] CRAN (R 4.0.3)                     
#>  evaluate       0.14        2019-05-28 [1] CRAN (R 4.0.3)                     
#>  fansi          0.5.0       2021-05-25 [1] CRAN (R 4.0.5)                     
#>  fastmap        1.1.0       2021-01-25 [1] CRAN (R 4.0.5)                     
#>  forcats      * 0.5.1       2021-01-27 [1] CRAN (R 4.0.5)                     
#>  foreach        1.5.1       2020-10-15 [1] CRAN (R 4.0.3)                     
#>  fs             1.5.0       2020-07-31 [1] CRAN (R 4.0.3)                     
#>  furrr          0.2.2       2021-01-29 [1] CRAN (R 4.0.5)                     
#>  future         1.21.0      2020-12-10 [1] CRAN (R 4.0.3)                     
#>  generics       0.1.0       2020-10-31 [1] CRAN (R 4.0.3)                     
#>  ggplot2      * 3.3.3       2020-12-30 [1] CRAN (R 4.0.4)                     
#>  glmnet       * 4.1-1       2021-02-21 [1] CRAN (R 4.0.5)                     
#>  globals        0.14.0      2020-11-22 [1] CRAN (R 4.0.3)                     
#>  glue           1.4.2       2020-08-27 [1] CRAN (R 4.0.3)                     
#>  gower          0.2.2       2020-06-23 [1] CRAN (R 4.0.3)                     
#>  GPfit          1.0-8       2019-02-08 [1] CRAN (R 4.0.4)                     
#>  gtable         0.3.0       2019-03-25 [1] CRAN (R 4.0.3)                     
#>  hardhat        0.1.5       2020-11-09 [1] CRAN (R 4.0.4)                     
#>  haven          2.3.1       2020-06-01 [1] CRAN (R 4.0.3)                     
#>  highr          0.9         2021-04-16 [1] CRAN (R 4.0.5)                     
#>  hms            1.0.0       2021-01-13 [1] CRAN (R 4.0.5)                     
#>  htmltools      0.5.1.9003  2021-05-07 [1] Github (rstudio/htmltools@e12171e) 
#>  httr           1.4.2       2020-07-20 [1] CRAN (R 4.0.5)                     
#>  infer        * 0.5.4.9000  2021-03-27 [1] Github (tidymodels/infer@66d24a0)  
#>  ipred          0.9-11      2021-03-12 [1] CRAN (R 4.0.4)                     
#>  iterators      1.0.13      2020-10-15 [1] CRAN (R 4.0.3)                     
#>  jsonlite       1.7.2       2020-12-09 [1] CRAN (R 4.0.3)                     
#>  knitr          1.33        2021-04-24 [1] CRAN (R 4.0.5)                     
#>  lattice        0.20-41     2020-04-02 [2] CRAN (R 4.0.5)                     
#>  lava           1.6.9       2021-03-11 [1] CRAN (R 4.0.4)                     
#>  lhs            1.1.1       2020-10-05 [1] CRAN (R 4.0.4)                     
#>  lifecycle      1.0.0       2021-02-15 [1] CRAN (R 4.0.4)                     
#>  listenv        0.8.0       2019-12-05 [1] CRAN (R 4.0.3)                     
#>  lubridate      1.7.10      2021-02-26 [1] CRAN (R 4.0.4)                     
#>  magrittr       2.0.1       2020-11-17 [1] CRAN (R 4.0.3)                     
#>  MASS           7.3-53.1    2021-02-12 [2] CRAN (R 4.0.5)                     
#>  Matrix       * 1.3-2       2021-01-06 [2] CRAN (R 4.0.5)                     
#>  modeldata    * 0.1.0       2020-10-22 [1] CRAN (R 4.0.4)                     
#>  modelr         0.1.8       2020-05-19 [1] CRAN (R 4.0.3)                     
#>  munsell        0.5.0       2018-06-12 [1] CRAN (R 4.0.3)                     
#>  nnet           7.3-15      2021-01-24 [2] CRAN (R 4.0.5)                     
#>  parallelly     1.25.0      2021-04-30 [1] CRAN (R 4.0.5)                     
#>  parsnip      * 0.1.5.9003  2021-05-22 [1] Github (tidymodels/parsnip@46a2018)
#>  pillar         1.6.1       2021-05-16 [1] CRAN (R 4.0.5)                     
#>  pkgconfig      2.0.3       2019-09-22 [1] CRAN (R 4.0.3)                     
#>  plyr           1.8.6       2020-03-03 [1] CRAN (R 4.0.3)                     
#>  pROC           1.17.0.1    2021-01-13 [1] CRAN (R 4.0.4)                     
#>  prodlim        2019.11.13  2019-11-17 [1] CRAN (R 4.0.4)                     
#>  ps             1.6.0       2021-02-28 [1] CRAN (R 4.0.5)                     
#>  purrr        * 0.3.4       2020-04-17 [1] CRAN (R 4.0.3)                     
#>  R6             2.5.0       2020-10-28 [1] CRAN (R 4.0.3)                     
#>  Rcpp           1.0.6       2021-01-15 [1] CRAN (R 4.0.4)                     
#>  readr        * 1.4.0       2020-10-05 [1] CRAN (R 4.0.5)                     
#>  readxl         1.3.1       2019-03-13 [1] CRAN (R 4.0.3)                     
#>  recipes      * 0.1.16.9000 2021-05-29 [1] Github (tidymodels/recipes@0806713)
#>  reprex         2.0.0       2021-04-02 [1] CRAN (R 4.0.5)                     
#>  rlang        * 0.4.11.9000 2021-05-29 [1] Github (r-lib/rlang@7797cdf)       
#>  rmarkdown      2.8.1       2021-05-07 [1] Github (rstudio/rmarkdown@e98207f) 
#>  rpart          4.1-15      2019-04-12 [1] CRAN (R 4.0.5)                     
#>  rsample      * 0.1.0       2021-05-08 [1] CRAN (R 4.0.5)                     
#>  rstudioapi     0.13        2020-11-12 [1] CRAN (R 4.0.3)                     
#>  rvest          1.0.0       2021-03-09 [1] CRAN (R 4.0.5)                     
#>  scales       * 1.1.1       2020-05-11 [1] CRAN (R 4.0.3)                     
#>  sessioninfo    1.1.1       2018-11-05 [1] CRAN (R 4.0.3)                     
#>  shape          1.4.6       2021-05-19 [1] CRAN (R 4.0.5)                     
#>  stringi        1.5.3       2020-09-09 [1] CRAN (R 4.0.3)                     
#>  stringr      * 1.4.0       2019-02-10 [1] CRAN (R 4.0.3)                     
#>  survival       3.2-10      2021-03-16 [2] CRAN (R 4.0.5)                     
#>  tibble       * 3.1.2       2021-05-16 [1] CRAN (R 4.0.5)                     
#>  tidymodels   * 0.1.3       2021-04-19 [1] CRAN (R 4.0.5)                     
#>  tidyr        * 1.1.3       2021-03-03 [1] CRAN (R 4.0.5)                     
#>  tidyselect     1.1.1       2021-04-30 [1] CRAN (R 4.0.3)                     
#>  tidyverse    * 1.3.1       2021-04-15 [1] CRAN (R 4.0.5)                     
#>  timeDate       3043.102    2018-02-21 [1] CRAN (R 4.0.3)                     
#>  tune         * 0.1.5.9000  2021-05-22 [1] Github (tidymodels/tune@b0e83a7)   
#>  utf8           1.2.1       2021-03-12 [1] CRAN (R 4.0.3)                     
#>  vctrs        * 0.3.8       2021-04-29 [1] CRAN (R 4.0.3)                     
#>  withr          2.4.2       2021-04-18 [1] CRAN (R 4.0.5)                     
#>  workflows    * 0.2.2       2021-03-10 [1] CRAN (R 4.0.4)                     
#>  workflowsets * 0.0.2       2021-04-16 [1] CRAN (R 4.0.5)                     
#>  xfun           0.22        2021-03-11 [1] CRAN (R 4.0.5)                     
#>  xml2           1.3.2       2020-04-23 [1] CRAN (R 4.0.5)                     
#>  yaml           2.2.1       2020-02-01 [1] CRAN (R 4.0.3)                     
#>  yardstick    * 0.0.8       2021-03-28 [1] CRAN (R 4.0.5)                     
#> 
#> [1] C:/Users/Albert/Documents/R/win-library/4.0
#> [2] C:/Program Files/R/R-4.0.5/library

配方步骤 需要有一个 tunable S3 方法用于任何你想调整的参数, 比如 digits.

查看 this article about creating your own recipe step, but I don't think you need to create your own recipe step altogether; you only need to make a tunable method for the step you are using, which is under "Other step methods"