-
Notifications
You must be signed in to change notification settings - Fork 4
/
09-judging_model_effectiveness.Rmd
245 lines (205 loc) · 9.72 KB
/
09-judging_model_effectiveness.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
# Judging model effectiveness
**Learning objectives:**
- Explain why **measures of model fit** to actual data **are important** even for purely inferential models.
- Use `{yardstick}` to compute regression metrics.
- Recognize `{yardstick}` function output.
- Create a metric set with `yardstick::metric_set()`.
- Use `{yardstick}` to compute binary classification metrics.
- Describe the common arguments for `{yardstick}` classification metrics.
- Visualize a binary classification model fit using `{yardstick}` and `{ggplot2}`.
- Use `{yardstick}` to compute multi-class classification metrics.
- Extend an explicitly binary metric such as `sensitivity()` to multiple classes using `{yardstick}`.
- Combine `{yardstick}` metrics with `dplyr::group_by()`.
- Visualize a model fit for different groups using `{yardstick}`, `{dplyr}`, and `{ggplot2}`
## Measures of Model Fit
- Empirical Validation: a quantitative approach for estimating effectiveness
- Focused on how close our predictions come to the observed data
- Optimization of statistical characteristics of the model does not imply that the model fits the data well
- Choice of which metrics to examine can be critical
![](images/performance-reg-metrics-1.png)
## Disclaimers
- These examples are to demonstrate metric evaluation not good data science!
- Explore the full set of metrics available through `{yardstick}`
- Talk through use cases for different metrics
![](images/horse-meme-examples.png)
## Regression Metrics
Load in the Data
```{r load-ames}
library(tidymodels)
library(glmnet)
library(ranger)
set.seed(1123)
data(ames)
ames <- ames %>%
mutate(
under_budget = as.factor(if_else(Sale_Price<=160000,1,0)),
Sale_Price = log10(Sale_Price))
```
```{r best-models-setup, eval=FALSE}
#Cross-fold validation
ames_folds <- vfold_cv(ames, v = 5)
#Create Recipe
ames_recipe <-
recipe(formula = Sale_Price ~ Gr_Liv_Area + Full_Bath + Half_Bath + Lot_Area + Neighborhood + Overall_Cond,
data = ames) %>%
step_dummy(all_nominal())
#Set the model and hyperparameters
ames_spec <-
linear_reg(penalty = tune(), mixture = tune()) %>%
set_mode("regression") %>%
set_engine("glmnet")
#Create workflow
ames_workflow <-
workflow() %>%
add_recipe(ames_recipe) %>%
add_model(ames_spec)
#Create metric set of all regression metrics
ames_tune <-
tune_grid(
ames_workflow,
metrics =
metric_set(rmse, rsq, rsq_trad, mae, mpe, mape, smape, mase, ccc, rpiq, rpd, huber_loss, huber_loss_pseudo, iic),
resamples = ames_folds,
grid = grid_latin_hypercube(penalty(), mixture(), size = 8)
)
#Pick the best model for each metric and pull out the predictions
best_models <-
tibble(
metric_name = c('rmse', 'rsq', 'rsq_trad', 'mae', 'mpe', 'mape', 'smape', 'mase',
'ccc','rpiq', 'rpd', 'huber_loss', 'huber_loss_pseudo', 'iic')) %>%
mutate(metric_best = map(metric_name, ~select_best(ames_tune, .x)),
wf_best = map(metric_best, ~finalize_workflow(ames_workflow, .x)),
fit_best = map(wf_best, ~fit(.x, data = ames)),
df_pred = map(fit_best, ~ames %>% bind_cols(predict(.x, new_data = ames)) %>% select(Sale_Price, .pred))) %>%
select(-c(wf_best, fit_best)) %>%
unnest(cols = c(metric_name, metric_best, df_pred))
#Plot!
best_models %>%
mutate(metric_desc = factor(
metric_name,
levels = c('rmse', 'rsq', 'rsq_trad', 'mae', 'mpe', 'mape', 'smape', 'mase',
'ccc','rpiq', 'rpd', 'huber_loss', 'huber_loss_pseudo', 'iic'),
labels = c('rmse\nwhen you cannot afford\n to have a big error',
'rsq\nwhen you want a measure\n of consistency/correlation\n and not accuracy',
'rsq_trad\n r-sq not constrained\n between 0 and 1',
'mae\nwhen large errors are not\n exponentially\n worse than small errors',
'mpe\nwhen you want an easy way\n to calculate accuracy',
'mape\nwhen you want to use mpe\n with a better\n representation of error',
'smape\nwhen you want to use\n mape expressed as a %',
'mase\nwhen you need a scale\n independent metric\n for time-series data',
'ccc\nwhen you want to measure\n the distance from \nperferct linearity',
'rpiq\nwhen you need a different\n measue of consistency/correlation\n and not accuracy',
'rpd\nwhen you need a different\n measue of consistency/correlation\n and not accuracy',
'huber_loss\nwhen you need a loss\n function less sensitive to outliers',
'huber_loss_pseudo\nwhen you need\n a smoothed version of huber_loss',
'iic\nwhen you need an\n alternative to the traditional\n correlation coefficient'))) %>%
ggplot(aes(x = Sale_Price, y = .pred)) +
geom_abline(lty = 2) +
geom_point(alpha = 0.5) +
labs(y = "Predicted Sale Price (log10)", x = "Sale Price (log10)") +
coord_obs_pred() +
facet_wrap(~metric_desc, ncol = 2) +
theme_minimal() +
theme(panel.spacing = unit(2, "lines"),
strip.text.x = element_text(size = 8))
```
![](images/09-best_models.png)
```{r best-models-load, include=FALSE}
best_models <- readRDS(here::here("data", "09-best_models.rds"))
```
```{r best-models}
best_models %>% select(metric_name, penalty, mixture) %>% distinct()
```
## Binary Classification Metrics
Note: This code might take several minutes (or longer) to run.
```{r binary-classification-setup, eval=FALSE}
#Cross-fold validation
ames_folds_binary <- vfold_cv(ames, v = 5)
#Create Recipe
ames_recipe_binary <-
recipe(formula = under_budget ~ Gr_Liv_Area + Full_Bath + Half_Bath + Lot_Area + Neighborhood + Overall_Cond,
data = ames)
#Set the model and hyperparameters
ames_spec_binary <-
rand_forest(mtry = tune(), trees = tune(), min_n = tune()) %>%
set_mode("classification") %>%
set_engine("ranger")
#Create workflow
ames_workflow_binary <-
workflow() %>%
add_recipe(ames_recipe_binary) %>%
add_model(ames_spec_binary)
#Create metric set of all binary metrics
ames_tune_binary <-
tune_grid(
ames_workflow_binary,
metrics =
metric_set(sens,spec,recall,precision,mcc,j_index,f_meas,accuracy,
kap,ppv,npv,bal_accuracy,detection_prevalence),
resamples = ames_folds_binary,
grid = grid_regular(
mtry(range = c(2, 6)),
min_n(range = c(2, 20)),
trees(range = c(10,100)),
levels = 10
)
)
#Pick the best model for each metric and pull out the predictions
best_models_binary <-
tibble(
metric_name = c('recall','sens','spec', 'precision','mcc','j_index','f_meas','accuracy',
'kap','ppv','npv','bal_accuracy','detection_prevalence')) %>%
mutate(metric_best = map(metric_name, ~select_best(ames_tune_binary, .x)),
wf_best = map(metric_best, ~finalize_workflow(ames_workflow_binary, .x)),
fit_best = map(wf_best, ~fit(.x, data = ames)),
df_pred = map(fit_best, ~ames %>% bind_cols(predict(.x, new_data = ames)) %>% select(under_budget, .pred_class))) %>%
select(-c(wf_best, fit_best)) %>%
unnest(cols = c(metric_name, metric_best, df_pred))
# Plot!
best_models_binary %>%
mutate(metric_desc = factor(
metric_name,
levels = c('recall','sens','spec', 'precision','mcc','j_index','f_meas','accuracy',
'kap','ppv','npv','bal_accuracy','detection_prevalence'),
labels = c('recall\nhow many observations out \nof all positive observations \nhave we classified as positive',
'sens\nhow many observations out \nof all positive observations \nhave we classified as positive',
'spec\nhow many observations out \nof all negative observations \nhave we classified as negative',
'precision\nhow many observations \npredicted as positive are \nin fact positive',
'mcc\ncorrelation between \npredicted classes and ground truth',
'j_index\nbalance between \nsensitivity and specificity',
'f_meas\nbalance between \nprecision and recall',
'accuracy\nhow many observations,\n both positive and negative,\n were correctly classified',
'kap\nhow much better is your model\n over the random classifier\n that predicts based on class frequencies',
'ppv\nhow many observations\n predicted as positive\n are in fact positive',
'npv\nhow many predictions\n out of all negative\n predictions were correct',
'bal_accuracy\nbalance between\n sensitivity and specificity',
'detection_prevalence\nhow many positive\n predictions were correct of\n all the predictions'))) %>%
group_by(metric_desc, under_budget, .pred_class) %>%
summarise(bin_count = n()) %>%
ungroup() %>%
ggplot(aes(x = under_budget, y = .pred_class, fill = bin_count, label = bin_count)) +
scale_fill_binned() +
geom_tile() +
geom_label() +
coord_fixed() +
facet_wrap(~metric_desc, ncol = 2) +
theme_minimal() +
theme(panel.spacing = unit(2, "lines"),
strip.text.x = element_text(size = 8))
```
![](images/09-best_models_binary.png)
## References
[Regression Classification Metrics](https://www.h2o.ai/blog/regression-metrics-guide/)
[Binary Classification Metrics](https://towardsdatascience.com/the-ultimate-guide-to-binary-classification-metrics-c25c3627dd0a)
[Tidymodels Function Reference](https://yardstick.tidymodels.org/reference/index.html)
[Custom Metrics](https://yardstick.tidymodels.org/articles/custom-metrics.html)
## Videos de las reuniones
### Cohorte 1
`r knitr::include_url("https://www.youtube.com/embed/G70IA1Ah42w")`
<details>
<summary> Chat de la reunión </summary>
```
00:26:11 Esme: https://datasciencebook.ca/regression1.html#multivariable-knn-regression
00:27:46 Diana García: https://inria.github.io/scikit-learn-mooc/python_scripts/metrics_classification.html
```
</details>