-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path07 - r-udfs-modeling.qmd
211 lines (124 loc) · 3.92 KB
/
07 - r-udfs-modeling.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
---
title: "Modeling"
---
## Catch up
```{r}
library(sparklyr)
library(dplyr)
sc <- spark_connect(method = "databricks_connect")
```
## 01 - Get sample of data
*Download a sampled data set locally to R*
1. Create a pointer to the `lendingclub` data. It is in the `samples` schema
```{r}
lendingclub_dat <- tbl(sc, I("workshops.samples.lendingclub"))
```
2. Using `slice_sample()`, download 20K records, and name it `lendingclub_sample`
```{r}
```
3. Preview the data using the `View()` command
```{r}
```
4. Keep only `int_rate`, `term`, `bc_util`, `bc_open_to_buy` and `all_util`
fields. Remove the percent sign out of `int_rate`, and coerce it to numeric.
Save resulting table to a new variable called `lendingclub_prep`
```{r}
```
5. Preview the data using `glimpse()`
```{r}
```
6. Disconnect from Spark
```{r}
```
## 02 - Create model using `tidymodels`
1. Run the following code to create a `workflow` that contains the pre-processing
steps, and a linear regression model
```{r}
library(tidymodels)
lendingclub_rec <- recipe(int_rate ~ ., data = lendingclub_prep) |>
step_mutate(term = trimws(substr(term, 1,2))) |>
step_mutate(across(everything(), 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_wf
```
2. Fit the model in the workflow, now in a variable called `lendingclub_wf`, with
the `lendingclub_prep` data
```{r}
```
3. Measure the performance of the model using `metrics()`. Make sure to use
`augment()` to add the predictions first
```{r}
```
4. Run a histogram over the predictions
```{r}
```
## 03 - Using Vetiver
*Convert the workflow into a `vetiver` model*
1. Load the `vetiver` package
```{r}
```
2. Convert to Vetiver using `vetiver_model()`. Name the variable `lendingclub_vetiver`
```{r}
```
3. Save `lendingclub_vetiver` as "lendingclub.rds"
```{r}
```
## 04 - Create prediction function
*Creating a self-contained prediction function that will read the model, and then run the predictions*
1. Create a very simple function that takes `x`, and it assumes it will be a
data frame. Inside the function, it will read the "lendingclub.rds" file, and
then use it to predict against `x`. Name the function `predict_vetiver`
```{r}
```
2. Test the `predict_vetiver` function against `lendingclub_prep`
```{r}
```
3. Modify the function, so that it will add the predictions back to `x`. The
new variable should be named `ret_pred`. The function should output the
modified `x`
```{r}
```
4. Test the `predict_vetiver` function against `lendingclub_prep`
```{r}
predict_vetiver(lendingclub_prep)
```
5. At the beginning of the function, load the `workflows` and `vetiver` libraries
```{r}
```
6. Test the `predict_vetiver` function against `lendingclub_prep`
```{r}
```
## 05 - Predict in Spark
*Run the predictions in Spark against the entire data set*
1. Add conditional statement that reads the RDS file if it's available locally,
and if not, read it from: "/Volumes/workshops/models/vetiver/lendingclub.rds"
```{r}
```
2. Re-connect to your Spark cluster
```{r}
```
3. Create a pointer to the `lendingclub` data. It is in the `samples` schema
```{r}
lendingclub_dat <- tbl(sc, I("workshops.samples.lendingclub"))
```
4. Select the `int_rate`, `term`, `bc_util`, `bc_open_to_buy`, and `all_util`
fields from `lendingclub_dat`. And then pass just the top rows (using `head()`)
to `spark_apply()`. Use the updated `predict_vetiver` to run the model.
```{r}
```
5. Add the `columns` specification to the `spark_apply()` call
```{r}
```
6. Append `compute()` to the end of the code, remove `head()`, and save the
results into a variable called `lendingclub_predictions`
```{r}
```
7. Preview the `lendingclub_predictions` table
```{r}
```