10.1 Exercise: Train on split data

ames <- AmesHousing::make_ames()
ames_split <- rsample::initial_split(ames,
                                    strata = Sale_Price,
                                    breaks = 4)
ames_train  <- training(ames_split)
ames_test   <- testing(ames_split)
lm_spec <- parsnip::linear_reg() %>%
  parsnip::set_engine("lm")

lm_fit <- workflows::workflow() %>%
  workflows::add_formula(Sale_Price ~ Gr_Liv_Area) %>%
  workflows::add_model(lm_spec) %>%
  parsnip::fit(data = ames_train)
price_pred <- lm_fit %>%
  stats::predict(new_data = ames_test) %>%
  dplyr::mutate(price_truth = ames_test$Sale_Price)
yardstick::rmse(price_pred, truth = price_truth, estimate = .pred)
## # A tibble: 1 x 3
##   .metric .estimator .estimate
##   <chr>   <chr>          <dbl>
## 1 rmse    standard      53810.