forked from perlatex/R_for_Data_Science
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tidymodels.Rmd
290 lines (190 loc) · 6.32 KB
/
tidymodels.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
# 机器学习 {#tidymodels}
Rstudio工厂的 Max Kuhn 大神正主持**机器学习**的开发,日臻成熟了,感觉很强大啊。
```{r tidymodels-1, message = FALSE, warning = FALSE}
library(tidyverse)
library(tidymodels)
```
## 数据
```{r tidymodels-2, eval=FALSE}
penguins <- read_csv("./demo_data/penguins.csv") %>%
janitor::clean_names() %>%
drop_na()
penguins %>%
head()
```
```{r tidymodels-3, eval=FALSE}
penguins %>%
ggplot(aes(x = bill_length_mm, y = bill_depth_mm,
color = species, shape = species)
) +
geom_point()
```
## 机器学习
<!-- ?predict.model_fit -->
<!-- https://tidymodels.github.io/model-implementation-principles/model-predictions.html -->
```{r tidymodels-4, eval=FALSE}
split <- penguins %>%
mutate(species = as_factor(species)) %>%
mutate(species = fct_lump(species, 1)) %>%
initial_split()
split
training_data <- training(split)
training_data
testing_data <- testing(split)
testing_data
```
## model01
```{r tidymodels-5, eval=FALSE}
model_logistic <- parsnip::logistic_reg() %>%
set_engine("glm") %>%
set_mode("classification") %>%
fit(species ~ bill_length_mm + bill_depth_mm, data = training_data)
bind_cols(
predict(model_logistic, new_data = testing_data, type = "class"),
predict(model_logistic, new_data = testing_data, type = "prob"),
testing_data
)
predict(model_logistic, new_data = testing_data) %>%
bind_cols(testing_data) %>%
count(.pred_class, species)
```
## model02
```{r tidymodels-6, eval=FALSE}
model_neighbor <- parsnip::nearest_neighbor(neighbors = 10) %>%
set_engine("kknn") %>%
set_mode("classification") %>%
fit(species ~ bill_length_mm, data = training_data)
predict(model_neighbor, new_data = testing_data) %>%
bind_cols(testing_data) %>%
count(.pred_class, species)
```
## model03
```{r tidymodels-7, eval=FALSE}
model_multinom <- parsnip::multinom_reg() %>%
set_engine("nnet") %>%
set_mode("classification") %>%
fit(species ~ bill_length_mm, data = training_data)
predict(model_multinom, new_data = testing_data) %>%
bind_cols(testing_data) %>%
count(.pred_class, species)
```
## model04
```{r tidymodels-8, eval=FALSE}
model_decision <- parsnip::decision_tree() %>%
set_engine("rpart") %>%
set_mode("classification") %>%
fit(species ~ bill_length_mm, data = training_data)
predict(model_decision, new_data = testing_data) %>%
bind_cols(testing_data) %>%
count(.pred_class, species)
```
## workflow
### 使用 recipes
```{r tidymodels-9, message=FALSE, warning=FALSE}
library(tidyverse)
library(tidymodels)
library(workflows)
penguins <- readr::read_csv("./demo_data/penguins.csv") %>%
janitor::clean_names()
split <- penguins %>%
tidyr::drop_na() %>%
rsample::initial_split(prop = 3/4)
training_data <- rsample::training(split)
testing_data <- rsample::testing(split)
```
参考[tidy modeling in R](https://www.tmwr.org/recipes.html), 被预测变量在分割前,应该先处理,比如标准化。
但这里的案例,我为了偷懒,被预测变量`bill_length_mm`,暂时保留**不变**。
预测变量做标准处理。
```{r tidymodels-10}
penguins_lm <-
parsnip::linear_reg() %>%
#parsnip::set_engine("lm")
parsnip::set_engine("stan")
penguins_recipe <-
recipes::recipe(bill_length_mm ~ bill_depth_mm + sex, data = training_data) %>%
recipes::step_normalize(all_numeric(), -all_outcomes()) %>%
recipes::step_dummy(all_nominal())
broom::tidy(penguins_recipe)
```
```{r tidymodels-11, out.width='80%', fig.align='center', echo = FALSE}
knitr::include_graphics("images/recipes-process.png")
```
```{r tidymodels-12, eval=FALSE}
penguins_recipe %>%
recipes::prep(data = training_data) %>% #or prep(retain = TRUE)
recipes::juice()
penguins_recipe %>%
recipes::prep(data = training_data) %>%
recipes::bake(new_data = testing_data) # recipe used in new_data
train_data <-
penguins_recipe %>%
recipes::prep(data = training_data) %>%
recipes::bake(new_data = NULL)
test_data <-
penguins_recipe %>%
recipes::prep(data = training_data) %>%
recipes::bake(new_data = testing_data)
```
### workflows的思路更清晰
workflows的思路让模型结构更清晰。 这样`prep()`, `bake()`, and `juice()` 就可以省略了,只需要recipe和model,他们往往是成对出现的
```{r tidymodels-13}
wflow <-
workflows::workflow() %>%
workflows::add_recipe(penguins_recipe) %>%
workflows::add_model(penguins_lm)
wflow_fit <-
wflow %>%
parsnip::fit(data = training_data)
```
```{r tidymodels-14}
wflow_fit %>%
workflows::pull_workflow_fit() %>%
broom.mixed::tidy()
wflow_fit %>%
workflows::pull_workflow_prepped_recipe()
```
先提取模型,用在 `predict()` 是可以的,但这样太麻烦了
```{r tidymodels-15, eval=FALSE}
wflow_fit %>%
workflows::pull_workflow_fit() %>%
stats::predict(new_data = test_data) # note: test_data not testing_data
```
因为,`predict()` 会自动的将recipes(对training_data的操作),应用到testing_data
这个不错,参考[这里](https://www.tidymodels.org/start/recipes/)
```{r tidymodels-16}
penguins_pred <-
predict(
wflow_fit,
new_data = testing_data %>% dplyr::select(-bill_length_mm),
type= "numeric"
) %>%
dplyr::bind_cols(testing_data %>% dplyr::select(bill_length_mm))
penguins_pred
```
```{r tidymodels-17}
penguins_pred %>%
ggplot(aes(x = bill_length_mm, y = .pred)) +
geom_abline(linetype = 2) +
geom_point(alpha = 0.5) +
labs(y = "Predicted ", x = "bill_length_mm")
```
### 模型评估
参考<https://www.tmwr.org/performance.html#regression-metrics>
```{r tidymodels-18}
penguins_pred %>%
yardstick::rmse(truth = bill_length_mm, estimate = .pred)
```
```{r tidymodels-19}
# 自定义一个指标评价函数my_multi_metric,就是放一起,感觉不够tidyverse
my_multi_metric <- yardstick::metric_set(rmse, rsq, mae, ccc)
penguins_pred %>%
my_multi_metric(truth = bill_length_mm, estimate = .pred)
```
```{r tidymodels-20, echo = F}
# remove the objects
# ls() %>% stringr::str_flatten(collapse = ", ")
rm(my_multi_metric, penguins, penguins_lm, penguins_pred, penguins_recipe, split, testing_data, training_data, wflow, wflow_fit)
```
```{r tidymodels-21, echo = F, message = F, warning = F, results = "hide"}
pacman::p_unload(pacman::p_loaded(), character.only = TRUE)
```