-
Notifications
You must be signed in to change notification settings - Fork 1
/
2022_01_25_board_games.Rmd
442 lines (316 loc) · 8.67 KB
/
2022_01_25_board_games.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
---
title: "Board games"
date: 2022-01-25
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)
library(scales)
theme_set(theme_light())
```
# Load the weekly Data
Dowload the weekly data and make available in the `tt` object.
```{r Load}
tt <- tt_load("2022-01-25")
```
# Readme
Take a look at the readme for the weekly data to get insight on the dataset.
This includes a data dictionary, source, and a link to an article on the data.
```{r Readme, eval = interactive()}
tt
```
# Glimpse Data
Take an initial look at the format of the data available.
```{r Glimpse}
tt %>%
map(glimpse)
```
# Wrangle
Explore the data and process it into a nice format for plotting! Access each dataset by name by using a dollarsign after the `tt` object and then the name of the data set.
```{r Wrangle}
ratings <- tt$ratings
details <- tt$details
```
# Visualize
Using your processed dataset, create your unique visualization.
Is the target "average" or "bayes_average"?
```{r Visualize}
ratings %>%
ggplot(aes(average)) +
geom_histogram()
ratings %>%
ggplot(aes(bayes_average)) +
geom_histogram()
ratings %>%
ggplot(aes(users_rated)) +
geom_histogram() +
scale_x_log10()
```
Game rank is according to "bayes_average". So the target is likely to be "bayes_average", which tends to increase as "users_rated" increases.
```{r}
ratings %>%
count(rank, sort = TRUE)
ratings %>%
ggplot(aes(rank, average)) +
geom_point(alpha = 0.2)
ratings %>%
ggplot(aes(rank, bayes_average)) +
geom_point(alpha = 0.2)
ratings %>%
ggplot(aes(users_rated, bayes_average)) +
geom_point(alpha = 0.2) +
scale_x_log10()
```
Game year doesn't mean much. "yearpublished" doesn't either.
```{r}
ratings %>%
ggplot(aes(year, bayes_average)) +
geom_point(alpha = 0.2)
rat_det <- ratings %>%
left_join(details, by = "id")
rat_det %>%
ggplot(aes(yearpublished, bayes_average)) +
geom_point(alpha = 0.2)
```
```{r}
rat_det %>%
ggplot(aes(minplayers, bayes_average, group = minplayers)) +
geom_boxplot()
rat_det %>%
ggplot(aes(maxplayers, bayes_average, group = maxplayers)) +
geom_boxplot() +
scale_x_log10()
```
```{r}
rat_det %>%
ggplot(aes(playingtime, bayes_average)) +
geom_point(alpha = 0.2) +
scale_x_log10()
rat_det %>%
ggplot(aes(minplaytime, bayes_average)) +
geom_point(alpha = 0.2) +
scale_x_log10()
rat_det %>%
ggplot(aes(maxplaytime, bayes_average)) +
geom_point(alpha = 0.2) +
scale_x_log10()
```
```{r}
rat_det %>%
count(boardgamecategory, sort = TRUE)
plot_cat <- function(df, var) {
df %>%
filter(!is.na({{ var }})) %>%
mutate({{ var }} := str_remove_all({{ var }}, "\\[|\\]|'|\\\"")) %>%
separate_rows({{ var }}, sep = ", ") %>%
mutate({{ var }} := fct_lump_n({{ var }}, n = 10)) %>%
add_count({{ var }}) %>%
mutate(
{{ var }} := paste0(as.character({{ var }}), " (", n, ")"),
{{ var }} := fct_reorder({{ var }}, bayes_average)
) %>%
ggplot(aes(bayes_average, {{ var }})) +
geom_boxplot()
}
rat_det %>%
plot_cat(boardgamecategory)
```
```{r}
rat_det %>%
count(boardgamemechanic, sort = TRUE)
rat_det %>%
plot_cat(boardgamemechanic)
```
```{r}
rat_det %>%
count(boardgamefamily, sort = TRUE)
```
```{r}
rat_det %>%
count(boardgameexpansion, sort = TRUE)
```
```{r}
rat_det %>%
count(boardgameimplementation, sort = TRUE)
```
```{r}
rat_det %>%
count(boardgamedesigner, sort = TRUE)
rat_det %>%
plot_cat(boardgamedesigner)
```
```{r}
rat_det %>%
count(boardgameartist, sort = TRUE)
rat_det %>%
plot_cat(boardgameartist)
```
```{r}
rat_det %>%
count(boardgamepublisher, sort = TRUE)
rat_det %>%
plot_cat(boardgamepublisher)
```
```{r}
rat_det %>%
ggplot(aes(owned, bayes_average)) +
geom_point(alpha = 0.2) +
scale_x_log10()
rat_det %>%
ggplot(aes(trading, bayes_average)) +
geom_point(alpha = 0.2) +
scale_x_log10()
rat_det %>%
ggplot(aes(wanting, bayes_average)) +
geom_point(alpha = 0.2) +
scale_x_log10()
rat_det %>%
ggplot(aes(wishing, bayes_average)) +
geom_point(alpha = 0.2) +
scale_x_log10()
```
```{r}
library(tidytext)
tidy_desc <- details %>%
unnest_tokens(word, description) %>%
anti_join(get_stopwords(), by = "word") %>%
filter(str_detect(word, "^[:alpha:]+$"))
tidy_desc %>%
count(word, sort = TRUE)
p <- tidy_desc %>%
left_join(ratings, by = "id") %>%
group_by(word) %>%
summarize(
n = n(),
rating = mean(bayes_average)
) %>%
ggplot(aes(n, rating)) +
geom_hline(yintercept = mean(ratings$bayes_average), color = "blue") +
geom_text(aes(label = word), check_overlap = TRUE,
vjust = "top", hjust = "left") +
scale_x_log10(limits = c(3, 1e+02)) +
scale_y_continuous(limits = c(3, 9)) +
labs(x = "# of appearances in description", y = "Bayes average of ratings",
title = "'disguise', 'flocks' and 'bumped' tend to lower ratings,\nwhile common words coverge to the average rating",
subtitle = "Top-left of each word indicates rating\nBlue horizontal line is the average rating",
caption = "Source: Kaggle")
p
```
# Save Image
Save your image for sharing. Be sure to use the `#TidyTuesday` hashtag in your post on twitter!
```{r}
# This will save your most recent plot
ggsave("image/board_games.png", p, width = 6, height = 6)
```
# Imitate Julia Silge
```{r}
ratings_joined <- ratings %>%
left_join(details, by = "id")
ratings_joined %>%
ggplot(aes(average)) +
geom_histogram()
```
```{r}
ratings_joined %>%
filter(!is.na(minage)) %>%
mutate(minage = cut_number(minage, 4)) %>%
ggplot(aes(minage, average, fill = minage)) +
geom_boxplot(show.legend = FALSE, alpha = 0.2)
```
## Build models
Let's consider how to [spend our data budget](https://www.tmwr.org/splitting.html):
- create training and testing sets
- create resampling folds from the *training* set
```{r}
library(tidymodels)
set.seed(123)
game_split <- ratings_joined %>%
select(name, average, starts_with(c("min", "max")), boardgamecategory) %>%
na.omit() %>%
initial_split(strata = average)
game_train <- training(game_split)
game_test <- testing(game_split)
set.seed(234)
game_folds <- vfold_cv(game_train, strata = average)
game_folds
```
Let's create a recipe.
```{r}
library(textrecipes)
split_category <- function(x) {
x %>%
str_remove_all("[:punct:]") %>%
str_trim() %>%
str_squish() %>%
str_to_lower() %>%
str_replace_all(" ", "_") %>%
str_split(", ")
}
game_rec <- recipe(average ~ ., data = game_train) %>%
update_role(name, new_role = "id") %>%
step_tokenize(boardgamecategory, custom_token = split_category) %>%
step_tokenfilter(boardgamecategory, max_tokens = 30) %>%
step_tf(boardgamecategory)
game_rec %>%
prep() %>%
bake(new_data = NULL) %>% names()
```
Let's create a [**model specification**](https://www.tmwr.org/models.html) for each model we want to try:
```{r}
xgb_spec <-
boost_tree(
trees = tune(),
mtry = tune(),
min_n = tune(),
learn_rate = 0.01
) %>%
set_engine("xgboost") %>%
set_mode("regression")
xgb_wf <- workflow(game_rec, xgb_spec)
xgb_wf
```
## Evaluate models
These models have no tuning parameters so we can evaluate them as they are. [Learn about tuning hyperparameters here.](https://www.tidymodels.org/start/tuning/)
```{r}
library(finetune)
doParallel::registerDoParallel(cores = 5)
set.seed(234)
xgb_game_rs <- tune_race_anova(
xgb_wf,
resamples = game_folds,
grid = 20,
control = control_race(verbose_elim = TRUE)
)
xgb_game_rs
```
```{r}
plot_race(xgb_game_rs)
```
```{r}
show_best(xgb_game_rs)
```
```{r}
select_best(xgb_game_rs)
```
These models perform very similarly, so perhaps we would choose the simpler, linear model. The function `last_fit()` *fits* one final time on the training data and *evaluates* on the testing data. This is the first time we have used the testing data.
```{r}
xgb_last <- xgb_wf %>%
finalize_workflow(select_best(xgb_game_rs)) %>%
last_fit(game_split)
xgb_last
```
```{r}
library(vip)
xgb_fit <- xgb_last %>%
extract_fit_parsnip()
vip(xgb_fit, geom = "point", num_features = 12)
```
Give up SHAP.