-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy path07-db-modeling.Rmd
237 lines (173 loc) · 6.36 KB
/
07-db-modeling.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
```{r db-modeling, include = FALSE}
eval_model <- FALSE
if(Sys.getenv("GLOBAL_EVAL") != "") eval_model <- Sys.getenv("GLOBAL_EVAL")
```
```{r, eval = eval_model, include = FALSE}
library(tidymodels)
library(yaml)
library(modeldb)
library(dbplot)
library(pins)
library(dbplyr)
library(RPostgres)
library(connections)
library(config)
```
# Modeling with databases
## Single step sampling
*Use PostgreSQL TABLESAMPLE clause*
1. Use `connection_open()` to open a Database connection
```{r, eval = eval_model}
con <- connection_open(
RPostgres::Postgres(),
host = "localhost",
user = get("user"),
password = get("pwd"),
port = 5432,
dbname = "postgres",
bigint = "integer"
)
```
2. Set the `orders` variable to point to the **orders** table
```{r, eval = eval_model}
orders <- tbl(con, in_schema("retail", "orders"))
```
3. Set the `orders_view` variable to point to the **v_orders** table
```{r, eval = eval_model}
```
4. Pipe `orders` into the function `show_query()`
```{r, eval = eval_model}
```
5. Pipe the previous command into the `class()` function to see the kind of output `show_query()` returns
```{r, eval = eval_model}
```
6. Replace `show_query()` with `remote_query()` to compare the output types
```{r, eval = eval_model}
```
7. Replace `class()` with `build_sql()`. Use `con` as the value for the `con` argument
```{r, eval = eval_model}
```
8. Add *" TABLESAMPLE BERNOULLI (0.1)"* to `build_sql()` as another `...` argument
```{r, eval = eval_model}
```
9. Pipe the code into `tbl()`. Use `con` for the `con` argument, and `.` for the rest
```{r, eval = eval_model}
```
10. Use `inner_join()` to add the information from the `orders_view` pointer, use `order_id` as the matching field
```{r, eval = eval_model}
```
11. Assign the resulting code to a variable `orders_sample_db`
```{r, eval = eval_model}
orders_sample_db <-
```
12. Use `collect()` to load the results of `orders_sample_db` to a new variable called `orders_sample`
```{r, eval = eval_model}
orders_sample <- collect(orders_sample_db)
```
13. Load the `dbplot` library
```{r, eval = eval_model}
library(dbplot)
```
14. Use `dbplot_histogram()` to visualize the distribution of `order_total` from `orders_sample`
```{r, eval = eval_model}
orders_sample %>%
dbplot_histogram(order_total, binwidth = 5)
```
15. Use `dbplot_histogram()` to visualize the distribution of `order_total` from `orders_view`
```{r, eval = eval_model}
orders_view %>%
dbplot_histogram(order_total, binwidth = 5)
```
## Using `tidymodels` for modeling
*Fit and measure the model's performance using functions from `parsnip` and `yardstick`*
1. Load the `tidymodels` library
```{r, eval = eval_model}
library(tidymodels)
```
2. Start with the `linear_reg()` command, pipe into `set_engine()`, and use *"lm"* as its sole argument
```{r, eval = eval_model}
```
3. Pipe into the `fit()` command. Use the formula: `order_total ~ order_qty`, and `orders_sample` as the `data` argument
```{r, eval = eval_model}
```
4. Assign the previous code to a variable called `parsnip_model`
```{r, eval = eval_model}
parsnip_model <-
```
5. Use `bind_cols()` to add the predictions to `order_sample`. Calculate the prediction with `predict()`
```{r, eval = eval_model}
orders_sample %>%
bind_cols(predict(parsnip_model, orders_sample))
```
6. Pipe the code into the `metrics()` function. Use `order_total` as the `truth` argument, and `.pred` as the `estimate` argument
```{r, eval = eval_model}
```
## Score with `tidypredict`
1. Load the `tidypredict` library
```{r, eval = eval_model}
library(tidypredict)
```
2. Use the `parse_model()` function to parse `parsnip_model`, and assign it to a variable called `parsed_parsnip`
```{r, eval = eval_model}
```
3. Use `str()` to see the `parsed_parsnip` object's structure
```{r, eval = eval_model}
```
4. Use `tidypredict_fit()` to view the `dplyr` formula that calculates the prediction
```{r, eval = eval_model}
```
5. Use `head()` to get the first 10 records from `orders_view`
```{r, eval = eval_model}
orders_view %>%
head(10)
```
6. Pipe the code into `mutate()`. Assign to a new `my_pred` variable the results of `tidypredict_fit()`. Make sure to prefix `tidypredict_fit()` with the bang-bang operator so that the formula is evaluated.
```{r, eval = eval_model}
orders_view %>%
head(10) %>%
```
7. Replace the `mutate()` command with `tidypredict_to_column()`
```{r, eval = eval_model}
orders_view %>%
head(10) %>%
tidypredict_to_column(parsnip_model)
```
8. Load the `yaml` library
```{r, eval = eval_model}
library(yaml)
```
9. Use `write_yaml()` to save the contents of `parsed_parsnip` into a file called **model.yaml**
```{r, eval = eval_model}
```
10. Using `read_yaml()`, read the contents of the **model.yaml** file into the a new variable called `loaded_model`
```{r, eval = eval_model}
```
11. Use `as_parsed_model()` to convert the `loaded_model` variable into a `tidypredict` parsed model object, assign the results to `loaded_model_2`
```{r, eval = eval_model}
loaded_model_2 <- as_parsed_model(loaded_model)
```
## Run predictions in DB
1. Load the `modeldb` library
```{r, eval = eval_model}
library(modeldb)
```
2. Use `select()` to pick the `order_total` and `order_qty` fields from the `orders_sample_db` table pointer
```{r, eval = eval_model}
orders_sample_db %>%
select(order_total, order_qty)
```
3. Pipe the code into the `linear_regression_db()` function, pass `order_total` as the only argument
```{r, eval = eval_model}
orders_sample_db %>%
select(order_total, order_qty) %>%
```
4. Assign the model results to a new variable called `db_model`
```{r, eval = eval_model}
db_model <-
```
5. Use `as_parsed_model()` to convert `db_model` to a parsed model object. Assign to new a variable called `pm`
```{r, eval = eval_model}
```
6. Use `head()` to get the top 10 records, and then pipe into `tidypredict_to_column()` to add the results from `pm`
```{r, eval = eval_model}
```