Setting Up
# List of packages
packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer",
"fst", "viridis", "knitr", "kableExtra", "rmarkdown", "ggridges", "viridis", "questionr", "flextable", "infer","effects", "survey", "MASS", "dplyr", "htmltools")
# 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.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
## Warning: package 'modelsummary' was built under R version 4.2.3
## Warning in !is.null(rmarkdown::metadata$output) && rmarkdown::metadata$output
## %in% : 'length(x) = 2 > 1' in coercion to 'logical(1)'
## Warning: package 'fst' was built under R version 4.2.3
## Warning: package 'viridis' was built under R version 4.2.3
## Loading required package: viridisLite
## Warning: package 'viridisLite' was built under R version 4.2.3
## Warning: package 'knitr' was built under R version 4.2.3
## Warning: package 'kableExtra' was built under R version 4.2.3
##
## Attaching package: 'kableExtra'
##
## The following object is masked from 'package:dplyr':
##
## group_rows
## Warning: package 'rmarkdown' was built under R version 4.2.3
## Warning: package 'ggridges' was built under R version 4.2.3
## Warning: package 'questionr' was built under R version 4.2.3
## Warning: package 'flextable' was built under R version 4.2.3
##
## Attaching package: 'flextable'
##
## The following objects are masked from 'package:kableExtra':
##
## as_image, footnote
##
## The following object is masked from 'package:purrr':
##
## compose
## Warning: package 'infer' 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.
## Warning: package 'survey' was built under R version 4.2.3
## Loading required package: grid
## Loading required package: Matrix
## Warning: package 'Matrix' was built under R version 4.2.3
##
## Attaching package: 'Matrix'
##
## The following objects are masked from 'package:tidyr':
##
## expand, pack, unpack
##
## Loading required package: survival
## Warning: package 'survival' was built under R version 4.2.3
##
## Attaching package: 'survey'
##
## The following object is masked from 'package:graphics':
##
## dotchart
## Warning: package 'MASS' was built under R version 4.2.3
##
## Attaching package: 'MASS'
##
## The following object is masked from 'package:dplyr':
##
## select
## Warning: package 'htmltools' was built under R version 4.2.3
## [[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"
##
## [[13]]
## [1] "flextable" "questionr" "ggridges" "rmarkdown" "kableExtra"
## [6] "knitr" "viridis" "viridisLite" "fst" "RColorBrewer"
## [11] "modelsummary" "lubridate" "forcats" "stringr" "dplyr"
## [16] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [21] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [26] "datasets" "methods" "base"
##
## [[14]]
## [1] "infer" "flextable" "questionr" "ggridges" "rmarkdown"
## [6] "kableExtra" "knitr" "viridis" "viridisLite" "fst"
## [11] "RColorBrewer" "modelsummary" "lubridate" "forcats" "stringr"
## [16] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [21] "ggplot2" "tidyverse" "stats" "graphics" "grDevices"
## [26] "utils" "datasets" "methods" "base"
##
## [[15]]
## [1] "effects" "carData" "infer" "flextable" "questionr"
## [6] "ggridges" "rmarkdown" "kableExtra" "knitr" "viridis"
## [11] "viridisLite" "fst" "RColorBrewer" "modelsummary" "lubridate"
## [16] "forcats" "stringr" "dplyr" "purrr" "readr"
## [21] "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [26] "graphics" "grDevices" "utils" "datasets" "methods"
## [31] "base"
##
## [[16]]
## [1] "survey" "survival" "Matrix" "grid" "effects"
## [6] "carData" "infer" "flextable" "questionr" "ggridges"
## [11] "rmarkdown" "kableExtra" "knitr" "viridis" "viridisLite"
## [16] "fst" "RColorBrewer" "modelsummary" "lubridate" "forcats"
## [21] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [26] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [31] "grDevices" "utils" "datasets" "methods" "base"
##
## [[17]]
## [1] "MASS" "survey" "survival" "Matrix" "grid"
## [6] "effects" "carData" "infer" "flextable" "questionr"
## [11] "ggridges" "rmarkdown" "kableExtra" "knitr" "viridis"
## [16] "viridisLite" "fst" "RColorBrewer" "modelsummary" "lubridate"
## [21] "forcats" "stringr" "dplyr" "purrr" "readr"
## [26] "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [31] "graphics" "grDevices" "utils" "datasets" "methods"
## [36] "base"
##
## [[18]]
## [1] "MASS" "survey" "survival" "Matrix" "grid"
## [6] "effects" "carData" "infer" "flextable" "questionr"
## [11] "ggridges" "rmarkdown" "kableExtra" "knitr" "viridis"
## [16] "viridisLite" "fst" "RColorBrewer" "modelsummary" "lubridate"
## [21] "forcats" "stringr" "dplyr" "purrr" "readr"
## [26] "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [31] "graphics" "grDevices" "utils" "datasets" "methods"
## [36] "base"
##
## [[19]]
## [1] "htmltools" "MASS" "survey" "survival" "Matrix"
## [6] "grid" "effects" "carData" "infer" "flextable"
## [11] "questionr" "ggridges" "rmarkdown" "kableExtra" "knitr"
## [16] "viridis" "viridisLite" "fst" "RColorBrewer" "modelsummary"
## [21] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [26] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [31] "stats" "graphics" "grDevices" "utils" "datasets"
## [36] "methods" "base"
ess <- read_fst("All-ESS-Data.fst")
## Warning: package 'fstcore' was built under R version 4.2.3
library(fst)
Creating and Cleaning Israel Dataset
# Creating Israel's dataset
israel_dataset <- ess[ess$cntry == "IL", ]
# Cleaning Israel's dataset
israel_clean <- israel_dataset %>%
dplyr::select(eduail1, hinctnta, happy, rlgdgr, essround, yrbrn, rlgdnm, imprich, inprdsc) %>%
filter(!yrbrn %in% c(7777, 8888)) %>%
mutate(
happy = ifelse(happy %in% c(77, 88, 99), NA, happy),
rlgdgr = ifelse(rlgdgr %in% c(77, 88, 99), NA, rlgdgr),
eduail1 = ifelse(eduail1 %in% c(4444, 5555, 7777, 8888, 9999), NA, eduail1),
hinctnta = ifelse(hinctnta %in% c(77, 88, 99), NA, hinctnta),
rlgdnm = ifelse(rlgdnm %in% c(77,99), NA, rlgdnm),
imprich = ifelse(imprich %in% c(7, 8, 9), NA, imprich),
inprdsc = ifelse(inprdsc %in% c(77, 88, 99), NA, inprdsc),
year = case_when(essround == 1 ~ 2002,
essround == 2 ~ 2004,
essround == 3 ~ 2006,
essround == 4 ~ 2008,
essround == 5 ~ 2010,
essround == 6 ~ 2012,
essround == 7 ~ 2014,
essround == 8 ~ 2016,
essround == 9 ~ 2018,
essround == 10 ~ 2020),
) %>%
filter(!is.na(happy) & !is.na(rlgdgr) & !is.na(eduail1) & !is.na(hinctnta) & !is.na(rlgdnm) & !is.na(imprich) & !is.na(inprdsc))
# Recoding Variables
israel_clean <- israel_clean %>%
mutate(
eduail1 = factor(
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) # Convert to character for consistency
),
levels = c("Not completed Primary Education", "Primary School", "Junior High School",
"Secondary School", "Post-Secondary School (Non-Tetiary)",
"Bachelor's Degree", "Master's Degree", "Doctorate Degree")
)
) %>%
mutate(
inprdsc = factor(
case_when(
inprdsc == 0 ~ "None",
inprdsc == 1 ~ "1",
inprdsc == 2 ~ "2",
inprdsc == 3 ~ "3",
inprdsc == 4 ~ "4-6",
inprdsc == 5 ~ "7-9",
inprdsc == 6 ~ "10 or more",
TRUE ~ as.character(inprdsc) # Convert to character for consistency
),
levels = c("None", "1", "2", "3", "4-6", "7-9", "10 or more")
)
)
Double-checking that dataset is clean
levels(israel_clean$eduail1)
## [1] "Not completed Primary Education" "Primary School"
## [3] "Junior High School" "Secondary School"
## [5] "Post-Secondary School (Non-Tetiary)" "Bachelor's Degree"
## [7] "Master's Degree" "Doctorate Degree"
levels(israel_clean$inprdsc)
## [1] "None" "1" "2" "3" "4-6"
## [6] "7-9" "10 or more"
Calculating test statistic for education level
test_stat <- israel_clean %>%
specify(explanatory = eduail1,
response = happy) %>%
hypothesize(null = "independence") %>%
calculate(stat = "F")
print(test_stat$stat)
## [1] 9.288192
Simulating the null distribution for education level
null_distribution <- israel_clean %>%
specify(explanatory = eduail1,
response = happy) %>%
hypothesize(null = "independence") %>%
generate(reps = 1000, type = "permute") %>%
calculate(stat = "f")
Calculating the p-value of sample for education level
p_val <- null_distribution %>%
get_pvalue(obs_stat = test_stat, direction = "greater")
## Warning: Please be cautious in reporting a p-value of 0. This result is an
## approximation based on the number of `reps` chosen in the `generate()` step.
## See `?get_p_value()` for more information.
p_val
## # A tibble: 1 × 1
## p_value
## <dbl>
## 1 0
Visualising for education level
null_distribution %>%
visualize() +
shade_p_value(obs_stat = test_stat, direction = "greater")
## Warning in min(diff(unique_loc)): no non-missing arguments to min; returning
## Inf

