-
Notifications
You must be signed in to change notification settings - Fork 145
/
2020_07_28_penguins.Rmd
213 lines (165 loc) · 5.55 KB
/
2020_07_28_penguins.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
---
title: "TidyTemplate"
date: 2020-07-28
output: html_output
---
# TidyTuesday
Join the R4DS Online Learning Community in the weekly #TidyTuesday event!
Every week we post a raw dataset, a chart or article related to that dataset, and ask you to explore the data.
While the dataset will be “tamed”, it will not always be tidy! As such you might need to apply various R for Data Science techniques to wrangle the data into a true tidy format.
The goal of TidyTuesday is to apply your R skills, get feedback, explore other’s work, and connect with the greater #RStats community!
As such we encourage everyone of all skills to participate!
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(tidytuesdayR)
theme_set(theme_light())
```
# Load the weekly Data
Dowload the weekly data and make available in the `tt` object.
```{r Load}
tt <- tt_load("2020-07-28")
```
```{r}
penguins <- tt$penguins
```
```{r}
penguins %>%
count(species, sort = TRUE)
penguins %>%
count(island, sort = TRUE)
penguins_pivoted <- penguins %>%
pivot_longer(cols = bill_length_mm:body_mass_g,
names_to = "metric",
values_to = "value")
penguins_pivoted %>%
ggplot(aes(value, fill = species)) +
geom_histogram(bins = 20) +
facet_wrap(~ metric, scales = "free_x")
penguins_pivoted %>%
ggplot(aes(value, fill = species)) +
geom_density(alpha = .5) +
facet_wrap(~ metric, scales = "free")
penguins_pivoted %>%
ggplot(aes(species, value)) +
geom_boxplot() +
facet_wrap(~ metric, scales = "free_y")
```
```{r}
penguins %>%
ggplot(aes(year, fill = species)) +
geom_bar()
penguins %>%
ggplot(aes(island, fill = species)) +
geom_bar()
```
### Models to predict species
```{r}
library(tidymodels)
set.seed(2020)
split <- penguins %>%
filter(!is.na(bill_length_mm)) %>%
mutate(species = fct_lump(species, 1)) %>%
initial_split()
training_data <- training(split)
splits <- training_data %>%
rsample::vfold_cv(v = 10)
my_metrics <- metric_set(accuracy, kap, roc_auc)
control <- control_resamples(extract = extract_model,
save_pred = TRUE)
logistic_spec <- logistic_reg(mode = "classification") %>%
set_engine("glm")
logistic_model <- logistic_spec %>%
fit_resamples(species ~ bill_length_mm,
resamples = splits,
metrics = my_metrics)
logistic_model_extended <- logistic_spec %>%
fit_resamples(species ~ bill_length_mm + bill_depth_mm +
flipper_length_mm + body_mass_g,
resamples = splits,
metrics = my_metrics)
nn <- nearest_neighbor(mode = "classification",
neighbors = 10) %>%
set_engine("kknn") %>%
fit_resamples(species ~ bill_length_mm + bill_depth_mm +
flipper_length_mm + body_mass_g,
resamples = splits,
metrics = my_metrics)
dt <- decision_tree(mode = "classification") %>%
set_engine("rpart") %>%
fit_resamples(species ~ bill_length_mm + bill_depth_mm +
flipper_length_mm + body_mass_g,
resamples = splits,
control = control,
metrics = my_metrics)
# Look at one decision tree
dt$.extracts[[1]]$.extracts[[1]]
bind_rows(
collect_metrics(logistic_model) %>% mutate(model = "Logistic: bill length"),
collect_metrics(logistic_model_extended) %>% mutate(model = "Logistic: 4 predictors"),
collect_metrics(nn) %>% mutate(model = "KNN: 10 neighbors"),
collect_metrics(dt) %>% mutate(model = "Decision Tree")
) %>%
ggplot(aes(mean, .metric, color = model)) +
geom_point() +
geom_errorbarh(aes(xmin = mean - std_err,
xmax = mean + std_err)) +
labs(title = "Cross validated accuracy metrics across models",
x = "Estimated metric (+/- standard error)",
y = "")
```
Evaluate nearest neighbor
```{r}
knn_trained <- nearest_neighbor(mode = "classification",
neighbors = 10) %>%
set_engine("kknn") %>%
fit(species ~ bill_length_mm + bill_depth_mm +
flipper_length_mm + body_mass_g,
data = training(split))
predictions <- bind_cols(predict(knn_trained, new_data = testing(split)),
predict(knn_trained, new_data = testing(split), type = "prob")) %>%
bind_cols(testing(split))
metric_set(kap, accuracy)(predictions, species, estimate = .pred_class)
metric_set(roc_auc)(predictions, species, .pred_Adelie)
predictions %>%
ggplot(aes(species, .pred_Adelie)) +
geom_boxplot()
```
```{r}
split_multiclass <- penguins %>%
filter(!is.na(bill_length_mm)) %>%
mutate(species = factor(species)) %>%
initial_split()
splits_multiclass <- training(split_multiclass) %>%
vfold_cv(10)
multiclass <- multinom_reg(penalty = 0) %>%
set_engine("glmnet") %>%
fit_resamples(species ~ bill_length_mm + bill_depth_mm +
flipper_length_mm + body_mass_g,
resamples = splits_multiclass,
control = control,
metrics = my_metrics)
multiclass %>%
unnest(.predictions) %>%
pivot_longer(cols = c(.pred_Adelie, .pred_Chinstrap, .pred_Gentoo),
names_to = "predicted_class",
values_to = "prob") %>%
ggplot(aes(predicted_class, prob)) +
geom_boxplot() +
facet_wrap(~ species)
```
```{r}
predict(model, new_data = testing(split)) %>%
bind_cols(testing(split)) %>%
metrics(species, .pred_class)
```
```{r}
library(modeldata)
data("lending_club")
lr_mod <- logistic_reg()
using_formula <-
lr_mod %>%
set_engine("glm") %>%
fit(Class ~ funded_amnt + int_rate, data = lending_club)
using_formula
```