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

Task 1

table(ess$ipeqopt)
## 
##      1      2      3      4      5      6      7      8      9 
## 144304 183640  78430  29925  12546   3191   1379   4697   4378
table(ess$impdiff)
## 
##      1      2      3      4      5      6      7      8      9 
##  62950 121575 110902  79325  60824  15931   1446   5096   4445
table(ess$sbeqsoc)
## 
##     1     2     3     4     5     7     8     9 
##  6753 40338 23999 21059  4872    97  3860   161
cz_data <- ess %>%
  filter(cntry == "CZ") %>%
  mutate(
    ipeqopt = ifelse(ipeqopt %in% c(7, 8, 9), NA, ipeqopt),
    impdiff = ifelse(impdiff %in% c(7, 8, 9), NA, impdiff),
    sbeqsoc = ifelse(sbeqsoc %in% c(7, 8, 9), NA, sbeqsoc), 
  )
table(cz_data$ipeqopt)
## 
##    1    2    3    4    5    6 
## 4003 7218 4831 1890  810  193
table(cz_data$impdiff)
## 
##    1    2    3    4    5    6 
## 2080 4746 5230 3604 2370  880
table(cz_data$sbeqsoc)
## 
##    1    2    3    4    5 
##  305 1067 1237 1049  473
datasummary_skim(cz_data %>% select(ipeqopt, impdiff, sbeqsoc))
Unique (#) Missing (%) Mean SD Min Median Max
ipeqopt 7 6 2.4 1.1 1.0 2.0 6.0
impdiff 7 6 3.1 1.3 1.0 3.0 6.0
sbeqsoc 6 79 3.1 1.1 1.0 3.0 5.0

The “ipeqopt” variable stands for “Important that people are treated equally and have equal opportunities”. The mean of 2.5 could be an evidence showing that most of the czech people believes in the idea of equality. Similarly with the two other variables, “impdiff” represent “Important to try new and different things in life” and “sbeqsoc” represent “Social benefits/services lead to a more equal society”. The mean for impdiff in Czech Republic is 3.1 means that most people do believe in trying new things. The mean for sbeqsoc is 3.1 shows that most czech people believe the play of social benefits/services in promoting equality.

Task 2

important_treated_equally <- cz_data %>%
  group_by(year) %>%
  summarize(mean_ipeqopt = mean(ipeqopt, na.rm = TRUE))

ggplot(important_treated_equally, aes(x = year, y = mean_ipeqopt)) +
  geom_line(color = "yellow", size = 1) +  
  geom_point(color = "red", size = 3) +  
  labs(title = "Belief about equal treatments/opportunities in Czech Republic(2002-2020)", 
       x = "Survey Year", 
       y = "Brief Scale (1-6), 1=Very Much 6=Not at all") +
  ylim(0, 6) +  
  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.

Based on the graph, the value of y is at the lowest point in the year of 2000, at this year, the Czech people beliefs in the idea of equality very much compared to other surveyed years. The value also reaches its highest point at the year of 2016, meaning that the idea of equality is a little less important for Czech people at that time. One thing to be mentioned is that the value of y remains mostly consistent over the 20 years of survey.

Task 3

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


task3hwplot <- ggplot(ess_selected, aes(x = reorder(cntry, -ipeqopt, FUN=median), y = ipeqopt, fill = cntry)) +
  geom_boxplot() +
  theme_minimal() + 
  theme(legend.position = "none") + 
  labs(title = "Belief about equal treatments/opportunities (Czech Republic, France, Portugal)", 
       x = "Country", 
       y = "Brief Scale (1-6), 1=Very Much 6=Not at all")

task3hwplot
## Warning: Removed 1603 rows containing non-finite values (`stat_boxplot()`).

The median for three countries are very similar at around the value of 2. However, their distributions are quite different. The box of CZ moves a little bit up than the box of FR meaning that Czech people is shown less favourate to the idea of equality compared to the French people. The box of PT lies from 1-3 means that Portuguese people are more centrist compared to French people and Czech people.

Task 4

cz_data <- cz_data %>%
  mutate(
    edulvla = case_when(
      essround < 5 & edulvla == 55 ~ NA_real_,
      TRUE ~ edulvla
    ),
    edulvlb = case_when(
      essround >= 5 & edulvlb == 5555 ~ NA_real_,
      TRUE ~ edulvlb
    ),
    edu_level = case_when(
      essround < 5 & edulvla == 5 ~ "Bachelors degree",
      essround >= 5 & edulvlb > 600 ~ "Bachelors degree",
      TRUE ~ "No Bachelors degree"
    )
  )

table(cz_data$edu_level)
## 
##    Bachelors degree No Bachelors degree 
##                2715               17375
ipeqoptedu <- datasummary_crosstab(ipeqopt ~ edu_level, data = cz_data)
ipeqoptedu
ipeqopt Bachelors degree No Bachelors degree All
1 N 596 3407 4003
% row 14.9 85.1 100.0
2 N 1021 6197 7218
% row 14.1 85.9 100.0
3 N 608 4223 4831
% row 12.6 87.4 100.0
4 N 232 1658 1890
% row 12.3 87.7 100.0
5 N 95 715 810
% row 11.7 88.3 100.0
6 N 31 162 193
% row 16.1 83.9 100.0
All N 2715 17375 20090
% row 13.5 86.5 100.0
cz_data <- cz_data %>%
  mutate(religion = case_when(
    rlgblg == 2 ~ "Not Religious",
    rlgblg == 1 ~ "Religious",
    rlgblg %in% c(7, 8, 9) ~ NA_character_,
    TRUE ~ as.character(rlgblg)
  ))

table(cz_data$religion)
## 
## Not Religious     Religious 
##         15458          4423
table(cz_data$ipeqopt, cz_data$religion) %>%
  cprop()
##        
##         Not Religious Religious All  
##   1      19.6          26.7      21.2
##   2      37.5          40.1      38.1
##   3      26.8          21.1      25.5
##   4      10.6           7.7      10.0
##   5       4.5           3.5       4.3
##   6       1.0           0.8       1.0
##   Total 100.0         100.0     100.0

There are a little bit more people answered 1 or 6 for the value of “ipeqopt”. I.e., there is a small scale of extreme values for the people with higher levels of education in Czech Republic in terms of supporting the idea of equality. At the same time, no other obvious tendencies are found with the value of education levels and beliefs in freedom. In terms of the value of whether religious or not, it is showing that non religious people answers less 1 and 2 than religious people in the Czech Republic, meaning that non-religious people are less supporting the idea of equality than the religious people.

Task 5

abc <- cz_data %>%
  filter(!is.na(religion) & !is.na(ipeqopt))

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

table(abc$ipeqopt)
## 
##    1    2    3    4    5    6 
## 3971 7143 4787 1869  805  188
table(abc$ipeqopt, abc$religion) %>%
  cprop() %>%
  as.data.frame() %>%
  filter(Var1 != "Total",
         Var2 != "All") %>%
  ggplot(aes(x=Var1, y=Freq, fill=Var2)) +
  geom_col(position = "dodge") +
  labs(title="Belief about equal treatments/opportunities",
      subtitle = "In Czech Republic",
       y = "Conditional Percentage",
       x = "Belief Scale (1 = Believe in equal treatments/opportunities)",
       fill = "Religious or Not Religious")

Similar to my answer to task 4, There is a tendency in Czech Republic for religious people supporting more in equality ideas than the non religious people. From an opposite side assumably, statistics shows that the non religious people in Czech Republic supports more right-winged ideas.