12.3 Put it all together

  1. Split the ames data (rsample)
  2. Create a lm_spec (parsnip)
  3. Create a pca_rec recipe (recipe)
  4. Create a workflow (workflows)
  5. Fit model (tune)

12.3.1 Solution

ames_split <- rsample::initial_split(ames)
ames_train <- rsample::training(ames_split)
ames_test <- rsample::testing(ames_split)
lm_spec <- parsnip::linear_reg() %>%
  parsnip::set_engine("lm")
pca_rec <-
  recipe(Sale_Price ~ ., data = ames_train) %>%
  step_novel(all_nominal()) %>%
  step_dummy(all_nominal()) %>%
  step_zv(all_predictors()) %>%
  step_center(all_predictors()) %>%
  step_scale(all_predictors()) %>%
  step_pca(all_predictors(), num_comp = 5)
pca_wf <- workflows::workflow() %>%
  workflows::add_recipe(pca_rec) %>%
  workflows::add_model(lm_spec)
pca_wf
## ══ Workflow ═════════════════════════════════════════════════════════════════
## Preprocessor: Recipe
## Model: linear_reg()
## 
## ── Preprocessor ─────────────────────────────────────────────────────────────
## 6 Recipe Steps
## 
## ● step_novel()
## ● step_dummy()
## ● step_zv()
## ● step_center()
## ● step_scale()
## ● step_pca()
## 
## ── Model ────────────────────────────────────────────────────────────────────
## Linear Regression Model Specification (regression)
## 
## Computational engine: lm
pca_wf %>%
  tune::last_fit(ames_split) %>%
  tune::collect_metrics()
## # A tibble: 2 x 3
##   .metric .estimator .estimate
##   <chr>   <chr>          <dbl>
## 1 rmse    standard   40883.   
## 2 rsq     standard       0.710