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.
Code: 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) norway_data <- read.fst(“/Users/mac/Desktop/norway_data.fst”) norway_happy <- norway_data %>% filter(cntry == “NO”) %>% select(happy) norway_happy\(y <- norway_happy\)happy table(norway_happy\(y) norway_happy\)y[norway_happy\(y %in% c(77:88)] <- NA table(norway_happy\)y) norway_happy_mean <- mean(norway_happy$y, na.rm = TRUE) print(norway_happy_mean)
belgium_data <- read.fst(“/Users/mac/Desktop/belgium_data.fst”)
belgium_happy <- belgium_data %>% filter(cntry == “BE”) %>%
select(happy) belgium_happy\(y <-
belgium_happy\)happy table(belgium_happy\(y) belgium_happy\)y[belgium_happy\(y %in% 77:99] <- NA
table(belgium_happy\)y)
belgium_happy_mean <- mean(belgium_happy$y, na.rm = TRUE)
print(belgium_happy_mean)
Answer: The Norway’s average is 7.975, and the Belgium’s average is 7.737. So Norway reports higher levels of happiness.
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.
Code: 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) ireland_data <- read.fst(“/Users/mac/Desktop/ireland_data.fst”) ireland_data <- ireland_data %>% filter(cntry == “IE”) %>% select(alcbnge) ireland_data\(alcbnge_category <- case_when( ireland_data\)alcbnge == 1 ~ “Never”, ireland_data\(alcbnge == 2 ~ "Less than once a month", ireland_data\)alcbnge == 3 ~ “Once a month”, ireland_data\(alcbnge == 4 ~ "Once a week", ireland_data\)alcbnge == 5 ~ “Daily”, TRUE ~ NA_character_ ) get_mode <- function(v) { tbl <- table(v) mode_vals <- as.character(names(tbl)[tbl == max(tbl)]) return(mode_vals) } mode_values <- get_mode(ireland_data$alcbnge_category) cat(“Most common category of binge drinking frequency in Ireland:”, paste(mode_values, collapse = “,”), “”)
Answer: Most common category of binge drinking frequency in Ireland: Less than once a month.
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.
Code: 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) portugal_data <- read.fst(“/Users/mac/Desktop/portugal_data.fst”) serbia_data <- read.fst(“/Users/mac/Desktop/serbia_data.fst”) portugal_plnftr <- portugal_data %>% filter(cntry == “PT”) %>% select(plnftr) summary_portugal <- summary(portugal_plnftr) serbia_plnftr <- serbia_data %>% filter(cntry == “RS”) %>% select(plnftr) summary_serbia <- summary(serbia_plnftr) print(summary_portugal) print(summary_serbia)
Answer:I’ve observed differences between Portugal and Serbia regarding attitudes towards planning for the future (the variable plnftr). The higher median in Portugal suggests that Portuguese people are more inclined to plan for the future.
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.
Code: 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) italy_data <- read.fst(“/Users/mac/Desktop/Italy_data.fst”) italy_data <- italy_data %>% filter(cntry == “IT”) %>% mutate( gndr = case_when( gndr == 1 ~ “Male”, gndr == 2 ~ “Female”, TRUE ~ as.character(gndr) ), stfdem = ifelse(stfdem %in% c(77, 88), NA, stfdem) # Replace ‘special_codes’ with actual codes for NA ) average_satisfaction_by_gender <- italy_data %>% group_by(gndr) %>% summarize(average_stfdem = mean(stfdem, na.rm = TRUE)) print(average_satisfaction_by_gender)
Answer: Italian women are less satisfied with democracy, beacuse the average score for women is lower than that for men.
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.
Code: 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) library(dplyr) france_data <- read.fst(“/Users/mac/Desktop/france_data.fst”) 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)”, x = ““, title =”Boxplot of satisfaction with the state of education vs. health services”) + theme_minimal()
Answer: The satisfaction score for the state of education exceeds that for health services, indicating that the median French person has a higher level of satisfaction with education compared to health services.