-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtallertidymodels.Rmd
336 lines (273 loc) · 8.37 KB
/
tallertidymodels.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
---
title: "Intro al uso de {tidymodels}"
subtitle: RLadies Santiago, Chile
author: "Sara Acevedo"
date: "Marzo 2020"
output:
xaringan::moon_reader:
css: ["default", "rladies", "rladies-fonts"]
nature:
highlightStyle: github
highlightLines: true
countIncrementalSlides: true
---
## Notas importantes antes de empezar
No olviden revisar el **código de conducta**. Este es un ambiente seguro y no se tolera el acoso: https://github.com/rladies/starter-kit/wiki/Code-of-Conduct#spanish
Presentaciones en Xaringan:
* https://github.com/semiramisCJ/taller_xaringan_RLadiesMty2020
* https://github.com/sporella/xaringan_github
El código estará disponible en GitHub: https://github.com/Saryace
Mis redes: twitter: @saryace instagram lab: @soilbiophysicslab twitter lab: @soilbiophysics1
---
## Notas importantes antes de empezar
## Plan para esta sesión:
.pull-left[
Cosas que veremos hoy
* Paquetes y sus usos
* Funciones más importantes
* Implementar un modelo lineal
* Visualización básica
]
.pull-right[
]
---
## Notas importantes antes de empezar
## Si hay cosas que no entiendes del taller
.pull-left[
* Es normal, quizás iremos algo rápido
* El código y la presentación quedará disponible
* Habrá espacios para preguntas
]
.pull-right[
<img src=https://1.bp.blogspot.com/-WdKQMDR7ijE/VlIm8xqINqI/AAAAAAAA404/1wWcyHthAkQ/s1600/pantera-2.gif img>
]
---
class: inverse, center, middle
# Empecemos
<img src=https://media.giphy.com/media/lluj1cauAlO2vQEm8A/giphy.gif img>
---
## Paquetes Tidymodels
##
* Sintaxis tidyverse
* Reproducibilidad de datos
* Developer Max Kuhn {library(caret)}
* Hoy usaremos rsample, parsnip, recipes y yardstick
* Otros: corrr, dials, workflows, tune
<img src=https://rviews.rstudio.com/post/2019-06-14-a-gentle-intro-to-tidymodels_files/figure-html/tidymodels.png img>
Figura: https://rviews.rstudio.com/2019/06/19/a-gentle-intro-to-tidymodels/
---
- Instalar los paquetes **tidyverse**, **tidymodels**, junto con sus dependencias
```{r eval=FALSE, tidy=FALSE}
install.packages(c("tidyverse", "tidymodels"), dependencies = TRUE)
```
- Instalar el paquete **remotes** y **ggsignif**, junto con sus dependencias
```{r eval=FALSE, tidy=FALSE}
install.packages(c("remotes", "ggsignif"), dependencies = TRUE)
```
- Instalar desde github el paquete **datos** y **corrr**
```{r eval=FALSE, tidy=FALSE}
remotes::install_github("cienciadedatos/datos")
remotes::install_github("tidymodels/corrr")
```
---
class: inverse, center, middle
# Base de datos: pinguinos
<img src=https://github.com/allisonhorst/palmerpenguins/raw/master/man/figures/lter_penguins.png img>
Artwork by [@allison_horst](https://github.com/allisonhorst/)
---
# library() y glimpse()
```{r message=FALSE}
# librerias
library(tidyverse)
library(tidymodels)
library(remotes)
library(datos)
library(ggsignif)
library(corrr)
# estilo ggplot
theme_set(theme_bw())
# cargar la database
pinguinos <- datos::pinguinos
# echar un vistazo
dplyr::glimpse(pinguinos)
```
---
## Un poco de limpieza
```{r}
# arbitrariamente eliminaremos
pinguinos_db <- pinguinos %>%
drop_na() %>% # las observaciones con datos ausentes
select(-anio) # la columna anio
# revisamos nuestro nuevo archivo
glimpse(pinguinos_db)
```
---
## Exploramos datos visualmente
```{r, fig.align='center',out.width = '400px' }
pinguinos_db %>% ggplot(aes(x=largo_aleta_mm, y=masa_corporal_g,
color = sexo, size =masa_corporal_g)) +
geom_point(alpha=0.5)
```
---
## Diferencias macho y hembra por especie
```{r, fig.align='center', out.width = '350px' }
pinguinos_db %>% ggplot(aes(x=sexo, y=masa_corporal_g, fill=sexo)) +
geom_boxplot() +
facet_wrap(~especie) +
geom_signif(comparisons = list(c("macho", "hembra")),
map_signif_level=TRUE,
test = "t.test")
```
---
## Correlación entre variables numéricas
```{r}
pinguinos_db %>%
select(-especie,-sexo,-isla) %>%
corrr::correlate()
```
---
# Correlación entre variables numéricas
```{r, fig.align='center', out.width = '350px', message=FALSE}
pinguinos_db %>%
select(-especie,-sexo,-isla) %>%
corrr::correlate() %>%
rearrange() %>% # ordena las correlaciones
shave() %>%# limpia las correlaciones repetidas
fashion()
```
---
# Correlación entre variables numéricas
```{r, fig.align='center', out.width = '350px', message=FALSE}
pinguinos_db %>%
select(-especie,-sexo,-isla) %>%
corrr::correlate() %>%
network_plot()
```
---
## Objetivo
* Predecir la masa corporal de un pinguino, en base a sus caracteristicas físicas
* Interpretar los resultados que obtendremos
---
class: inverse, center, middle
# Primer paso: dividir el dataset en entrenamiento y testeo
<img src=https://github.com/rstudio/hex-stickers/blob/master/thumbs/rsample.png?raw=true img>
https://github.com/rstudio/hex-stickers/blob/master/thumbs/rsample.png
---
## Dividimos el dataset en 80% entrenamiento y 20% testeo
```{r}
set.seed(1234)
division <- initial_split(data = pinguinos_db, prop = .8)
entrenamiento <- training(division)
testeo <- testing(division)
```
```{r}
nrow(entrenamiento)
nrow(testeo)
```
---
class: inverse, center, middle
# Segundo paso: crear una receta
<img src=https://github.com/rstudio/hex-stickers/blob/master/thumbs/recipes.png?raw=true img>
https://github.com/rstudio/hex-stickers/blob/master/thumbs/recipes.png
---
## Creamos una receta para usar nuestras variables
```{r}
masa_recipe <-recipe(masa_corporal_g ~ ., data = entrenamiento) %>%
step_corr(all_numeric()) %>%
step_dummy(all_nominal()) %>%
prep()
masa_recipe
```
---
## Creamos una receta para usar nuestras variables
```{r}
entrenamiento_juice <- masa_recipe %>%
juice()
testeo_bake <- masa_recipe %>%
bake(testeo)
```
---
## Creamos una receta para usar nuestras variables
```{r}
head(entrenamiento_juice, 3)
```
```{r}
head(testeo_bake, 3)
```
---
class: inverse, center, middle
# Tercer paso: usar recetas y entrenar nuestros datos
<img src=https://github.com/rstudio/hex-stickers/blob/master/thumbs/parsnip.png?raw=true img>
https://github.com/rstudio/hex-stickers/blob/master/thumbs/parsnip.png
---
## Creamos nuestro modelo
```{r}
modelo_lineal <- linear_reg() %>%
set_engine("lm") %>%
set_mode("regression")
translate(modelo_lineal)
```
---
class: inverse, center, middle
# Estoy perdid@, son muchos objetos y funciones
<img src=https://media.giphy.com/media/vVEjKbAUFtZzFzjYbz/giphy.gif img>
---
## Recapitulemos
* Datos:
```{r eval=FALSE}
entrenamiento #80%
testeo #20%
```
* Receta: creamos datos dummies
```{r eval=FALSE}
masa_recipe
```
* Nuevos set de datos
```{r eval=FALSE}
entrenamiento_juice
testeo_bake
```
* Creamos un modelo linear
```{r eval=FALSE}
modelo_lineal
```
---
```{r }
ml_ajuste <- modelo_lineal %>%
fit(masa_corporal_g ~ ., data = entrenamiento_juice)
ml_ajuste
```
---
```{r }
lm_prediccion <- ml_ajuste %>%
predict(testeo_bake) %>%
bind_cols(testeo_bake)
lm_prediccion
```
---
class: inverse, center, middle
# Yardstick: evaluar el modelo
<img src=https://github.com/rstudio/hex-stickers/blob/master/thumbs/yardstick.png?raw=true img>
https://github.com/rstudio/hex-stickers/blob/master/thumbs/yardstick.png?raw=true
---
```{r }
lm_prediccion %>% metrics(truth = masa_corporal_g, estimate = .pred)
```
---
```{r, fig.align='center', out.width = '450px', message=FALSE}
lm_prediccion %>% ggplot(aes(x=masa_corporal_g, y=.pred,
color=masa_corporal_g)) +
geom_point(alpha=0.5) +
geom_abline() +
coord_equal() +
ylim(c(2000,6000)) +
xlim(c(2000,6000))
```
---
# Mas información, códigos y talleres
* [Tidymodels.org](https://www.tidymodels.org/)
* [Latin R](https://github.com/tidymodels-latam-workshops/latinR2020)
* [Linear and Bayesian Regression Models with tidymodels package, Masumbuko Semba](https://semba-blog.netlify.app/05/11/2020/regression-with-tidymodels/)
* [Tidymodel and glmnet, Jun Kang](https://www.jkangpathology.com/post/tidymodel-and-glmnet/)
* [Canal de youtube de Silvia Silge](https://www.youtube.com/channel/UCTTBgWyJl2HrrhQOOc710kA)
---