# List of packages
packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer", 
              "fst", "viridis", "knitr", "rmarkdown", "ggridges", "viridis", "questionr", "flextable") # add any you need here

# Install packages if they aren't installed already
new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)

# Load the 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: 'flextable'
## 
## 
## The following object is masked from 'package:purrr':
## 
##     compose
## [[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] "rmarkdown"    "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] "ggridges"     "rmarkdown"    "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"    "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"        
## 
## [[11]]
##  [1] "questionr"    "ggridges"     "rmarkdown"    "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] "flextable"    "questionr"    "ggridges"     "rmarkdown"    "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]
}
netherlands_data <- ess %>% 
  filter(cntry == "NL") 
write_fst(netherlands_data, "~/Desktop/netherlands_data.fst") 
df <- read_fst("~/Desktop/netherlands_data.fst")
happy_by_year <- netherlands_data %>%
  group_by(year) %>%
  summarize(mean_happy = mean(happy, na.rm = TRUE))
happy_by_year
## # A tibble: 10 × 2
##     year mean_happy
##    <dbl>      <dbl>
##  1  2002       7.96
##  2  2004       7.81
##  3  2006       7.81
##  4  2008       7.80
##  5  2010       7.91
##  6  2012       7.93
##  7  2014       7.78
##  8  2016       8.01
##  9  2018       8.10
## 10  2020       8.02
happy_by_year <- netherlands_data %>%
  group_by(year) %>%
  summarize(mean_happy = mean(happy, na.rm = TRUE))
happy_by_year
## # A tibble: 10 × 2
##     year mean_happy
##    <dbl>      <dbl>
##  1  2002       7.96
##  2  2004       7.81
##  3  2006       7.81
##  4  2008       7.80
##  5  2010       7.91
##  6  2012       7.93
##  7  2014       7.78
##  8  2016       8.01
##  9  2018       8.10
## 10  2020       8.02
ggplot(happy_by_year, aes(x = year, y = mean_happy)) +
  geom_line(color = "blue", size = 1) +  # Line to show the trend
  geom_point(color = "red", size = 3) +  # Points to highlight each year's value
  labs(title = "Happiness in the Netherlands (2002-2020)", 
       x = "Year", 
       y = "Mean Happiness (0-10 scale)") +
  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.

ess$happy[ess$happy %in% c(77, 88, 99)] <- NA

avg_happy_netherlands <- netherlands_data %>%
  group_by(year) %>%
  summarize(mean_happy_netherlands = mean(happy, na.rm = TRUE))

avg_happy_all_countries <- ess %>%
  group_by(year) %>%
  summarize(mean_happy_all = mean(happy, na.rm = TRUE))

combined_data <- left_join(avg_happy_netherlands, avg_happy_all_countries, by = "year")

long_data <- combined_data %>%
 pivot_longer(
    cols = starts_with("mean_happy"),
  names_to = "happy_Type",  
    values_to = "Mean_happy"  
  )
ggplot(long_data) +
  geom_line(aes(x = year, y = Mean_happy, color = happy_Type), size = 1) +
  geom_point(aes(x = year, y = Mean_happy, color = happy_Type), size = 3) +
  labs(title = "Happiness in the Netherlands (2002-2020)", 
       x = "Year", 
       y = "Happiness (0-10 scale)") +
  ylim(0, 10) +
  scale_color_manual(values = c("blue", "red"), 
                     name = "Dataset", 
                     labels = c("Netherlands", "All ESS"), 
                     breaks = c("mean_happy_netherlands", "mean_happy_all")) + 
  theme_minimal()

