# List of packages
packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer", 
              "fst", "viridis", "knitr", "kableExtra", "rmarkdown", "ggridges", "viridis", "questionr") # 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.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ 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
## Loading required package: viridisLite
## 
## 
## Attaching package: 'kableExtra'
## 
## 
## The following object is masked from 'package:dplyr':
## 
##     group_rows
## [[1]]
##  [1] "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"     "readr"    
##  [7] "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"     "graphics" 
## [13] "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[2]]
##  [1] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
##  [6] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [11] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [16] "datasets"     "methods"      "base"        
## 
## [[3]]
##  [1] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
##  [6] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [11] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [16] "datasets"     "methods"      "base"        
## 
## [[4]]
##  [1] "RColorBrewer" "modelsummary" "lubridate"    "forcats"      "stringr"     
##  [6] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [11] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [16] "utils"        "datasets"     "methods"      "base"        
## 
## [[5]]
##  [1] "fst"          "RColorBrewer" "modelsummary" "lubridate"    "forcats"     
##  [6] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [11] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [16] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[6]]
##  [1] "viridis"      "viridisLite"  "fst"          "RColorBrewer" "modelsummary"
##  [6] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [11] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [16] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [21] "methods"      "base"        
## 
## [[7]]
##  [1] "knitr"        "viridis"      "viridisLite"  "fst"          "RColorBrewer"
##  [6] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
## [11] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [16] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [21] "datasets"     "methods"      "base"        
## 
## [[8]]
##  [1] "kableExtra"   "knitr"        "viridis"      "viridisLite"  "fst"         
##  [6] "RColorBrewer" "modelsummary" "lubridate"    "forcats"      "stringr"     
## [11] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [16] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [21] "utils"        "datasets"     "methods"      "base"        
## 
## [[9]]
##  [1] "rmarkdown"    "kableExtra"   "knitr"        "viridis"      "viridisLite" 
##  [6] "fst"          "RColorBrewer" "modelsummary" "lubridate"    "forcats"     
## [11] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [16] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [21] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[10]]
##  [1] "ggridges"     "rmarkdown"    "kableExtra"   "knitr"        "viridis"     
##  [6] "viridisLite"  "fst"          "RColorBrewer" "modelsummary" "lubridate"   
## [11] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [16] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [21] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [26] "base"        
## 
## [[11]]
##  [1] "ggridges"     "rmarkdown"    "kableExtra"   "knitr"        "viridis"     
##  [6] "viridisLite"  "fst"          "RColorBrewer" "modelsummary" "lubridate"   
## [11] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [16] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [21] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [26] "base"        
## 
## [[12]]
##  [1] "questionr"    "ggridges"     "rmarkdown"    "kableExtra"   "knitr"       
##  [6] "viridis"      "viridisLite"  "fst"          "RColorBrewer" "modelsummary"
## [11] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [16] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [21] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [26] "methods"      "base"
ess <- read_fst("All-ESS-Data.fst")
library(fst) 

##Mission 1 and 2

# Creating a table for variables of interest
table(ess$happy)
## 
##      0      1      2      3      4      5      6      7      8      9     10 
##   3933   3540   7343  13759  17016  52117  43707  87869 130802  76229  51006 
##     77     88     99 
##    330   2163    741
table(ess$hinctnta)
## 
##     1     2     3     4     5     6     7     8     9    10    77    88    99 
## 27483 31409 31414 31134 30205 28657 28297 26624 22420 22099 41851 23623  2987
table(ess$eduyrs)
## 
##     0     1     2     3     4     5     6     7     8     9    10    11  11.5 
##  3074   822  1376  3072  9433  5913 10136 10161 29136 28228 32029 49334     4 
##    12    13    14  14.5    15    16    17    18  18.5    19    20    21    22 
## 80272 45535 33862     1 34024 32604 24410 19866     1  9734  9369  3192  2625 
##    23    24    25    26    27    28    29    30    31    32    33    34    35 
##  1510  1022  1004   308   191   142    80   269    28    32    23    13    35 
##    36    37    38    39    40    41    42    43    44    45    47    48    50 
##    18     7    10     9    67     2     6     4     4    11     2     6    14 
##    51    54    55    56    60    65    76    77    88    99 
##     4     2     2     2     3     1     3   881  3760  2867
table(ess$rlgdgr)
## 
##     0     1     2     3     4     5     6     7     8     9    10    77    88 
## 74056 27810 34542 37920 30711 79276 47135 54811 48445 20683 30189   978  2901 
##    99 
##  1098

Mission 3

# Creating Israel's dataset
israel_dataset <- ess[ess$cntry == "IL", ]

