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
## `modelsummary` has built-in support to draw text-only (markdown) tables.
##   To generate tables in other formats, you must install one or more of
##   these libraries:
##   
## install.packages(c(
##     "kableExtra",
##     "gt",
##     "flextable",
##    
##   "huxtable",
##     "DT"
## ))
## 
##   Alternatively, you can set markdown as the default table format to
##   silence this alert:
##   
## config_modelsummary(factory_default = "markdown")
## [[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.

ess <- read_fst("All-ESS-Data.fst")

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

Note: Make sure to provide both your markdown file and R pubs link. If you do not submit both, you will be penalized 2 pts. out of the 5 pts. total.

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?

This portion of the task will be providing the mean variable for Norway’s happiness.

norway_happy <- ess %>% 
  filter(cntry == "NO") %>% 
  select(happy)

This chunk demonstrates the filtering and selection of the desired country and the measurable variable.

filter(cntry == “NO”) Filters the the desired ESS Data for Norway or NO, as well as selecting (happy) the desired measurable variable.

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

This chunk demonstrates a table which measures the country’s happiness from 0 being “Extremely unhappy” to 10 being “Extremely happy”

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

This chunk of code cleans the table of outliers, not consistent with the happiness ratings.

mean_y <- mean(norway_happy$y, na.rm = TRUE)
cat("Mean of 'y' is:", mean_y, "\n")
## Mean of 'y' is: 7.975005

After completing the sequence, this chunk finds the mean/average happines for Norway, which is the “y” variable.

summary(norway_happy)
##      happy              y         
##  Min.   : 0.000   Min.   : 0.000  
##  1st Qu.: 7.000   1st Qu.: 7.000  
##  Median : 8.000   Median : 8.000  
##  Mean   : 8.076   Mean   : 7.975  
##  3rd Qu.: 9.000   3rd Qu.: 9.000  
##  Max.   :88.000   Max.   :10.000  
##                   NA's   :22

This chuck summarizes the finds, showing both the median, mean, minimum and maximum.

After demonstrating the average happiness for Norway, I will no also provide the average/mean for Belgium.

belgium_happy <- ess %>%
  filter(cntry == "BE") %>% 
  select(happy)

filter(cntry == “BE”) This filters the the desired ESS Data for Belgium or BE, as well as selecting (happy) the desired measurable variable.

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

This chunk demonstrates a table which measures the country’s happiness from 0 being “Extremely unhappy” to 10 being “Extremely happy”, also including the outliers through 77 - 88.

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

This chunk cleans the table of outliers, which measures the rating of happiness of Belgium.

mean_y <- mean(belgium_happy$y, na.rm = TRUE)
cat("Mean of 'y' is:", mean_y, "\n")
## Mean of 'y' is: 7.737334

The line of code finds the level of happiness experienced in Belgium.

summary(belgium_happy)
##      happy              y         
##  Min.   : 0.000   Min.   : 0.000  
##  1st Qu.: 7.000   1st Qu.: 7.000  
##  Median : 8.000   Median : 8.000  
##  Mean   : 7.839   Mean   : 7.737  
##  3rd Qu.: 9.000   3rd Qu.: 9.000  
##  Max.   :99.000   Max.   :10.000  
##                   NA's   :22

When comparing the average happiness of the two country, it is evident that Norway has a slightly higher level of happiness in comparison to Belgium, with Norway having a 7.9 rating, to Belgium’s 7.7 rating.

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

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

ireland_ccbinge$y <- ireland_ccbinge$alcbnge

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

##This line of code finds the frequency of binge drinking in Ireland, assessing how often respondents drink through rating 1 “Daily or almost daily” to 6 “Never”.

mean_y <- mean(ireland_ccbinge$y, na.rm = TRUE)
median_y <- median(ireland_ccbinge$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

##This line of code finds the average and median on how often respondent binge drink for the “y” variable, with results showing mean “3.066977” and median “3”.

df <- ireland_ccbinge %>%
  mutate(
    y_category = case_when(
      y == 1 ~ "Daily or almost 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 or almost daily", 
                             "Weekly", 
                             "Monthly", 
                             "Less than monthly", 
                             "Never")
  )

table(df$y_category)
## 
## Daily or almost daily                Weekly               Monthly 
##                    65                   650                   346 
##     Less than monthly                 Never 
##                   417                   239

##This line of code demonstrates the frequency of drinking, categorizing how often Irish respondent drink.

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

##This line of code demonstrates that the average Irish respondent binge drink on a weekly basis.

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.

result <- ess %>%
  filter(cntry %in% c("PT", "RS")) %>%

  mutate(plnftr = recode(plnftr, `77` = NA_real_, `88` = NA_real_, `99` = NA_real_)) %>%
  
  group_by(cntry) %>%
  summarize(mean_plnftr = mean(plnftr, na.rm = TRUE))

print(result)
## # A tibble: 2 × 2
##   cntry mean_plnftr
##   <chr>       <dbl>
## 1 PT           5.42
## 2 RS           4.14

##This line of code finds the variable plnftr (planning for future) between two countries, Portuagal and Serbia, revealing that Portugal, on average has a higher average for plnftr, with 5.4 percent, compared to Serbia with a lower percentile of 4.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 ~ NA_character_
    ),
    stfdem = ifelse(stfdem %in% c(77, 88), NA, stfdem)
  )

##This line of code outlines the satisfaction of democracy (stdfem) between Italian respondents, categorizing by gender (gndr).

mean_male_stfdem <- italy_data %>%
  filter(gndr == "Male") %>%
  summarize(mean_stfdem_men = mean(stfdem, na.rm = TRUE))

print(mean_male_stfdem)
##   mean_stfdem_men
## 1        4.782646

##This line of code demonstrate the percentage for stfdem for men, revealing that mean have a 4.7 percent satisfaction with democracy.

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.69
## 2 Male     4.78
## 3 <NA>     3.25

##This line of code reveals the average for stfdem for both male and female respondents, percentages showing that males have a slightly higher average for satisfactory of democracy than females, revealing that males have a 4.7 average for satisfaction for democracy, and females with 4.6 percent satisfaction.

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 = "Satisfaction with the State of Education vs. Health Services") +
  theme_minimal()
## Warning: Removed 364 rows containing non-finite values (`stat_boxplot()`).

##This chuck of code demonstrates the stfedu (Satisfaction for Education) and stfhlth (Satisfaction for Health Services) in France, revealing that, respondents tend to have a higher satisfaction with health services over education.