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"
#ess <- read_fst("All-ESS-Data.fst")
belgium_data <- read.fst("belgium_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")
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
mean_y <- mean(belgium_happy$y, na.rm = TRUE)
cat("Mean of 'y' is:", mean_y, "\n")
## Mean of 'y' is: 7.737334
# Now, let's determine the median of 'y'.
median_y <- median(belgium_happy$y, na.rm = TRUE)
cat("Median of 'y' is:", median_y, "\n")
## Median of 'y' is: 8
# Using tidyverse syntax principles, we can get both the mean and median at once.
belgium_happy %>%
  summarize(
    mean_y = mean(y, na.rm = TRUE),
    median_y = median(y, na.rm = TRUE)
  ) %>%
  print()
##     mean_y median_y
## 1 7.737334        8
mode_y <- belgium_happy %>%
  filter(!is.na(y)) %>%
  count(y) %>%
  arrange(desc(n)) %>%
  slice(1) %>%
  pull(y)

cat("\nMode of Y:", mode_y, "\n")
## 
## Mode of Y: 8
sd_y <- sd(belgium_happy$y, na.rm = TRUE)
cat("Standard Deviation of 'y':", sd_y, "\n")
## Standard Deviation of 'y': 1.52045
# Let's compare if had not cleaned properly
belgium_happy2 <- belgium_data %>%
  filter(cntry == "BE") %>%
  select(happy)

sd_happy2 <- sd(belgium_happy2$happy, na.rm = TRUE)
cat("Standard Deviation of 'happy not cleaned':", sd_happy2, "\n")
## Standard Deviation of 'happy not cleaned': 3.234511
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
estonia_ccworried <- belgium_data %>%
  filter(cntry == "EE") %>%
  select(wrclmch)

estonia_ccworried$y <- estonia_ccworried$wrclmch

table(estonia_ccworried$y)
## < table of extent 0 >
# Recode values 6 through 8 to NA
estonia_ccworried$y[estonia_ccworried$y %in% 6:8] <- NA
# Compute mean and median
mean_y <- mean(estonia_ccworried$y, na.rm = TRUE)
median_y <- median(estonia_ccworried$y, na.rm = TRUE)

cat("Mean of 'y':", mean_y, "\n")
## Mean of 'y': NaN
cat("Median of 'y':", median_y, "\n")
## Median of 'y': NA
# Converting to categories to get mode as a category instead of a number
df <- estonia_ccworried %>%
  mutate(
    y_category = case_when(
      y == 1 ~ "Not at all worried",
      y == 2 ~ "Not very worried",
      y == 3 ~ "Somewhat worried",
      y == 4 ~ "Very worried",
      y == 5 ~ "Extremely worried",
      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
                             "Not at all worried", 
                             "Not very worried", 
                             "Somewhat worried", 
                             "Very worried", 
                             "Extremely worried")
  )
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `y_category = fct_relevel(...)`.
## Caused by warning:
## ! 5 unknown levels in `f`: Not at all worried, Not very worried, Somewhat
## worried, Very worried, and Extremely worried
# To confirm the conversion:
table(df$y_category)
## < table of extent 0 >
# 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)
## Warning in max(tbl): no non-missing arguments to max; returning -Inf
cat("Mode of y category:", paste(mode_values, collapse = ", "), "\n")
## Mode of y category:
result <- france_data %>%
  # Step 1: Filter for the countries of interest
  filter(cntry %in% c("GB", "FR", "FI", "CZ", "IT")) %>%
  
  # Step 2: Recode 77, 88, 99 to NA for 'imueclt'
  mutate(imueclt = recode(imueclt, `77` = NA_real_, `88` = NA_real_, `99` = NA_real_)) %>%
  
  # Step 3: Compute mean by country
  group_by(cntry) %>%
  summarize(mean_imueclt = mean(imueclt, na.rm = TRUE))

print(result)
## # A tibble: 1 × 2
##   cntry mean_imueclt
##   <chr>        <dbl>
## 1 FR            5.33
france_data <- france_data %>% 
  filter(cntry == "FR")

# Convert gender and lrscale (representing pol ID self-placement from left to right)
france_data <- france_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
  )
# Compute mean for male
mean_male_lrscale <- france_data %>%
  filter(gndr == "Male") %>%
  summarize(mean_lrscale_men = mean(lrscale, na.rm = TRUE))

print(mean_male_lrscale)
##   mean_lrscale_men
## 1         4.929045
# Compute average of lrscale by gender
means_by_gender <- france_data %>%
  group_by(gndr) %>% # here you are "grouping by" your second variable
  summarize(lrscale = mean(lrscale, na.rm = TRUE)) # here you are summarizing your variable of interest

print(means_by_gender)
## # A tibble: 2 × 2
##   gndr   lrscale
##   <chr>    <dbl>
## 1 Female    4.87
## 2 Male      4.93
# Bar chart
ggplot(france_data %>% 
         mutate(clsprty = ifelse(clsprty %in% c(7, 8), NA, clsprty)),
       aes(x = clsprty)) + 
  geom_bar(na.rm = TRUE) +
  labs(title = "Bar chart for 'Feeling close to any Party' in France", 
       x = "X-axis",  # note: you would want to label these appropriately but showcasing what happens when you just put 'X-axis'
       y = "Y-axis") # as well as 'Y-axis'

# Histogram
ggplot(france_data, aes(x = stflife)) + 
  geom_histogram(binwidth = 1) +
  labs(title = "Satisfaction with Life in France (ESS, 2002-2020) ", 
       x = "Satisfaction with Life (0-10)", 
       y = "Count")