# Filtering for variables of interest and Cleaning data
#Cleaning dataset
israel_subset <- israel_dataset %>%
  select(eduyrs, hinctnta, happy, rlgdgr, essround, dscrgrp) %>%
  mutate(
    happy = ifelse(happy %in% c(77, 88, 99), NA, happy),
    rlgdgr = ifelse(rlgdgr %in% c(77, 88, 99), NA, rlgdgr),
    eduyrs = ifelse(eduyrs %in% c(77, 88, 99), NA, eduyrs),
    hinctnta = ifelse(hinctnta %in% c(77, 88, 99), NA, hinctnta),
    dscrgrp = ifelse(dscrgrp %in% c(7, 8, 9), NA, dscrgrp),
  ) %>%
  filter(!is.na(happy) & !is.na(rlgdgr) & !is.na(eduyrs) & !is.na(hinctnta) & !is.na(dscrgrp))

# Creating table for variable of happiness levels in Israel
table(israel_subset$happy)
## 
##    0    1    2    3    4    5    6    7    8    9   10 
##   54   49  117  191  319  750  794 1457 2516 1617 1458
# Creating table for variable of number of years of education in Israel
table(israel_subset$eduyrs)
## 
##    0    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15 
##   66    8   22   35   62   52  108   70  479  168  491  398 2828  389  741  934 
##   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31 
## 1056  402  450  142  211   55   59   19   19   20    7    7    3    1   10    1 
##   33   34   36   40   43   60 
##    1    2    3    1    1    1
# Creating table for variable of total household income (in deciles) in Israel
table(israel_subset$hinctnta)
## 
##    1    2    3    4    5    6    7    8    9   10 
##  909 1301 1490 1203 1098  881  754  688  502  496

Mission 4

datasummary_skim(israel_subset %>% select(hinctnta, happy, eduyrs, rlgdgr, dscrgrp))
Unique (#) Missing (%) Mean SD Min Median Max
hinctnta 10 0 4.7 2.6 1.0 4.0 10.0
happy 11 0 7.5 2.0 0.0 8.0 10.0
eduyrs 38 0 13.3 3.8 0.0 12.0 60.0
rlgdgr 11 0 4.9 3.4 0.0 5.0 10.0
dscrgrp 2 0 1.8 0.4 1.0 2.0 2.0

Mission 5

# Frequency check for the variable of happiness levels 
freq(israel_subset$happy)
##       n    % val%
## 0    54  0.6  0.6
## 1    49  0.5  0.5
## 2   117  1.3  1.3
## 3   191  2.0  2.0
## 4   319  3.4  3.4
## 5   750  8.0  8.0
## 6   794  8.5  8.5
## 7  1457 15.6 15.6
## 8  2516 27.0 27.0
## 9  1617 17.3 17.3
## 10 1458 15.6 15.6
# Compute the frequencies for the variable of happiness levels 
israel_happycount <- israel_subset %>%
  count(happy, name = "Count")

#Visualisation through Lollipop sticks
ggplot(israel_happycount, aes(x = happy, y = Count)) + 
  geom_segment(aes(xend = happy, yend = 0, color = happy)) + 
  geom_point(aes(color = happy), size = 3) +
  scale_x_continuous(breaks = 0:10) +
  labs(
    title = "Happiness Levels in Israel",
    x = "Happiness Levels (0-10)",
    y = "Count" 
  ) + 
  theme_classic()

Mission 6

table(israel_subset$happy, israel_subset$hinctnta) %>%
  cprop(
    tab,
    digits = 1,
    total = TRUE,
    percent = FALSE, 
    drop = TRUE, 
    n = FALSE,
  )
##        
##         1     2     3     4     5     6     7     8     9     10    All  
##   0       2.0   1.2   0.3   0.7   0.3   0.3   0.0   0.3   0.0   0.0   0.6
##   1       1.8   0.8   0.9   0.0   0.4   0.0   0.1   0.4   0.2   0.0   0.5
##   2       3.2   1.9   1.1   1.2   1.3   1.0   0.3   0.4   0.4   0.2   1.3
##   3       4.5   2.6   2.7   1.6   2.0   1.5   0.8   0.7   1.4   0.8   2.0
##   4       4.7   4.5   3.4   5.4   4.2   2.8   1.6   1.5   1.2   0.8   3.4
##   5      12.2  11.1   9.2   9.5   7.5   8.3   5.0   3.5   2.6   2.8   8.0
##   6      10.3  10.8   9.4  10.1   6.9   9.1   7.0   7.7   3.4   3.6   8.5
##   7      15.7  15.1  15.4  17.0  16.4  16.3  17.0  15.0  16.1   9.5  15.6
##   8      19.0  22.8  24.7  23.5  25.8  29.9  34.4  31.7  35.3  39.3  27.0
##   9      12.0  13.6  15.4  16.7  17.9  15.3  17.8  24.6  25.9  27.4  17.3
##   10     14.5  15.6  17.4  14.3  17.5  15.4  16.0  14.2  13.5  15.5  15.6
##   Total 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0

Mission 7

happyinc_israel <- datasummary_crosstab(hinctnta ~ happy , data = israel_subset)

happyinc_israel
hinctnta 0 1  2  3  4  5  6  7  8  9  10 All
1 N 18 16 29 41 43 111 94 143 173 109 132 909
% row 2.0 1.8 3.2 4.5 4.7 12.2 10.3 15.7 19.0 12.0 14.5 100.0
2 N 15 10 25 34 58 144 141 197 297 177 203 1301
% row 1.2 0.8 1.9 2.6 4.5 11.1 10.8 15.1 22.8 13.6 15.6 100.0
3 N 5 14 17 40 50 137 140 230 368 230 259 1490
% row 0.3 0.9 1.1 2.7 3.4 9.2 9.4 15.4 24.7 15.4 17.4 100.0
4 N 8 0 15 19 65 114 122 204 283 201 172 1203
% row 0.7 0.0 1.2 1.6 5.4 9.5 10.1 17.0 23.5 16.7 14.3 100.0
5 N 3 4 14 22 46 82 76 180 283 196 192 1098
% row 0.3 0.4 1.3 2.0 4.2 7.5 6.9 16.4 25.8 17.9 17.5 100.0
6 N 3 0 9 13 25 73 80 144 263 135 136 881
% row 0.3 0.0 1.0 1.5 2.8 8.3 9.1 16.3 29.9 15.3 15.4 100.0
7 N 0 1 2 6 12 38 53 128 259 134 121 754
% row 0.0 0.1 0.3 0.8 1.6 5.0 7.0 17.0 34.4 17.8 16.0 100.0
8 N 2 3 3 5 10 24 53 103 218 169 98 688
% row 0.3 0.4 0.4 0.7 1.5 3.5 7.7 15.0 31.7 24.6 14.2 100.0
9 N 0 1 2 7 6 13 17 81 177 130 68 502
% row 0.0 0.2 0.4 1.4 1.2 2.6 3.4 16.1 35.3 25.9 13.5 100.0
10 N 0 0 1 4 4 14 18 47 195 136 77 496
% row 0.0 0.0 0.2 0.8 0.8 2.8 3.6 9.5 39.3 27.4 15.5 100.0
All N 54 49 117 191 319 750 794 1457 2516 1617 1458 9322
% row 0.6 0.5 1.3 2.0 3.4 8.0 8.5 15.6 27.0 17.3 15.6 100.0

Mission 8

# Create year variable
table(israel_subset$essround)
## 
##    4    5    6    7    8   10 
## 1678 1450 1617 1847 1865  865
israel_subset$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:10){
  israel_subset$year[israel_subset$essround == i] <- replacements[i]
}

