-
Notifications
You must be signed in to change notification settings - Fork 145
/
2020_10_20_beer_awards.Rmd
222 lines (179 loc) · 5.4 KB
/
2020_10_20_beer_awards.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
---
title: "Great American Beer Festival"
date: 2020-10-20
output: html_output
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(tidytuesdayR)
library(scales)
library(glue)
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-10-20")
beer_awards <- tt$beer_awards %>%
mutate(state = str_to_upper(state),
medal = fct_relevel(medal, c("Bronze", "Silver")))
beer_awards %>%
count(year) %>%
ggplot(aes(year, n)) +
geom_col()
beer_awards %>%
count(city, state, sort = TRUE)
beer_awards %>%
count(category, sort = TRUE)
beer_awards %>%
count(state, sort = TRUE)
beer_awards %>%
count(brewery, city, state, sort = TRUE)
beer_awards %>%
count(category, sort = TRUE) %>%
View()
beer_awards %>%
filter(category == "Bock")
```
What are the most-awarded beers?
```{r}
beer_counts <- beer_awards %>%
count(beer_name, brewery, medal, city, state)
beer_counts %>%
mutate(beer_name = glue("{ beer_name } ({ brewery })")) %>%
filter(fct_lump(beer_name, 16, w = n) != "Other") %>%
mutate(beer_name = fct_reorder(beer_name, n, sum)) %>%
ggplot(aes(n, beer_name, fill = medal)) +
geom_col() +
labs(title = "Which beers have won the most awards?",
x = "# of awards",
y = "",
fill = "")
beer_counts %>%
pivot_wider(names_from = medal, values_from = n,
values_fill = list(n = 0)) %>%
mutate(total = Bronze + Silver + Gold) %>%
arrange(desc(total))
```
Give 1 point for Bronze, 2 for Silver, 3 for Gold
```{r}
by_brewery <- beer_awards %>%
group_by(brewery) %>%
summarize(n_medals = n(),
n_beers = n_distinct(beer_name),
n_gold = sum(medal == "Gold"),
weighted_medals = sum(as.integer(medal))) %>%
arrange(desc(n_medals))
beer_awards %>%
count(brewery, medal) %>%
filter(fct_lump(brewery, 16, w = n) != "Other") %>%
mutate(brewery = fct_reorder(brewery, n, sum)) %>%
ggplot(aes(n, brewery, fill = medal)) +
geom_col() +
labs(title = "Which breweries have won the most awards?",
x = "# of awards",
y = "",
fill = "")
beer_awards %>%
count(state, medal) %>%
filter(fct_lump(state, 16, w = n) != "Other") %>%
mutate(state = fct_reorder(state, n, sum)) %>%
ggplot(aes(n, state, fill = medal)) +
geom_col() +
labs(title = "Which states have won the most awards?",
x = "# of awards",
y = "",
fill = "")
```
```{r}
library(tidylo)
library(tidytext)
beer_awards %>%
filter(fct_lump(state, 12) != "Other",
fct_lump(category, 10) != "Other") %>%
count(state, category) %>%
complete(state, category, fill = list(n = 0)) %>%
bind_log_odds(state, category, n) %>%
mutate(state = fct_reorder(state, -n, sum)) %>%
mutate(category = reorder_within(category, log_odds_weighted, state)) %>%
ggplot(aes(log_odds_weighted, category, fill = log_odds_weighted > 0)) +
geom_col() +
scale_y_reordered() +
facet_wrap(~ state, scales = "free_y") +
theme(legend.position = "none") +
labs(x = "Weighted log-odds",
y = "Category",
title = "What types of beer are over- or under-represented in awards each state?",
subtitle = "For the 12 states and the 10 categories with the most awards")
```
States
```{r}
by_year_state <- beer_awards %>%
add_count(year, name = "year_total") %>%
mutate(state = fct_lump(state, 9)) %>%
count(year, state, year_total, sort = TRUE) %>%
mutate(pct_year = n / year_total)
by_year_state %>%
filter(state != "Other") %>%
ggplot(aes(year, pct_year, color = state)) +
geom_line() +
expand_limits(y = 0) +
scale_y_continuous(labels = percent) +
facet_wrap(~ state)
by_year_state %>%
filter(state == "WI") %>%
glm(cbind(n, year_total - n) ~ year,
data = .,
family = "binomial") %>%
summary()
library(broom)
by_year_state %>%
filter(state != "Other") %>%
mutate(state = state.name[match(state, state.abb)]) %>%
group_by(state) %>%
summarize(model = list(glm(cbind(n, year_total - n) ~ year, family = "binomial"))) %>%
mutate(tidied = map(model, tidy, conf.int = TRUE)) %>%
unnest(tidied) %>%
filter(term == "year") %>%
mutate(p.value = format.pval(p.value),
state = fct_reorder(state, estimate)) %>%
ggplot(aes(estimate, state)) +
geom_point() +
geom_vline(xintercept = 0, lty = 2) +
geom_errorbarh(aes(xmin = conf.low, xmax = conf.high), height = .1) +
labs(x = "Estimated slope",
title = "Which states become more or less frequent medal winners?",
y = "")
```
```{r}
library(ggthemes)
map_data("state")
beer_awards %>%
mutate(state = str_to_lower(state.name[match(state, state.abb)])) %>%
count(state) %>%
right_join(map_data("state"), by = c(state = "region")) %>%
ggplot(aes(long, lat, fill = n, group = group)) +
geom_polygon() +
theme_map() +
coord_map() +
labs(fill = "# of award-winning beers")
```
Dead end: bringing in Kaggle data
```{r}
beer_awards
breweries <- read_csv("~/Downloads/archive/breweries.csv") %>%
rename(brewery_id = X1,
brewery = name)
beers <- read_csv("~/Downloads/archive/beers.csv") %>%
rename(beer_name = name) %>%
inner_join(breweries, by = "brewery_id")
beers
beer_awards %>%
distinct(beer_name, brewery) %>%
semi_join(beers, by = c("beer_name"))
# DEAD END!
```
Things I didn't work on:
* Put dots for cities + breweries that win many awards
* Per-capita award winning beers