Calculating test statistic for number of intimate friends
friend_stat <- israel_clean %>%
specify(explanatory = inprdsc,
response = happy) %>%
hypothesize(null = "independence") %>%
calculate(stat = "F")
print(friend_stat$stat)
## [1] 18.358
Simulating the null distribution for number of intimate friends
null_friend <- israel_clean %>%
specify(explanatory = inprdsc,
response = happy) %>%
hypothesize(null = "independence") %>%
generate(reps = 1000, type = "permute") %>%
calculate(stat = "F")
Calculating the p-value of sample for number of intimate
friends
p_friend <- null_friend %>%
get_pvalue(obs_stat = friend_stat, direction = "greater")
## Warning: Please be cautious in reporting a p-value of 0. This result is an
## approximation based on the number of `reps` chosen in the `generate()` step.
## See `?get_p_value()` for more information.
p_friend
## # A tibble: 1 × 1
## p_value
## <dbl>
## 1 0
Visualising for number of intimate friends
null_friend %>%
visualize() +
shade_p_value(obs_stat = friend_stat, direction = "greater")
## Warning in min(diff(unique_loc)): no non-missing arguments to min; returning
## Inf

Modelling
model1 <- lm(happy ~ eduail1, data = israel_clean)
model2 <- lm(happy ~ eduail1 + inprdsc, data = israel_clean)
model3 <- lm(happy ~ eduail1 + inprdsc + eduail1*inprdsc, data = israel_clean)
modelsummary(
list(model1, model2, model3))
|
|
(1)
|
(2)
|
(3)
|
|
(Intercept)
|
7.165
|
6.292
|
6.400
|
|
|
(0.193)
|
(0.221)
|
(0.592)
|
|
eduail1Primary School
|
−0.139
|
−0.008
|
−0.576
|
|
|
(0.231)
|
(0.228)
|
(0.648)
|
|
eduail1Junior High School
|
−0.028
|
−0.012
|
0.165
|
|
|
(0.233)
|
(0.230)
|
(0.709)
|
|
eduail1Secondary School
|
0.434
|
0.414
|
0.482
|
|
|
(0.201)
|
(0.198)
|
(0.616)
|
|
eduail1Post-Secondary School (Non-Tetiary)
|
0.590
|
0.528
|
−0.030
|
|
|
(0.213)
|
(0.211)
|
(0.693)
|
|
eduail1Bachelor’s Degree
|
0.751
|
0.639
|
1.418
|
|
|
(0.209)
|
(0.207)
|
(0.714)
|
|
eduail1Master’s Degree
|
0.729
|
0.590
|
−1.400
|
|
|
(0.225)
|
(0.223)
|
(0.967)
|
|
eduail1Doctorate Degree
|
0.867
|
0.759
|
0.100
|
|
|
(0.392)
|
(0.388)
|
(1.450)
|
|
inprdsc1
|
|
0.824
|
0.081
|
|
|
|
(0.138)
|
(0.693)
|
|
inprdsc2
|
|
0.846
|
0.933
|
|
|
|
(0.141)
|
(0.738)
|
|
inprdsc3
|
|
1.144
|
1.100
|
|
|
|
(0.141)
|
(0.714)
|
|
inprdsc4-6
|
|
1.065
|
1.667
|
|
|
|
(0.142)
|
(0.764)
|
|
inprdsc7-9
|
|
1.349
|
3.600
|
|
|
|
(0.209)
|
(1.964)
|
|
inprdsc10 or more
|
|
1.168
|
0.600
|
|
|
|
(0.184)
|
(1.108)
|
|
eduail1Primary School × inprdsc1
|
|
|
1.065
|
|
|
|
|
(0.776)
|
|
eduail1Junior High School × inprdsc1
|
|
|
0.169
|
|
|
|
|
(0.829)
|
|
eduail1Secondary School × inprdsc1
|
|
|
0.642
|
|
|
|
|
(0.722)
|
|
eduail1Post-Secondary School (Non-Tetiary) × inprdsc1
|
|
|
1.236
|
|
|
|
|
(0.805)
|
|
eduail1Bachelor’s Degree × inprdsc1
|
|
|
−0.070
|
|
|
|
|
(0.824)
|
|
eduail1Master’s Degree × inprdsc1
|
|
|
2.725
|
|
|
|
|
(1.085)
|
|
eduail1Doctorate Degree × inprdsc1
|
|
|
2.419
|
|
|
|
|
(1.634)
|
|
eduail1Primary School × inprdsc2
|
|
|
0.537
|
|
|
|
|
(0.847)
|
|
eduail1Junior High School × inprdsc2
|
|
|
−0.107
|
|
|
|
|
(0.880)
|
|
eduail1Secondary School × inprdsc2
|
|
|
−0.478
|
|
|
|
|
(0.768)
|
|
eduail1Post-Secondary School (Non-Tetiary) × inprdsc2
|
|
|
0.332
|
|
|
|
|
(0.844)
|
|
eduail1Bachelor’s Degree × inprdsc2
|
|
|
−0.718
|
|
|
|
|
(0.857)
|
|
eduail1Master’s Degree × inprdsc2
|
|
|
1.881
|
|
|
|
|
(1.093)
|
|
eduail1Doctorate Degree × inprdsc2
|
|
|
−0.433
|
|
|
|
|
(2.409)
|
|
eduail1Primary School × inprdsc3
|
|
|
0.639
|
|
|
|
|
(0.830)
|
|
eduail1Junior High School × inprdsc3
|
|
|
−0.046
|
|
|
|
|
(0.864)
|
|
eduail1Secondary School × inprdsc3
|
|
|
−0.057
|
|
|
|
|
(0.745)
|
|
eduail1Post-Secondary School (Non-Tetiary) × inprdsc3
|
|
|
0.461
|
|
|
|
|
(0.824)
|
|
eduail1Bachelor’s Degree × inprdsc3
|
|
|
−0.979
|
|
|
|
|
(0.832)
|
|
eduail1Master’s Degree × inprdsc3
|
|
|
1.857
|
|
|
|
|
(1.070)
|
|
eduail1Doctorate Degree × inprdsc3
|
|
|
0.000
|
|
|
|
|
(1.722)
|
|
eduail1Primary School × inprdsc4-6
|
|
|
0.096
|
|
|
|
|
(0.880)
|
|
eduail1Junior High School × inprdsc4-6
|
|
|
−1.188
|
|
|
|
|
(0.943)
|
|
eduail1Secondary School × inprdsc4-6
|
|
|
−0.857
|
|
|
|
|
(0.793)
|
|
eduail1Post-Secondary School (Non-Tetiary) × inprdsc4-6
|
|
|
−0.099
|
|
|
|
|
(0.866)
|
|
eduail1Bachelor’s Degree × inprdsc4-6
|
|
|
−1.513
|
|
|
|
|
(0.877)
|
|
eduail1Master’s Degree × inprdsc4-6
|
|
|
1.383
|
|
|
|
|
(1.101)
|
|
eduail1Doctorate Degree × inprdsc4-6
|
|
|
−0.417
|
|
|
|
|
(1.666)
|
|
eduail1Primary School × inprdsc7-9
|
|
|
−1.424
|
|
|
|
|
(2.383)
|
|
eduail1Junior High School × inprdsc7-9
|
|
|
−1.451
|
|
|
|
|
(2.123)
|
|
eduail1Secondary School × inprdsc7-9
|
|
|
−2.424
|
|
|
|
|
(1.988)
|
|
eduail1Post-Secondary School (Non-Tetiary) × inprdsc7-9
|
|
|
−1.345
|
|
|
|
|
(2.051)
|
|
eduail1Bachelor’s Degree × inprdsc7-9
|
|
|
−3.487
|
|
|
|
|
(2.034)
|
|
eduail1Master’s Degree × inprdsc7-9
|
|
|
−0.850
|
|
|
|
|
(2.175)
|
|
eduail1Doctorate Degree × inprdsc7-9
|
|
|
−2.433
|
|
|
|
|
(2.603)
|
|
eduail1Primary School × inprdsc10 or more
|
|
|
2.192
|
|
|
|
|
(1.251)
|
|
eduail1Junior High School × inprdsc10 or more
|
|
|
−1.165
|
|
|
|
|
(1.401)
|
|
eduail1Secondary School × inprdsc10 or more
|
|
|
0.532
|
|
|
|
|
(1.144)
|
|
eduail1Post-Secondary School (Non-Tetiary) × inprdsc10 or more
|
|
|
1.065
|
|
|
|
|
(1.217)
|
|
eduail1Bachelor’s Degree × inprdsc10 or more
|
|
|
−0.946
|
|
|
|
|
(1.218)
|
|
eduail1Master’s Degree × inprdsc10 or more
|
|
|
2.733
|
|
|
|
|
(1.406)
|
|
eduail1Doctorate Degree × inprdsc10 or more
|
|
|
1.400
|
|
|
|
|
(1.964)
|
|
Num.Obs.
|
3092
|
3092
|
3092
|
|
R2
|
0.021
|
0.046
|
0.065
|
|
R2 Adj.
|
0.018
|
0.042
|
0.048
|
|
AIC
|
12758.6
|
12688.0
|
12710.5
|
|
BIC
|
12812.9
|
12778.6
|
13054.6
|
|
Log.Lik.
|
−6370.290
|
−6329.011
|
−6298.242
|
|
RMSE
|
1.90
|
1.87
|
1.86
|
## Adding Stars
modelsummary(
list(model1, model2, model3),
fmt = 1,
estimate = c( "{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}"),
statistic = NULL,
coef_omit = "Intercept")
|
|
(1)
|
(2)
|
(3)
|
|
eduail1Primary School
|
−0.1 (0.2)
|
0.0 (0.2)
|
−0.6 (0.6)
|
|
eduail1Junior High School
|
0.0 (0.2)
|
0.0 (0.2)
|
0.2 (0.7)
|
|
eduail1Secondary School
|
0.4 (0.2)*
|
0.4 (0.2)*
|
0.5 (0.6)
|
|
eduail1Post-Secondary School (Non-Tetiary)
|
0.6 (0.2)**
|
0.5 (0.2)*
|
0.0 (0.7)
|
|
eduail1Bachelor’s Degree
|
0.8 (0.2)***
|
0.6 (0.2)**
|
1.4 (0.7)*
|
|
eduail1Master’s Degree
|
0.7 (0.2)**
|
0.6 (0.2)**
|
−1.4 (1.0)
|
|
eduail1Doctorate Degree
|
0.9 (0.4)*
|
0.8 (0.4)+
|
0.1 (1.5)
|
|
inprdsc1
|
|
0.8 (0.1)***
|
0.1 (0.7)
|
|
inprdsc2
|
|
0.8 (0.1)***
|
0.9 (0.7)
|
|
inprdsc3
|
|
1.1 (0.1)***
|
1.1 (0.7)
|
|
inprdsc4-6
|
|
1.1 (0.1)***
|
1.7 (0.8)*
|
|
inprdsc7-9
|
|
1.3 (0.2)***
|
3.6 (2.0)+
|
|
inprdsc10 or more
|
|
1.2 (0.2)***
|
0.6 (1.1)
|
|
eduail1Primary School × inprdsc1
|
|
|
1.1 (0.8)
|
|
eduail1Junior High School × inprdsc1
|
|
|
0.2 (0.8)
|
|
eduail1Secondary School × inprdsc1
|
|
|
0.6 (0.7)
|
|
eduail1Post-Secondary School (Non-Tetiary) × inprdsc1
|
|
|
1.2 (0.8)
|
|
eduail1Bachelor’s Degree × inprdsc1
|
|
|
−0.1 (0.8)
|
|
eduail1Master’s Degree × inprdsc1
|
|
|
2.7 (1.1)*
|
|
eduail1Doctorate Degree × inprdsc1
|
|
|
2.4 (1.6)
|
|
eduail1Primary School × inprdsc2
|
|
|
0.5 (0.8)
|
|
eduail1Junior High School × inprdsc2
|
|
|
−0.1 (0.9)
|
|
eduail1Secondary School × inprdsc2
|
|
|
−0.5 (0.8)
|
|
eduail1Post-Secondary School (Non-Tetiary) × inprdsc2
|
|
|
0.3 (0.8)
|
|
eduail1Bachelor’s Degree × inprdsc2
|
|
|
−0.7 (0.9)
|
|
eduail1Master’s Degree × inprdsc2
|
|
|
1.9 (1.1)+
|
|
eduail1Doctorate Degree × inprdsc2
|
|
|
−0.4 (2.4)
|
|
eduail1Primary School × inprdsc3
|
|
|
0.6 (0.8)
|
|
eduail1Junior High School × inprdsc3
|
|
|
0.0 (0.9)
|
|
eduail1Secondary School × inprdsc3
|
|
|
−0.1 (0.7)
|
|
eduail1Post-Secondary School (Non-Tetiary) × inprdsc3
|
|
|
0.5 (0.8)
|
|
eduail1Bachelor’s Degree × inprdsc3
|
|
|
−1.0 (0.8)
|
|
eduail1Master’s Degree × inprdsc3
|
|
|
1.9 (1.1)+
|
|
eduail1Doctorate Degree × inprdsc3
|
|
|
0.0 (1.7)
|
|
eduail1Primary School × inprdsc4-6
|
|
|
0.1 (0.9)
|
|
eduail1Junior High School × inprdsc4-6
|
|
|
−1.2 (0.9)
|
|
eduail1Secondary School × inprdsc4-6
|
|
|
−0.9 (0.8)
|
|
eduail1Post-Secondary School (Non-Tetiary) × inprdsc4-6
|
|
|
−0.1 (0.9)
|
|
eduail1Bachelor’s Degree × inprdsc4-6
|
|
|
−1.5 (0.9)+
|
|
eduail1Master’s Degree × inprdsc4-6
|
|
|
1.4 (1.1)
|
|
eduail1Doctorate Degree × inprdsc4-6
|
|
|
−0.4 (1.7)
|
|
eduail1Primary School × inprdsc7-9
|
|
|
−1.4 (2.4)
|
|
eduail1Junior High School × inprdsc7-9
|
|
|
−1.5 (2.1)
|
|
eduail1Secondary School × inprdsc7-9
|
|
|
−2.4 (2.0)
|
|
eduail1Post-Secondary School (Non-Tetiary) × inprdsc7-9
|
|
|
−1.3 (2.1)
|
|
eduail1Bachelor’s Degree × inprdsc7-9
|
|
|
−3.5 (2.0)+
|
|
eduail1Master’s Degree × inprdsc7-9
|
|
|
−0.9 (2.2)
|
|
eduail1Doctorate Degree × inprdsc7-9
|
|
|
−2.4 (2.6)
|
|
eduail1Primary School × inprdsc10 or more
|
|
|
2.2 (1.3)+
|
|
eduail1Junior High School × inprdsc10 or more
|
|
|
−1.2 (1.4)
|
|
eduail1Secondary School × inprdsc10 or more
|
|
|
0.5 (1.1)
|
|
eduail1Post-Secondary School (Non-Tetiary) × inprdsc10 or more
|
|
|
1.1 (1.2)
|
|
eduail1Bachelor’s Degree × inprdsc10 or more
|
|
|
−0.9 (1.2)
|
|
eduail1Master’s Degree × inprdsc10 or more
|
|
|
2.7 (1.4)+
|
|
eduail1Doctorate Degree × inprdsc10 or more
|
|
|
1.4 (2.0)
|
|
Num.Obs.
|
3092
|
3092
|
3092
|
|
R2
|
0.021
|
0.046
|
0.065
|
|
R2 Adj.
|
0.018
|
0.042
|
0.048
|
|
AIC
|
12758.6
|
12688.0
|
12710.5
|
|
BIC
|
12812.9
|
12778.6
|
13054.6
|
|
Log.Lik.
|
−6370.290
|
−6329.011
|
−6298.242
|
|
RMSE
|
1.90
|
1.87
|
1.86
|
Interaction Plot
interaction_plot <- effect("eduail1*inprdsc", model3, na.rm=TRUE)
plot(interaction_plot,
main="Interaction effect Education Level x Number of Intimate Friends",
xlab="Education Level",
ylab="Happiness Levels")

