6.3 Make Predictions

- Rules to Live by:
- Returns a tibble
- Column names are … erh Predictable
- Return the same number of rows as are in the data set
- some predict functions omit observations with
NAvalues. Which is great if that’s what you intend, but if you aren’t expecting that behavior you would have to find out the hard way.
- some predict functions omit observations with
# create example test set
ames_test_small <- ames_test %>% slice(1:5)
# predict on test set
predict(lm_form_fit, new_data = ames_test_small) %>%
knitr::kable()| .pred |
|---|
| 5.223697 |
| 5.221967 |
| 5.284242 |
| 5.239113 |
| 5.314692 |
- Combining
bind_colswith our predict function we can merge our predictions back to the test set.
# add predictions together with actuals
ames_test_small %>%
select(Sale_Price) %>%
bind_cols(predict(lm_form_fit, ames_test_small)) %>%
# Add 95% prediction intervals to the results:
bind_cols(predict(lm_form_fit, ames_test_small, type = "pred_int")) %>%
knitr::kable()| Sale_Price | .pred | .pred_lower | .pred_upper |
|---|---|---|---|
| 5.021189 | 5.223697 | 4.907039 | 5.540355 |
| 5.235528 | 5.221967 | 4.905310 | 5.538625 |
| 5.278525 | 5.284242 | 4.967563 | 5.600922 |
| 5.060698 | 5.239113 | 4.922441 | 5.555785 |
| 5.596808 | 5.314692 | 4.997995 | 5.631388 |