rm(list=ls()); gc()
##           used (Mb) gc trigger  (Mb) limit (Mb) max used (Mb)
## Ncells 1078750 57.7    2195830 117.3         NA  1479859 79.1
## Vcells 1856782 14.2    8388608  64.0      16384  2385601 18.3
ess <- read_fst("All-ESS-Data.fst")
#belgium_data <- read.fst("belgium_data.fst")
#estonia_data <- read.fst("estonia_data.fst")
#france_data <- read.fst("france_data.fst")
#norway_data <- read.fst("norway_data.fst")
#ireland_data <- read.fst("ireland_data.fst")
#portugal_data <- read.fst("portugal_data.fst")
#serbia_data <- read.fst("serbia_data.fst")
#italy_data <- read.fst("italy_data.fst")
## Task 1
##Data for Belgium 
belgium_happy <- ess %>%
  filter(cntry == "BE") %>% 
  select(happy)
belgium_happy$y <- belgium_happy$happy
# Recode values 77 through 99 to NA
belgium_happy$y[belgium_happy$y %in% 77:99] <- NA

mean_y <- mean(belgium_happy$y, na.rm = TRUE)
cat("Mean of 'y' is:", mean_y, "\n")
## Mean of 'y' is: 7.737334
##Data for Norway 
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
# Recoding values 77 through 99 to NA
norway_happy$y[norway_happy$y %in% 77:99] <- NA
# Table
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
#Calculating the Mean for most happy people
mean_y <-mean(norway_happy$y, na.rm = TRUE)
cat ("Mean of 'y' is:" , mean_y, "\n")
## Mean of 'y' is: 7.975005
##Overall, Norway reports higher levels of happiness comapred to Belgium
## Task 2
ireland_alcbnge <- ess %>%
  filter(cntry == "IE") %>%
  select(alcbnge)
#Mode and table for Irelands data on binge drinking
ireland_alcbnge$alcbnge_category <- case_when(
      ireland_alcbnge == 1 ~ "Never",
      ireland_alcbnge == 2 ~ "Less than monthly",
      ireland_alcbnge == 3 ~ "Monthly",
      ireland_alcbnge == 4 ~ "Weekly",
      ireland_alcbnge == 5 ~ "Daily or almost daily",
      TRUE ~ NA_character_
  )
table(ireland_alcbnge$alcbnge_category)
## 
## Daily or almost daily     Less than monthly               Monthly 
##                   239                   650                   346 
##                 Never                Weekly 
##                    65                   417
#The most common category selected for binge drinking in Ireland is Somewhat worried
## Task 3
# Code Portugal
portugal_plnftr <- ess %>%
  filter(cntry == "PT") %>%
  select(plnftr)

# Code Serbia
serbia_plnftr <- ess %>%
  filter(cntry == "RS") %>%
  select(plnftr)

# Summary Portugal
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
# Summary Serbia
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
# When looking at the data, Portugal has the higher mean for individuals who take things day by day, with 27% according to the ESS website.  Serbia has a mean of 33.1% of people who plan for the future which is more poeple who plan for the future compared to Portugal. The output for Portugal shows a higher mean, median as well as a higher 1st quintile comapred to the smaller mean,median,a nd 1st quintile that Serbia contains regarding people who do and do not take things day by day.  The higher numbers in Portugal show more citizens who take things day by day compared to the low number of people who do this in Serbia.
## Task 4
# Italy Code
italy_stfdem <- ess %>%
  filter(cntry == "IT") %>%
  select(stfdem, gndr)

# Recode the values of 77, 88, 99 to NA for 'stfdem'
italy_stfdem$stfdem[italy_stfdem$stfdem %in% c(77, 88, 99)] <- NA

# Presenting gender theough the groupby function 
mean_dissatisfaction <- italy_stfdem %>%
  group_by(gndr) %>%
  summarize(mean_stfdem = mean(stfdem, na.rm = TRUE))

print(mean_dissatisfaction)
## # A tibble: 3 × 2
##    gndr mean_stfdem
##   <dbl>       <dbl>
## 1     1        4.78
## 2     2        4.66
## 3     9        3.25
## The ESS site shows 1 representing males in Italy and 2 representing females in Italy.  This proves that the males have a higher mean of being dissatisfied with democracy compared to the females in Italy.
## Task 5
france_data <- ess %>% 
  filter(cntry == "FR")


ggplot(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)),
       aes(x = variable, y = value)) +
  geom_boxplot() +
  labs(title = "Boxplot of satisfaction with the state of education vs. health services",
       x = "",  # Remove x-axis label
       y = "Satisfaction (0-10)")  # Change y-axis label
## Warning: Removed 364 rows containing non-finite values (`stat_boxplot()`).

##I would say that, according to the data in the boxplot, the median French person is more satisfied with stfhlth data compared to the stfedu because the stfhlth data is higher up and consists of higher data.