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

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
## 
## 
## 
## Attaching package: 'ggdist'
## 
## 
## The following objects are masked from 'package:ggridges':
## 
##     scale_point_color_continuous, scale_point_color_discrete,
##     scale_point_colour_continuous, scale_point_colour_discrete,
##     scale_point_fill_continuous, scale_point_fill_discrete,
##     scale_point_size_continuous
## [[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"        
## 
## [[13]]
##  [1] "ggdist"       "questionr"    "ggridges"     "rmarkdown"    "kableExtra"  
##  [6] "knitr"        "viridis"      "viridisLite"  "fst"          "RColorBrewer"
## [11] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
## [16] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [21] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [26] "datasets"     "methods"      "base"        
## 
## [[14]]
##  [1] "ggdist"       "questionr"    "ggridges"     "rmarkdown"    "kableExtra"  
##  [6] "knitr"        "viridis"      "viridisLite"  "fst"          "RColorBrewer"
## [11] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
## [16] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [21] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [26] "datasets"     "methods"      "base"
ess <- read_fst("All-ESS-Data.fst") 
setwd('C:/Users/evand/Desktop/SOC202') 
getwd()
## [1] "C:/Users/evand/Desktop/SOC202"

TASK 1

table(ess$prtyban) 
## 
##     1     2     3     4     5     7     8     9 
## 61829 78569 41139 34272  9964   289 15731   313
table(ess$polactiv)  
## 
##     1     2     3     4     5     7     8     9 
## 17445  9621  5267  6299  3288    37   377    25
table(ess$stfeco) 
## 
##     0     1     2     3     4     5     6     7     8     9    10    77    88 
## 39574 25671 45330 59142 55182 75639 57084 58459 41634 13226  8012   424 10562 
##    99 
##   616
gb_data <- ess %>%
  filter(cntry == "GB") %>%
  mutate(
    prtyban = ifelse(prtyban %in% c(7, 8, 9), NA, prtyban),
    polactiv = ifelse(polactiv %in% c(7, 8, 9), NA, polactiv),
    stfeco = ifelse(stfeco %in% c(77, 88, 99), NA, stfeco)
  )


datasummary_skim(gb_data %>% 
                   select(prtyban, polactiv, stfeco))
Unique (#) Missing (%) Mean SD Min Median Max
prtyban 6 49 2.5 1.1 1.0 2.0 5.0
polactiv 6 90 2.3 1.4 1.0 2.0 5.0
stfeco 12 2 4.4 2.3 0.0 5.0 10.0

I note that the variable stfeco has a slightly normal distribution in the bar chart and has a much higher mean than

the other two variables presented, but this can likely be attributed to the fact that there is double the maximum value of stfeco than the other variables.

TASK 2

I will use stfeco (How satisfied are you with the present state of economy in your country?) as my main variable.

gb_data <- gb_data %>%
  mutate(year = case_when(
    essround == 1 ~ 2002,
    essround == 2 ~ 2004,
    essround == 3 ~ 2006,
    essround == 4 ~ 2008,
    essround == 5 ~ 2010,
    essround == 6 ~ 2012,
    essround == 7 ~ 2014,
    essround == 8 ~ 2016,
    essround == 9 ~ 2018,
    essround == 10 ~ 2020,
    TRUE ~ NA_real_
  ))

eco_by <- gb_data %>% 
  group_by(year) %>% 
  summarize(mean_sat = mean(stfeco, na.rm = TRUE))

ggplot(eco_by, aes(x = year, y = mean_sat)) + geom_line(color = "blue", size = 1) + # Line to show the trend
  geom_point(color = "red", size = 3) +  labs(title = "Satisfaction With the Present State of Economy in Great Britain (2002-2020)", x = "Survey Year", y = "Satisfaction (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.

Regarding the table created, I notice that between the years 2005 and 2010 there was a steep decline in satisfaction

with the state of economics in Great Britain, which can be likely attributed to Great Britain’s Great Recession.

TASK 3

task3_cntry <- ess %>% 
  filter(cntry %in% c("FR", "GB", "NL")) %>% 
  mutate(stfeco = ifelse(stfeco %in% c(77, 88, 99), NA, stfeco))

task3plot <- ggplot(task3_cntry, aes(x = reorder(cntry, -stfeco, FUN=median), y = stfeco, fill = cntry)) +
  geom_boxplot() + theme_minimal() + theme(legend.position = "none") + 
  labs(title = "Boxplot comparison for satisfaction with the present state of economy (France, Great Britain, Netherlands)", 
       x = "Country", y = "Scale (0-10)")

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

I note that the Netherlands reports the highest satisfaction with the present state of economy, especially compared to France which does not have values above 5.

TASK 4

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

gb_data <- gb_data %>%
  mutate(born_in_country = recode(brncntr, `1` = "Yes", `2` = "No", `7` = NA_character_, `8` = NA_character_, `9` = NA_character_))

stfecorel <- datasummary_crosstab(stfeco ~ religion, data = gb_data)

stfecobrn <- datasummary_crosstab(stfeco ~ religion, data = gb_data)

proptable <- table(gb_data$stfeco, gb_data$religion) 
cprop(proptable)
##        
##         No    Yes   All  
##   0       6.7   5.5   6.1
##   1       5.2   4.6   4.9
##   2      10.6   9.2  10.0
##   3      14.6  13.3  14.0
##   4      14.7  15.1  14.9
##   5      17.4  17.7  17.6
##   6      12.9  13.3  13.1
##   7      10.7  11.9  11.3
##   8       5.6   7.1   6.3
##   9       1.0   1.5   1.2
##   10      0.6   0.8   0.7
##   Total 100.0 100.0 100.0

I note that among those who describe themselves as religious, the most selected response was 5, meaning they are between both ends of the satisfaction spectrum. This reflects my observation that the table is relatively evenly distributed, being slightly skewed towards left-centred. Within the second table, I observe that those who were not born in Great Britain are more satisfied with the state of the economy than those who are born in the country.

TASK 5

gb_clean <- gb_data %>% 
  filter(!is.na(religion) & !is.na(stfeco))

gb_probs <- gb_clean %>% 
  count(stfeco, religion) %>% 
  group_by(religion) %>% 
  mutate(prob = n / sum(n))

ggplot(gb_probs, aes(x = as.factor(stfeco), y = prob, color = religion)) + geom_point() + 
  geom_line(aes(group = religion)) + 
  labs(title = "Conditional Probabilities of Satisfaction With the Present Economic State", subtitle = "by Belonging to a religion or denomination", x = "Satisfaction", y = "Probability") + 
  theme_minimal()

I note that there is a very similar report of satisfaction with the economy from religious and non-religious individuals, but there is a much lower conditional probability from both groups of being extremely or very satisfied with the economy, relative to being extremely unsatisfied or very unsatisfied with the economy.