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)
## ── 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
## [[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")
rm(list=ls()); gc() 
##           used (Mb) gc trigger   (Mb) limit (Mb)   max used   (Mb)
## Ncells 1098003 58.7    2196105  117.3         NA    1502405   80.3
## Vcells 1888757 14.5 1072251184 8180.7      16384 1304873991 9955.4
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)
## [[1]]
##  [1] "fstcore"      "modelsummary" "fst"          "lubridate"    "forcats"     
##  [6] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [11] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [16] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[2]]
##  [1] "fstcore"      "modelsummary" "fst"          "lubridate"    "forcats"     
##  [6] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [11] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [16] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[3]]
##  [1] "fstcore"      "modelsummary" "fst"          "lubridate"    "forcats"     
##  [6] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [11] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [16] "grDevices"    "utils"        "datasets"     "methods"      "base"
ess <- read_fst("All-ESS-Data.fst")

Task 1: 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?

belgium_happy <- ess %>% 
  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
mean_y <- mean(belgium_happy$y, na.rm = TRUE)
cat("Mean of 'y' is:", mean_y, "\n")
## Mean of 'y' is: 7.838519
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
mean_y <- mean(Norway_happy$y, na.rm = TRUE)
cat("Mean of 'y' is:", mean_y, "\n")
## Mean of 'y' is: 8.076377

#mean of belgium happy is 7.84. and mean of norway happy is 8.08, therefore Norway reports a higher level of happiness.

ireland_data <- read.fst("ireland_data.fst")

Task 2: what is the most common category selected, for Irish respondents, for frequency of binge drinking? The variable of interest is: alcbnge.

ireland_ccdrinking <- ess %>%
  filter(cntry == "IL") %>%
  select(alcbnge)
ireland_ccdrinking$y <- ireland_ccdrinking$alcbnge

table(ireland_ccdrinking$y)
## 
##    1    2    3    4    5    7    8    9 
##   15  153  149  264  362    5   69 1545
ireland_ccdrinking$y[ireland_ccdrinking$y %in% 6:8] <- NA

table(ireland_ccdrinking$y)
## 
##    1    2    3    4    5    9 
##   15  153  149  264  362 1545
mean_y <- mean(ireland_ccdrinking$y, na.rm = TRUE)
median_y <- median(ireland_ccdrinking$y, na.rm = TRUE)

cat("Mean of 'y':", mean_y, "\n")
## Mean of 'y': 7.049437
cat("Median of 'y':", median_y, "\n")
## Median of 'y': 9
df <- ireland_ccdrinking %>%
  mutate(
    y_category = case_when(
      y == 1 ~ "Don't Binge Drink",
      y == 2 ~ "Rarely Binge Drink",
      y == 3 ~ "Somewhat Binge Drink",
      y == 4 ~ "Frequently Binge Drink",
      y == 5 ~ "Extreme Binge Drink",
      TRUE ~ NA_character_  ),
    y_category = fct_relevel(factor(y_category),  
                             
                             "Don't Binge Drink", 
                             "Rarely Binge Drink", 
                             "Somewhat Binge Drink", 
                             "Frequently Binge Drink", 
                             "Extreme Binge Drink")
  )
table(df$y_category)
## 
##      Don't Binge Drink     Rarely Binge Drink   Somewhat Binge Drink 
##                     15                    153                    149 
## Frequently Binge Drink    Extreme Binge Drink 
##                    264                    362
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: Extreme Binge Drink

#The most common stratergy for irish respondents is “extreme binge drink”

task 3: 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", "SE")) %>%
  

  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 SE           5.02

#as Portuals mean is higher than serbia, they tend to take everyday as it comes more, in comparison to serbia.

Task 4: 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.

italy_data <- ess %>% 
  filter(cntry == "IT")

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
  )
mean_male_lrscale <- italy_data %>%
  filter(gndr == "Male") %>%
  summarize(mean_lrscale_men = mean(lrscale, na.rm = TRUE))

print(mean_male_lrscale)
##   mean_lrscale_men
## 1         5.202087
mean_female_lrscale <- italy_data %>%
  filter(gndr == "Female") %>%
  summarize(mean_lrscale_female = mean(lrscale, na.rm = TRUE))

print(mean_female_lrscale)
##   mean_lrscale_female
## 1            5.086377
means_by_gender <- italy_data %>%
  group_by(gndr) %>% 
  summarize(lrscale = mean(lrscale, na.rm = TRUE)) 

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

so therefore, Men are more dissatisfied with democracy in Italy, in comparison to women.

Task 5: 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", title = "Boxplot of satisfaction with the state of education vs. health services") +
  theme_minimal()
## Warning: Removed 364 rows containing non-finite values (`stat_boxplot()`).

# from the boxplot it looks like french citizens are more satisfied wiht the health system in comparison to the education system.