Getting the equation output
# Installing package
remotes::install_github("datalorax/equatiomatic", force = TRUE)
## Downloading GitHub repo datalorax/equatiomatic@HEAD
## xfun (0.40 -> 0.41 ) [CRAN]
## rlang (1.1.1 -> 1.1.2) [CRAN]
## dplyr (1.1.3 -> 1.1.4) [CRAN]
## Installing 3 packages: xfun, rlang, dplyr
## Warning: package 'dplyr' is in use and will not be installed
## Installing packages into 'C:/Users/yi en/AppData/Local/R/win-library/4.2'
## (as 'lib' is unspecified)
## package 'xfun' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'xfun'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\yi en\AppData\Local\R\win-library\4.2\00LOCK\xfun\libs\x64\xfun.dll to
## C:\Users\yi en\AppData\Local\R\win-library\4.2\xfun\libs\x64\xfun.dll:
## Permission denied
## Warning: restored 'xfun'
## package 'rlang' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'rlang'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\yi en\AppData\Local\R\win-library\4.2\00LOCK\rlang\libs\x64\rlang.dll
## to C:\Users\yi en\AppData\Local\R\win-library\4.2\rlang\libs\x64\rlang.dll:
## Permission denied
## Warning: restored 'rlang'
##
## The downloaded binary packages are in
## C:\Users\yi en\AppData\Local\Temp\RtmpqSCq2X\downloaded_packages
## ── R CMD build ─────────────────────────────────────────────────────────────────
## WARNING: Rtools is required to build R packages, but is not currently installed.
##
## Please download and install Rtools 4.2 from https://cran.r-project.org/bin/windows/Rtools/.
##
✔ checking for file 'C:\Users\yi en\AppData\Local\Temp\RtmpqSCq2X\remotes37283e3628a2\datalorax-equatiomatic-29ff168/DESCRIPTION' (357ms)
##
─ preparing 'equatiomatic': (956ms)
## checking DESCRIPTION meta-information ...
checking DESCRIPTION meta-information ...
✔ checking DESCRIPTION meta-information
##
─ checking for LF line-endings in source and make files and shell scripts
##
─ checking for empty or unneeded directories
##
─ building 'equatiomatic_0.3.1.tar.gz'
##
##
## Installing package into 'C:/Users/yi en/AppData/Local/R/win-library/4.2'
## (as 'lib' is unspecified)
# Getting the equation
equatiomatic::extract_eq(model2, use_coefs = TRUE)
\[
\operatorname{\widehat{happy}} = 6.29 -
0.01(\operatorname{eduail1}_{\operatorname{Primary\ School}}) -
0.01(\operatorname{eduail1}_{\operatorname{Junior\ High\ School}}) +
0.41(\operatorname{eduail1}_{\operatorname{Secondary\ School}}) +
0.53(\operatorname{eduail1}_{\operatorname{Post-Secondary\ School\
(Non-Tetiary)}}) +
0.64(\operatorname{eduail1}_{\operatorname{Bachelor's\ Degree}}) +
0.59(\operatorname{eduail1}_{\operatorname{Master's\ Degree}}) +
0.76(\operatorname{eduail1}_{\operatorname{Doctorate\ Degree}}) +
0.82(\operatorname{inprdsc}_{\operatorname{1}}) +
0.85(\operatorname{inprdsc}_{\operatorname{2}}) +
1.14(\operatorname{inprdsc}_{\operatorname{3}}) +
1.06(\operatorname{inprdsc}_{\operatorname{4-6}}) +
1.35(\operatorname{inprdsc}_{\operatorname{7-9}}) +
1.17(\operatorname{inprdsc}_{\operatorname{10\ or\ more}})
\]