Hello!
I'm learning my way through the very nifty tidymodels ecosystem. I'm currently stumped with how to extract the bare fitted model object from a workflow after fitting. Specifically, I'm trying to do an anova
on a lm regression model. pull_workflow_fit()
gives me an object back with a model_fit
and a _lm
class, but not a bare lm
object (that would be accepted by anova
for instance.
I'm sure I'm just missing a step from my understanding of the tidymodels ecosystem. Appreciate the help. Reprex is below!
library(tidymodels)
#> ── Attaching packages ────────────────────────────────────── tidymodels 0.1.2 ──
#> ✓ broom 0.7.2 ✓ recipes 0.1.15
#> ✓ dials 0.0.9 ✓ rsample 0.0.8
#> ✓ dplyr 1.0.2 ✓ tibble 3.0.4
#> ✓ ggplot2 3.3.2 ✓ tidyr 1.1.2
#> ✓ infer 0.5.3 ✓ tune 0.1.2
#> ✓ modeldata 0.1.0 ✓ workflows 0.2.1
#> ✓ parsnip 0.1.4 ✓ yardstick 0.0.7
#> ✓ purrr 0.3.4
#> ── Conflicts ───────────────────────────────────────── tidymodels_conflicts() ──
#> x purrr::discard() masks scales::discard()
#> x dplyr::filter() masks stats::filter()
#> x dplyr::lag() masks stats::lag()
#> x recipes::step() masks stats::step()
lm_mod <- linear_reg() %>%
set_mode("regression") %>%
set_engine("lm")
recipe1 <- recipe(mpg ~ cyl, data = mtcars)
recipe2 <- recipe(mpg ~ cyl + hp, data = mtcars)
wf <- workflow() %>%
add_recipe(recipe1) %>%
add_model(lm_mod)
model1 <- wf %>% fit(mtcars) %>% pull_workflow_fit()
model2 <- wf %>% update_recipe(recipe2) %>% fit(mtcars) %>% pull_workflow_fit()
anova(model1, model2)
#> Error in UseMethod("anova"): no applicable method for 'anova' applied to an object of class "c('_lm', 'model_fit')"