R - Tidymodels -在workflow_set中使用glm模型时出错



我试图将许多glm模型拟合到我的数据集,使用tidymodels包选择性能最好的一个。因为我想尝试预处理和模型规范的不同组合,所以我使用workflow_set()函数来生成它们,使用workflow_map()来拟合它们并获得性能指标。

然而,当将workflow_map()应用到工作流时,我得到一个神秘的错误:

if (rlang::call_name(x) == "tune"){:参数长度为零

我最多能发现的是这个错误是由workflow_map()函数中的check_fn()函数返回的,但是我不明白我的代码中的潜在问题。

如何在workflow_set中使用glm模型?

library(tidyverse)
library(tidymodels)
data("mtcars")
glm_recipe = recipe(hp ~ disp, data = mtcars)
glm_model = linear_reg() %>%
set_engine("glm", family = stats::gaussian("log"))
glm_workflows = workflow_set(list(glm_recipe), list(glm_model))
# fitting and predicting works
glm_workflows %>% extract_workflow("recipe_linear_reg") %>% fit(mtcars) %>% predict(mtcars)
#> # A tibble: 32 x 1
#>    .pred
#>    <dbl>
#>  1  117.
#>  2  117.
#>  3  103.
#>  4  151.
#>  5  195.
#>  6  138.
#>  7  195.
#>  8  113.
#>  9  112.
#> 10  120.
#> # ... with 22 more rows
# this doesn't work
workflow_map(glm_workflows)
#> Error in if (rlang::call_name(x) == "tune") {: argument is of length zero
#> Execution stopped; returning current results
#> # A workflow set/tibble: 1 x 4
#>   wflow_id          info             option    result    
#>   <chr>             <list>           <list>    <list>    
#> 1 recipe_linear_reg <tibble [1 x 4]> <opts[0]> <list [0]>

由reprex包(v2.0.1)创建于2022-06-02

你得到这个错误,因为你传递给family是命名空间。将family = stats::gaussian("log")family = gaussian("log")切换将解决此错误。这里有一个bug报告。

library(tidyverse)
library(tidymodels)
data("mtcars")
glm_recipe = recipe(hp ~ disp, data = mtcars)
glm_model = linear_reg() %>%
set_engine("glm", family = gaussian("log"))
glm_workflows = workflow_set(list(glm_recipe), list(glm_model))
splits = vfold_cv(mtcars)
workflow_map(glm_workflows, resamples = splits)
#> # A workflow set/tibble: 1 × 4
#>   wflow_id          info             option    result   
#>   <chr>             <list>           <list>    <list>   
#> 1 recipe_linear_reg <tibble [1 × 4]> <opts[1]> <rsmp[+]>

由reprex包(v2.0.1)创建于2022-06-03

最新更新