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

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
serbia_health$y[serbia_health$y %in% 77:99] <- NA

# checking again
table(serbia_health$y)
## 
##    1    2    3    4    5    8    9 
##  620 1051 1332  411  107    3   24

Serbia’s average level of subjective health

mean_y <- mean(serbia_health$y, na.rm = TRUE)
cat("Mean of 'y' is:", mean_y, "\n")
## Mean of 'y' is: 2.575254

Belgium

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
belgium_health$y[belgium_health$y %in% 77:99] <- NA

# checking again
table(belgium_health$y)
## 
##    1    2    3    4    5    7    8    9 
## 4033 9262 3338  674  137    2    3    2

Belgium’s average level of subjective health

mean_y <- mean(belgium_health$y, na.rm = TRUE)
cat("Mean of 'y' is:", mean_y, "\n")
## Mean of 'y' is: 2.063377

On average, based on the ESS data, Serbia reports higher levels of subjective health than Belgium. Serbia’s mean level of subjective health is 2.575254 and Belgium’s mean level of subjective health is 2.063377.

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

norway_sclmeet <- ess %>%
  filter(cntry == "NO") %>%
  select(sclmeet)

norway_sclmeet$y <- norway_sclmeet$sclmeet

table(norway_sclmeet$y)
## 
##    1    2    3    4    5    6    7   77   88 
##   44  356  746 2569 2119 5764 4458    2    7
# Recode values 77 through 99 to NA
norway_sclmeet$y[norway_sclmeet$y %in% 77:99] <- NA
 # Converting to categories to get mode as a category instead of a number
df <- norway_sclmeet %>%
  mutate(
    y_category = case_when(
      y == 1 ~ "Never",
      y == 2 ~ "Less than once a month",
      y == 3 ~ "Once a month",
      y == 4 ~ "Several times a month",
      y == 5 ~ "Once a week",
      y == 6 ~ "Several times a week",
      y == 7 ~ "Every day",
      TRUE ~ NA_character_
    ),
    y_category = fct_relevel(factor(y_category),
                             "Never", 
                             "Less than once a month", 
                             "Once a month", 
                             "Several times a month", 
                             "Once a week",
                             "Several times a week",
                             "Every day")
  )

# To confirm the conversion:
table(df$y_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

The most common category selected, for Norwegian respondents, for how often they socially meet with friends or relatives is several times a week at 5764.

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

estonia_wrclmch <- ess %>% 
  filter(cntry == "EE") %>% 
  select(wrclmch)
estonia_wrclmch$y <- estonia_wrclmch$wrclmch

table(estonia_wrclmch$y)
## 
##    1    2    3    4    5    6    7    8 
##  343  852 1612  565  125   56    1    7
estonia_wrclmch$y[estonia_wrclmch$y %in% 7:9] <- NA

# checking again
table(estonia_wrclmch$y)
## 
##    1    2    3    4    5    6 
##  343  852 1612  565  125   56
summary(estonia_wrclmch)
##     wrclmch            y        
##  Min.   :1.000   Min.   :1.000  
##  1st Qu.:2.000   1st Qu.:2.000  
##  Median :3.000   Median :3.000  
##  Mean   :2.855   Mean   :2.844  
##  3rd Qu.:3.000   3rd Qu.:3.000  
##  Max.   :8.000   Max.   :6.000  
##  NA's   :13295   NA's   :13303

Portugal

portugal_wrclmch <- ess %>% 
  filter(cntry == "PT") %>% 
  select(wrclmch)
portugal_wrclmch$y <- portugal_wrclmch$wrclmch

table(portugal_wrclmch$y)
## 
##    1    2    3    4    5    6    7    8 
##   55  251 1171 1203  380   13    4   31
portugal_wrclmch$y[portugal_wrclmch$y %in% 7:9] <- NA

# checking again
table(portugal_wrclmch$y)
## 
##    1    2    3    4    5    6 
##   55  251 1171 1203  380   13
summary(portugal_wrclmch)
##     wrclmch            y        
##  Min.   :1.000   Min.   :1.000  
##  1st Qu.:3.000   1st Qu.:3.000  
##  Median :4.000   Median :4.000  
##  Mean   :3.583   Mean   :3.534  
##  3rd Qu.:4.000   3rd Qu.:4.000  
##  Max.   :8.000   Max.   :6.000  
##  NA's   :14773   NA's   :14808

The 1st quartile for Estonia is 2, while the 1st quartile for Portugal is 3. Where, 2 is “Not very worried” and 3 is “Somewhat worried”. There is a significant amount of people from Estonia that answer “Not very worried”, whereas that value is slightly more frequent for people from Portugal that answer “Somewhat worried”.

The average for Estonia (mean: 2.855) is higher than for Portugal (mean: 3.583). This indicates that people from Portugal are more worried about climate change than those from Serbia.

Task 4

Provide code and answer.

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

Italy

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

# Convert gender and stfhlth
italy_data <- italy_data %>%
  mutate(
    gndr = case_when(
      gndr == 1 ~ "Male",
      gndr == 2 ~ "Female",
      TRUE ~ as.character(gndr)
    ),
    stfhlth = ifelse(stfhlth %in% c(77, 99), NA, stfhlth)  # Convert stfhlth values
  )
# Compute average of stfhlth by gender
means_by_gender <- italy_data %>%
  group_by(gndr) %>% 
  summarize(stfhlth = mean(stfhlth, na.rm = TRUE)) 

italy_data$gndr[italy_data$gndr %in% 9] <- NA

print(means_by_gender)
## # A tibble: 3 × 2
##   gndr   stfhlth
##   <chr>    <dbl>
## 1 9        10.5 
## 2 Female    6.15
## 3 Male      6.48

Using the variables stfhlth and gndr, on average, men are more satisfied with health services than women in Italy - although the difference is very slight. The mean satisfaction with health services for men is 6.478153, while the mean satisfaction with health services for women is 6.147297.

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

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 democracy vs. 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 the ESS data, the median French person is more satisfied with the democracy over the national government. The boxplots depict that the median satisfaction with the democracy is higher than the median satisfaction with the national government (median of 5 vs. 4).