Chapter 12 Model building

To combine the data preparation with the model building, we use the package workflows.

A workflow is an object that can bundle together your pre-processing, modeling, and post-processing requests.

12.1 Specify model

lm_spec <- 
  linear_reg() %>% 
  set_engine("lm") %>% 
  set_mode(mode = "regression")

12.2 Create workflow

lm_wflow <-
 workflow() %>%
 add_model(lm_spec) %>% 
 add_recipe(housing_rec)

12.3 Evaluate model

Now we can fit the model and collect the performance metrics with collect_metrics():

lm_wflow_eval <- 
  lm_wflow %>% 
  fit_resamples(
    median_house_value ~ ., 
    resamples = cv_folds
    ) 

lm_wflow_eval%>% 
    collect_metrics()
## # A tibble: 2 x 6
##   .metric .estimator      mean     n  std_err .config             
##   <chr>   <chr>          <dbl> <int>    <dbl> <chr>               
## 1 rmse    standard   68760.        5 853.     Preprocessor1_Model1
## 2 rsq     standard       0.648     5   0.0111 Preprocessor1_Model1