First things first: setting up your environment

In the list following c(, you would be any package you need in between quotation marks.

# 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.4
## ✔ 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"

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.

Loading Data into R

ess <- read_fst("All-ESS-Data.fst")
belgium_data <- read.fst("belgium_data.fst")
norway_data <- read.fst("norway_data.fst")
ireland_data <- read.fst("ireland_data.fst")
portugal_data <- read.fst("portugal_data.fst")
serbia_data <- read.fst("serbia_data.fst")
italy_data <- read.fst("italy_data.fst")

Subsetting

belgium_happy <- belgium_data %>% # note: if you work from belgium_data replace "ess" with belgium_data
  filter(cntry == "BE") %>% 
  select(happy)



```r
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
# need to remove 77, 88, 99 or else will alter results. See data portal for what they represent (e.g. DK, Refusal, etc.)

# Recode values 77 through 99 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
# Calculate the mean happiness for Norway
norway_happy <- norway_data %>%
  filter(cntry == "NO") %>%
  summarize(mean_happy = mean(happy, na.rm = TRUE))
# Calculate the mean happiness for Belgium
belgium_mean_happy <- belgium_data %>%
  filter(cntry == "BE") %>%
  summarize(mean_happy = mean(happy, na.rm = TRUE))

Results

cat(“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.

# Create a new variable 'alcbnge_category' based on the information in the link
ireland_data = ireland_data %>%
  mutate(alcbnge_category = case_when(
    alcbnge == 1 ~ "Every day or almost every day",
    alcbnge == 2 ~ "More than once a week",
    alcbnge == 3 ~ "Every week",
    alcbnge == 4 ~ "Every month",
    alcbnge == 5 ~ "Less often than monthly",
    alcbnge == 6 ~ "Never",
    TRUE ~ "Unknown or Not Stated"
  ))
# Count the frequency of each category for Irish respondents
alcbnge = ireland_data %>%
  filter(cntry == "IE") %>%
  count(alcbnge_category, sort = TRUE)

Get the most common category

most_common_category <- ireland_alcbnge [1, “alcbnge_category”] cat(“The most common category for frequency of binge drinking among Irish respondents is:”, most_common_category, “unknown or not stated”)

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.

#Filter data for Portugal and Serbia
portugal_data <- portugal_data %>% filter(cntry == "PT")
serbia_data <- serbia_data %>% filter(cntry == "RS")
# Summary for the variable 'plnftr' in Portugal
summary(portugal_data$plnftr)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##   0.000   3.000   5.000   6.426   8.000  88.000   14604
# Summary for the variable 'plnftr' in Serbia
summary(serbia_data$plnftr)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##   0.000   0.000   4.000   4.983   8.000  88.000    1505

Explanation

Firstly, when looking at the coded data, we can see that Portugal (6.426) has a higher mean than Serbia (4.983), therefore we can conclude that on average people in Portugal are more likely to plan their futures when compared to Serbians.

However the medians of Portugal(5) and Serbia (4) are different as they both plan their futures. However, Portugal just has to have a higher rate.

To add on, the minimum and maximum variables are the same amongst both countries indicating similar distribution and uniformity between the countries.

Moving on to quartiles, Portugal has a higher 1st quartile (3,000) than Serbia (0.000) which indicates that th values in the lower 25% are higher in Portugal, and in the third quartile both countries share the same variable (8,000) which indicates that the upper 25% has similar values.

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

#Filter data for Italy, and for gender categories 1 (Men) and 2 (Women)
italy_data <- italy_data %>%
  filter(cntry == "IT", gndr %in% c(1, 2))
# Calculate the mean satisfaction with democracy for men and women
mean_satisfaction <- italy_data %>%
  group_by(gndr) %>%
  summarize(mean_stfdem = mean(stfdem, na.rm = TRUE))
# Print the results
print(mean_satisfaction)
## # A tibble: 2 × 2
##    gndr mean_stfdem
##   <dbl>       <dbl>
## 1     1        7.43
## 2     2        8.43
(mean_satisfaction$mean_stfdem[1] < mean_satisfaction$mean_stfdem[2]) 
## [1] TRUE
  cat("On average, women in Italy are more dissatisfied with democracy.\n")
## On average, women in Italy are more dissatisfied with democracy.

Mean is the measure of central tendency, meaning higher mean is higher average of unsatisfied women when it comes to democracy in Italy.

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.

boxplot_data <- italy_data %>%
  select(stfedu, stfhlth) %>%
  gather(key = "variable", value = "satisfaction", stfedu, stfhlth)
# Boxplot with original labels
boxplot_original <- ggplot(boxplot_data, aes(x = variable, y = satisfaction)) +
  geom_boxplot() +
  labs(title = "Boxplot of Satisfaction with Education vs. Health Services",
       y = "Satisfaction (0-10)")
# Print the original boxplot
print(boxplot_original)

# Boxplot with modified labels
boxplot_modified <- ggplot(boxplot_data, aes(x = variable, y = satisfaction)) +
  geom_boxplot() +
  labs(title = "Boxplot of Satisfaction with the state of Education vs. Health Services",
       x = NULL,   # Remove x-axis label
       y = "Satisfaction (0-10)")
# Print the modified boxplot
print(boxplot_modified)

# Explanation

In order to interpret the boxplot graph of “stfedu” and “stfhlth” to figure out if the median French person is more satisfied with the education or health systems we can look at the median of both charts, we see that the median line which is in the center of the box is higher for health services suggesting median satisfaction with health is higher. however the outliers and box heights are similar visually which indicates that there isnt a great difference between value spreads. To conclude, according to the ESS data provided the French are more satisfied with health services.