UseMethod("required_pkgs") 错误:没有适用于 'required_pkgs' 的方法应用于 class "workflow" 的对象

Error in UseMethod("required_pkgs") : no applicable method for 'required_pkgs' applied to an object of class "workflow"

我正在关注 Jan Kirenz tutorial 使用 Tidymodel 进行分类。到目前为止一切顺利,直到我尝试使用函数 fit_resamples() 评估模型。我不断收到错误消息 Error in UseMethod("required_pkgs") : no applicable method for 'required_pkgs' applied to an object of class "workflow" .

他在该部分使用的代码是:

log_res <- 
  log_wflow %>% 
  fit_resamples(
    resamples = cv_folds, 
    metrics = metric_set(
      recall, precision, f_meas, 
      accuracy, kap,
      roc_auc, sens, spec),
    control = control_resamples(
      save_pred = TRUE)
    ) 

我尝试使用函数 documentation page 中的示例,但我收到了相同的错误消息。

library(tidymodels)

set.seed(6735)
folds <- vfold_cv(mtcars, v = 5)

spline_rec <- recipe(mpg ~ ., data = mtcars) %>%
  step_ns(disp) %>%
  step_ns(wt)

lin_mod <- linear_reg() %>%
  set_engine("lm")

control <- control_resamples(save_pred = TRUE)

spline_res <- fit_resamples(lin_mod, spline_rec, folds, control = control)

#Error in UseMethod("required_pkgs") : no applicable method for 'required_pkgs' applied to an object of class "workflow"

有谁知道这里的问题是什么,我该如何解决?我找不到任何关于此问题的提及。

这是我的 sessionInfo():

R version 4.1.1 (2021-08-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

Random number generation:
 RNG:     Mersenne-Twister 
 Normal:  Inversion 
 Sample:  Rounding 
 
locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] workflowsets_0.1.0 tune_0.1.6         modeldata_0.1.1    infer_1.0.0        dials_0.0.9        scales_1.1.1       broom_0.7.9       
 [8] tidymodels_0.1.3   yardstick_0.0.8    GGally_2.1.2       keras_2.6.0        xgboost_1.4.1.1    ranger_0.13.1      parsnip_0.1.7     
[15] recipes_0.1.16     workflows_0.2.3    rsample_0.1.0      skimr_2.1.3        visdat_0.5.3       gt_0.3.1           forcats_0.5.1     
[22] stringr_1.4.0      dplyr_1.0.7        purrr_0.3.4        readr_2.0.1        tidyr_1.1.3        tibble_3.1.4       ggplot2_3.3.5     
[29] tidyverse_1.3.1   

loaded via a namespace (and not attached):
 [1] colorspace_2.0-2   ellipsis_0.3.2     class_7.3-19       base64enc_0.1-3    fs_1.5.0           rstudioapi_0.13    listenv_0.8.0     
 [8] furrr_0.2.3        farver_2.1.0       bit64_4.0.5        prodlim_2019.11.13 fansi_0.5.0        lubridate_1.7.10   xml2_1.3.2        
[15] codetools_0.2-18   splines_4.1.1      knitr_1.33         zeallot_0.1.0      jsonlite_1.7.2     pROC_1.18.0        dbplyr_2.1.1      
[22] png_0.1-7          tfruns_1.5.0       compiler_4.1.1     httr_1.4.2         backports_1.2.1    assertthat_0.2.1   Matrix_1.3-4      
[29] fastmap_1.1.0      cli_3.0.1          htmltools_0.5.2    tools_4.1.1        gtable_0.3.0       glue_1.4.2         Rcpp_1.0.7        
[36] DiceDesign_1.9     cellranger_1.1.0   vctrs_0.3.8        iterators_1.0.13   timeDate_3043.102  gower_0.2.2        xfun_0.25         
[43] globals_0.14.0     rvest_1.0.1        lifecycle_1.0.0    future_1.22.1      MASS_7.3-54        ipred_0.9-11       vroom_1.5.4       
[50] hms_1.1.0          parallel_4.1.1     RColorBrewer_1.1-2 yaml_2.2.1         curl_4.3.2         reticulate_1.20    sass_0.4.0        
[57] rpart_4.1-15       reshape_0.8.8      stringi_1.7.4      tensorflow_2.6.0   foreach_1.5.1      checkmate_2.0.0    lhs_1.1.1         
[64] hardhat_0.1.6      lava_1.6.10        repr_1.1.3         rlang_0.4.11       pkgconfig_2.0.3    lattice_0.20-44    labeling_0.4.2    
[71] bit_4.0.4          tidyselect_1.1.1   parallelly_1.27.0  plyr_1.8.6         magrittr_2.0.1     R6_2.5.1           generics_0.1.0    
[78] DBI_1.1.1          pillar_1.6.2       haven_2.4.3        whisker_0.4        withr_2.4.2        survival_3.2-11    nnet_7.3-16       
[85] future.apply_1.8.1 modelr_0.1.8       crayon_1.4.1       utf8_1.2.2         tzdb_0.1.2         grid_4.1.1         readxl_1.3.1      
[92] data.table_1.14.0  reprex_2.0.1       digest_0.6.27      GPfit_1.0-8        munsell_0.5.0     

当我附加名为 tune 的包时,你问题中的第二个块工作正常。我认为通过 library(tidymodels) 包装器将 tidymodels 系列附加到您的工作区是一种更好的方法,而不是单独附加。

如果 tidymodels 包安装正确,(运行 a <- require(tidymodels)a 应该合乎逻辑 TRUE)这段代码将起作用;

library(tidymodels)

set.seed(6735)
folds <- vfold_cv(mtcars, v = 5)

spline_rec <- recipe(mpg ~ ., data = mtcars) %>%
  step_ns(disp) %>%
  step_ns(wt)

lin_mod <- linear_reg() %>%
  set_engine("lm")

control <- control_resamples(save_pred = TRUE)

spline_res <- fit_resamples(lin_mod, spline_rec, folds, control = control)