netherlands_data_clean <- netherlands_data %>%
  mutate(stflife = ifelse(stflife %in% c(77, 88, 99), NA, stflife),
         enjlf = ifelse(enjlf %in% c(7, 8, 9), NA, enjlf),
         dscrgrp = ifelse(dscrgrp %in% c(7, 8, 9), NA, dscrgrp),
         rlgatnd = ifelse(rlgatnd %in% c(77, 88, 99), NA, rlgatnd),
         health = ifelse(health %in% c(7, 8, 9), NA, health),
         ) %>%
  select(enjlf, stflife, stfeco, dscrgrp, rlgatnd)

datasummary_skim(netherlands_data_clean)
Unique (#) Missing (%) Mean SD Min Median Max
enjlf 5 69 3.1 0.8 1.0 3.0 4.0
stflife 12 0 7.7 1.6 0.0 8.0 10.0
stfeco 14 0 6.7 9.3 0.0 6.0 99.0
dscrgrp 3 0 1.9 0.3 1.0 2.0 2.0
rlgatnd 8 0 5.9 1.5 1.0 7.0 7.0
table(netherlands_data$happy, netherlands_data$health) %>%
  cprop()
##        
##         1     2     3     4     5     7     8     All  
##   0       0.1   0.1   0.2   0.9   4.5   0.0   0.0   0.2
##   1       0.1   0.0   0.1   1.3   3.6   0.0   0.0   0.1
##   2       0.1   0.2   0.5   2.6  10.9   0.0   0.0   0.4
##   3       0.2   0.4   1.5   4.5   3.6   0.0   0.0   0.8
##   4       0.4   0.7   2.5   6.4   9.1   0.0   0.0   1.4
##   5       1.1   2.0   4.5  11.3   7.3   0.0   0.0   2.8
##   6       2.3   4.4   8.9  14.7  13.6   0.0   0.0   5.6
##   7      11.2  20.7  25.7  20.8  18.2  50.0  20.0  20.3
##   8      40.4  45.3  37.9  22.7  17.3   0.0  40.0  41.6
##   9      31.1  19.6  12.5   8.6   7.3  50.0  20.0  19.3
##   10     13.1   6.5   5.4   5.9   4.5   0.0  20.0   7.3
##   77      0.0   0.0   0.1   0.1   0.0   0.0   0.0   0.0
##   88      0.1   0.1   0.2   0.1   0.0   0.0   0.0   0.1
##   99      0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
##   Total 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0
table(netherlands_data$happy, netherlands_data$dscrgrp) %>%
  cprop()
##        
##         1     2     7     8     All  
##   0       0.5   0.1   0.0   1.5   0.2
##   1       0.5   0.1   0.0   0.0   0.1
##   2       1.3   0.3   0.0   2.9   0.4
##   3       1.9   0.7   0.0   2.9   0.8
##   4       3.6   1.2   0.0   2.9   1.4
##   5       6.8   2.5   0.0   5.9   2.8
##   6       9.6   5.2   0.0  13.2   5.6
##   7      23.0  20.0  83.3  20.6  20.3
##   8      32.8  42.4   0.0  32.4  41.6
##   9      13.2  19.9  16.7  11.8  19.3
##   10      6.5   7.4   0.0   5.9   7.3
##   77      0.1   0.0   0.0   0.0   0.0
##   88      0.2   0.1   0.0   0.0   0.1
##   99      0.0   0.0   0.0   0.0   0.0
##   Total 100.0 100.0 100.0 100.0 100.0
table(netherlands_data$happy, netherlands_data$rlgatnd) %>%
  cprop()
##        
##         1     2     3     4     5     6     7     77    88    99    All  
##   0       0.0   0.0   0.1   0.1   0.1   0.1   0.2   0.0   0.0   0.0   0.2
##   1       0.0   0.0   0.1   0.2   0.1   0.1   0.2   0.0  10.0   0.0   0.1
##   2       0.0   0.3   0.1   0.2   0.3   0.2   0.5   0.0   0.0   0.0   0.4
##   3       0.0   0.5   0.6   0.2   0.4   0.6   1.1   0.0   0.0   0.0   0.8
##   4       4.8   1.0   1.1   0.9   1.2   1.4   1.5   0.0   0.0   0.0   1.4
##   5       0.0   2.9   2.5   2.3   3.0   3.2   2.9   0.0   0.0   0.0   2.8
##   6       9.5   5.2   5.0   5.5   5.1   5.4   5.8   0.0   0.0   0.0   5.6
##   7      11.9  16.4  19.6  20.6  19.7  21.4  20.4  25.0  50.0   0.0  20.3
##   8      31.0  41.7  41.4  42.2  43.6  42.2  41.0  50.0  40.0   0.0  41.6
##   9      21.4  21.2  19.8  20.6  19.3  18.2  19.3  25.0   0.0   0.0  19.3
##   10     21.4  10.6   9.6   7.3   7.0   7.0   6.9   0.0   0.0 100.0   7.3
##   77      0.0   0.2   0.0   0.0   0.0   0.1   0.0   0.0   0.0   0.0   0.0
##   88      0.0   0.0   0.1   0.0   0.1   0.1   0.1   0.0   0.0   0.0   0.1
##   99      0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
##   Total 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0
netherlands_data %>%

  group_by(rlgatnd) %>%
  summarise(
    mean_happy = mean(happy, na.rm = TRUE),
    std_happy = sd(happy, na.rm = TRUE)
  )
## # A tibble: 10 × 3
##    rlgatnd mean_happy std_happy
##      <dbl>      <dbl>     <dbl>
##  1       1       8.14     1.54 
##  2       2       8.10     3.07 
##  3       3       7.98     2.49 
##  4       4       7.89     1.24 
##  5       5       7.98     3.63 
##  6       6       7.94     3.65 
##  7       7       7.87     3.41 
##  8      77       8        0.756
##  9      88       6.8      2.10 
## 10      99      10       NA
netherlands_data %>%

  group_by(health) %>%
  summarise(
    mean_happy = mean(happy, na.rm = TRUE),
    std_happy = sd(happy, na.rm = TRUE)
  )
## # A tibble: 7 × 3
##   health mean_happy std_happy
##    <dbl>      <dbl>     <dbl>
## 1      1       8.42      2.74
## 2      2       7.98      2.73
## 3      3       7.65      4.37
## 4      4       6.76      4.39
## 5      5       5.67      2.70
## 6      7       8         1.41
## 7      8       8.4       1.14
netherlands_data %>%

  group_by(dscrgrp) %>%
  summarise(
    mean_happy = mean(happy, na.rm = TRUE),
    std_happy = sd(happy, na.rm = TRUE)
  )
## # A tibble: 4 × 3
##   dscrgrp mean_happy std_happy
##     <dbl>      <dbl>     <dbl>
## 1       1       7.45     4.41 
## 2       2       7.95     3.17 
## 3       7       7.33     0.816
## 4       8       7.03     1.98
netherlands_subset <- netherlands_data_clean %>%
  mutate(dscrgrp = recode(as.character(dscrgrp),
                      '1' = "Yes", 
                      '2' = "No",
                      `7` = NA_character_,
                      `8` = NA_character_,
                      `9` = NA_character_))
netherlands_happy <- ess %>%
  filter(cntry == "NL") %>%
  select(happy)
netherlands_dscrgrp <- ess %>%
  filter(cntry == "NL") %>%
  select(dscrgrp)
ggplot(netherlands_data, aes(x = dscrgrp, y = happy, fill = factor(dscrgrp))) +
  geom_boxplot() +
  labs(title = "Happiness by Self-Identified Discrimiation",
       x = "Whether one feels discriminated against in the Netherlands",
       y = "Average Happiness") +
  scale_fill_viridis_d(name = "Happiness", option = "B") +
  scale_fill_manual(values = c("Yes" = "pink", "No" = "orange")) +
  theme_classic()
## Scale for fill is already present.
## Adding another scale for fill, which will replace the existing scale.