3

How can I fit a model using this tidymodels workflow?

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 +`.
David Rubinger
  • 3,580
  • 1
  • 20
  • 29
  • seems like you'd need to figure where (and ideally **why**) this restriction is imposed ... working around it could break something downstream, if all of the package machinery is assuming that the intercept is present ... – Ben Bolker Aug 31 '21 at 20:51

1 Answers1

2

You can use the formula argument to add_model() to override the terms of the model. This is typically used for survival and Bayesian models, so be extra careful that you know what you are doing here, because you are circumventing some of the guardrails of tidymodels by doing this:

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

Created on 2021-09-01 by the reprex package (v2.0.1)

Julia Silge
  • 10,848
  • 2
  • 40
  • 48
  • 1
    Thanks! Is there a reason why this needs a workaround rather than part of the standard workflow? – David Rubinger Sep 01 '21 at 19:38
  • Yes. It's a bit long to explain in a comment here, but I'll point you to [this discussion of how the formula is overloaded with multiple purposes in R](https://www.tmwr.org/base-r.html#formula) and how [tidymodels handles the "blueprint" of training data to later be used at prediction time](https://hardhat.tidymodels.org/reference/default_formula_blueprint.html). – Julia Silge Sep 02 '21 at 16:32
  • @JuliaSilge would you consider a simpler interface for removing the intercept in a recipe using step_rm? – kennyB May 13 '22 at 02:16