# List of packages
packages <- c("tidyverse", "fst", "modelsummary") # 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.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"
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.

# For Norway
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
# Recode values 77 through 88 to NA
Norway_happy$y[Norway_happy$y %in% 77:88] <- NA

# checking again
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
# Now, let's determine the median of 'y'.
median_y <- median(Norway_happy$y, na.rm = TRUE)
cat("Median of 'y' is:", median_y, "\n")
## Median of 'y' is: 8
Norway_happy %>%
  summarize(
    mean_y = mean(y, na.rm = TRUE),
    median_y = median(y, na.rm = TRUE)
  ) %>%
  print()
##     mean_y median_y
## 1 7.975005        8
# For Belgium
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
# Recode values 77 through 88 to NA
belgium_happy$y[belgium_happy$y %in% 77:99] <- NA

# checking again
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
# Comparing results
result <- ess %>%
  
  filter(cntry %in% c("NO", "BE")) %>%
  
 
  mutate(happy = recode(happy, `77` = NA_real_, `88` = NA_real_, `99` = NA_real_)) %>%
  
 
  group_by(cntry) %>%
  summarize(mean_happy = mean(happy, na.rm = TRUE))

print(result)
## # A tibble: 2 × 2
##   cntry mean_happy
##   <chr>      <dbl>
## 1 BE          7.74
## 2 NO          7.98

On average, Norway reports higher levels of happiness than Belgium.

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_data <- ess %>%
  filter(cntry == "IE") %>%
  select(alcbnge)

ireland_data$x <- ireland_data$alcbnge

table(ireland_data$x)
## 
##   1   2   3   4   5   6   7   8 
##  65 650 346 417 239 641  26   6
# Compute mean and median
mean_x <- mean(ireland_data$x, na.rm = TRUE)
median_x <- median(ireland_data$x, na.rm = TRUE)

cat("Mean of 'x':", mean_x, "\n")
## Mean of 'x': 3.908787
cat("Median of 'x':", median_x, "\n")
## Median of 'x': 4
# Converting to categories to get mode as a category instead of a number
df <- ireland_data %>%
  mutate(
    x_category = case_when(
      x == 1 ~ "Daily or almost daily",
      x == 2 ~ "Weekly",
      x == 3 ~ "Monthly",
      x == 4 ~ "Less than monthly",
      x == 5 ~ "Never",
      TRUE ~ NA_character_
    ),
    x_category = fct_relevel(factor(x_category),  ### here you would put the categories in order you want them to appear or else it will appear alphabetically
                             "Daily or almost daily", 
                             "Weekly", 
                             "Monthly", 
                             "Less than monthly", 
                             "Never")
  )

# To confirm the conversion:
table(df$x_category)
## 
## Daily or almost daily                Weekly               Monthly 
##                    65                   650                   346 
##     Less than monthly                 Never 
##                   417                   239

The most common category selected for Irish respondents concerning the frequency of binge drinking was category 2 (Weekly).

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.

# For Portugal
portugal_plnftr <- ess %>% 
  filter(cntry == "PT") %>% 
  select(plnftr) 

portugal_plnftr$y <- portugal_plnftr$plnftr

table(portugal_plnftr$y)
## 
##   0   1   2   3   4   5   6   7   8   9  10  88 
## 114 184 313 356 264 481 262 382 345 166 370  40
# Recode values 77 through 88 to NA
portugal_plnftr$y[portugal_plnftr$y %in% 88] <- NA

# checking again
table(portugal_plnftr$y)
## 
##   0   1   2   3   4   5   6   7   8   9  10 
## 114 184 313 356 264 481 262 382 345 166 370
summary(portugal_plnftr)
##      plnftr             y         
##  Min.   : 0.000   Min.   : 0.000  
##  1st Qu.: 3.000   1st Qu.: 3.000  
##  Median : 5.000   Median : 5.000  
##  Mean   : 6.426   Mean   : 5.418  
##  3rd Qu.: 8.000   3rd Qu.: 8.000  
##  Max.   :88.000   Max.   :10.000  
##  NA's   :14604    NA's   :14644
#For Serbia
serbia_plnftr <- ess %>% 
  filter(cntry == "SE") %>% 
  select(plnftr) 

serbia_plnftr$y <- serbia_plnftr$plnftr

table(serbia_plnftr$y)
## 
##   0   1   2   3   4   5   6   7   8   9  10  77  88  99 
## 151 155 398 517 301 503 270 405 374 145 237   2   7   1
# Recode values 77 through 88 to NA
serbia_plnftr$y[serbia_plnftr$y %in% 77:99] <- NA

