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")
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:

table(ess$happy)
## 
##      0      1      2      3      4      5      6      7      8      9     10 
##   3933   3540   7343  13759  17016  52117  43707  87869 130802  76229  51006 
##     77     88     99 
##    330   2163    741
table(ess$ipeqopt)
## 
##      1      2      3      4      5      6      7      8      9 
## 144304 183640  78430  29925  12546   3191   1379   4697   4378
table(ess$imwbcnt)
## 
##      0      1      2      3      4      5      6      7      8      9     10 
##  26607  18119  31951  44795  46898 136926  46688  48299  37746  12610  15653 
##     77     88     99 
##    748  22278   1237
gb_data <- ess %>% # Start with the original 'ess' dataset
  filter(cntry == "GB") %>% # Filter the data to only include rows where country (cntry) is France ("FR")
  mutate(
    happy = ifelse(happy %in% c(77, 88, 99), NA, happy), # If 'clsprty' is 2, set it to 0. If it's 7, 8, or 9, set it to NA.
    ipeqopt = ifelse(ipeqopt %in% c(7, 8, 9), NA, ipeqopt), # For 'stfdem', set values 77, 88, and 99 to NA.
    imwbcnt = ifelse(imwbcnt %in% c(77, 88, 99), NA, imwbcnt), # For 'trstplt', set values 77, 88, and 99 to NA.
  )
