【问题标题】:How to fit a model without an intercept using R tidymodels workflow?如何使用 R tidymodels 工作流程拟合没有截距的模型?
【发布时间】:2021-08-31 20:12:21
【问题描述】:

如何使用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 +`.

【问题讨论】:

  • 似乎您需要弄清楚施加此限制的位置(理想情况下为什么)...解决它可能会破坏下游的某些东西,如果所有的包装机械假设存在截距...

标签: r tidymodels r-parsnip


【解决方案1】:

您可以使用add_model()formula 参数来覆盖模型的条款。这通常用于生存模型和贝叶斯模型,所以要格外小心,知道自己在这里做什么,因为这样做会绕过 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 package (v2.0.1) 于 2021-09-01 创建

【讨论】:

猜你喜欢
  • 2018-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-14
相关资源
最近更新 更多