# checking again
table(serbia_plnftr$y)
## 
##   0   1   2   3   4   5   6   7   8   9  10 
## 151 155 398 517 301 503 270 405 374 145 237
summary(serbia_plnftr)
##      plnftr             y         
##  Min.   : 0.000   Min.   : 0.000  
##  1st Qu.: 3.000   1st Qu.: 3.000  
##  Median : 5.000   Median : 5.000  
##  Mean   : 5.254   Mean   : 5.018  
##  3rd Qu.: 7.000   3rd Qu.: 7.000  
##  Max.   :99.000   Max.   :10.000  
##  NA's   :14750    NA's   :14760

When using the summary() function for the variable plnftr, concerning both the countries Portugal and Serbia, it is evident that although the first quartiles and medians of both countries are similar, their means are quite different. Portugal has a mean of around 6.4 while Serbia has a mean of around 5.2. Portugal also has a higher third quartile than Serbia, a 8.0 compared to a 7.0, but Serbia has a higher maximum of 99 whereas Portugal has a maxiumum of 88. Subsequently, Serbia has a slightly higher number of NA’s than Portugal, Serbia with 14750 and Portugal with 14604.

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 <- read.fst("italy_data.fst")
italy_stfdem <- ess %>% 
  filter(cntry == "IT") %>% 
  select(stfdem)

italy_stfdem$y <- italy_stfdem$stfdem

table(italy_stfdem$y)
## 
##    0    1    2    3    4    5    6    7    8    9   10   77   88   99 
##  712  365  733  962 1221 1785 1724 1300  733  138  102   57  344    2
# Recode values 77 through 99 to NA
italy_stfdem$y[italy_stfdem$y %in% 77:99] <- NA

# checking again
table(italy_stfdem$y)
## 
##    0    1    2    3    4    5    6    7    8    9   10 
##  712  365  733  962 1221 1785 1724 1300  733  138  102
italy_stfdem <- ess %>% 
  filter(cntry == "IT")

italy_stfdem <- italy_stfdem %>%
  mutate(
    gndr = case_when(
      gndr == 1 ~ "Male",
      gndr == 2 ~ "Female",
      TRUE ~ as.character(gndr)
    ),
    stfdem = ifelse(stfdem %in% c(77, 88), NA, stfdem)  
  )
table(italy_stfdem$gndr)
## 
##      9 Female   Male 
##     13   5329   4836
italy_stfdem$gndr[italy_stfdem$gndr %in% 9] <- NA

#check table again
table(italy_stfdem$gndr)
## 
## Female   Male 
##   5329   4836
italy_stfdem <- ess %>% 
  filter(cntry == "IT")

# Convert gender and lrscale (representing pol ID self-placement from left to right)
italy_stfdem <- italy_stfdem %>%
  mutate(
    gndr = case_when(
      gndr == 1 ~ "Male",
      gndr == 2 ~ "Female",
      TRUE ~ as.character(gndr)
    ),
    stfdem = ifelse(stfdem %in% c(77, 88), NA, stfdem)  
  )
# Compute mean for male
mean_male_stfdem <- italy_stfdem %>%
  filter(gndr == "Male") %>%
  summarize(mean_stfdem_men = mean(stfdem, na.rm = TRUE))

print(mean_male_stfdem)
##   mean_stfdem_men
## 1        4.782646
# Compute mean for female
mean_female_stfdem <- italy_stfdem %>%
  filter(gndr == "Female") %>%
  summarize(mean_stfdem_women = mean(stfdem, na.rm = TRUE))

print(mean_female_stfdem)
##   mean_stfdem_women
## 1          4.694652
means_by_gender <- italy_stfdem %>%
  group_by(gndr) %>% 
  summarize(stfdem = mean(stfdem, na.rm = TRUE)) 

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

On average, using the variables stfdem and gndr, men are slightly more dissatisfied with democracy in Italy than women. This is evident by the mean output generated for both men and women, men with a mean of around 4.78 and women with a mean of around 4.69 from a scale of 0-10 (0 being extremely dissatisfied and 10 being extremely satisfied). Relatively their average may seem quite similar.

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 <- read.fst("france_data.fst")
france_data %>%
  # Setting values to NA
  mutate(stfedu = ifelse(stfedu %in% c(77, 88, 99), NA, stfedu),
         stfhlth = ifelse(stfhlth %in% c(77, 88, 99), NA, stfhlth)) %>%
  # Reshaping the data
  select(stfedu, stfhlth) %>%
  gather(variable, value, c(stfedu, stfhlth)) %>%
  # Creating the boxplot
  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 ESS data, the median french person is more satisfied with health services, as the boxplot for sltfhlth demonstrates a significantly larger first quartile and a higher median than the boxplot for stfedu.