-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRest_API.Rmd
More file actions
215 lines (165 loc) · 4 KB
/
Copy pathRest_API.Rmd
File metadata and controls
215 lines (165 loc) · 4 KB
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
---
title: "Rest API for MCL"
output: html_notebook
---
```{r}
library(tidymodels)
library(vetiver)
library(plumber)
```
##The XGBoost model to launch
##XGBoost Model with combined fearure selected set - "parsimonious"
#Split dataset into training and test set.
#Make folds for cross-validation
```{r}
library(tidymodels)
library(doParallel)
set.seed(102)
mcl_split <- initial_split(MCL_parsimony, strata=status)
mcl_train <- training(mcl_split)
mcl_test <- testing(mcl_split)
mcl_folds<-vfold_cv(mcl_train, strata=status)
```
#Data preprocessing steps with recipe. Note: this will create new levels
in a factor variable. This is okay with the XGBoost model
```{r}
mcl_recipe<-recipe(status ~.,data=mcl_train) %>%
update_role(pt_id, new_role = "id variable") %>%
step_other(all_nominal_predictors(), threshold=0.10) %>%
step_dummy(all_nominal_predictors()) %>%
step_nzv(all_nominal_predictors())
mcl_prep <- prep(mcl_recipe)
juice(mcl_prep)
```
#Tune hyperparameters
```{r}
xgb_spec <- boost_tree(
trees= 2000,
tree_depth = tune(),
min_n = tune(),
loss_reduction = tune(),
sample_size = tune(),
mtry= tune(),
learn_rate = tune()) %>%
set_engine("xgboost") %>%
set_mode("classification")
xgb_spec
```
#Use grid based / space fitting to cover hyperparameter space
```{r}
xgb_grid <- grid_latin_hypercube(
min_n(),
sample_size=sample_prop(),
tree_depth(),
loss_reduction (),
learn_rate(),
finalize(mtry(), mcl_train),
size = 50
)
xgb_grid
```
#Finalize workflow with recipe and hyperparameter tuning specifications
```{r}
xgb_wf <- workflow() %>%
add_recipe(mcl_recipe) %>%
add_model(xgb_spec)
xgb_wf
```
#Set up parallel processing #Tune using the grid of hyperparameters and
the resampled folds #You may get warnings for new levels created for
sparse features - the hyperparameter fit will still work.
```{r}
doParallel::registerDoParallel()
set.seed(102)
xgb_res <- tune_grid(
xgb_wf,
resamples = mcl_folds,
grid = xgb_grid,
control = control_grid(save_pred = TRUE)
)
xgb_res
```
#Collect metrics
```{r}
collect_metrics(xgb_res)
```
#Visualize hyperparameter metrics for the possible models
```{r}
xgb_res %>%
collect_metrics() %>%
filter(.metric == "roc_auc") %>%
select(mean, mtry:sample_size) %>%
pivot_longer(mtry:sample_size,
values_to = "value",
names_to = "parameter"
) %>%
ggplot(aes(value, mean, color = parameter)) +
geom_point(alpha = 0.8, show.legend = FALSE) +
facet_wrap(~parameter, scales = "free_x") +
labs(x = NULL, y = "AUC")
```
#Show the best performing set of parameters
```{r}
show_best(xgb_res, "roc_auc")
```
#Select best hyperparameters based on AUC
```{r}
best_auc <- select_best(xgb_res, "roc_auc")
best_auc
```
#Finalize workflow with the hyperparameter values
```{r}
final_xgb <-
finalize_workflow(xgb_wf, best_auc)
final_xgb
```
##Incorporate the hyperparameter values into the last fit and fit the
model on the test set.
```{r}
xgb_last_pars <- final_xgb %>%
last_fit(mcl_split) %>% mutate(model="Parsimonious")
```
#Evaluate model performance.
```{r}
final_res <- last_fit(final_xgb, mcl_split)
collect_metrics(final_res)
```
#Make a confusion matrix showing predictions.
```{r}
collect_predictions(xgb_last_pars) %>%
conf_mat(status, .pred_class)
```
#Which variables were most important to the prediction? Make Variable
Importance Plot (VIP).
```{r}
library(vip)
xgb_fit <-extract_fit_parsnip(final_res)
vip(xgb_fit, geom="col", num_features=24, aesthetics=list(color="grey50", fill=" dark blue"))
```
##Deploy the model
```{r}
mcl <- final_res %>%
extract_workflow() %>%
vetiver_model("MCL")
mcl
```
```{r}
augment(mcl, slice_sample(mcl_test, n=10))
```
#Set up API
```{r}
##pipe to "pr_run()"
pr() %>%
vetiver_api(mcl)
```
#For probabilities instead of class use this.
```{r}
pr() %>%
vetiver_api(mcl, type="prob") %>%
pr_run()
```
#Generate plumber and docker files
```{r}
vetiver_write_plumber(mcl, "HAHill/MCL_predict", rsconnect = FALSE)
vetiver_write_docker(mcl)
```