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.

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(fst)
setwd("/Users/kaylapatricia/Desktop/soc222/homework 1")
norway_data <- read.fst("norway_data.fst")
norway_happy <- norway_data %>%
  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
norway_happy$y[norway_happy$y %in% 77:88] <- NA
table(norway_happy$y)
## 
##    0    1    2    3    4    5    6    7    8    9   10 
##   15   29   59  163  238  730  817 2617 5235 3796 2344
mean_y <- mean(norway_happy$y, na.rm = TRUE)
cat("Mean of 'y' is:", mean_y, "\n")
## Mean of 'y' is: 7.975005

The average for the variable ‘happy’ for the country of Norway is 7.975005

belgium_data <- read.fst("belgium_data.fst")
belgium_happy <- 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
belgium_happy$y[belgium_happy$y %in% 77:99] <- NA
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

The average for the variable ‘happy’ in the country of Belgium is 7.737334 On average, Norway reports higher levels of happiness.

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.

setwd("/Users/kaylapatricia/Desktop/soc222/homework 1")
ireland_data <- read.fst("ireland_data.fst")
ireland_alcbnge <- ireland_data %>%
  filter(cntry == "IE") %>%
  select(alcbnge)
ireland_alcbnge$y <- ireland_alcbnge$alcbnge
table(ireland_alcbnge$y)
## 
##   1   2   3   4   5   6   7   8 
##  65 650 346 417 239 641  26   6
ireland_alcbnge$y[ireland_alcbnge$y %in% 7:8] <- NA
table(ireland_alcbnge$y)
## 
##   1   2   3   4   5   6 
##  65 650 346 417 239 641
mean_y <- mean(ireland_alcbnge$y, na.rm = TRUE)
median_y <- median(ireland_alcbnge$y, na.rm = TRUE)

cat("Mean of 'y':", mean_y, "\n")
## Mean of 'y': 3.864292
cat("Median of 'y':", median_y, "\n")
## Median of 'y': 4
library(forcats)
df <- ireland_alcbnge %>%
   mutate(
    y_category = case_when(
      y == 1 ~ "Daily or almost daily",
      y == 2 ~ "Weekly",
      y == 3 ~ "Monthly",
      y == 4 ~ "Less than monthly",
      y == 5 ~ "Never",
       TRUE ~ NA_character_
    ),
    y_category = fct_relevel(factor(y_category),
                             "Daily or almost daily",
                             "Weekly",
                             "Monthly",
                             "Less than monthly",
                             "Never")
   )
table(df$y_category)
## 
## Daily or almost daily                Weekly               Monthly 
##                    65                   650                   346 
##     Less than monthly                 Never 
##                   417                   239
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: Weekly

The most common category selected for Irish respondents for frequency of binge drinking is ‘weekly’.

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.

portugal_data <- read.fst("portugal_data.fst")
portugal_plnftr <- portugal_data %>%
  filter(cntry == "PT") %>% 
  select(plnftr)
summary(portugal_plnftr)
##      plnftr      
##  Min.   : 0.000  
##  1st Qu.: 3.000  
##  Median : 5.000  
##  Mean   : 6.426  
##  3rd Qu.: 8.000  
##  Max.   :88.000  
##  NA's   :14604
serbia_data <- read.fst("serbia_data.fst")
serbia_plnftr <- serbia_data %>%
  filter(cntry == "RS") %>% 
  select(plnftr)
summary(serbia_plnftr)
##      plnftr      
##  Min.   : 0.000  
##  1st Qu.: 0.000  
##  Median : 4.000  
##  Mean   : 4.983  
##  3rd Qu.: 8.000  
##  Max.   :88.000  
##  NA's   :1505

In comparing the two countries, Portugal has a higher median indicating participants plan for the future more than participants in Serbia.

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

setwd("/Users/kaylapatricia/Desktop/soc222/homework 1")
italy_data <- read.fst("italy_data.fst")
italy_stfdem <- italy_data %>%
  filter(cntry == "IT") %>% 
  select(stfdem)
italy_stfdem$y <- italy_stfdem$stfdem
table(italy_stfdem$y)
## 
##    0    1    2    3    4    5    6    7    8    9   10   77   88   99 
##  712  365  733  962 1221 1785 1724 1300  733  138  102   57  344    2
italy_stfdem$y[italy_stfdem$y %in% 77:99] <- NA
table(italy_stfdem$y)
## 
##    0    1    2    3    4    5    6    7    8    9   10 
##  712  365  733  962 1221 1785 1724 1300  733  138  102
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) 
  )
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
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

In Italy, men are more satisfied with democracy than women.

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.

library(ggplot2)
library(tidyr)
setwd("/Users/kaylapatricia/Desktop/soc222/homework 1")
france_data <- read.fst("france_data.fst")
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 = "Satisfaction (0-10)", x = " ", title = "Boxplot of satisfaction with the state of education vs. health services") +
  theme_minimal()
## Warning: Removed 364 rows containing non-finite values (`stat_boxplot()`).

The median french person is more satisfied with health services, represented by the higher median in the stfhlth boxplot.