forked from sol-eng/bike_predict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument.qmd
233 lines (182 loc) · 4.71 KB
/
document.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
---
title: "Model Step 1 - Train and Deploy Model"
date: "`r lubridate::now(tzone = 'EST')` EST"
output: html_document
---
This notebook trains a model to predict the number of bikes at a given bike docking station. The model is trained using the *bike_model_data* table from *Content DB*. The trained model is then:
- pinned to RStudio Connect
- deployed as a plumber API to Rstudio Connect using vetiver.
```{r setup, include=FALSE}
knitr::opts_chunk$set(warning = FALSE, message = FALSE, collapse = TRUE)
library(dplyr)
library(dbplyr)
library(glue)
library(recipes)
library(parsnip)
library(workflows)
library(vetiver)
library(rsconnect)
# Internally developed packages
library(bikeHelpR)
# The following packages are not directly called, but are requirements. They
# need to be here so that `rsconnect::writeManifest` is able to capture them
# as a dependency.
library(ranger)
```
## Get data
Connect to the database:
```{r connect_to_db}
con <- DBI::dbConnect(odbc::odbc(), "Content DB")
```
Split the data into a train/test split:
```{r train_test_split_data}
all_days <- tbl(con, "bike_model_data")
# Get a vector that contains all of the dates.
dates <- all_days %>%
distinct(date) %>%
collect() %>%
arrange(desc(date)) %>%
pull(date) %>%
as.Date()
# Split the data into test and train.
n_days_test <- 2
n_days_to_train <- 10
train_end_date <- dates[n_days_test + 1]
train_start_date <- train_end_date - n_days_to_train
# Training data split.
train_data <- all_days %>%
filter(
date >= train_start_date,
date <= train_end_date
) %>%
distinct() %>%
collect()
print(glue::glue(
"The model will be trained on data from {start} to {end} ",
"({num_obs} observations). ",
start = min(train_data$date),
end = max(train_data$date),
num_obs = scales::comma(nrow(train_data)),
))
# Test data split.
test_data <- all_days %>%
filter(date > train_end_date) %>%
distinct() %>%
collect()
print(glue::glue(
"The model will be tested on data from {start} to {end} ",
"({num_obs} observations). ",
start = min(test_data$date),
end = max(test_data$date),
num_obs = scales::comma(nrow(test_data)),
))
```
## Train the model
### Data preprocessing
Define a recipe to clean the data.
```{r define_recipe}
# Define a recipe to clean the data.
recipe_spec <-
recipe(n_bikes ~ ., data = train_data) %>%
step_dummy(dow) %>%
step_integer(id, date)
# Preview the cleaned training data.
recipe_spec %>%
prep(train_data) %>%
bake(head(train_data)) %>%
glimpse()
```
### Fit model
Fit a random forest model:
```{r fit_model}
model_spec <-
rand_forest() %>%
set_mode("regression") %>%
set_engine("ranger")
model_workflow <-
workflow() %>%
add_recipe(recipe_spec) %>%
add_model(model_spec)
model_fit <- fit(model_workflow, data = train_data)
model_fit
```
## Model evaluation
```{r evaluate_model}
predictions <- predict(model_fit, test_data)
results <- test_data %>%
mutate(preds = predictions$.pred)
oos_metrics(results$n_bikes, results$preds)
```
## Model deployment
### `vetiver`
Create a `vetiver` model object.
```{r create_vetiver_model}
model_name <- "bike_predict_model_r"
pin_name <- glue("sam.edwardes/{model_name}")
# Get the train and test data ranges. This will be passed into the pin metadata
# so that other scripts can access this information.
date_metadata <- list(
train_dates = c(
as.character(min(train_data$date)),
as.character(max(train_data$date))
),
test_dates = c(
as.character(min(test_data$date)),
as.character(max(test_data$date))
)
)
print(date_metadata)
# Create the vetiver model.
v <- vetiver_model(
model_fit,
model_name,
versioned = TRUE,
save_ptype = train_data %>%
head(1) %>%
select(-n_bikes),
metadata = date_metadata
)
v
```
### `pins`
Save the model as a *pin* to RStudio Connect:
```{r pin_model}
# Use RStudio Connect as a board.
board <- pins::board_rsconnect(
server = Sys.getenv("CONNECT_SERVER"),
key = Sys.getenv("CONNECT_API_KEY"),
versioned = TRUE
)
# Write the model to the board.
board %>%
vetiver_pin_write(vetiver_model = v)
```
### `plumber`
Then, deploy the model as a plumber API to RStudio Connect.
```{r deploy_model_api_to_connect}
# Add server
rsconnect::addServer(
url = "https://colorado.posit.co/rsc/__api__",
name = "colorado"
)
# Add account
rsconnect::connectApiUser(
account = "sam.edwardes",
server = "colorado",
apiKey = Sys.getenv("CONNECT_API_KEY"),
)
# Deploy to Connect
vetiver_deploy_rsconnect(
board = board,
name = pin_name,
appId = "11314",
launch.browser = FALSE,
appTitle = "Bike Predict - Model - API",
predict_args = list(debug = FALSE),
account = "sam.edwardes",
server = "colorado"
)
```
```{r close_db_connection}
DBI::dbDisconnect(con)
```