Rummaan Chaudhry SOC222

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)
## Warning: package 'readr' was built under R version 4.2.3
## Warning: package 'dplyr' was built under R version 4.2.3
## Warning: package 'stringr' was built under R version 4.2.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ 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")
## Warning: package 'fstcore' was built under R version 4.2.3

Task 1

Interpretation:

unique(ess$cntry)
##  [1] "AT" "BE" "CH" "CZ" "DE" "DK" "ES" "FI" "FR" "GB" "GR" "HU" "IE" "IL" "IT"
## [16] "LU" "NL" "NO" "PL" "PT" "SE" "SI" "EE" "IS" "SK" "TR" "UA" "BG" "CY" "RU"
## [31] "HR" "LV" "RO" "LT" "AL" "XK" "ME" "RS" "MK"
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
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

Next step calculatng Mean

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 is for the country happy is 7.98

Next Step: Calculating the mean for the variable happy for the country of 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
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

Next step calculating the mean of the variable y

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

Asnwer for Task 1

On average Norway reports higher levels of happiness when comapred to Belgium

Task 2

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

ireland_alcbnge <- ess %>%
  filter(cntry == "IL") %>%
  select(alcbnge)

ireland_alcbnge$y <- ireland_alcbnge$alcbnge
table(ireland_alcbnge$y)
## 
##    1    2    3    4    5    7    8    9 
##   15  153  149  264  362    5   69 1545
ireland_alcbnge$y[ireland_alcbnge$y %in% 6:9] <- NA
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.853659
cat("Median of 'y':", median_y, "\n")
## Median of 'y': 4
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),  ### Placing thr categories in the order i would like them
                             "Daily or almost daily", 
                             "Weekly", 
                             "Monthly", 
                             "Less than monthly", 
                             "Never")
  )

# To confirm the conversion:
table(df$y_category)
## 
## Daily or almost daily                Weekly               Monthly 
##                    15                   153                   149 
##     Less than monthly                 Never 
##                   264                   362

Calculating the Mode

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: Never

Answer for Task 2:

The most common category selected for Irish respondants, for frequency of binge drinking was “Never”.

##Task 3

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_plnftr <- ess %>% 
  filter(cntry == "PT") %>% 
  select(plnftr)
portugal_plnftr$y <- portugal_plnftr$plnftr

table(portugal_plnftr$y)
## 
##   0   1   2   3   4   5   6   7   8   9  10  88 
## 114 184 313 356 264 481 262 382 345 166 370  40
portugal_plnftr$y[portugal_plnftr$y %in% 88] <- NA

table(portugal_plnftr$y)
## 
##   0   1   2   3   4   5   6   7   8   9  10 
## 114 184 313 356 264 481 262 382 345 166 370
summary(portugal_plnftr)
##      plnftr             y         
##  Min.   : 0.000   Min.   : 0.000  
##  1st Qu.: 3.000   1st Qu.: 3.000  
##  Median : 5.000   Median : 5.000  
##  Mean   : 6.426   Mean   : 5.418  
##  3rd Qu.: 8.000   3rd Qu.: 8.000  
##  Max.   :88.000   Max.   :10.000  
##  NA's   :14604    NA's   :14644
serbia_plnftr <- ess %>% 
  filter(cntry == "RS") %>% 
  select(plnftr)
serbia_plnftr$y <- serbia_plnftr$plnftr

table(serbia_plnftr$y)
## 
##   0   1   2   3   4   5   6   7   8   9  10  77  88 
## 587 133 152 138  95 246  70  87 103  47 364   4  17
serbia_plnftr$y[serbia_plnftr$y %in% 77:88] <- NA

table(serbia_plnftr$y)
## 
##   0   1   2   3   4   5   6   7   8   9  10 
## 587 133 152 138  95 246  70  87 103  47 364
summary(serbia_plnftr)
##      plnftr             y         
##  Min.   : 0.000   Min.   : 0.000  
##  1st Qu.: 0.000   1st Qu.: 0.000  
##  Median : 4.000   Median : 4.000  
##  Mean   : 4.983   Mean   : 4.143  
##  3rd Qu.: 8.000   3rd Qu.: 8.000  
##  Max.   :88.000   Max.   :10.000  
##  NA's   :1505     NA's   :1526

Answer for Task 3:

The mean for the respondants from Portugal for the variable plnftr is 5.418 while similarly the mean for Serbia’s respondants is 4.143. This showcases that while Serbians liek to plan their future, Portugese are more likely to take each day as it comes.

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

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

italy_data <- italy_data %>%
  mutate(
    gndr = case_when(
      gndr == 1 ~ "Male",
      gndr == 2 ~ "Female",
      TRUE ~ as.character(gndr)
    ),
    stfdem = ifelse(stfdem %in% c(77, 88), NA, stfdem) 
  )
means_by_gender <- italy_data %>%
  group_by(gndr) %>%
  summarize(stfdem = mean(stfdem, na.rm = TRUE))
print(means_by_gender)
## # A tibble: 3 × 2
##   gndr   stfdem
##   <chr>   <dbl>
## 1 9        3.25
## 2 Female   4.69
## 3 Male     4.78

Aswer to Task 4: With the average for males being 4.78 and the average for females being 4.69, it is clear that males are more content with democracy.

##Task 5:

france_data <- read.fst(“france_data.fst”)

for this code chunk i kept getting the following error and was thus not able to run it:

Error in read_fst(path, columns, from, to, as.data.table, old_format) : Error opening fst file for reading, please check access rights and file availability

If my code had run my next code chunk would read like this:

rance_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 (0-10)”, x = ““, title =”Boxplot of satisfaction with the state of education vs. health services”) + theme_minimal()

Answer to Task 5:

For health services from the respondents from the country of france the median satisfaction is around 7 while the median for the variable of education is 5. This illustrates that there is more staisfaction with health services in comparison to education servcies.