table(france_data$stflife)
## 
##    0    1    2    3    4    5    6    7    8    9   10   77   88 
##  542  264  669 1020 1082 2567 1899 3334 4359 1751 1518    9   24
## setting 77 and 88 to NA
ggplot(france_data %>% 
         mutate(stflife = ifelse(stflife %in% c(77, 88), NA, stflife)),
       aes(x = stflife)) + 
  geom_histogram(binwidth = 1) +
  labs(title = "Satisfaction with Life in France (ESS, 2002-2020) ", 
       x = "Satisfaction with Life (0-10)", 
       y = "Count")
## Warning: Removed 33 rows containing non-finite values (`stat_bin()`).

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 = "Y-axis", x = "X-axis", title = "Boxplot of stfedu vs. stfhlth") +
  theme_minimal()
## Warning: Removed 364 rows containing non-finite values (`stat_boxplot()`).

#all of these codes above are used to load data

Homework 1 (5%): due by next lecture on Jan. 23

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.

# Load necessary libraries
library(dplyr)
library(fst)

# Read the data for Norway and Belgium
norway_data <- read_fst("norway_data.fst")
belgium_data <- read_fst("belgium_data.fst")

# Calculate the average 'happy' for Norway
mean_happy_norway <- norway_data %>%
  summarize(mean_happy_norway = mean(happy, na.rm = TRUE))

# Calculate the average 'happy' for Belgium
mean_happy_belgium <- belgium_data %>%
  summarize(mean_happy_belgium = mean(happy, na.rm = TRUE))

# Print the results
print(mean_happy_norway)
##   mean_happy_norway
## 1          8.076377
print(mean_happy_belgium)
##   mean_happy_belgium
## 1           7.838519

#norway reports higher levels of happiness with 8.07 and belgium reports 7.83

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.

# Read the data from the FST file
ireland_data <- read_fst("ireland_data.fst")

# Filter the data for Ireland
ireland_alcbnge <- ireland_data %>%
  filter(cntry == "IE") %>%
  select(alcbnge)

# Determine the mode of the alcbnge category 
mode_alcbnge <- table(ireland_alcbnge$alcbnge) %>% 
  names() %>% 
  as.numeric() %>% 
  which.max() %>% 
  as.character()

cat("Most common category for frequency of binge drinking in Ireland:", mode_alcbnge, "\n")
## Most common category for frequency of binge drinking in Ireland: 8
# Code for Ireland
ireland_alcbnge <- ireland_data %>%
  filter(cntry == "IE") %>%
  select(alcbnge)

ireland_alcbnge$alcbnge_category <- case_when(    
  ireland_alcbnge$alcbnge == 0 ~ "Never", 
  ireland_alcbnge$alcbnge == 1 ~ "Less than monthly",
  ireland_alcbnge$alcbnge == 2 ~ "Monthly",
  ireland_alcbnge$alcbnge == 3 ~ "Weekly",
  ireland_alcbnge$alcbnge == 4 ~ "Daily or almost daily",
  TRUE ~ NA_character_
)

# To confirm conversion
table(ireland_alcbnge$alcbnge_category)
## 
## Daily or almost daily     Less than monthly               Monthly 
##                   417                    65                   650 
##                Weekly 
##                   346

#the most common category selected of binge drinking in Ireland is daily or almost daily with 417


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. 

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




```r
# Read data for Portugal
portugal_data <- read_fst("portugal_data.fst")

# Summary for Portugal
summary_portugal <- summary(portugal_data$plnftr)

# Read data for Serbia
serbia_data <- read_fst("serbia_data.fst")

# Summary for Serbia
summary_serbia <- summary(serbia_data$plnftr)

# Display the summaries
cat("Summary for Portugal:\n", summary_portugal, "\n\n")
## Summary for Portugal:
##  0 3 5 6.426305 8 88 14604
cat("Summary for Serbia:\n", summary_serbia, "\n")
## Summary for Serbia:
##  0 0 4 4.983358 8 88 1505

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

# Read the data for Italy
italy_data <- read_fst("italy_data.fst")

# Filter data for Italy
italy_data <- italy_data %>% 
  filter(cntry == "IT") %>%
  select(stfdem, gndr)

# Recode values 77 and 88 to NA for 'stfdem'
italy_data$stfdem[italy_data$stfdem %in% c(77, 88)] <- NA

# Convert gender to meaningful labels
italy_data$gndr <- ifelse(italy_data$gndr == 1, "Male", "Female")

# Calculate mean dissatisfaction by gender
mean_dissatisfaction <- italy_data %>%
  group_by(gndr) %>%
  summarize(mean_stfdem = mean(stfdem, na.rm = TRUE))

# Print the results
print(mean_dissatisfaction)
## # A tibble: 2 × 2
##   gndr   mean_stfdem
##   <chr>        <dbl>
## 1 Female        4.69
## 2 Male          4.78

#men are more dissastified with democracy in Italy with 4.78 and females with 4.69

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.

# Load necessary libraries
library(dplyr)
library(fst)
library(ggplot2)

# Load necessary libraries
library(dplyr)
library(fst)
library(ggplot2)
library(reshape2)
## 
## Attaching package: 'reshape2'
## The following object is masked from 'package:tidyr':
## 
##     smiths
# Read the data for France
france_data <- read_fst("france_data.fst")

# Changing the data for boxplot
france_data <- 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)) 

# Creating boxplot
france_data %>%
  select(stfedu, stfhlth) %>%
  melt(id.vars = NULL) %>%
  ggplot(aes(x = variable, y = value)) +
  geom_boxplot() +
  labs(y = "Satisfaction (0-10)", x = NULL, title = "Boxplot of satisfaction with the state of education vs. health services")
## Warning: Removed 364 rows containing non-finite values (`stat_boxplot()`).