-
Notifications
You must be signed in to change notification settings - Fork 0
/
refit.qmd
307 lines (226 loc) · 7.36 KB
/
refit.qmd
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
---
title: "Refitting Models in Snowflake, using Posit Connect"
---
```{r setup}
#| echo: false
#| include: false
library(odbc)
library(DBI)
library(dbplyr)
library(tidyverse)
library(glue)
library(tidymodels)
library(vetiver)
library(pins)
library(arrow)
# Use SSH key if on Connect, otherwise use managed credentials
if (Sys.getenv("RSTUDIO_PRODUCT") == "CONNECT"){
# grab base64-encoded ssh key from environment variable and cache as tempfile
cached_key <- tempfile()
readr::write_file(openssl::base64_decode(Sys.getenv("SNOWFLAKE_SSH_KEY")), file = cached_key)
# The ambient credential feature in odbc::snowflake() causes unexpected overwrites, so we'll use the basic Snowflake driver.
# Change arguments to match your own credentials and other information
con <- dbConnect(
odbc::odbc(),
driver = "Snowflake",
server = paste0(Sys.getenv("SNOWFLAKE_ACCOUNT"), ".snowflakecomputing.com"),
uid = "SVC_SOLENG",
role = "SOLENG",
warehouse = "DEFAULT_WH",
database = "LENDING_CLUB",
schema = "PUBLIC",
# Settings to set Snowflake key-pair auth
authenticator = "SNOWFLAKE_JWT",
PRIV_KEY_FILE = cached_key
)
} else {
con <- dbConnect(
odbc::snowflake(),
warehouse = "DEFAULT_WH",
database = "LENDING_CLUB",
schema = "PUBLIC"
)
}
```
## Model Monitoring
First, let's find our active model version
<!--
IMPORTANT -- if you're running this for the first time, you may run into namespacing errors in Connect or permissioning errors in Snowflake. That means someone has already run this with the same `model_name` -- to run this flawlessly, pick a new model_name below.
-->
```{r}
model_name <- "interest_rate_prediction"
board <- board_connect()
model_versions <- board |>
pin_versions(glue("{board$account}/{model_name}"))
model_versions
```
```{r}
model_version <- model_versions |>
filter(active) |>
pull(version)
```
### Compute Performance Statistics
```{r}
lendingclub_dat <- con |> tbl("LOAN_DATA") |>
mutate(ISSUE_YEAR = as.integer(str_sub(ISSUE_D, start = 5)),
ISSUE_MONTH = as.integer(case_match(
str_sub(ISSUE_D, end = 3),
"Jan" ~ 1,
"Feb" ~ 2,
"Mar" ~ 3,
"Apr" ~ 4,
"May" ~ 5,
"Jun" ~ 6,
"Jul" ~ 7,
"Aug" ~ 8,
"Sep" ~ 9,
"Oct" ~ 10,
"Nov" ~ 11,
"Dec" ~ 12
))
) |>
filter(ISSUE_YEAR >= 2016, ISSUE_YEAR < 2018)
```
We'll use the view we fit with this model version to compute predictions
```{r}
lendingclub_predictions <- con |> tbl(glue("{model_name}_v{model_version}"))
existing_predictions <- lendingclub_dat |>
left_join(lendingclub_predictions, by = "ID") |>
select(ID, ISSUE_YEAR, ISSUE_MONTH, INT_RATE, .pred) |>
collect()
```
### Model Performance Over Time
```{r}
# Extract the metrics from our last model run
extracted_metrics <- board |>
pin_meta(glue("{board$account}/{model_name}")) %>%
pluck("user", "metrics") %>%
as_tibble()
metrics <- existing_predictions |>
mutate(date = ymd(glue("{ISSUE_YEAR}-{str_pad(ISSUE_MONTH, 2, pad=0)}-01"))) |>
arrange(date) |>
vetiver_compute_metrics(date, "month", INT_RATE, .pred)
metrics |>
vetiver_plot_metrics() +
geom_hline(aes(yintercept = .estimate, color = .metric),
data = extracted_metrics,
linewidth = 1.5, alpha = 0.7, lty = 2)
```
## Retraining our Model on New Data
Let's grab a sample of 20k rows to refit.
```{r}
lendingclub_prep <- lendingclub_dat |>
slice_sample(n = 20000) |>
select(INT_RATE, TERM, BC_UTIL, BC_OPEN_TO_BUY, ALL_UTIL) |>
mutate(
INT_RATE = as.numeric(stringr::str_remove(INT_RATE, "%"))
) |>
filter(!if_any(everything(), is.na)) |>
collect()
```
Now, let's refit our model on the new data!
```{r}
lendingclub_rec <- recipe(INT_RATE ~ ., data = lendingclub_prep) |>
step_mutate(TERM = (TERM == "60 months")) |>
step_mutate(across(!TERM, as.numeric)) |>
step_normalize(all_numeric_predictors()) |>
step_impute_mean(all_of(c("BC_OPEN_TO_BUY", "BC_UTIL"))) |>
step_filter(!if_any(everything(), is.na))
lendingclub_lr <- linear_reg()
lendingclub_wf <- workflow() |>
add_model(lendingclub_lr) |>
add_recipe(lendingclub_rec)
lendingclub_fit <- lendingclub_wf |>
fit(data = lendingclub_prep)
```
### Model Statistics
```{r}
lendingclub_metric_set <- metric_set(rmse, mae, rsq)
lendingclub_metrics <- lendingclub_fit |>
augment(lendingclub_prep) |>
lendingclub_metric_set(truth = INT_RATE, estimate = .pred)
lendingclub_metrics
```
### Selecting the best model
We'll select the best model based on which has the lowest RMSE
```{r}
#| output: asis
old_rmse <- extracted_metrics |>
filter(.metric == "rmse") |>
pull(.estimate) |>
round(digits = 4)
new_rmse <- lendingclub_metrics |>
filter(.metric == "rmse") |>
pull(.estimate) |>
round(digits = 4)
if (old_rmse > new_rmse) {
update_model <- TRUE
cat(
"\n::: {.callout-tip}",
"## Model Will Be Updated",
glue("New Model RMSE of {new_rmse} is lower than the Previous RMSE of {old_rmse}. Updating model in Snowflake!"),
":::\n",
sep = "\n"
)
} else {
update_model <- FALSE
cat(
"\n::: {.callout-important}",
"## Model Will Not Be Updated",
glue("New Model RMSE of {new_rmse} is higher than the Previous RMSE of {old_rmse}. No updates will occur. Please note that the cells following this block will not be evaluated."),
":::\n",
sep = "\n"
)
}
```
### Updating the Model
Note that all of these code blocks use `if (update_model){...}` -- that uses the update_model flag set in the previous code chunk to control execution.
#### Pin the new version to Posit Connect
```{r}
if (update_model){
v <- vetiver_model(lendingclub_fit, model_name, metadata = list(metrics = lendingclub_metrics))
board |>
vetiver_pin_write(v)
model_versions <- board |>
pin_versions(glue("{board$account}/{model_name}"))
model_version <- model_versions |>
filter(active) |>
pull(version)
}
```
#### Create an orbital object
```{r}
if (update_model){
library(orbital)
library(tidypredict)
orbital_obj <- orbital(lendingclub_fit)
## Add predictions column to table
res <- tbl(con, "LOAN_DATA") |>
mutate(!!!orbital_inline(orbital_obj))
# select only the prediction column and the ID column
pred_name <- ".pred"
res <- select(res, any_of(c("ID", pred_name)))
# Translate the dbplyr `tbl` into a sql query string
generated_sql <- remote_query(res)
}
```
Translate the generated SQL into a Snowflake View. We'll start by creating a 'versioned' view, linked to the version of the model we just fit.
```{r}
if (update_model){
versioned_view_name <- glue("{model_name}_v{model_version}")
snowflake_view_statement <- glue::glue_sql( "CREATE OR REPLACE VIEW {`versioned_view_name`} AS ", generated_sql, .con = con )
con |> DBI::dbExecute(snowflake_view_statement)
}
```
We'll next create a 'main' view, which we'll keep updated to match the latest version of the model. This will allow downstream projects to always reference this view, and get the latest updates.
```{r}
if (update_model){
main_view_name <- glue("{model_name}_latest")
main_view_statement <- glue::glue_sql(
"CREATE OR REPLACE VIEW {`main_view_name`} AS ",
"SELECT * FROM {`versioned_view_name`}",
.con = con
)
con |> DBI::dbExecute(main_view_statement)
}
```