Setting Up
# List of packages
packages <- c("tidyverse", "infer", "fst", "modelsummary", "effects") # 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)
## Warning: package 'tidyverse' was built under R version 4.2.3
## Warning: package 'ggplot2' was built under R version 4.2.3
## Warning: package 'tibble' was built under R version 4.2.3
## Warning: package 'tidyr' was built under R version 4.2.3
## Warning: package 'readr' was built under R version 4.2.3
## Warning: package 'purrr' 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
## Warning: package 'forcats' was built under R version 4.2.3
## Warning: package 'lubridate' was built under R version 4.2.3
## ── 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.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
## Warning: package 'infer' was built under R version 4.2.3
## Warning: package 'fst' was built under R version 4.2.3
## Warning: package 'modelsummary' was built under R version 4.2.3
## Warning: package 'effects' was built under R version 4.2.3
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.2.3
## lattice theme set by effectsTheme()
## See ?effectsTheme for details.
## [[1]]
## [1] "lubridate" "forcats" "stringr" "dplyr" "purrr" "readr"
## [7] "tidyr" "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [13] "grDevices" "utils" "datasets" "methods" "base"
##
## [[2]]
## [1] "infer" "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [7] "readr" "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [13] "graphics" "grDevices" "utils" "datasets" "methods" "base"
##
## [[3]]
## [1] "fst" "infer" "lubridate" "forcats" "stringr" "dplyr"
## [7] "purrr" "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [13] "stats" "graphics" "grDevices" "utils" "datasets" "methods"
## [19] "base"
##
## [[4]]
## [1] "modelsummary" "fst" "infer" "lubridate" "forcats"
## [6] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [11] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [16] "grDevices" "utils" "datasets" "methods" "base"
##
## [[5]]
## [1] "effects" "carData" "modelsummary" "fst" "infer"
## [6] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [11] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [16] "stats" "graphics" "grDevices" "utils" "datasets"
## [21] "methods" "base"
ess <- read_fst("All-ESS-Data.fst")
## Warning: package 'fstcore' was built under R version 4.2.3
Recoding Education Level in Israel
# Filter for Israel
israel_data <- ess %>%
filter(cntry == "IL")
israel_dataset <- ess[ess$cntry == "IL", ]
# Cleaning Israel's dataset
israel_clean <- israel_dataset %>%
select(eduail1, hinctnta, happy, essround) %>%
mutate(
happy = ifelse(happy %in% c(77, 88, 99), NA, happy),
eduail1 = ifelse(eduail1 %in% c(4444, 5555, 7777, 8888, 9999), NA, eduail1),
hinctnta = ifelse(hinctnta %in% c(77, 88, 99), NA, hinctnta),
) %>%
filter(!is.na(happy) & !is.na(eduail1) & !is.na(hinctnta)) %>%
# Recoding Education Level
mutate(
eduail1 = case_when(
eduail1 == 1 ~ "Not completed Primary Education",
eduail1 == 2 ~ "Primary School",
eduail1 == 3 ~ "Junior High School",
eduail1 %in% 4:9 ~ "Secondary School",
eduail1 %in% 10:11 ~ "Post-Secondary School (Non-Tetiary)",
eduail1 %in% 12:13 ~ "Bachelor's Degree",
eduail1 %in% 14:16 ~ "Master's Degree",
eduail1 == 17 ~ "Doctorate Degree",
TRUE ~ as.character(eduail1)
)
)
Modelling of Education Level and Happiness Levels in Israel
model1_IL <- lm(happy ~ eduail1, data = israel_clean)
model2_IL <- lm(happy ~ eduail1 + hinctnta, data = israel_clean)
model3_IL <- lm(happy ~ eduail1 + hinctnta + eduail1*hinctnta, data = israel_clean)
modelsummary(
list(model1_IL, model2_IL, model3_IL))
|
|
(1)
|
(2)
|
(3)
|
|
(Intercept)
|
7.899
|
7.024
|
7.230
|
|
|
(0.078)
|
(0.120)
|
(0.222)
|
|
eduail1Doctorate Degree
|
0.163
|
0.008
|
0.785
|
|
|
(0.348)
|
(0.344)
|
(1.110)
|
|
eduail1Junior High School
|
−0.882
|
−0.533
|
−1.238
|
|
|
(0.147)
|
(0.149)
|
(0.342)
|
|
eduail1Master’s Degree
|
−0.025
|
−0.159
|
−0.658
|
|
|
(0.138)
|
(0.137)
|
(0.425)
|
|
eduail1Not completed Primary Education
|
−0.709
|
−0.139
|
−0.433
|
|
|
(0.203)
|
(0.209)
|
(0.417)
|
|
eduail1Post-Secondary School (Non-Tetiary)
|
−0.127
|
0.006
|
−0.052
|
|
|
(0.118)
|
(0.117)
|
(0.310)
|
|
eduail1Primary School
|
−0.954
|
−0.484
|
−0.800
|
|
|
(0.144)
|
(0.150)
|
(0.321)
|
|
eduail1Secondary School
|
−0.299
|
−0.074
|
−0.264
|
|
|
(0.094)
|
(0.096)
|
(0.250)
|
|
hinctnta
|
|
0.139
|
0.106
|
|
|
|
(0.015)
|
(0.033)
|
|
eduail1Doctorate Degree × hinctnta
|
|
|
−0.100
|
|
|
|
|
(0.144)
|
|
eduail1Junior High School × hinctnta
|
|
|
0.165
|
|
|
|
|
(0.069)
|
|
eduail1Master’s Degree × hinctnta
|
|
|
0.073
|
|
|
|
|
(0.058)
|
|
eduail1Not completed Primary Education × hinctnta
|
|
|
0.073
|
|
|
|
|
(0.141)
|
|
eduail1Post-Secondary School (Non-Tetiary) × hinctnta
|
|
|
0.005
|
|
|
|
|
(0.050)
|
|
eduail1Primary School × hinctnta
|
|
|
0.071
|
|
|
|
|
(0.076)
|
|
eduail1Secondary School × hinctnta
|
|
|
0.029
|
|
|
|
|
(0.040)
|
|
Num.Obs.
|
3301
|
3301
|
3301
|
|
R2
|
0.024
|
0.051
|
0.053
|
|
R2 Adj.
|
0.022
|
0.048
|
0.049
|
|
AIC
|
13683.2
|
13594.8
|
13600.5
|
|
BIC
|
13738.1
|
13655.8
|
13704.2
|
|
Log.Lik.
|
−6832.608
|
−6787.399
|
−6783.246
|
|
RMSE
|
1.92
|
1.89
|
1.89
|
# Adding Stars
modelsummary(
list(model1_IL, model2_IL, model3_IL),
fmt = 1,
estimate = c( "{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}"),
statistic = NULL,
coef_omit = "Intercept")
|
|
(1)
|
(2)
|
(3)
|
|
eduail1Doctorate Degree
|
0.2 (0.3)
|
0.0 (0.3)
|
0.8 (1.1)
|
|
eduail1Junior High School
|
−0.9 (0.1)***
|
−0.5 (0.1)***
|
−1.2 (0.3)***
|
|
eduail1Master’s Degree
|
0.0 (0.1)
|
−0.2 (0.1)
|
−0.7 (0.4)
|
|
eduail1Not completed Primary Education
|
−0.7 (0.2)***
|
−0.1 (0.2)
|
−0.4 (0.4)
|
|
eduail1Post-Secondary School (Non-Tetiary)
|
−0.1 (0.1)
|
0.0 (0.1)
|
−0.1 (0.3)
|
|
eduail1Primary School
|
−1.0 (0.1)***
|
−0.5 (0.1)**
|
−0.8 (0.3)*
|
|
eduail1Secondary School
|
−0.3 (0.1)**
|
−0.1 (0.1)
|
−0.3 (0.3)
|
|
hinctnta
|
|
0.1 (0.0)***
|
0.1 (0.0)**
|
|
eduail1Doctorate Degree × hinctnta
|
|
|
−0.1 (0.1)
|
|
eduail1Junior High School × hinctnta
|
|
|
0.2 (0.1)*
|
|
eduail1Master’s Degree × hinctnta
|
|
|
0.1 (0.1)
|
|
eduail1Not completed Primary Education × hinctnta
|
|
|
0.1 (0.1)
|
|
eduail1Post-Secondary School (Non-Tetiary) × hinctnta
|
|
|
0.0 (0.0)
|
|
eduail1Primary School × hinctnta
|
|
|
0.1 (0.1)
|
|
eduail1Secondary School × hinctnta
|
|
|
0.0 (0.0)
|
|
Num.Obs.
|
3301
|
3301
|
3301
|
|
R2
|
0.024
|
0.051
|
0.053
|
|
R2 Adj.
|
0.022
|
0.048
|
0.049
|
|
AIC
|
13683.2
|
13594.8
|
13600.5
|
|
BIC
|
13738.1
|
13655.8
|
13704.2
|
|
Log.Lik.
|
−6832.608
|
−6787.399
|
−6783.246
|
|
RMSE
|
1.92
|
1.89
|
1.89
|