packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer",
"fst", "viridis", "knitr", "kableExtra", "rmarkdown", "ggridges", "viridis", "questionr")
new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)
lapply(packages, library, character.only = TRUE)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## Loading required package: viridisLite
##
##
## Attaching package: 'kableExtra'
##
##
## The following object is masked from 'package:dplyr':
##
## group_rows
## [[1]]
## [1] "lubridate" "forcats" "stringr" "dplyr" "purrr" "readr"
## [7] "tidyr" "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [13] "grDevices" "utils" "datasets" "methods" "base"
##
## [[2]]
## [1] "modelsummary" "lubridate" "forcats" "stringr" "dplyr"
## [6] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [11] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [16] "datasets" "methods" "base"
##
## [[3]]
## [1] "modelsummary" "lubridate" "forcats" "stringr" "dplyr"
## [6] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [11] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [16] "datasets" "methods" "base"
##
## [[4]]
## [1] "RColorBrewer" "modelsummary" "lubridate" "forcats" "stringr"
## [6] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [11] "ggplot2" "tidyverse" "stats" "graphics" "grDevices"
## [16] "utils" "datasets" "methods" "base"
##
## [[5]]
## [1] "fst" "RColorBrewer" "modelsummary" "lubridate" "forcats"
## [6] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [11] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [16] "grDevices" "utils" "datasets" "methods" "base"
##
## [[6]]
## [1] "viridis" "viridisLite" "fst" "RColorBrewer" "modelsummary"
## [6] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [11] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [16] "stats" "graphics" "grDevices" "utils" "datasets"
## [21] "methods" "base"
##
## [[7]]
## [1] "knitr" "viridis" "viridisLite" "fst" "RColorBrewer"
## [6] "modelsummary" "lubridate" "forcats" "stringr" "dplyr"
## [11] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [16] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [21] "datasets" "methods" "base"
##
## [[8]]
## [1] "kableExtra" "knitr" "viridis" "viridisLite" "fst"
## [6] "RColorBrewer" "modelsummary" "lubridate" "forcats" "stringr"
## [11] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [16] "ggplot2" "tidyverse" "stats" "graphics" "grDevices"
## [21] "utils" "datasets" "methods" "base"
##
## [[9]]
## [1] "rmarkdown" "kableExtra" "knitr" "viridis" "viridisLite"
## [6] "fst" "RColorBrewer" "modelsummary" "lubridate" "forcats"
## [11] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [16] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [21] "grDevices" "utils" "datasets" "methods" "base"
##
## [[10]]
## [1] "ggridges" "rmarkdown" "kableExtra" "knitr" "viridis"
## [6] "viridisLite" "fst" "RColorBrewer" "modelsummary" "lubridate"
## [11] "forcats" "stringr" "dplyr" "purrr" "readr"
## [16] "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [21] "graphics" "grDevices" "utils" "datasets" "methods"
## [26] "base"
##
## [[11]]
## [1] "ggridges" "rmarkdown" "kableExtra" "knitr" "viridis"
## [6] "viridisLite" "fst" "RColorBrewer" "modelsummary" "lubridate"
## [11] "forcats" "stringr" "dplyr" "purrr" "readr"
## [16] "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [21] "graphics" "grDevices" "utils" "datasets" "methods"
## [26] "base"
##
## [[12]]
## [1] "questionr" "ggridges" "rmarkdown" "kableExtra" "knitr"
## [6] "viridis" "viridisLite" "fst" "RColorBrewer" "modelsummary"
## [11] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [16] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [21] "stats" "graphics" "grDevices" "utils" "datasets"
## [26] "methods" "base"
ess <- read_fst("All-ESS-Data.fst")
table(ess$essround)
##
## 1 2 3 4 5 6 7 8 9 10
## 42359 47537 43000 56752 52458 54673 40185 44387 49519 59685
ess$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:10){
ess$year[ess$essround == i] <- replacements[i]
}
uk_data <- ess %>%
filter(cntry == "GB") %>%
mutate(
vote = ifelse(vote == 2, 0, ifelse(vote %in% c(7, 8, 9), NA, vote)),
tvtot = ifelse(tvtot %in% c(77, 88, 99), NA, tvtot),
wrclmch = ifelse(wrclmch %in% c(77, 88, 99), NA, wrclmch),
)
datasummary_skim(uk_data %>% select(vote, tvtot, wrclmch))
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max | ||
|---|---|---|---|---|---|---|---|---|
| vote | 4 | 0 | 0.8 | 0.7 | 0.0 | 1.0 | 3.0 | |
| tvtot | 9 | 25 | 5.0 | 2.0 | 0.0 | 5.0 | 7.0 | |
| wrclmch | 9 | 85 | 3.1 | 1.1 | 1.0 | 3.0 | 8.0 |
tvtot has a mean of 5.0, which means that the average person watches 2 and 2.5 hours of TV on a weekday. wrclmch has a mean of 3.1 which means people on average are somewhat worried about climate change. The mean for vote is 0.8, which shows that the majority of people voted in the last national election.
table(ess$vote)
##
## 1 2 3 7 8 9
## 350196 99401 35872 1195 3078 813
vote_by_year <- uk_data %>%
group_by(year) %>%
summarize(mean_vote = mean(vote , na.rm = TRUE))
vote_by_year
## # A tibble: 10 × 2
## year mean_vote
## <dbl> <dbl>
## 1 2002 0.836
## 2 2004 0.829
## 3 2006 0.836
## 4 2008 0.838
## 5 2010 0.812
## 6 2012 0.818
## 7 2014 0.839
## 8 2016 0.893
## 9 2018 0.915
## 10 2020 0.908
ggplot(vote_by_year, aes(x = year, y = mean_vote)) +
geom_line(color = "pink", size = 1) +
geom_point(color = "purple", size = 3) +
labs(title = "Average of People that Voted in the Last National Election",
subtitle = "(UK 2002-2020)",
x = "Survey Year",
y = "Average (1 = yes, 0 = no)")+
ylim(0, 1) +
theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
The average amount of people that voted is pretty stable with a mean of around 0.8 throughout these years, but slightly increased in the years 2016 to 2020, reaching a mean of over 0.9 in 2018 and 2020.
country_proportions <- ess %>%
filter(!vote %in% c(7, 8, 9)) %>%
group_by(cntry) %>%
summarize(
total_respondents = n(),
yes_count = sum(vote == 1),
proportion_yes = yes_count / total_respondents
) %>%
filter(cntry %in% c("GB", "BE", "CH")) %>%
arrange(-proportion_yes)
p4 <- ggplot(country_proportions, aes(x = reorder(cntry, -proportion_yes), y = proportion_yes, fill = cntry)) +
geom_bar(stat = "identity") +
labs(
title = "Proportion of People who voted in the last National Election",
subtitle = "(Belgium, Great Britain, Switzerland)",
x = "Country",
y = "Proportion",
fill = "Country"
) +
scale_fill_viridis_d() + # using viridis package to color
theme_minimal()
p4
Looking at the three countries, Belgium (BE) has the highest proportion
of people who voted of around 0.8. Switzerland (CH) has the lowest
proportion of people who voted in the last national election with a
proportion around 0.55. Great Britain (GB) is in between the other two
countries with a proportion of around 0.7.
uk_data <- uk_data %>%
mutate(born_in_country = case_when(
brncntr == 1 ~ 'yes',
brncntr == 2 ~ 'no',
))
table(uk_data$born_in_country)
##
## no yes
## 2409 18564
table(uk_data$brncntr)
##
## 1 2 7 8
## 18564 2409 3 3
votebrncntr <- datasummary_crosstab(vote ~ born_in_country, data = uk_data)
votebrncntr
| vote | no | yes | All | |
|---|---|---|---|---|
| 0 | N | 740 | 4540 | 5281 |
| % row | 14.0 | 86.0 | 100.0 | |
| 1 | N | 1217 | 13357 | 14576 |
| % row | 8.3 | 91.6 | 100.0 | |
| 3 | N | 444 | 614 | 1058 |
| % row | 42.0 | 58.0 | 100.0 | |
| All | N | 2409 | 18564 | 20979 |
| % row | 11.5 | 88.5 | 100.0 |
The table shows that 86% of people who did not vote were born in the UK. It also shows that 91.6% of people who did vote in the last election were born in the UK. The third option is people who were not eligible to vote, and 42% of those people were not born in the UK. Overall, 88.5% of the people who answered this question about voting, were born in the UK.
table(uk_data$vote, uk_data$born_in_country) %>%
cprop()
##
## no yes All
## 0 30.8 24.5 25.2
## 1 50.7 72.2 69.7
## 3 18.5 3.3 5.1
## Total 100.0 100.0 100.0
uk_clean <- uk_data %>%
filter(!is.na(born_in_country) & !is.na(vote))
uk_probs <- uk_clean %>%
count(vote, born_in_country) %>%
group_by(born_in_country) %>%
mutate(prob = n / sum(n))
ggplot(uk_probs, aes(x = as.factor(vote), y = prob, color = born_in_country)) +
geom_point() +
geom_line(aes(group = born_in_country)) +
labs(title = "Conditional Probabilities of Voting",
subtitle = "by being born in the country",
x = "Vote Status",
y = "Probability",) +
theme_minimal()
Of the people not born in the UK, 30.8% did not vote, and 50.7% did vote. The remaining 18.5% of the people not born in the UK were not eligible to vote. Of the people who were born in the UK, 24.5% did not vote, 72.2% voted, and only 3.3% of the rest of those born in the UK were not eligible to vote. Based on this table, people born in the UK were more likely to vote in the last national election, compared to those not born in the UK.