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

Task 1

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]
}
france_data <- ess %>%
  filter(cntry == "FR") %>%
  mutate(
    wrkprty = ifelse(wrkprty == 2, 0, ifelse(wrkprty %in% c(7, 8, 9), NA, wrkprty)),
    stfgov = ifelse(stfgov %in% c(77, 88, 99), NA, stfgov),
    polintr = ifelse(polintr == 4, 0, ifelse(polintr %in% c(7, 8, 9), NA, polintr)),
  )

datasummary_skim(france_data %>% select(wrkprty, stfgov, polintr))
Unique (#) Missing (%) Mean SD Min Median Max
wrkprty 3 10 0.0 0.2 0.0 0.0 1.0
stfgov 12 1 3.7 2.3 0.0 4.0 10.0
polintr 5 0 1.8 1.1 0.0 2.0 3.0

From task 1, I noticed that most French people have worked in a political party or action group in the last 12 months. The satisfaction with the national government seems left-skewed, and the variable for interest in politics seems right-skewed.

Task 2

france_data %>%
  ggplot(aes(x = stfgov, y = as.factor(year), fill = as.factor(year))) +
  geom_density_ridges() + 
  labs(title = "Satisfaction with the National Government", 
       x = "Satisfaction (0-10)", y = "Survey Year",
       fill = "Survey Year") +
  theme_minimal()
## Picking joint bandwidth of 0.435
## Warning: Removed 280 rows containing non-finite values
## (`stat_density_ridges()`).

I noticed that the trend for satisfaction with the national government in 2002 concentrated in the center and moved to the left around 2012 to 2018, then moved to the center in 2020.

Task 3

ess_selected <- ess %>%
  filter(cntry %in% c("FR", "ES", "NO")) %>%
  mutate(stfgov = ifelse(stfgov %in% c(77, 88, 99), NA, stfgov))


task3plot <- ggplot(ess_selected, aes(x = reorder(cntry, -stfgov, FUN=median), y = stfgov, fill = cntry)) +
  geom_boxplot() +
  theme_minimal() + 
  theme(legend.position = "none") + 
  labs(title = "Boxplot comparison for satisfaction with National Government (France, Spain, Norway)", 
       x = "Country", 
       y = "Scale (0-10)")

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

By comparing the satisfaction with the national government between France, Spain and Norway, I noticed that France and Spain showed a similar trend while more people in Norway were satisfied with the national government.

Task 4

france_data <- france_data %>%
  mutate(stf = recode(as.character(stfgov), 
                      '0' = "0",
                      '1' = "1",
                      '2' = "2", 
                      '3' = "3", 
                      '4' = "4",
                      '5' = "5",
                      '6' = "6",
                      '7' = "7",
                      '8' = "8",
                      '9' = "9",
                      '10'= "10"))

table(france_data$stf)
## 
##    0    1   10    2    3    4    5    6    7    8    9 
## 2131 1241  102 2381 2823 2640 3538 1843 1275  650  134
table(france_data$stfgov)
## 
##    0    1    2    3    4    5    6    7    8    9   10 
## 2131 1241 2381 2823 2640 3538 1843 1275  650  134  102
clsprtystfgov <- datasummary_crosstab(clsprty ~ stf, data = france_data)

clsprtystfgov
clsprty 0 1  10  2  3  4  5  6  7  8  9 All
1 N 1049 649 47 1215 1363 1236 1606 1017 762 416 96 9507
% row 11.0 6.8 0.5 12.8 14.3 13.0 16.9 10.7 8.0 4.4 1.0 100.0
2 N 1045 583 50 1128 1421 1350 1863 809 490 223 38 9194
% row 11.4 6.3 0.5 12.3 15.5 14.7 20.3 8.8 5.3 2.4 0.4 100.0
7 N 16 3 2 7 14 15 17 3 6 3 0 101
% row 15.8 3.0 2.0 6.9 13.9 14.9 16.8 3.0 5.9 3.0 0.0 100.0
8 N 21 6 3 31 25 39 52 14 17 8 0 236
% row 8.9 2.5 1.3 13.1 10.6 16.5 22.0 5.9 7.2 3.4 0.0 100.0
All N 2131 1241 102 2381 2823 2640 3538 1843 1275 650 134 19038
% row 11.2 6.5 0.5 12.5 14.8 13.9 18.6 9.7 6.7 3.4 0.7 100.0
france_data <- france_data %>%
  mutate(pdwrk_recode = case_when(
    pdwrk == 1 ~ 'yes',
    pdwrk == 0 ~ 'no', 
  ))

table(france_data$pdwrk_recode)
## 
##   no  yes 
## 9466 9572
table(france_data$pdwrk)
## 
##    0    1 
## 9466 9572
clsprtypdwrk <- datasummary_crosstab(clsprty ~ pdwrk_recode, data = france_data)

clsprtypdwrk
clsprty no yes All
1 N 4934 4573 9507
% row 51.9 48.1 100.0
2 N 4309 4885 9194
% row 46.9 53.1 100.0
7 N 63 38 101
% row 62.4 37.6 100.0
8 N 160 76 236
% row 67.8 32.2 100.0
All N 9466 9572 19038
% row 49.7 50.3 100.0
france_data <- france_data %>%
  mutate(religion = case_when(
    rlgblg == 2 ~ "No",
    rlgblg == 1 ~ "Yes",
    rlgblg %in% c(7, 8, 9) ~ NA_character_,
    TRUE ~ as.character(rlgblg)
  ))

table(france_data$religion)
## 
##   No  Yes 
## 9434 9509
table(france_data$rlgblg)
## 
##    1    2    7    8 
## 9509 9434   53   42
clsprtyrlgblg <- datasummary_crosstab(clsprty ~ religion, data = france_data)

clsprtyrlgblg
clsprty No Yes All
1 N 4345 5135 9507
% row 45.7 54.0 100.0
2 N 4963 4183 9194
% row 54.0 45.5 100.0
7 N 28 59 101
% row 27.7 58.4 100.0
8 N 98 132 236
% row 41.5 55.9 100.0
All N 9434 9509 19038
% row 49.6 49.9 100.0
table(france_data$clsprty, france_data$stf) %>%
  cprop()
##        
##         0     1     10    2     3     4     5     6     7     8     9     All  
##   1      49.2  52.3  46.1  51.0  48.3  46.8  45.4  55.2  59.8  64.0  71.6  50.4
##   2      49.0  47.0  49.0  47.4  50.3  51.1  52.7  43.9  38.4  34.3  28.4  48.0
##   7       0.8   0.2   2.0   0.3   0.5   0.6   0.5   0.2   0.5   0.5   0.0   0.5
##   8       1.0   0.5   2.9   1.3   0.9   1.5   1.5   0.8   1.3   1.2   0.0   1.2
##   Total 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0
table(france_data$clsprty, france_data$pdwrk_recode) %>%
  cprop()
##        
##         no    yes   All  
##   1      52.1  47.8  49.9
##   2      45.5  51.0  48.3
##   7       0.7   0.4   0.5
##   8       1.7   0.8   1.2
##   Total 100.0 100.0 100.0
table(france_data$clsprty, france_data$religion) %>%
  cprop()
##        
##         No    Yes   All  
##   1      46.1  54.0  50.0
##   2      52.6  44.0  48.3
##   7       0.3   0.6   0.5
##   8       1.0   1.4   1.2
##   Total 100.0 100.0 100.0

I noticed that more people have paid work and religion while fewer are satisfied with the national government.

Task 5

df <- france_data %>%
  filter(!is.na(pdwrk_recode) & !is.na(clsprty))

df <- df %>%
  mutate(pdwrk_recode = case_when(
    pdwrk == 1 ~ 'Yes',
    pdwrk == 0 ~ 'No', 
  ))

table(df$clsprty)
## 
##    1    2    7    8 
## 9507 9194  101  236
table(df$clsprty, df$pdwrk_recode) %>%
  cprop() %>%
  as.data.frame() %>%
  filter(Var1 != "Total",
         Var2 != "All") %>%
  ggplot(aes(x=Var1, y=Freq, fill=Var2)) +
  geom_col(position = "dodge") +
  labs(title="Have been doing paid work last 7 days",
       y = "Conditional Percentage",
       x = "Paid work",
       fill = "Paid Work vs. Not")

From the visualization, I noticed a similar percentage of having and not having a paid job for the last 7 days.