#Aggregating Happiness levels
happiness_byyear <- israel_subset %>%
  group_by(year) %>%
  summarize(mean_happy = mean(happy, na.rm = TRUE))%>%
  filter(!is.na(year))

happiness_byyear
## # A tibble: 6 × 2
##    year mean_happy
##   <dbl>      <dbl>
## 1  2008       7.34
## 2  2010       7.36
## 3  2012       7.73
## 4  2014       7.49
## 5  2016       7.84
## 6  2020       7.14
# Visualisation of Happiness levels over Years 
ggplot(happiness_byyear, aes(x = year, y = mean_happy)) +
  geom_line(color = "blue", size = 1) +  # Line to show the trend
  geom_point(color = "red", size = 3) +  # Points to highlight each year's value
  labs(title = "Happiness Levels (2014-2020)" , 
       x = "Year",
       y = "Mean Happiness (0-10 scale)") +
  ylim(0, 10) +
  theme_minimal() 
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

Mission 9

# Recoding Variable
israel_subset <- israel_subset %>%
  mutate(dscrgrp = recode(as.character(dscrgrp),
                      '1' = "Yes", 
                      '2' = "No"))

# Creating Visualisation 
ggplot(israel_subset, aes(x = dscrgrp, y = eduyrs, fill = factor(dscrgrp))) +
  geom_boxplot() +
  labs(title = "Education years by Self-Identified Discrimiation",
       x = "Whether one feels discriminated against in Israel",
       y = "Average Education years") +
  scale_fill_viridis_d(name = "Education Level", option = "B") +
  scale_fill_manual(values = c("Yes" = "yellow", "No" = "orange")) +
  theme_classic() 
## Scale for fill is already present.
## Adding another scale for fill, which will replace the existing scale.

Mission 12

# Visualisation of Happiness levels over Years (Changing y-lim)
ggplot(happiness_byyear, aes(x = year, y = mean_happy)) +
  geom_line(color = "blue", size = 1) +  # Line to show the trend
  geom_point(color = "red", size = 3) +  # Points to highlight each year's value
  labs(title = "Happiness Levels (2014-2020)" , 
       x = "Year",
       y = "Mean Happiness (0-10 scale)") +
  ylim(7, 8) +
  theme_minimal()