3

如何使用此tidymodels工作流程拟合模型?

library(tidymodels)
workflow() %>% 
  add_model(linear_reg() %>% set_engine("lm")) %>% 
  add_formula(mpg ~ 0 + cyl + wt) %>% 
  fit(mtcars)
#> Error: `formula` must not contain the intercept removal term: `+ 0` or `0 +`.
4

1 回答 1

2

您可以使用formula参数来add_model()覆盖模型的条款。这通常用于生存模型和贝叶斯模型,所以要格外小心,知道自己在这里做什么,因为这样做会绕过 tidymodels 的一些护栏:

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

mod <- linear_reg()
rec <- recipe(mpg ~ cyl + wt, data = mtcars)

workflow() %>%
  add_recipe(rec) %>%
  add_model(mod, formula = mpg ~ 0 + cyl + wt) %>%
  fit(mtcars)
#> ══ Workflow [trained] ══════════════════════════════════════════════════════════
#> Preprocessor: Recipe
#> Model: linear_reg()
#> 
#> ── Preprocessor ────────────────────────────────────────────────────────────────
#> 0 Recipe Steps
#> 
#> ── Model ───────────────────────────────────────────────────────────────────────
#> 
#> Call:
#> stats::lm(formula = mpg ~ 0 + cyl + wt, data = data)
#> 
#> Coefficients:
#>   cyl     wt  
#> 2.187  1.174

reprex 包于 2021-09-01 创建(v2.0.1)

于 2021-09-01T16:35:50.180 回答