-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathex-3-code-along.Rmd
More file actions
355 lines (281 loc) · 11.4 KB
/
Copy pathex-3-code-along.Rmd
File metadata and controls
355 lines (281 loc) · 11.4 KB
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
---
title: "Wrangling, tidying, and visualizing data with the Tidyverse"
date: "`r Sys.Date()`"
author: "___"
output:
html_document:
toc: true
---
## Intro
Today we're looking at data from the General Assembly of the United Nations.
The data is provided by the [TidyTuesday community](https://github.com/rfordatascience/tidytuesday/blob/master/data/2021/2021-03-23/readme.md) and comprises three tables:
- `unvotes` contains the voting decision (yes/no/abstain) of each country for every issue/resolution
- `roll_calls` contains information on each resolution, e.g., the voting date, the session number and a description
- `issues` contains the issue code and a descriptive topic name
I placed the three tables in a `.sqlite` database to show you how to work with large data frames using dplyr.
Working with data frames stored in databases is useful if:
- Your data is already in a database
- Your data volume is that high that it does not all fit in memory at once
See also the [dbplyr vignette](https://cran.r-project.org/web/packages/dbplyr/vignettes/dbplyr.html)
## Research questions
- RQ 1: Which topics were voted on most frequently?
- RQ 2: How did the number of roll calls change over time?
- RQ 3: How did the importance of issues change over time?
- RQ 4: Are there subgroups of countries with different voting behavior?
## Loading the data
Here, we could copy the tables of the DB into memory, because they aren't that large.
But I want to show some of the functionalities in case you run into a real database where the tables don't fit into memory.
```{r setup}
library(tidyverse)
theme_set(theme_minimal(base_size = 18))
knitr::opts_chunk$set(
fig.width = 28 / 2.54,
fig.height = 14 / 2.54
)
```
```{r}
con <- RSQLite::dbConnect(RSQLite::SQLite(), "unvotes.sqlite")
dbWriteTable(con, "iris", iris)
RSQLite::dbListTables(con)
tbl(con, "iris")
```
```{r connect-to-database}
library(RSQLite)
# Connect to database
con <- dbConnect(RSQLite::SQLite(), "unvotes.sqlite")
# List all tables
RSQLite::dbListTables(con)
# We can use tbl() to take a reference to a table in the database:
unvotes_db <- tbl(con, "unvotes")
# When printing, the table mostly looks like a regular tibble.
# The main difference is that it's a remote source in a SQLite database.
unvotes_db
# Close the connection:
# dbDisconnect(con)
```
See also the [RStudio website for working with databases](https://db.rstudio.com).
## Writing SQL code
We can write actual SQL code:
```{r wow-i-can-run-sql-code-from-r}
dbGetQuery(con, 'SELECT * FROM unvotes WHERE country_code = "US" LIMIT 5')
```
We can even add SQL code chunks to an R Markdown document:
```{sql connection=con}
SELECT * FROM unvotes WHERE country_code = "US" LIMIT 5
```
The most important difference between _ordinary data frames_ and _remote database_ queries is that your R code is translated into SQL and executed in the database, not in R.
When working with databases, dplyr tries to be as lazy as possible:
- It never pulls data into R unless you explicitly ask for it.
- It delays doing any work until the last possible moment: it collects together everything you want to do and then sends it to the database in one step.
See also RStudio's guide on [`dbplyr`](https://dbplyr.tidyverse.org/articles/dbplyr.html).
_Do you see a difference in the output of the remote data frame in the following code chunk compared to an in-memory data frame?_
```{r seemless-dplyr-with-database-tables}
unvotes_db %>%
filter(country_code == "US") %>%
head(5)
```
Let's do something more useful with the data.
## Calculate the percentage of 'yes' votes per country
```{r}
```
```{r}
# iris %>%
# group_by(Species) %>%
# summarize(., avg_sepal_length = mean(Sepal.Length))
#
# x <- group_by(iris, Species)
# summarize(x, avg_sepal_length = mean(Sepal.Length))
```
```{r perc-of-yes-per-country}
unvotes_db %>%
group_by(country) %>% #1
summarize(perc_yes = 100 * sum(vote == "yes") / n()) #2
```
## Load all tables into memory
```{r load-all-tables-into-memory}
library(RSQLite)
dbDisconnect(con) #close potentially open connections
con <- dbConnect(RSQLite::SQLite(), "unvotes.sqlite")
unvotes <- collect(tbl(con, "unvotes"))
issues <- collect(tbl(con, "issues"))
roll_calls <- collect(tbl(con, "roll_calls"))
dbDisconnect(con)
```
## RQ 1: Which topics were voted on most frequently?
We visualize the number of votings per issue category.
forcats
```{r most-pressing-topics}
issues %>%
# "count" the number of observations for each issue
count(issue) %>% #1
# put the count variable on the x-axis, put `issue` on the y-axis
ggplot(aes(x = n, y = fct_reorder(issue, n))) + #2
# geom_bar() vs. geom_col()?
geom_col() + #3
labs(
title = "Resolutions per topic in UN GA votings",
x = "Number of resolutions",
y = NULL
)
```
The output should look as follows:
```{r}
knitr::include_graphics("ex-3-code-along-most-pressing-topics.png")
```
## RQ 2: How did the number of roll calls change over time?
We want to show the number of votings per year over time.
Therefore, we need to extract the year of the voting.
First, we need to convert the date column from `double` to `Date` (`lubridate::as_date`).
```{r votings-per-year}
library(lubridate)
roll_calls %>%
# convert from double (number of days before 01-01-1970 (Unix epoch)) to `date`
mutate(date_clean = as_date(date)) %>%
# extract year component from date
mutate(year = year(date_clean)) %>%
# make a bar plot showing the year on the x-axis and the number of votings on the y-axis
ggplot(aes(x = year)) + #1
geom_bar() + #2
labs(
x = "Year",
y = "Number of votings",
title = "UN GA votings by year"
)
```
## RQ 3: How did the importance of issues change over time?
stringr
```{r issue-proportion-over-time-data}
df <- roll_calls %>%
select(rcid, date) %>%
mutate(date_clean = lubridate::as_date(date)) %>%
mutate(year = lubridate::year(date_clean)) %>%
# create bins of 5 years
mutate(year_disc = cut(
year,
breaks = c(seq(1945,2020, 5)),
labels = paste0(c(1945, seq(1951, 2016, 5)), "-", seq(1950,2020,5))
)) %>%
# prettify factor levels
mutate(year_disc = str_replace(year_disc, "\\d{2}(\\d{2})-\\d{2}(\\d{2})", "\\1-\n\\2")) %>%
# order the factor levels by year
mutate(year_disc = fct_reorder(year_disc, year)) %>%
select(rcid, year_disc)
# Join `df` with `issues`; keep all votings from `df`
df_plot <- left_join(df, issues, by = "rcid") %>% #1
# some roll calls are not assigned to an issue category
# make NA values of `issue` explicit, i.e., convert them to an additional factor level
mutate(issue = fct_explicit_na(issue, na_level = "Uncategorized"))
df_plot
label_pos <- df_plot %>%
# filter last year in the data
filter(year_disc == levels(year_disc)[nlevels(year_disc)]) %>% #1
# count the number of votings per issue
count(issue) %>% #2
# compute relative count of votings
mutate(perc = n / sum(n)) %>% #3
# sort rows by factor order
arrange(desc(issue)) %>%
# position of the label equals the sum of proportions of the issues below plus half of an issue's own proportion
mutate(pos = cumsum(lag(perc, default = 0)) + 0.5 * perc)
label_pos
```
```{r issue-proportion-over-time-plot}
# year bins on x-axis, fill bars by issue
ggplot(df_plot, aes(x = year_disc, #1
fill = issue)) + #2
geom_bar(position = "fill") +
colorblindr::scale_fill_OkabeIto() +
theme(panel.grid = element_blank()) +
theme(axis.ticks = element_line()) +
labs(x = "Year", y = NULL,
title = "Issue distribution of United Nations votings") +
geom_text(
data = label_pos,
aes(x = nlevels(df_plot$year_disc) + 0.75,
y = pos,
label = str_wrap(issue, 20)),
size = 16 / .pt,
hjust = 0, lineheight = 0.85, fontface = "italic"
) +
guides(fill = FALSE) +
scale_x_discrete(expand = c(0,0,0.5,0)) +
scale_y_continuous(labels = scales::percent)
```
The output should look as follows:
```{r}
knitr::include_graphics("ex-3-code-along-issue-distribution-over-time.png")
```
## RQ 4: Are there subgroups of countries with different voting behavior?
Cluster and visualize countries based on voting similarity.
Here, we combine the popular kmeans clustering and UMAP dimensionality reduction algorithm to discover and visualize subgroups of countries based on their voting behavior in UN GA votings?
Procedure:
1. Count the number of votes per country in recent years, i.e., after 2009.
To do so, we need to combine `unvotes` (containing `country`) with `roll_calls` (which contains `date`).
Calculate quantiles and identify a good threshold for the minimum number of votes a country must have cast.
2. Filter countries that voted "sufficiently" often. (Let's say 900 is the threshold ;-) )
3. Convert `vote` to numeric: no = no, abstain = 0.5, yes = 1
4. Use `complete()` to replace NA with 0.5
5. Reshape data so that we can apply clustering: `pivot_wider()`
6. Apply k-means
7. How to visualize clustering in a 926 dim-space? -> UMAP
8. ggrepel to highlight representative countries.
```{r voting-quantiles}
# Task 1:
unvotes %>%
left_join(roll_calls %>% select(rcid, date)) %>%
mutate(date = lubridate::as_date(date)) %>%
filter(lubridate::year(date) >= 2010) %>%
count(country) %>%
summarize(quant = seq(0,1,0.1), value = quantile(n, seq(0,1,0.1)))
```
```{r country-subgroups-based-on-voting-behavior}
df <- unvotes %>%
# join with roll_calls for date column
_____(roll_calls %>% select(rcid, date)) %>%
# convert date from double to date class
mutate(date = _____(date)) %>%
# filter votings from 2010 or later
filter(_____ >= 2010) %>%
# add number of observations per country as column
add_count(country) %>%
# filter countries with at least 900 votings
filter(_____ >= 900) %>%
select(rcid, country, vote) %>%
# if a country didn't vote on a particular roll call, add an observation with `vote == "abstain"`
complete(rcid, country, fill = list(vote = "abstain")) %>%
# kmeans requires numeric columns so we convert the answers to numeric
mutate(vote = case_when(
vote == "yes" ~ 1,
vote == "no" ~ 0,
TRUE ~ 0.5 # abstain
)) %>%
# reshape the data such that rows represent countries, columns represent votings, and cells represent how a country voted in a particular voting
pivot_wider(names_from = _____, values_from = _____)
```
The data frame should look as follows:
```{r}
knitr::include_graphics("country-subgroups-based-on-voting-behavior.png")
```
```{r clustering-dimred-plot}
# apply kmeans without country column
clustering <- kmeans(df %>% select(-country), 2)
str(clustering)
# apply UMAP
set.seed(123)
projection <- umap::umap(df %>% select(-country))
plot_data <- as_tibble(projection$layout) %>%
mutate(cluster_id = clustering$cluster) %>%
mutate(country = df$country)
# Visualize projection in two-dimensional scatterplot
# Map cluster membership to point color
ggplot(plot_data, aes(x = V1, y = V2, color = as.factor(cluster_id))) +
geom_point() +
ggrepel::geom_text_repel(
data = plot_data %>% slice_sample(n = 35),
aes(label = country), seed = 7, size = 14/.pt
) +
guides(color = FALSE) +
theme_void() +
scale_x_continuous(expand = c(0.5,0)) +
scale_y_continuous(expand = c(0.3,0))
```