packages <- c("tidyverse", "fst", "modelsummary")

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.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ 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
## [[1]]
##  [1] "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"     "readr"    
##  [7] "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"     "graphics" 
## [13] "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[2]]
##  [1] "fst"       "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"    
##  [7] "readr"     "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"    
## [13] "graphics"  "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[3]]
##  [1] "modelsummary" "fst"          "lubridate"    "forcats"      "stringr"     
##  [6] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [11] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [16] "utils"        "datasets"     "methods"      "base"
library(fst)
ess <- read_fst("All-ESS-Data.fst")

Task 1

Provide code and answer.

Prompt and question: calculate the average for the variable ‘happy’ for the country of Norway. On average, based on the ESS data, who reports higher levels of happiness: Norway or Belgium?

Note: we already did it for Belgium. You just need to compare to Norway’s average, making sure to provide the code for both.

belgium_happy <- ess %>% 
  filter(cntry == "BE") %>% 
  select(happy)
belgium_happy$y <- belgium_happy$happy
table(belgium_happy$y)
## 
##    0    1    2    3    4    5    6    7    8    9   10   77   88   99 
##   50   27  104  194  234  830  999 3503 6521 3402 1565    3   16    3
belgium_happy$y[belgium_happy$y %in% 77:99] <- NA
table(belgium_happy$y)
## 
##    0    1    2    3    4    5    6    7    8    9   10 
##   50   27  104  194  234  830  999 3503 6521 3402 1565
mean_y <- mean(belgium_happy$y, na.rm = TRUE)
cat("Mean of 'y' is:", mean_y, "\n")
## Mean of 'y' is: 7.737334
norway_happy <- ess %>% 
  filter(cntry == "NO") %>% 
  select(happy)
norway_happy$y <- norway_happy$happy
table(norway_happy$y)
## 
##    0    1    2    3    4    5    6    7    8    9   10   77   88 
##   15   29   59  163  238  730  817 2617 5235 3796 2344   12   10
norway_happy$y[norway_happy$y %in% 77:99] <- NA
table(norway_happy$y)
## 
##    0    1    2    3    4    5    6    7    8    9   10 
##   15   29   59  163  238  730  817 2617 5235 3796 2344
mean_y <- mean(norway_happy$y, na.rm = TRUE)
cat("Mean of 'y' is:", mean_y, "\n")
## Mean of 'y' is: 7.975005

Based on the calculated averages, Norway reports higher happiness of 7.975 where Belgium scores lower with 7.737

Task 2

Provide code and answer.

Prompt and question: what is the most common category selected, for Irish respondents, for frequency of binge drinking? The variable of interest is: alcbnge.

More info here: https://ess-search.nsd.no/en/variable/0c65116e-7481-4ca6-b1d9-f237db99a694.

Hint: need to convert numeric value entries to categories as specified in the variable information link. We did similar steps for Estonia and the climate change attitude variable.

Ireland_alcbnge <- ess %>%
  filter(cntry == "IE") %>%
  select(wrclmch)

Ireland_alcbnge$y <- Ireland_alcbnge$wrclmch

table(Ireland_alcbnge$y)
## 
##    1    2    3    4    5    6    7    8 
##  386  938 2080  820  217   51    3   32
df <- Ireland_alcbnge %>%
  mutate(
    y_category = case_when(
      y == 1 ~ "Daily / alsmot daily",
      y == 2 ~ "Weekly",
      y == 3 ~ "Monthly",
      y == 4 ~ "Less than monthly",
      y == 5 ~ "Never",
      TRUE ~ NA_character_
    ),
    y_category = fct_relevel(factor(y_category),  ### here you would put the categories in order you want them to appear or else it will appear alphabetically
                             "Daily / alsmot daily", 
                             "Weekly", 
                             "Monthly", 
                             "Less than monthly", 
                             "Never")
  )

# To confirm the conversion:
table(df$y_category)
## 
## Daily / alsmot daily               Weekly              Monthly 
##                  386                  938                 2080 
##    Less than monthly                Never 
##                  820                  217
get_mode <- function(v) {
  tbl <- table(v)
  mode_vals <- as.character(names(tbl)[tbl == max(tbl)])
  return(mode_vals)
}

mode_values <- get_mode(df$y_category)
cat("Mode of y category:", paste(mode_values, collapse = ", "), "\n")
## Mode of y category: Monthly

The most common category selected is Monthly

Task 3

Provide code and answer.

