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

Task 1

Provide code and answer.

Prompt and question: calculate the average for the variable ‘health’ for the countries of Serbia and Belgium. On average, based on the ESS data, who reports higher levels of subjective health: Serbia and Belgium?

serbia_health <- ess %>%
  filter(cntry == "RS") %>% 
  select(health)
serbia_health$y <- serbia_health$health

table(serbia_health$y)
## 
##    1    2    3    4    5    8    9 
##  620 1051 1332  411  107    3   24
mean_serbia <- mean(serbia_health$y, na.rm = TRUE)
cat("Mean of 'y' is:", mean_serbia, "\n")
## Mean of 'y' is: 2.575254
belgium_health <- ess %>%
  filter(cntry == "BE") %>% 
  select(health)
belgium_health$y <- belgium_health$health

table(belgium_health$y)
## 
##    1    2    3    4    5    7    8    9 
## 4033 9262 3338  674  137    2    3    2
mean_belgium <- mean(belgium_health$y, na.rm = TRUE)
cat("Mean of 'y' is:", mean_belgium, "\n")
## Mean of 'y' is: 2.063377

Based on the ESS data, Serbia reports higher levels of subjective health, with an average score of 2.58, whereas Belgium’s average is 2.07.

Task 2

Provide code and answer.

Prompt and question: what is the most common category selected, for Norwegian respondents, for how often they socially meet with friends or relatives. The variable of interest is: sclmeet.

norway_social_meet <- ess %>%
  filter(cntry == "NO") %>%
  select(sclmeet)
table(norway_social_meet$sclmeet)
## 
##    1    2    3    4    5    6    7   77   88 
##   44  356  746 2569 2119 5764 4458    2    7
norway_social_meet$sclmeet[norway_social_meet$sclmeet %in% 77:88] <- NA
table(norway_social_meet$sclmeet)
## 
##    1    2    3    4    5    6    7 
##   44  356  746 2569 2119 5764 4458
norway_social_meet$sclmeet_category <- factor(norway_social_meet$sclmeet, labels = c("Never", "Less than once a month", "Once a month", "Several times a month", "Once a week", "Several times a week", "Every day"))

table(norway_social_meet$sclmeet_category)
## 
##                  Never Less than once a month           Once a month 
##                     44                    356                    746 
##  Several times a month            Once a week   Several times a week 
##                   2569                   2119                   5764 
##              Every day 
##                   4458
get_mode <- function(v) {
  tbl <- table(v)
  mode_vals <- as.character(names(tbl)[tbl == max(tbl)])
  return(mode_vals)
}

mode_norway_social_meet <- get_mode(norway_social_meet$sclmeet_category)
mode_norway_social_meet
## [1] "Several times a week"

The most common category selected by Norwegian respondents in terms of how often they socially meet with friends or relatives is the “Several times a week” option, with 5764 responses.

Task 3

Provide code and answer.

Prompt and question: when you use the summary() function for the variable wrclmch (i.e., how worried about climate change) for both the countries of Estonia and Portugal – 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.

estonia_wrclmch <- ess %>%
  filter(cntry == "EE") %>%
  select(wrclmch)
table(estonia_wrclmch$wrclmch)
## 
##    1    2    3    4    5    6    7    8 
##  343  852 1612  565  125   56    1    7
estonia_wrclmch$wrclmch[estonia_wrclmch$wrclmch %in% 6:8] <- NA
table(estonia_wrclmch$wrclmch)
## 
##    1    2    3    4    5 
##  343  852 1612  565  125
summary(estonia_wrclmch)
##     wrclmch     
##  Min.   :1.000  
##  1st Qu.:2.000  
##  Median :3.000  
##  Mean   :2.793  
##  3rd Qu.:3.000  
##  Max.   :5.000  
##  NA's   :13359
portugal_wrclmch <- ess %>%
  filter(cntry == "PT") %>%
  select(wrclmch)
