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)
Loading Data into R
belgium_data <- “belgium_data_fst”
ess <- “All-ESS-Data.fst”
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.
Subsetting
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)
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)
mean_y <- mean(belgium_happy$y, na.rm = TRUE) cat(“Mean of ‘y’ is:”, mean_y, “”)
norway_happy <- norway_data %>% # note: if you work from belgium_data replace “ess” with belgium_data filter(cntry == “NO”) %>% select(happy)
norway_happy\(y <- norway_happy\)happy
table(norway_happy$y)
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 norway_happy\(y[norway_happy\)y %in% 77:99] <- NA
checking again table(norway_happy$y)
mean_y <- mean(norway_happy$y, na.rm = TRUE) cat(“Mean of ‘y’ is:”, mean_y, “”)
Therefore, on average based on the ESS data, Norway reports higher levels of happiness compared to Belgium.
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.
Loading data into R
ireland_ccworried <- ireland_data %>% filter(cntry == “IE”) %>% select(alcbnge)
ireland_ccworried\(y <- ireland_ccworried\)alcbnge
table(ireland_ccworried$y)
Recode values 6 through 8 to NA ireland_ccworried\(y[ireland_ccworried\)y %in% 6:8] <- NA
Converting to categories to get mode as a category instead of a number df <- ireland_ccworried %>% mutate( y_category = case_when( y == 1 ~ “Daily”, y == 2 ~ “Weekly”, y == 3 ~ “Monthly”, y == 4 ~ “Rarely”, y == 5 ~ “Never”, TRUE ~ NA_character_ ), y_category = fct_relevel(factor(y_category), ### here you would put the categories in order you want them to appear or else it will appear alphabetically “Daily”, “Weekly”, “Monthly”, “Rarely”, “Never”) )
To confirm the conversion: table(df$y_category)
Let’s 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 = “,”), “”)
Therefore, for Irish respondants, the most common category chosen for binge drinking frequency is weekly.
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.
Loading data into R
portugal_ccworried <- portugal_data %>% filter(cntry == “PT”) %>% select(plnftr) summary(portugal_ccworried)
serbia_ccworried <- serbia_data %>% filter(cntry == “RS”) %>% select(plnftr) summary(serbia_ccworried)
Based on the means and medians produced for both Portugal and Serbia, I can conclude that people in Serbia tend to plan for the future while those in Portugal live in the present day.
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
Loading data into R
italy_data <- italy_data %>% 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
table(italy_data$gndr)
Compute average of lrscale by gender means_by_gender <- italy_data %>% group_by(gndr) = %>% summarize(lrscale = mean(lrscale, na.rm = TRUE))
print(means_by_gender)
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.
Loading data into R
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(title = “Boxplot of satisfaction with the state of education vs. health services”) + theme_minimal()
Results tell that the median for health system satisfaction is higher than the satisfaction for the state of education in France.