Prompt and question: when you use the summary() function for the variable plnftr (about planning for future or taking every each day as it comes from 0-10) for both the countries of Portugal and Serbia, what do you notice? What stands out as different when you compare the two countries (note: look up the variable information on the ESS website to help with interpretation)? Explain while referring to the output generated.

portugal_plnftr <- ess %>% 
  filter(cntry == "PT") %>% 
  mutate(plnftr = recode(plnftr, `77` = NA_real_, `88` = NA_real_, `99` = NA_real_)) %>%
  select(plnftr)
summary(portugal_plnftr)
##      plnftr      
##  Min.   : 0.000  
##  1st Qu.: 3.000  
##  Median : 5.000  
##  Mean   : 5.418  
##  3rd Qu.: 8.000  
##  Max.   :10.000  
##  NA's   :14644
serbia_plnftr <- ess %>% 
  filter(cntry == "RS") %>% 
  mutate(plnftr = recode(plnftr, `77` = NA_real_, `88` = NA_real_, `99` = NA_real_)) %>%
  select(plnftr)
summary(serbia_plnftr)
##      plnftr      
##  Min.   : 0.000  
##  1st Qu.: 0.000  
##  Median : 4.000  
##  Mean   : 4.143  
##  3rd Qu.: 8.000  
##  Max.   :10.000  
##  NA's   :1526

From the summery of both countries, it appears that Serbia plans more for the future than Portugalwhich can be shown through both the mean and median as a lower number closer to 0 refers to planning for the future as much as possible and a higher number closer to 10 indicates that they take each day as it comes.

A similarity is that between portugal and serbia, both 3rd Qu are 8.0. Some differences are that the difference between portugals mean and median is 0.4 which is a bigger gap than Serbia with only 0.1.

Task 4

Provide code and answer.

Prompt and question: using the variables stfdem and gndr, answer the following: on average, who is more dissastified with democracy in Italy, men or women? Explain while referring to the output generated.

Info on variable here: https://ess.sikt.no/en/variable/query/stfdem/page/1

Italy_data <- ess %>% 
  filter(cntry == "IT")
Italy_data <- Italy_data %>%
  mutate(
    gndr = case_when(
      gndr == 1 ~ "Male",
      gndr == 2 ~ "Female",
      TRUE ~ as.character(gndr)
    ),
    stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem)
  )
Italy_data$gndr[Italy_data$gndr %in% 9] <- NA
means_by_gender <- Italy_data %>%
  group_by(gndr) %>% 
  summarize(stfdem = mean(stfdem, na.rm = TRUE)) 

print(means_by_gender)
## # A tibble: 3 × 2
##   gndr   stfdem
##   <chr>   <dbl>
## 1 Female   4.66
## 2 Male     4.78
## 3 <NA>     3.25

Based on the output, women are more dissatisfied with democracy in Italy than men. With the lower numbers towards 0 indicating extremely dissatisfied and higher numbers closer to 10 indicating extremely satisfied, Wwmen have the lower average of 4.59 where as men have the slightly higher number of 4.71.

Task 5

Provide code and answer.

Prompt: Interpret the boxplot graph of stfedu and stfhlth that we generated already: according to ESS data, would we say that the median French person is more satisfied with the education system or health services? Explain.

Change the boxplot graph: provide the code to change some of the key labels: (1) Change the title to: Boxplot of satisfaction with the state of education vs. health services; (2) Remove the x-axis label; (3) Change the y-axis label to: Satisfaction (0-10).

Hint: copy the boxplot code above and just replace or cut what is asked.

france_data <- ess %>% 
  filter(cntry == "FR")
france_data %>%
  mutate(stfedu = ifelse(stfedu %in% c(77, 88, 99), NA, stfedu),
         stfhlth = ifelse(stfhlth %in% c(77, 88, 99), NA, stfhlth)) %>%
  select(stfedu, stfhlth) %>%
  gather(variable, value, c(stfedu, stfhlth)) %>%
  ggplot(aes(x = variable, y = value)) +
  geom_boxplot() +
  labs(y = "Satisfaction (0-10)", title = "Boxplot of satisfaction with the state of education vs. health services") +
  theme_minimal()
## Warning: Removed 364 rows containing non-finite values (`stat_boxplot()`).

According to the ESS data with 0 being extremely bad and 10 being extremely good the median french person is more satisfied with health services than education. Shown on the boxplot, stfhlth ranks higher than stfedu indicating that french people obtain greater satisfaction from health services and lower amounts from education.