R - Tidymodels - 在 workflow_set 中使用 glm 模型时出错
R - Tidymodels - Error when using a glm model within a workflow_set
我正在尝试使用 tidymodels
包将许多 glm
模型拟合到我的数据集,select 表现最好的模型。由于我想尝试预处理和模型规范的不同组合,我使用 workflow_set()
函数生成它们,并使用 workflow_map()
来拟合它们并获得性能指标。
但是,在将 workflow_map()
应用到工作流时,我收到一个神秘错误:
Error in if (rlang::call_name(x) == "tune") {: argument is of length zero
我能发现的最多的是这个错误是由 check_fn()
函数在 workflow_map()
函数中返回的,但我不明白我的代码中的潜在问题。
如何在 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 package (v2.0.1)
创建于 2022-06-02
您收到此错误是因为您传递给 family
的内容已命名空间。
将 family = stats::gaussian("log")
切换为 family = gaussian("log")
将解决此错误。错误报告已 filed here。
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 package (v2.0.1)
于 2022-06-03 创建
我正在尝试使用 tidymodels
包将许多 glm
模型拟合到我的数据集,select 表现最好的模型。由于我想尝试预处理和模型规范的不同组合,我使用 workflow_set()
函数生成它们,并使用 workflow_map()
来拟合它们并获得性能指标。
但是,在将 workflow_map()
应用到工作流时,我收到一个神秘错误:
Error in if (rlang::call_name(x) == "tune") {: argument is of length zero
我能发现的最多的是这个错误是由 check_fn()
函数在 workflow_map()
函数中返回的,但我不明白我的代码中的潜在问题。
如何在 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 package (v2.0.1)
创建于 2022-06-02您收到此错误是因为您传递给 family
的内容已命名空间。
将 family = stats::gaussian("log")
切换为 family = gaussian("log")
将解决此错误。错误报告已 filed here。
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 package (v2.0.1)
于 2022-06-03 创建