Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Package installation setup

packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer", 
              "fst", "viridis", "knitr", "kableExtra", "rmarkdown", "ggridges", "viridis", "questionr", "dplyr")

Install packages

new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)

lapply(packages, library, character.only = TRUE)

Set the wd

setwd("/Users/sophiafernandes/Desktop/SOC202 Folder")
getwd()

Load dataset

ess <- read_fst("All-ESS-Data.fst")
table(ess$essround)

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]
}

TASK 1

iceland_data <- ess %>%
 filter(cntry == "IS") %>%
 mutate(
 trstlgl = ifelse(trstlgl %in% c(77, 88, 99), NA, trstlgl),
 hmsacld = ifelse(hmsacld %in% c(7, 8, 9), NA, hmsacld),
 hmsfmlsh = ifelse(hmsfmlsh %in% c(7, 8, 9), NA, hmsfmlsh),
 )

 table(iceland_data$trstlgl)

  0   1   2   3   4   5   6   7   8   9  10 
 97  66 165 254 313 498 578 741 794 308  84 
 table(iceland_data$hmsacld)

   1    2    3    4    5 
1650  697  146   67   58 
 table(iceland_data$hmsfmlsh)

   1    2    3    4    5 
  40   26   63  450 2039 
datasummary_skim(iceland_data %>% select(trstlgl, hmsacld, hmsfmlsh))

TASK 2

trust_by_year <- iceland_data %>%
  group_by(year) %>%
  summarize(mean_trust = mean(trstlgl, na.rm = TRUE))
trust_by_year

print(ggplot(trust_by_year, aes(x = year, y = mean_trust)) +
geom_line(color = "magenta", size = 1) +
geom_point(color = "turquoise", size = 3) +
labs(title = "Trust in Legal System in Iceland (2004-2020)", 
     x = "Survey Year",
     y = "Trust (0-10)") +
ylim(5.5, 6.5) +
theme_light())

TASK 3

I chose the countries germany and russia to compare the dataset to.

 russia_data <- ess %>%
 filter(cntry == "RU") %>%
 mutate(
 trstlgl = ifelse(trstlgl %in% c(77, 88, 99), NA, trstlgl),
 )
 table(russia_data$trstlgl)

   0    1    2    3    4    5    6    7    8    9   10 
1646  964 1257 1501 1222 2070  880  805  673  287  366 
 datasummary_skim(russia_data %>% select(trstlgl)
 )
 View(trust_by_year)
 trust_by_year_russia <- russia_data %>%
 group_by(year) %>%
 summarize(mean_trust = mean(trstlgl, na.rm = TRUE))
 trust_by_year_russia
# A tibble: 5 × 2
   year mean_trust
  <dbl>      <dbl>
1  2006       3.74
2  2008       4.12
3  2010       3.81
4  2012       3.57
5  2016       4.38
 germany_data <- ess %>%
 filter(cntry == "DE") %>%
 mutate(
 trstlgl = ifelse(trstlgl %in% c(77, 88, 99), NA, trstlgl),
 )
 table(germany_data$trstlgl)

   0    1    2    3    4    5    6    7    8    9   10 
1270  781 1693 2857 2868 5202 3851 5267 5907 2790 1481 
 trust_by_year_germany <- germany_data %>%
 group_by(year) %>%
 summarize(mean_trust = mean(trstlgl, na.rm = TRUE))
 trust_by_year_germany
# A tibble: 10 × 2
    year mean_trust
   <dbl>      <dbl>
 1  2002       5.57
 2  2004       5.43
 3  2006       5.46
 4  2008       5.64
 5  2010       5.54
 6  2012       5.83
 7  2014       5.74
 8  2016       6.04
 9  2018       6.13
10  2020       6.14

ess_selected <- ess %>%
  filter(cntry %in% c("RU", "DE", "IS")) %>%
  mutate(trstlgl = ifelse(trstlgl %in% c(77, 88, 99), NA, trstlgl))
  
  summary_data <- ess_selected %>%
     group_by(cntry, year) %>%
     summarise(avg_trstlgl = mean(trstlgl, na.rm = TRUE)) %>%
          ungroup()
  
 plot <- ggplot(summary_data, aes(x = year, y = avg_trstlgl)) +
 geom_point() + 
  geom_smooth(method = 'loess', se = FALSE) +
 labs(
  x = "Year",
 y = "Average Trust in Legal System"
 ) +
 facet_wrap(~ cntry, ncol = 2, scales = "free_y") +
 scale_color_viridis_c() +
 theme_minimal() +
 theme(legend.position = "none")
 plot

I noticed that while creating this graph, there was not data available for the same years for each all countries. However, it is still possble to look at trends. I chose Germany and Russia as they have the largest populations in Europe, in comparison to iceland which has one of the smallest populations. The scales of the graphs need to be adjusted to accurately compare the data.

TASK 4

iceland_data <- iceland_data %>%
     mutate(
 edulvla = case_when(
     essround < 5 & edulvla == 55 ~ NA_real_,
     TRUE ~ edulvla
 ),
 edulvlb = case_when(
     essround >= 5 & edulvlb == 5555 ~ NA_real_,
     TRUE ~ edulvlb
 ),
 educ_level = case_when(
     essround < 5 & edulvla == 5 ~ "BA",
     essround >= 5 & edulvlb > 600 ~ "BA",
     TRUE ~ "No BA"
 )
 )
 table(iceland_data$educ_level)

   BA No BA 
 1347  2628 
 trstlgledu <- datasummary_crosstab(trstlgl ~ educ_level, data = iceland_data)
 trstlgledu
 table(iceland_data$trstlgl, iceland_data$educ_level) %>%
     cprop()
       
        BA    No BA All  
  0       0.9   3.3   2.5
  1       0.8   2.1   1.7
  2       2.7   5.0   4.2
  3       4.2   7.7   6.5
  4       6.1   9.0   8.0
  5       9.3  14.6  12.8
  6      16.7  13.9  14.8
  7      22.3  17.3  19.0
  8      25.2  17.8  20.4
  9       9.8   6.9   7.9
  10      1.9   2.3   2.2
  Total 100.0 100.0 100.0

TASK 5

iceland_data <- iceland_data %>%
  mutate(
    edulvla = case_when(
      essround < 5 & edulvla == 55 ~ NA_real_,
      TRUE ~ edulvla
    ),
    edulvlb = case_when(
      essround >= 5 & edulvlb == 5555 ~ NA_real_,
      TRUE ~ edulvlb
    ),
    educ_level = case_when(
      essround < 5 & edulvla == 5 ~ "BA",
      essround >= 5 & edulvlb > 600 ~ "BA",
      TRUE ~ "No BA"
    )
  )
  
 iceland_clean <- iceland_data %>%
 filter(!is.na(educ_level) & !is.na(trstlgl))
 iceland_probs <- iceland_clean %>%
     count(trstlgl, educ_level) %>%
     group_by(educ_level) %>%
     mutate(prob = n / sum(n))
 ggplot(iceland_probs, aes(x = as.factor(trstlgl), y = prob, color = educ_level)) +
     geom_point() +
     geom_line(aes(group = educ_level)) +
     labs(title = "Conditional Probabilities of Trust in Legal System In Iceland",
          subtitle = "by Education Level",
          x = "Trust Scale", 
          y = "Probability") +
     theme_minimal()