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

Loading Data into R

Let’s load our data, fst style. Remember the data needs to be in the folder where you’re working from. You can enter getwd() to check your working directory.

getwd()
## [1] "/Users/nataliazagoriti/Desktop/222_hw1"
ess <- read_fst("All-ESS-Data.fst") #loading in ESS data

Important

If you struggle to load the entire ESS dataset on your device (e.g., you get a memory error code), you can individually load the country datasets below after downloading them from the course website. Otherwise, skip this step and jump to “Subsetting”.

Remove hashtags and run the following commands to read each country dataset as specified below.

For both the homework and tutorial, you will need:

belgium_data <- read.fst("belgium_data.fst")
#estonia_data <- read.fst("estonia_data.fst")
#france_data <- read.fst("france_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

We will first filter to Belgium and select the “happy” variable. See details here: https://ess-search.nsd.no/en/variable/39ea4d77-af95-470e-b958-45d11d43bf5e

We will then rename the happy column variable to ‘y’ for easier referencing (i.e., y typically refers to the outcome variable)

But first, we need to understand how to filter. Countries in the ESS dataset are stored in the “cntry” column (for country) under their two letter country codes. Here’s a reference: https://www23.statcan.gc.ca/imdb/p3VD.pl?Function=getVD&TVD=141329

Let’s practice creating a table of all unique entries.

unique(ess$cntry)
##  [1] "AT" "BE" "CH" "CZ" "DE" "DK" "ES" "FI" "FR" "GB" "GR" "HU" "IE" "IL" "IT"
## [16] "LU" "NL" "NO" "PL" "PT" "SE" "SI" "EE" "IS" "SK" "TR" "UA" "BG" "CY" "RU"
## [31] "HR" "LV" "RO" "LT" "AL" "XK" "ME" "RS" "MK"

BE stands for Belgium. Here’s how we are going to filter to a specific value (Belgium) within the cntry column and “select” our variable of interest –> happy

belgium_happy <- belgium_data %>% # note: if you work from belgium_data replace "ess" with belgium_data
  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
# 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

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.

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
# need to remove 77, 88, 99 or else will alter results.

# Recode values 77 through 99 to NA
norway_happy$y[norway_happy$y %in% 77:99] <- 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

The average for the variable ‘happy’ for the country of Norway is 7.975005. Belgium’s average of happiness was 7.737334, where Norway’s average of happiness is 7.975005, meaning that 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_bdfreq <- ess %>%
  filter(cntry == "IE") %>%
  select(alcbnge)

ireland_bdfreq$y <- ireland_bdfreq$alcbnge

table(ireland_bdfreq$y)
## 
##   1   2   3   4   5   6   7   8 
##  65 650 346 417 239 641  26   6
# Recode values 6 through 8 to NA.
ireland_bdfreq$y[ireland_bdfreq$y %in% 6:8] <- NA

#checking again 
table(ireland_bdfreq$y)
## 
##   1   2   3   4   5 
##  65 650 346 417 239
# Compute mean and median
mean_y <- mean(ireland_bdfreq$y, na.rm = TRUE)
median_y <- median(ireland_bdfreq$y, na.rm = TRUE)

cat("Mean of 'y':", mean_y, "\n")
## Mean of 'y': 3.066977
cat("Median of 'y':", median_y, "\n")
## Median of 'y': 3
# Converting to categories to get mode as a category instead of a number
df <- ireland_bdfreq %>%
  mutate(
    y_category = case_when(
      y == 1 ~ "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),  
                             "daily", 
                             "weekly", 
                             "monthly", 
                             "less than monthly", 
                             "never")
  )
# To confirm the conversion:
table(df$y_category)
## 
##             daily            weekly           monthly less than monthly 
##                65               650               346               417 
##             never 
##               239
# To determine the mode of our newly created category:
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: weekly

The most common category selected for Irish respondents, for the frequency of binge drinking was 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.

portugal_plnftr <- ess %>% 
  filter(cntry == "PT") %>% 
  select(plnftr)
summary(portugal_plnftr$plnftr) 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##   0.000   3.000   5.000   6.426   8.000  88.000   14604
serbia_plnftr <- ess %>% 
  filter(cntry == "RS") %>% 
  select(plnftr)
summary(serbia_plnftr$plnftr)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##   0.000   0.000   4.000   4.983   8.000  88.000    1505

Looking at these results, the first thing we can say is that for both Portugal and Serbia the minimum value is 0, indicating that some respondents have chosen to plan for the future . In Portugal, The 1st quartile is 3, indicating that 25% of respondents in Portugal have a lower inclination towards planning for the future, whereas in Serbia the 1st quartile is again 0, suggesting that 25% of respondents have an inclination to plan for the future. The median planning score in Portugal is 5, whereas in Serbia, it is 4. This suggests that on average, population in Serbia is more inclined to plan for the future compared to those in Portugal, who prefer to take each day as it comes.The mean planning score in Portugal is 6.426, higher than the mean in Serbia, which is 4.983. This further supports the idea that respondents in Portugal have a more “negative” attitude towards future planning. In conclusion, when comparing Portugal and Serbia, Serbia generally exhibits a higher average inclination towards planning for the future, shown by a higher mean and median.

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

# Convert gender and lrscale (representing pol ID self-placement from left to right)
italy_data <- italy_data %>%
  mutate(
    gndr = case_when(
      gndr == 1 ~ "Male",
      gndr == 2 ~ "Female",
      TRUE ~ as.character(gndr)
    ),
    lrscale = ifelse(lrscale %in% c(77, 88), NA, lrscale)  # Convert lrscale values
  )
# Computing average of lrscale by gender
means_by_gender <- italy_data %>%
  group_by(gndr) %>% # grouping by second group
  summarize(lrscale = mean(lrscale, na.rm = TRUE)) # summarizing variable of interest 

print(means_by_gender)
## # A tibble: 3 × 2
##   gndr   lrscale
##   <chr>    <dbl>
## 1 9         4.6 
## 2 Female    5.09
## 3 Male      5.20

Italian women had an average of 5.09 and Italian men had an average of 5.20. The results show that men on average, had a slightly higher rate of satisfaction with democracy then women in Italy (10 being “extremely satisfied” with the democracy in Italy, and 0 being “extremly dissastified”).

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 %>%
  # 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, we see that the median for education system satisfaction is 5, while the median for satisfaction with the health system is around 7. We would say that the median French person is more satisfied with health services in comparison to the education system.