forked from haerski/mortality
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaseline_models.Rmd
304 lines (270 loc) · 8.88 KB
/
baseline_models.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
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
# Baseline model
The goal of this documents is to compare some tuned and untuned models on a "baseline" dataset. We want to predict whether AE in 2020 goes above 3. We are only using 2019 data here! Next steps will be adding time-dependent data (e.g. COVID deaths per week per zipcode).
## Common data
```{r}
library(tidyverse)
library(tidymodels)
library(probably)
library(themis)
library(feather)
library(magrittr)
library(skimr)
library(vip)
per <- read_feather("data/simulation_data/all_persons.feather")
```
Compute some summary statistic for each client.
```{r}
clients <-
per %>%
group_by(client) %>%
summarize(
zip3 = first(zip3),
size = n(),
volume = sum(FaceAmt),
avg_qx = mean(qx),
avg_age = mean(Age),
per_male = sum(Sex == "Male") / size,
per_blue_collar = sum(collar == "blue") / size,
expected = sum(qx * FaceAmt),
actual_2020 = sum(FaceAmt[year == 2020], na.rm = TRUE),
ae_2020 = actual_2020 / expected,
actual_2019 = sum(FaceAmt[year == 2019], na.rm = TRUE),
ae_2019 = actual_2019 / expected,
adverse = as_factor(if_else(ae_2020 > 3, "ae > 3", "ae < 3"))
) %>%
relocate(adverse, ae_2020, .after = zip3) %>%
mutate(adverse = fct_relevel(adverse, c("ae > 3", "ae < 3")))
```
We can add some demographic information based on zip3.
```{r}
zip_data <-
read_feather("data/data.feather") %>%
mutate(
density = POP / AREALAND,
AREALAND = NULL,
AREA = NULL,
HU = NULL,
vaccinated = NULL,
per_lib = NULL,
per_green = NULL,
per_other = NULL,
per_rep = NULL,
unempl_2020 = NULL,
deaths_covid = NULL,
deaths_all = NULL
) %>%
rename(
unemp = unempl_2019,
hes_uns = hes_unsure,
str_hes = strong_hes,
income = Median_Household_Income_2019
)
```
There seems to be some clients with some zip codes that we cannot deal with. These are the ones
```{r}
clients %>%
anti_join(zip_data, by = "zip3") %>%
select(zip3)
```
These correspond to the following areas
ZIP3 | Area |
-----|------------|
969 | Guam, Palau, Federated States of Micronesia, Northern Mariana Islands, Marshall Islands |
093 | Military bases in Iraq and Afghanistan |
732 | Not in use |
872 | Not in use |
004 | Not in use |
202 | Washington DC, Government 1 |
We ignore clients with these zip codes. There are also two clients in DC for which we're missing election data. We will ignore those as well.
```{r}
clients %<>%
inner_join(zip_data, by = "zip3") %>%
drop_na()
```
We now have our full dataset. Behold!
```{r}
skim(clients)
```
## Workflow set
We'll evaluate models using a workflow set. To make our life easier, we will remove some variables and use a formula instead of a recipe.
```{r}
clients <-
clients %>%
select(-client, -zip3, -ae_2020, -actual_2020, -actual_2019)
```
We now gather our recipes and models.
```{r}
with2019_rec <-
recipe(adverse ~ ., data = clients) %>%
step_zv(all_predictors()) %>%
step_normalize(all_predictors(), -all_nominal())
no2019_rec <-
with2019_rec %>%
step_rm(ae_2019)
log_spec <-
logistic_reg() %>%
set_engine("glm") %>%
set_mode("classification")
tuned_log_spec <-
logistic_reg(penalty = 0.00118) %>%
set_engine("glmnet") %>%
set_mode("classification")
forest_spec <-
rand_forest(trees = 1000) %>%
set_mode("classification") %>%
set_engine("ranger", num.threads = 8, importance = "impurity", seed = 123)
tuned_forest_spec <-
rand_forest(trees = 1000, mtry = 12, min_n = 21) %>%
set_mode("classification") %>%
set_engine("ranger", num.threads = 8, importance = "impurity", seed = 123)
# Samara's models
sln_spec <-
mlp() %>%
set_engine("nnet") %>%
set_mode("classification")
svm_rbf_spec <-
svm_rbf() %>%
set_engine("kernlab") %>%
set_mode("classification")
svm_poly_spec <-
svm_poly() %>%
set_engine("kernlab") %>%
set_mode("classification")
knn_spec <-
nearest_neighbor() %>%
set_engine("kknn") %>%
set_mode("classification")
models <- list(log = log_spec,
logtuned = tuned_log_spec,
forest = forest_spec,
foresttuned = tuned_forest_spec,
neural = sln_spec,
svmrbf = svm_rbf_spec,
svmpoly = svm_poly_spec,
knnspec = knn_spec)
recipes <- list("with2019ae" = with2019_rec,
"no2019ae" = no2019_rec)
wflows <- workflow_set(recipes, models)
```
Data splitting
```{r}
set.seed(30308)
init <- initial_split(clients, strata = adverse)
set.seed(30308)
crossval <- vfold_cv(training(init), strata = adverse)
```
Fit all models and estimate metrics using 10-fold cross-validation. We're not performing any tuning here (although we could do that very easily!!!).
```{r collapse = TRUE}
fit_wflows <-
wflows %>%
workflow_map(fn = "fit_resamples",
seed = 30332,
resamples = crossval,
control = control_resamples(save_pred = TRUE),
metrics = metric_set(roc_auc, sens, accuracy),
verbose = TRUE)
```
Comparing our metrics for the models (unfortunately I couldn't figure out how to show which recipe was picked...)
```{r rank_baseline_models}
autoplot(fit_wflows, metric = "roc_auc")
autoplot(fit_wflows)
fit_wflows %>% collect_metrics()
```
This graph now shows which recipe was picked
```{r ae2019_improvement}
fit_wflows %>%
collect_metrics() %>%
separate(wflow_id, into = c("rec", "mod"), sep = "_", remove = FALSE) %>%
ggplot(aes(x = rec, y = mean, color = mod, group = mod)) +
geom_point() + geom_line() + facet_wrap(~ factor(.metric))
```
Looks like adding the 2019 AE didn't help much! This is evidence for our hypothesis (AE 2019 doesn't hhave much effect on the final outcome).
It's also possible that the machine learning models (svn and neural net) could benefit from some tuning.
Here are the models ranked by `roc_auc`
```{r}
fit_wflows %>% rank_results("roc_auc") %>% select(wflow_id) %>% unique()
```
We will pick `no2019ae_forest` as the "final" model.
```{r}
final_wflow <-
fit_wflows %>%
pull_workflow_set_result("no2019ae_forest")
```
Right now, given a test case, tries to find the probability that `ae > 3`. If that number is greater than 0.5, the model predicts `ae > 3`, if not, the model predicts `ae < 3`. This threshold of 0.5 can be changed, which will affect specificity and sensitivity. For the 10 models coming from the 10-fold CV, we compute specificity and sensitivity for all threshold in `seq(0.5, 1, 0.01)`, that is a grid from 0.5 to 1 with step of 0.01. We then average the estimate for the 10 models to get the following plot (could have also drawn error bars...)
```{r choosing_bin_threshold}
final_wflow <-
final_wflow %>%
rowwise() %>%
mutate(thr_perf = list(threshold_perf(.predictions, adverse, `.pred_ae > 3`, thresholds = seq(0.5, 1, by = 0.01))))
final_wflow %>%
select(thr_perf, id) %>%
unnest(thr_perf) %>%
group_by(.threshold, .metric) %>%
summarize(estimate = mean(.estimate)) %>%
filter(.metric != "distance") %>%
ggplot(aes(x = .threshold, y = estimate, color = .metric)) + geom_line()
```
We can now choose the threshold based on what we need. (what do we need?)
## Tuning some models
```{r collapse = TRUE}
tune_log_spec <-
logistic_reg(penalty = tune()) %>%
set_engine("glmnet") %>%
set_mode("classification")
tune_forest_spec <-
rand_forest(trees = 1000, mtry = tune(), min_n = tune()) %>%
set_mode("classification") %>%
set_engine("ranger", num.threads = 8, importance = "impurity", seed = 123)
# Samara's models
tune_sln_spec <-
mlp(hidden_units = tune(), penalty = tune(), epochs = tune()) %>%
set_engine("nnet") %>%
set_mode("classification")
tune_svm_rbf_spec <-
svm_rbf(cost = tune(), rbf_sigma = tune(), margin = tune()) %>%
set_engine("kernlab") %>%
set_mode("classification")
# tune_svm_poly_spec <-
# svm_poly() %>%
# set_engine("kernlab") %>%
# set_mode("classification")
# tune_knn_spec <-
# nearest_neighbor() %>%
# set_engine("kknn") %>%
# set_mode("classification")
models <- list(log = tune_log_spec,
forest = tune_forest_spec,
sln = tune_sln_spec,
svm = tune_svm_rbf_spec)
recipes <- list(no2019_rec)
wflows <- workflow_set(recipes, models)
# make a bigger grid!
# or use something like finetune!
results <-
wflows %>%
workflow_map(resamples = crossval,
grid = 10,
metrics = metric_set(roc_auc, accuracy, sens, spec, ppv, npv),
control = control_grid(save_pred = TRUE),
seed = 828282,
verbose = TRUE)
autoplot(results)
```
As a example, let's look at the results for the svm.
We won't touch the prediction thresholds here.
```{r}
tuned_svm <-
results %>%
pull_workflow_set_result("recipe_svm")
best_svm <-
tuned_svm %>%
select_best(metric = "roc_auc")
last_svm <-
results %>%
pull_workflow("recipe_svm") %>%
finalize_workflow(best_svm) %>%
last_fit(init,
metrics = metric_set(roc_auc, accuracy, sens, spec, ppv, npv))
last_svm %>%
collect_metrics()
```