forked from rladies-ames/tidytuesday
-
Notifications
You must be signed in to change notification settings - Fork 1
/
thirty-minutes.R
182 lines (139 loc) · 6.11 KB
/
thirty-minutes.R
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
# thirty minutes start after reading in the data
pg <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-07-28/penguins.csv')
# ready, set, go!
# goal: to make coolest viz I can in 30 minutes
library(tidyverse)
glimpse(pg)
summary(pg)
pg %>% count(species, island, sex)
pg %>%
ggplot() +
geom_point(aes(x = bill_length_mm, y = bill_depth_mm, color = species)) +
facet_grid(cols = vars(sex))
pg %>%
ggplot() +
geom_point(aes(x = bill_length_mm, y = bill_depth_mm, color = species)) +
facet_grid(cols = vars(sex), rows = vars(island))
# island is not a good var to use in a model because gentoo only on biscoe,
# torgersen only has adelie penguins
pg %>%
ggplot() +
geom_point(aes(x = bill_length_mm, y = bill_depth_mm, color = species)) +
facet_grid(cols = vars(sex), rows = vars(year))
# see the same general pattern over the years, so year not informative
# idea: can we predict the sex of the penguins with sex NA?
library(tidymodels)
pg %>%
select(-island, -year) -> pg2
# some are missing all info besides species. remove.
pg_test <- pg2 %>% filter(is.na(sex), !is.na(bill_length_mm))
pg_train <- pg2 %>% filter(!is.na(sex))
# let's do a decision tree
pg_cv <- vfold_cv(pg_train, strata = sex, v = 5)
pg_rec <- recipe(sex ~ ., data = pg_train) %>%
step_dummy(species)
tree_spec <- decision_tree(mode = "classification",
tree_depth = tune(),
cost_complexity = tune(),
min_n = 4) %>%
set_engine("rpart")
param_grid <- grid_regular(tree_depth(), cost_complexity(), levels = 5)
tree_wf <- workflow() %>%
add_recipe(pg_rec) %>%
add_model(tree_spec)
set.seed(23987)
tree_res <- tune_grid(
tree_wf,
resamples = pg_cv,
grid = param_grid,
control = control_grid(save_pred = T)
)
# pause timer while running. 9 mins 13 sec left
# resume
autoplot(tree_res)
collect_metrics(tree_res)
tree_res %>% show_best("roc_auc")
tree_res %>% show_best("accuracy")
hyperparams <- select_best(tree_res, metric = "accuracy")
# fit to full data
f_wf <- finalize_workflow(tree_wf, hyperparams)
tree_fit <- f_wf %>% fit(pg_train)
# predict
pg_test %>%
bind_cols(predict(tree_fit, new_data = pg_test)) -> pg_test2
pg_test3 <- pg_test2 %>% select(species:body_mass_g, sex = .pred_class)
ggplot() +
geom_point(data = pg_train, aes(x = bill_length_mm, y = bill_depth_mm,
color = species, shape = "Train Data"), alpha = .3) +
geom_point(data = pg_test3, aes(x = bill_length_mm, y = bill_depth_mm,
color = species, shape = "Test Data"), size = 2.5) +
facet_grid(cols = vars(sex), labeller = label_both) +
theme_bw() +
theme(legend.position = "top") +
scale_color_brewer(palette = "Set2") +
labs(x = "Bill Length (mm)", y = "Bill Depth (mm)")
ggplot() +
geom_point(data = pg_train, aes(x = bill_length_mm, y = bill_depth_mm,
color = sex, shape = "Train Data"), alpha = .3) +
geom_point(data = pg_test3, aes(x = bill_length_mm, y = bill_depth_mm,
color = sex, shape = 'Test Data'), size = 2.5) +
facet_grid(cols = vars(species), labeller = label_both) +
theme_bw() +
theme(legend.position = "top") +
scale_color_brewer(palette = "Set2") +
labs(x = "Bill Length (mm)", y = "Bill Depth (mm)")
# timer stopped
# time restarted
ggplot() +
geom_point(data = pg_train, aes(x = flipper_length_mm, y = body_mass_g,
color = sex, shape = "Train Data"), alpha = .3) +
geom_point(data = pg_test3, aes(x = flipper_length_mm, y = body_mass_g,
color = sex, shape = 'Test Data'), size = 2.5) +
facet_grid(cols = vars(species), labeller = label_both) +
theme_bw() +
theme(legend.position = "top") +
scale_color_brewer(palette = "Set2") +
labs(x = "Flipper Length (mm)", y = "Body Mass (g)")
ggplot() +
geom_point(data = pg_train, aes(x = flipper_length_mm, y = body_mass_g,
color = species, shape = "Train Data"), alpha = .4) +
geom_point(data = pg_test3, aes(x = flipper_length_mm, y = body_mass_g,
color = species, shape = 'Test Data'), size = 2) +
facet_grid(cols = vars(sex), labeller = label_both) +
theme_bw() +
theme(legend.position = "top",
panel.grid = element_blank()) +
scale_color_brewer(name = "Species", palette = "Set2") +
scale_shape(name = "Data Source") +
labs(x = "Flipper Length (mm)", y = "Body Mass (g)")
# variable importance
tree_fit %>%
pull_workflow_fit() %>% vi()
# use 2 most important vars
ggplot() +
geom_point(data = pg_train, aes(x = bill_depth_mm, y = body_mass_g,
color = species, shape = "Train Data"), alpha = .4) +
geom_point(data = pg_test3, aes(x = bill_depth_mm, y = body_mass_g,
color = species, shape = 'Test Data'), size = 2) +
facet_grid(cols = vars(sex), labeller = label_both) +
theme_bw() +
theme(legend.position = "top",
panel.grid = element_blank()) +
scale_color_brewer(name = "Species", palette = "Set2") +
scale_shape(name = "Data Source") +
labs(x = "Bill Depth (mm)", y = "Body Mass (g)")
ggsave(filename = "data/2020/2020-07-28/plot1.png", width = 8, height = 4, device = "png", units = "in")
ggplot() +
geom_point(data = pg_train, aes(x = bill_depth_mm, y = body_mass_g,
color = sex, shape = "Train Data"), alpha = .4) +
geom_point(data = pg_test3, aes(x = bill_depth_mm, y = body_mass_g,
color = sex, shape = 'Test Data'), size = 2) +
facet_grid(cols = vars(species), labeller = label_both) +
theme_bw() +
theme(legend.position = "top",
panel.grid = element_blank()) +
scale_color_brewer(name = "Species", palette = "Set2") +
scale_shape(name = "Data Source") +
labs(x = "Bill Depth (mm)", y = "Body Mass (g)")
ggsave(filename = "data/2020/2020-07-28/plot2.png", width = 8, height = 4, device = "png", units = "in")
# an additional 20 minutes, 20 sec. # done!