table(portugal_wrclmch$wrclmch)
## 
##    1    2    3    4    5    6    7    8 
##   55  251 1171 1203  380   13    4   31
portugal_wrclmch$wrclmch[portugal_wrclmch$wrclmch %in% 6:8] <- NA
table(portugal_wrclmch$wrclmch)
## 
##    1    2    3    4    5 
##   55  251 1171 1203  380
summary(portugal_wrclmch)
##     wrclmch     
##  Min.   :1.000  
##  1st Qu.:3.000  
##  Median :4.000  
##  Mean   :3.524  
##  3rd Qu.:4.000  
##  Max.   :5.000  
##  NA's   :14821

When comparing the two countries, Portugal tends to have higher average values for the variable “wrclmch,” suggesting that its respondents are more worried about climate change than Estonians. Portugal’s mean of 3.52 suggests the most frequently selected responses were likely 3, “Somewhat worried,” and 4, “Very worried.” In comparison, Estonia’s mean of 2.79 suggests respondents most likely chose 2, “Not very worried,” and 3, “Somewhat worried.” Portugal’s values for the 1st quartile (3.00) and median (4.00) are higher than Estonia’s values of 2.00 and 3.00, respectively. In addition, Portugal has a larger number of missing values at 14821, whereas Estonia has 13359, potentially influencing the overall interpretation of the results.

Task 4

Provide code and answer.

Prompt and question: using the variables stfhlth and gndr, answer the following: on average, who is more sastified with health services in Italy, men or women? Explain while referring to the output generated.

italy_data <- ess %>%
  filter(cntry == "IT") %>%
  select(stfhlth, gndr)
table(italy_data$gndr)
## 
##    1    2    9 
## 4836 5329   13
italy_data$gndr[italy_data$gndr %in% 9] <- NA
table(italy_data$stfhlth)
## 
##    0    1    2    3    4    5    6    7    8    9   10   77   88   99 
##  455  279  553  778  958 1470 1718 1869 1363  411  192   24  105    3
italy_data$stfhlth[italy_data$stfhlth %in% 77:99] <- NA
italy_data <- italy_data %>%
  mutate(
    gndr = case_when(
      gndr == 1 ~ "Male",
      gndr == 2 ~ "Female",
      TRUE ~ as.character(gndr)
    )
  )
italy_data <- italy_data %>% filter(!is.na(gndr))
italy_data <- italy_data %>% filter(!is.na(stfhlth))
table(italy_data$gndr)
## 
## Female   Male 
##   5260   4774
table(italy_data$stfhlth)
## 
##    0    1    2    3    4    5    6    7    8    9   10 
##  455  278  552  775  957 1466 1716 1869 1363  411  192
italy_data %>%
  group_by(gndr) %>%
  summarize(meanstfhlth = mean(stfhlth, na.rm = TRUE))
## # A tibble: 2 × 2
##   gndr   meanstfhlth
##   <chr>        <dbl>
## 1 Female        5.38
## 2 Male          5.54

On average, men are more satisfied with health services in Italy, with a mean score of 5.54, while women have a slightly lower mean score of 5.38.

Task 5

Create boxplots, in the same graph, for stfdem and stfgov for the country of France. Provide an appropriate title and labels. Further, tell us: according to ESS data, would we say that the median French person is more satisfied with the democracy or the national government? Explain.

france_data <- ess %>% 
  filter(cntry == "FR")
france_data %>%
  # Setting values to NA
  mutate(stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem),
         stfgov = ifelse(stfgov %in% c(77, 88, 99), NA, stfgov)) %>%
  # Reshaping the data
  select(stfdem, stfgov) %>%
  gather(variable, value, c(stfdem, stfgov)) %>%
  # Creating the boxplot
  ggplot(aes(x = variable, y = value)) +
  geom_boxplot() +
  labs(y = "Satisfaction (0-10)", x = "", title = "Boxplot of satisfaction with the way democracy works vs. the national government") +
  theme_minimal()
## Warning: Removed 557 rows containing non-finite values (`stat_boxplot()`).

median(france_data$stfdem)
## [1] 5
median(france_data$stfgov)
## [1] 4

According to ESS data, the median French person is more satisfied with the way democracy works than the national government. This can be seen on the boxplot graph, where the median is represented by the line inside both boxes. The median for the way democracy works is 5.0, whereas for the national government, it is 4.0.