/
05-Workflows.Rmd
109 lines (80 loc) · 2.4 KB
/
05-Workflows.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
---
title: "05-Workflows"
output: html_document
---
```{r setup, include=FALSE}
library(tidyverse)
library(AmesHousing)
library(tidymodels)
library(tune)
library(workflows)
ames <- make_ames() %>%
dplyr::select(-matches("Qu"))
set.seed(100)
ames_split <- initial_split(ames)
ames_train <- training(ames_split)
ames_test <- testing(ames_split)
fit_data <- function(object, model, data, ...) {
if (inherits(object, "formula")) {
object <- add_model(add_formula(workflow(), object, blueprint = hardhat::default_formula_blueprint(indicators = FALSE, ...)))
}
fit(object, data, ...)
}
fit_split <- function(object, model, split, ...) {
if (inherits(object, "formula")) {
object <- add_model(add_formula(workflow(), object, blueprint = hardhat::default_formula_blueprint(indicators = FALSE)), model)
}
tune::last_fit(object, split, ...)
}
# pca_rec <-
# recipe(Sale_Price ~ ., data = ames) %>%
# 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)
```
# Your Turn 1
Build a workflow that uses a linear model to predict `Sale_Price` with `Bedrooms_AbvGr`, `Full_Bath` and `Half_Bath` in `ames`. Save it as `bb_wf`.
```{r}
lm_spec <-
___________ %>%
___________ %>%
___________
bb_wf <-
___________ %>%
___________ %>%
___________
```
# Your Turn 2
Test the linear model that predicts `Sale_Price` with _everything else in `ames`_ on `ames_split`. What RMSE do you get?
Hint: Create a new workflow by updating `bb_wf`.
```{r}
all_wf <-
bb_wf %>%
___________
___________(all_wf, split = ___________) %>%
collect_metrics()
```
# Your Turn 3
Fill in the blanks to test the regression tree model that predicts `Sale_Price` with _everything else in `ames`_ on `ames_split`. What RMSE do you get?
*Hint: Create a new workflow by updating `all_wf`.*
```{r}
rt_spec <-
___________() %>%
set_engine(engine = "___________") %>%
set_mode("___________")
rt_wf <-
___________ %>%
___________
___________ %>%
collect_metrics()
```
# Your Turn 4
But what about the predictions of our model? Save the fitted object from your regression tree, and use `collect_predictions()` to see the predictions generated from the test data.
```{r}
all_fitwf <- ___________
___________ %>%
___________
```