library(fst)
## Warning: package 'fst' was built under R version 4.3.1
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)
## Warning: package 'tidyverse' was built under R version 4.3.1
## Warning: package 'ggplot2' was built under R version 4.3.1
## Warning: package 'tibble' was built under R version 4.3.1
## Warning: package 'tidyr' was built under R version 4.3.1
## Warning: package 'readr' was built under R version 4.3.1
## Warning: package 'purrr' was built under R version 4.3.1
## Warning: package 'dplyr' was built under R version 4.3.1
## Warning: package 'stringr' was built under R version 4.3.1
## Warning: package 'forcats' was built under R version 4.3.1
## Warning: package 'lubridate' was built under R version 4.3.1
## ── 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.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ 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
## Warning: package 'modelsummary' was built under R version 4.3.1
## Warning: package 'viridis' was built under R version 4.3.1
## Loading required package: viridisLite
## Warning: package 'viridisLite' was built under R version 4.3.1
## Warning: package 'knitr' was built under R version 4.3.1
## Warning: package 'kableExtra' was built under R version 4.3.1
##
## Attaching package: 'kableExtra'
##
## The following object is masked from 'package:dplyr':
##
## group_rows
## Warning: package 'rmarkdown' was built under R version 4.3.1
## Warning: package 'ggridges' was built under R version 4.3.1
## Warning: package 'questionr' was built under R version 4.3.1
## [[1]]
## [1] "lubridate" "forcats" "stringr" "dplyr" "purrr" "readr"
## [7] "tidyr" "tibble" "ggplot2" "tidyverse" "fst" "stats"
## [13] "graphics" "grDevices" "utils" "datasets" "methods" "base"
##
## [[2]]
## [1] "modelsummary" "lubridate" "forcats" "stringr" "dplyr"
## [6] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [11] "tidyverse" "fst" "stats" "graphics" "grDevices"
## [16] "utils" "datasets" "methods" "base"
##
## [[3]]
## [1] "modelsummary" "lubridate" "forcats" "stringr" "dplyr"
## [6] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [11] "tidyverse" "fst" "stats" "graphics" "grDevices"
## [16] "utils" "datasets" "methods" "base"
##
## [[4]]
## [1] "RColorBrewer" "modelsummary" "lubridate" "forcats" "stringr"
## [6] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [11] "ggplot2" "tidyverse" "fst" "stats" "graphics"
## [16] "grDevices" "utils" "datasets" "methods" "base"
##
## [[5]]
## [1] "RColorBrewer" "modelsummary" "lubridate" "forcats" "stringr"
## [6] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [11] "ggplot2" "tidyverse" "fst" "stats" "graphics"
## [16] "grDevices" "utils" "datasets" "methods" "base"
##
## [[6]]
## [1] "viridis" "viridisLite" "RColorBrewer" "modelsummary" "lubridate"
## [6] "forcats" "stringr" "dplyr" "purrr" "readr"
## [11] "tidyr" "tibble" "ggplot2" "tidyverse" "fst"
## [16] "stats" "graphics" "grDevices" "utils" "datasets"
## [21] "methods" "base"
##
## [[7]]
## [1] "knitr" "viridis" "viridisLite" "RColorBrewer" "modelsummary"
## [6] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [11] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [16] "fst" "stats" "graphics" "grDevices" "utils"
## [21] "datasets" "methods" "base"
##
## [[8]]
## [1] "kableExtra" "knitr" "viridis" "viridisLite" "RColorBrewer"
## [6] "modelsummary" "lubridate" "forcats" "stringr" "dplyr"
## [11] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [16] "tidyverse" "fst" "stats" "graphics" "grDevices"
## [21] "utils" "datasets" "methods" "base"
##
## [[9]]
## [1] "rmarkdown" "kableExtra" "knitr" "viridis" "viridisLite"
## [6] "RColorBrewer" "modelsummary" "lubridate" "forcats" "stringr"
## [11] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [16] "ggplot2" "tidyverse" "fst" "stats" "graphics"
## [21] "grDevices" "utils" "datasets" "methods" "base"
##
## [[10]]
## [1] "ggridges" "rmarkdown" "kableExtra" "knitr" "viridis"
## [6] "viridisLite" "RColorBrewer" "modelsummary" "lubridate" "forcats"
## [11] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [16] "tibble" "ggplot2" "tidyverse" "fst" "stats"
## [21] "graphics" "grDevices" "utils" "datasets" "methods"
## [26] "base"
##
## [[11]]
## [1] "ggridges" "rmarkdown" "kableExtra" "knitr" "viridis"
## [6] "viridisLite" "RColorBrewer" "modelsummary" "lubridate" "forcats"
## [11] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [16] "tibble" "ggplot2" "tidyverse" "fst" "stats"
## [21] "graphics" "grDevices" "utils" "datasets" "methods"
## [26] "base"
##
## [[12]]
## [1] "questionr" "ggridges" "rmarkdown" "kableExtra" "knitr"
## [6] "viridis" "viridisLite" "RColorBrewer" "modelsummary" "lubridate"
## [11] "forcats" "stringr" "dplyr" "purrr" "readr"
## [16] "tidyr" "tibble" "ggplot2" "tidyverse" "fst"
## [21] "stats" "graphics" "grDevices" "utils" "datasets"
## [26] "methods" "base"
ess <- read_fst("All-ESS-Data.fst")
## Warning: package 'fstcore' was built under R version 4.3.1
Task 1
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]
}
table(ess$stfdem)
##
## 0 1 2 3 4 5 6 7 8 9 10 77 88
## 29121 18116 31973 45014 46758 80154 55790 67816 59837 23346 12989 755 18211
## 99
## 675
table(ess$trstprl)
##
## 0 1 2 3 4 5 6 7 8 9 10 77 88
## 55098 27138 41246 54163 48476 87736 50867 52831 37693 11817 10317 549 12036
## 99
## 588
table(ess$polintr)
##
## 1 2 3 4 7 8 9
## 52651 171323 170951 93989 251 955 435
france_data <- ess %>%
filter(cntry == "FR") %>%
mutate(
sftdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem),
trstprl = ifelse(trstprl %in% c(77, 88, 99), NA, trstprl),
polintr = ifelse(polintr %in% c(7, 8, 9), NA, polintr)
)
table(france_data$trstprl)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 1826 767 1772 2363 2308 4468 1983 1716 1050 200 221
datasummary_skim(france_data %>% select(stfdem, trstprl, polintr))
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max | ||
|---|---|---|---|---|---|---|---|---|
| stfdem | 13 | 0 | 5.8 | 10.2 | 0.0 | 5.0 | 88.0 | |
| trstprl | 12 | 2 | 4.2 | 2.3 | 0.0 | 5.0 | 10.0 | |
| polintr | 5 | 0 | 2.6 | 1.0 | 1.0 | 3.0 | 4.0 |
table(france_data$sftdem)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 1374 779 1703 2228 2357 3563 2125 2298 1669 398 267
table(france_data$polintr)
##
## 1 2 3 4
## 2713 5720 6947 3648
datasummary_skim(france_data %>% select(trstprl, polintr, sftdem))
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max | ||
|---|---|---|---|---|---|---|---|---|
| trstprl | 12 | 2 | 4.2 | 2.3 | 0.0 | 5.0 | 10.0 | |
| polintr | 5 | 0 | 2.6 | 1.0 | 1.0 | 3.0 | 4.0 | |
| sftdem | 12 | 1 | 4.6 | 2.4 | 0.0 | 5.0 | 10.0 |
I notice that overall in France, the mean for the trust in parliament is very similar to the mean of satisfaction in democracy.
Task 2
trust_by_year <- france_data %>%
group_by(year) %>%
summarize(mean_trust = mean(trstprl, na.rm = TRUE))
trust_by_year
## # A tibble: 10 × 2
## year mean_trust
## <dbl> <dbl>
## 1 2002 4.47
## 2 2004 4.27
## 3 2006 4.31
## 4 2008 4.48
## 5 2010 4.15
## 6 2012 4.12
## 7 2014 3.98
## 8 2016 4.03
## 9 2018 4.14
## 10 2020 4.54
ggplot(trust_by_year, aes(x = year, y = mean_trust)) + geom_line(color = "blue", size = 1) + # Line to show the trend
geom_point(color = "red", size = 3) + # Points to highlight each year's value
labs(title = "Trust in Parliament in France (2002-2020)",
x = "Survey Year",
y = "Trust (0-10)") +
ylim(0, 10) + # Setting the y-axis limits from 0 to 10
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 trust for parliament has remained relatively stable. It has slightly
been rising.
Task 3
ess_selected <- ess %>%
filter(cntry %in% c("FR", "GB", "ES")) %>%
mutate(trstprl = ifelse(trstprl %in% c(77, 88, 99), NA, trstprl))
task3plot <- ggplot(ess_selected, aes(x = reorder(cntry, -trstprl, FUN=median), y = trstprl, fill = cntry)) +
geom_boxplot() +
theme_minimal() +
theme(legend.position = "none") +
labs(title = "Boxplot comparison for trust in parliament (France, England, Spain)",
x = "Country",
y = "Scale (0-10)")
task3plot
## Warning: Removed 1790 rows containing non-finite values (`stat_boxplot()`).
The trust in parliament for Spain and France are relatively similar,
meanwhile, England has a lower trust in parliament.
Task 4
france_data <- france_data %>%
mutate(
# Set 'other' to NA for rounds 1-4
edulvla = case_when(
essround < 5 & edulvla == 55 ~ NA_real_,
TRUE ~ edulvla
),
# Set 'other' to NA for rounds 5 and later
edulvlb = case_when(
essround >= 5 & edulvlb == 5555 ~ NA_real_,
TRUE ~ edulvlb
),
# Create educ_level column
educ_level = case_when(
essround < 5 & edulvla == 5 ~ "BA",
essround >= 5 & edulvlb > 600 ~ "BA",
TRUE ~ "No BA"
)
)
table(france_data$educ_level)
##
## BA No BA
## 4235 14803
trstprledu <- datasummary_crosstab(trstprl ~ educ_level, data = france_data)
trstprledu
| trstprl | BA | No BA | All | |
|---|---|---|---|---|
| 0 | N | 158 | 1668 | 1826 |
| % row | 8.7 | 91.3 | 100.0 | |
| 1 | N | 105 | 662 | 767 |
| % row | 13.7 | 86.3 | 100.0 | |
| 2 | N | 285 | 1487 | 1772 |
| % row | 16.1 | 83.9 | 100.0 | |
| 3 | N | 440 | 1923 | 2363 |
| % row | 18.6 | 81.4 | 100.0 | |
| 4 | N | 469 | 1839 | 2308 |
| % row | 20.3 | 79.7 | 100.0 | |
| 5 | N | 891 | 3577 | 4468 |
| % row | 19.9 | 80.1 | 100.0 | |
| 6 | N | 627 | 1356 | 1983 |
| % row | 31.6 | 68.4 | 100.0 | |
| 7 | N | 672 | 1044 | 1716 |
| % row | 39.2 | 60.8 | 100.0 | |
| 8 | N | 403 | 647 | 1050 |
| % row | 38.4 | 61.6 | 100.0 | |
| 9 | N | 83 | 117 | 200 |
| % row | 41.5 | 58.5 | 100.0 | |
| 10 | N | 57 | 164 | 221 |
| % row | 25.8 | 74.2 | 100.0 | |
| All | N | 4235 | 14803 | 19038 |
| % row | 22.2 | 77.8 | 100.0 |
table(france_data$trstprl, france_data$educ_level) %>%
cprop()
##
## BA No BA All
## 0 3.8 11.5 9.8
## 1 2.5 4.6 4.1
## 2 6.8 10.3 9.5
## 3 10.5 13.3 12.7
## 4 11.2 12.7 12.4
## 5 21.3 24.7 23.9
## 6 15.0 9.4 10.6
## 7 16.0 7.2 9.2
## 8 9.6 4.5 5.6
## 9 2.0 0.8 1.1
## 10 1.4 1.1 1.2
## Total 100.0 100.0 100.0
france_data <- france_data %>%
mutate(geo = recode(as.character(domicil),
'1' = "Urban",
'2' = "Peri-Urban", # or set to Urban | Regardless decision needs to be justified
'3' = "Rural",
'4' = "Rural",
'5' = "Rural",
'7' = NA_character_,
'8' = NA_character_,
'9' = NA_character_))
# check
table(france_data$geo)
##
## Peri-Urban Rural Urban
## 2361 13087 3584
trstprlgeo <- datasummary_crosstab(trstprl ~ geo, data = france_data)
trstprlgeo
| trstprl | Peri-Urban | Rural | Urban | All | |
|---|---|---|---|---|---|
| 0 | N | 163 | 1390 | 271 | 1826 |
| % row | 8.9 | 76.1 | 14.8 | 100.0 | |
| 1 | N | 84 | 562 | 120 | 767 |
| % row | 11.0 | 73.3 | 15.6 | 100.0 | |
| 2 | N | 183 | 1299 | 290 | 1772 |
| % row | 10.3 | 73.3 | 16.4 | 100.0 | |
| 3 | N | 285 | 1687 | 391 | 2363 |
| % row | 12.1 | 71.4 | 16.5 | 100.0 | |
| 4 | N | 276 | 1610 | 421 | 2308 |
| % row | 12.0 | 69.8 | 18.2 | 100.0 | |
| 5 | N | 569 | 3088 | 811 | 4468 |
| % row | 12.7 | 69.1 | 18.2 | 100.0 | |
| 6 | N | 269 | 1291 | 421 | 1983 |
| % row | 13.6 | 65.1 | 21.2 | 100.0 | |
| 7 | N | 244 | 1065 | 407 | 1716 |
| % row | 14.2 | 62.1 | 23.7 | 100.0 | |
| 8 | N | 166 | 609 | 275 | 1050 |
| % row | 15.8 | 58.0 | 26.2 | 100.0 | |
| 9 | N | 33 | 109 | 58 | 200 |
| % row | 16.5 | 54.5 | 29.0 | 100.0 | |
| 10 | N | 30 | 140 | 51 | 221 |
| % row | 13.6 | 63.3 | 23.1 | 100.0 | |
| All | N | 2361 | 13087 | 3584 | 19038 |
| % row | 12.4 | 68.7 | 18.8 | 100.0 |
table(france_data$trstprl, france_data$geo) %>%
cprop()
##
## Peri-Urban Rural Urban All
## 0 7.1 10.8 7.7 9.8
## 1 3.6 4.4 3.4 4.1
## 2 7.9 10.1 8.2 9.5
## 3 12.4 13.1 11.1 12.7
## 4 12.0 12.5 12.0 12.4
## 5 24.7 24.0 23.1 23.9
## 6 11.7 10.0 12.0 10.6
## 7 10.6 8.3 11.6 9.2
## 8 7.2 4.7 7.8 5.6
## 9 1.4 0.8 1.6 1.1
## 10 1.3 1.1 1.5 1.2
## Total 100.0 100.0 100.0 100.0
Having a bachelors degree in France seems to increase likelihood of having more trust in parliament, and living in the rural area tends to demonstrate a lower level of trust in parliament.
Task 5
df <- france_data %>%
filter(!is.na(educ_level) & !is.na(trstprl))
df <- df %>%
mutate(
trstprl = ifelse(trstprl %in% c(77,88,99), NA, trstprl))
table(df$trstprl)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 1826 767 1772 2363 2308 4468 1983 1716 1050 200 221
france_clean <- france_data %>%
filter(!is.na(geo) & !is.na(trstprl))
france_probs <- france_clean %>%
count(trstprl, geo) %>%
group_by(geo) %>%
mutate(prob = n / sum(n))
ggplot(france_probs, aes(x = as.factor(trstprl), y = prob, color = geo)) +
geom_point() +
geom_line(aes(group = geo)) +
labs(title = "Conditional Probabilities of Trust in Parliament",
subtitle = "by Place of Residence",
x = "Trust Scale",
y = "Probability") +
theme_minimal()
In general in France, regardless of geographic location, there is around
a 25% chance that there is only a moderate trust in parliament.