8.3 USING RECIPES

Preprocessing is part of a modeling workflow

lm_model <- linear_reg() %>% 
  set_engine("lm")

lm_wflow <- workflow() %>%
  add_model(lm_model)

lm_wflow <- 
  lm_wflow %>% 
  add_formula(Sale_Price ~ Longitude + Latitude)

# lm_wflow %>% 
#   add_recipe(simple_ames)
  
lm_wflow <- lm_wflow %>% 
  workflows::remove_formula() %>%
  add_recipe(simple_ames)
lm_fit <- fit(lm_wflow, ames_train)
predict(lm_fit, ames_test %>% slice(1:3))
## # A tibble: 3 × 1
##     .pred
##     <dbl>
## 1 154932.
## 2 192414.
## 3 262260.
lm_fit %>% 
  extract_recipe(estimated = TRUE)
lm_fit %>% 
  # This returns the parsnip object:
  extract_fit_parsnip() %>% 
  # Now tidy the linear model object:
  tidy() %>% 
  slice(1:5)
## # A tibble: 5 × 5
##   term                        estimate std.error statistic   p.value
##   <chr>                          <dbl>     <dbl>     <dbl>     <dbl>
## 1 (Intercept)                -2431230.  118494.    -20.5   1.25e- 85
## 2 Gr_Liv_Area                  263557.    7231.     36.4   3.48e-227
## 3 Year_Built                      900.      59.5    15.1   2.94e- 49
## 4 Neighborhood_College_Creek     2098.    4235.      0.495 6.20e-  1
## 5 Neighborhood_Old_Town          4194.    4277.      0.981 3.27e-  1