table(gb_data$happy)
## 
##    0    1    2    3    4    5    6    7    8    9   10 
##  113  112  240  502  670 1568 1541 3804 6099 3740 2557
table(gb_data$ipeqopt)
## 
##    1    2    3    4    5    6 
## 6561 8415 2994 1669  684  114
table(gb_data$imwbcnt)
## 
##    0    1    2    3    4    5    6    7    8    9   10 
## 1281  989 1654 2034 2135 4946 2035 2324 1801  705  712
datasummary_skim(gb_data %>% select(happy, ipeqopt, imwbcnt))
Unique (#) Missing (%) Mean SD Min Median Max
happy 12 0 7.5 1.9 0.0 8.0 10.0
ipeqopt 7 3 2.1 1.1 1.0 2.0 6.0
imwbcnt 12 2 4.9 2.5 0.0 5.0 10.0

Reference for variable values: https://ess-search.nsd.no/variable/query/happy%09/1

https://ess-search.nsd.no/variable/query/ipeqopt%09%09/1

https://ess-search.nsd.no/variable/query/imwbcnt%09/1

Things Noticed: People in the United Kingdom are fairly happier than not, and despite a clear majority voting that the belief that it is important that people are treated equally and have equal opportunities matches very much like or sounds like their own beliefs,about half of the reponses for the variable ‘imwbcnt’ leans more to the view that that immigrants make a country worse to live in.

Task 2:

belief_of_importance_by_year <- gb_data %>%
  group_by(year) %>%
  summarize(belief_of_importance_by_year = mean(ipeqopt, na.rm = TRUE))
belief_of_importance_by_year
## # A tibble: 10 × 2
##     year belief_of_importance_by_year
##    <dbl>                        <dbl>
##  1  2002                         2.27
##  2  2004                         2.20
##  3  2006                         2.15
##  4  2008                         2.19
##  5  2010                         2.08
##  6  2012                         1.99
##  7  2014                         2.02
##  8  2016                         2.16
##  9  2018                         2.10
## 10  2020                         1.90
gb_data %>%
  ggplot(aes(x = ipeqopt, y = as.factor(year), fill = as.factor(year))) +
  geom_density_ridges() + # geometry used for visualizing
  labs(title = "Belief that it is important that people are treated equally and have equal", 
       subtitle = "opportunities of people in the United Kingdom", 
       x = "Belief Scale (1 = Very much like me - 6 = Not like me at all)", y = "Survey Year",
       fill = "Survey Year") +
  theme_minimal()
## Picking joint bandwidth of 0.189
## Warning: Removed 542 rows containing non-finite values
## (`stat_density_ridges()`).

Things noticed: The responses for the belief that everyone should be treated equal and have equal oppourtunies have stayed pretty similar for almost two decades, with most people strongly agreeing with that statement.

Task 3:

ess_selected <- ess %>%
  filter(cntry %in% c("GB", "FR", "ES")) %>%
  mutate(ipeqopt = ifelse(ipeqopt %in% c(7, 8, 9), NA, ipeqopt))


task3plot <- ggplot(ess_selected, aes(x = reorder(cntry, -ipeqopt, FUN=median), y = ipeqopt, fill = cntry)) +
  geom_boxplot() +
  theme_minimal() + 
  theme(legend.position = "none") + 
  labs(title = "Comparison for the belief in importance that people are treated equally and have",
      subtitle = "equal opportunities (United Kingdom, France, Spain)", 
       x = "Country", 
       y = "Belief Scale (1 = Very much like me - 6 = Not like me at all)")

task3plot
## Warning: Removed 3326 rows containing non-finite values (`stat_boxplot()`).

Things noticed: The averages for the responses in all countries look almost the same, meaning that across the United Kingdom, Spain, and France, the majority of people all similarly agree that the belief of everyone being treated equally and having equal opportunities matches their own beliefs.

Task 4:

gb_data <- gb_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(gb_data$educ_level)
## 
##    BA No BA 
##  6116 14863
ipeqoptedu <- datasummary_crosstab(ipeqopt ~ educ_level, data = gb_data)
ipeqoptedu
ipeqopt BA No BA All
1 N 2266 4295 6561
% row 34.5 65.5 100.0
2 N 2338 6077 8415
% row 27.8 72.2 100.0
3 N 813 2181 2994
% row 27.2 72.8 100.0
4 N 391 1278 1669
% row 23.4 76.6 100.0
5 N 151 533 684
% row 22.1 77.9 100.0
6 N 27 87 114
% row 23.7 76.3 100.0
All N 6116 14863 20979
% row 29.2 70.8 100.0
gb_data <- gb_data %>%
  mutate(religion = case_when(
    rlgblg == 2 ~ "No",
    rlgblg == 1 ~ "Yes",
    rlgblg %in% c(7, 8, 9) ~ NA_character_,
    TRUE ~ as.character(rlgblg)
  ))



# check
table(gb_data$religion)
## 
##    No   Yes 
## 10923 10020
table(gb_data$ipeqopt, gb_data$religion) %>%
  cprop()
##        
##         No    Yes   All  
##   1      32.2  32.0  32.1
##   2      40.5  42.0  41.2
##   3      14.9  14.3  14.6
##   4       8.5   7.9   8.2
##   5       3.3   3.3   3.3
##   6       0.6   0.5   0.6
##   Total 100.0 100.0 100.0

Things noticed: Whether holding a BA or not holding a BA, it doesn’t really affect a person’s belief for equality. People who do hold BA’s overwhelmingly belief in equality, but the crosstab shows that that is true as well for people who do not hold BA’s.

In terms of the belief for equality and religion, the results are once again close, but people who do have a religion in the United Kingdom seem to believe for equality in everyone just slightly more that those who do not have a religion.

Task 5:

dgb <- gb_data %>%
  filter(!is.na(educ_level) & !is.na(ipeqopt))

dgb <- dgb %>%
  mutate(ipeqopt = ifelse(ipeqopt %in% c(7, 8, 9), NA, ipeqopt))

table(dgb$ipeqopt)
## 
##    1    2    3    4    5    6 
## 6561 8415 2994 1669  684  114
table(dgb$ipeqopt, dgb$educ_level) %>%
  cprop() %>%
  as.data.frame() %>%
  filter(Var1 != "Total",
         Var2 != "All") %>%
  ggplot(aes(x=Var1, y=Freq, fill=Var2)) +
  geom_col(position = "dodge") +
  labs(title="Belief that it is important that people are treated equally and have equal",
      subtitle = "opportunities of people in the United Kingdom",
       y = "Conditional Percentage",
       x = "Belief Scale (1 = Very much like me - 6 = Not like me at all)",
       fill = "At least BA vs. Not")

Things noticed: As the graph shows that people holding a BA has a greater percentage in belief that believing in equality sounds like something very true to them (look at 1 on the “Belief Scale”), and they have a lower percentage in responses stating that believing in equality does not sound a their own belief (look at 4-6 on the “Belief Scale”), I would say that it shows that people holding a BA might overall, slightly everso strongly believe in equality for people more than people without BA’s.