-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.Rmd
230 lines (160 loc) · 3.88 KB
/
main.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
---
title: "main"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Packages
```{r}
#devtools::install_github("dreamRs/apexcharter")
pacman::p_load(tidyverse, apexcharter, mlgraph, recipes, deeplyr)
# ggplot2::theme_set(ggplot2::theme_classic())
# devtools::document()
devtools::load_all()
# devtools::install()
```
```{r}
mtcars <- mtcars %>%
mutate(
id = 1:n(),
amnum = am,
am = as.factor(am),
cyl = as.factor(as.numeric(as.factor(cyl))),
cyl0num = as.numeric(as.factor(cyl))-1,
cyl0 = as.factor(cyl0num)
)
### PS
rec_linear <- recipes::recipe(hp ~ ., mtcars) %>%
update_role(id, new_role = "ID")
### automatic?
rec_binary<- recipes::recipe(am ~ ., mtcars) %>%
update_role(id, amnum, new_role = "ID")
### number of cylinders
rec_multi <- recipes::recipe(cyl ~ ., mtcars) %>%
update_role(id, cyl0, cyl0num, new_role = "ID")
```
# Binary
```{r}
fb <- fit_learner(rec_binary, mtcars, list(), "binary", "rpart")
fb$predict(mtcars)
nn <- eval_classifier(fb$preds, "am", path = ".")
```
## Confusion Matrix
```{r}
devtools::document()
confusion_df <- get_confusion_df(preds2, ft_winner)
gg_plot_confusion(confusion_df)
hc_plot_confusion(confusion_df)
ax_plot_confusion(confusion_df)
```
## ROC
```{r}
devtools::load_all()
roc_df <- get_roc_df(preds2, ft_winner)
# roc_df %>%
# mutate(id = 1:n()) %>%
# gather(var, value, -.threshold, -actual, -id) %>%
# sample_n(100) %>%
# arrange(id) %>%
# ggplot(aes(.threshold, value, color = var)) +
# geom_line()
#
#Metrics::
gg_plot_roc(roc_df)
#gg_plot_roc2(preds$target, preds$prob)
hc_plot_roc(roc_df)
ax_plot_roc(roc_df)
#ax_plot_roc(roc_df)
```
```{r}
devtools::load_all()
preds2 <- readRDS("/Volumes/storage/MEGA/projects/mlgraph/data/preds2.rds")
roc_df <- get_roc_df(preds2, ft_winner)
roc_df %>% ax_plot_roc()
preds2 %>%
mutate(ft_winner = as.factor(ft_winner)) %>%
eval_classifier(ft_winner)
```
## RP-Curve
computes the precision at every unique value of the probability column (in addition to infinity)
```{r}
rp_df <- get_rp_df(preds2$ft_winner01, preds2$prob)
gg_plot_rp(rp_df)
hc_plot_rp(rp_df)
ax_plot_rp(rp_df)
```
## Probability Density
```{r}
devtools::document()
dens_df <- get_density_df(fb$preds, am)
gg_plot_density(dens_df)
hc_plot_density(dens_df)
ax_plot_density(dens_df)
```
## Cutoff
```{r}
get_cutoff_df(preds2, ft_winner01) %>%
ggplot(aes(thres, value, colour = metric)) +
geom_line()
get_cutoff_df(preds2, ft_winner01) %>%
ax_plot_cutoff
```
## Class Error
```{r}
get_classes_df(preds2, ft_winner) %>%
ax_plot_classes()
```
## Avergage Performance
```{r}
d <- preds2 %>%
get_avg_df(ft_winner) %>%
ax_plot_avg()
d
```
# Other
```{r, eval = F}
get_gain_df <- function(actual, prob){
dplyr::tibble(actual, prob) %>%
dplyr::mutate_at(1, as.factor) %>%
dplyr::mutate(prob = 1-prob) %>%
yardstick::gain_curve(actual, prob)
}
get_lift_df <- function(actual, prob){
dplyr::tibble(actual, prob) %>%
dplyr::mutate_at(1, as.factor) %>%
dplyr::mutate(prob = 1-prob) %>%
yardstick::lift_curve(actual, prob)
}
gain_df <- get_gain_df(preds$target, preds$prob)
lift_df <- get_lift_df(preds$target, preds$prob)
gain_df %>%
ggplot2::ggplot(aes(x = .percent_tested, y = .percent_found)) +
ggplot2::geom_line()
lift_df %>%
ggplot2::ggplot(aes(x = .percent_tested, y = .lift)) +
ggplot2::geom_line()
```
## All together
```{r}
devtools::document()
plot_classifier <- function(li){
li %>%
imap(~{
do.call(glue::glue("ax_plot_{.y}"), list(.data = .x))
})
}
evals <- list(
confusion = "ft_winner",
classes = "ft_winner",
roc = "ft_winner",
cutoff = "ft_winner01",
density = "ft_winner",
avg = "ft_winner"
) %>%
eval_classifier(preds2)
evals %>%
plot_classifier
get_avg_df(preds2, "ft_winner")
do.call(mlgraph::get_avg_df, list(.data = preds2, actual = "ft_winner"))
```