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: 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", ]
# Survey weights code and design 
israel_dataset$weight <- israel_dataset$dweight * israel_dataset$pweight
survey_design <- svydesign(ids = ~1, data = israel_dataset, weights = ~weight)
# Cleaning Israel's dataset
israel_clean <- israel_dataset %>%
  dplyr::select(eduail1, hinctnta, happy, rlgdgr, essround, yrbrn, rlgdnm, imprich, inprdsc, weight) %>%
  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 %in% 1:9 ~ "Up to Secondary School",
        eduail1 %in% 10:13 ~ "Post-Secondary School (Non-Tetiary) or BA",
        eduail1 %in% 14:17 ~ "MA or PHD",
        TRUE ~ as.character(eduail1)  # Convert to character for consistency
      ),
      levels = c("Up to Secondary School", "Post-Secondary School (Non-Tetiary) or BA", 
                 "MA or PHD")
    )
  ) %>%
  mutate(
    inprdsc = factor(
      case_when(
        inprdsc %in% 0:3 ~ "0-3 friends",
        inprdsc == 4 ~ "4-6 friends",
        inprdsc %in% 5:6 ~ "7 or more friends",
        TRUE ~ as.character(inprdsc)  # Convert to character for consistency
      ),
      levels = c("0-3 friends", "4-6 friends", "7 or more friends")
    )) %>%
    mutate(
    hinctnta = factor(
      case_when(
        hinctnta %in% 1:3 ~ "1st-3rd Deciles",
        hinctnta %in% 4:6 ~ "4th-6th Deciles",
        hinctnta %in% 7:10 ~ "7th-10th Deciles",
        TRUE ~ as.character(hinctnta)  # Convert to character for consistency
      ),
  ))

Double-checking that dataset is clean

levels(israel_clean$eduail1)
## [1] "Up to Secondary School"                   
## [2] "Post-Secondary School (Non-Tetiary) or BA"
## [3] "MA or PHD"
levels(israel_clean$inprdsc)
## [1] "0-3 friends"       "4-6 friends"       "7 or more friends"
levels(israel_clean$hinctnta)
## [1] "1st-3rd Deciles"  "4th-6th Deciles"  "7th-10th Deciles"

Data Summary

datasummary_skim(israel_clean)
Unique (#) Missing (%) Mean SD Min Median Max
happy 11 0 7.6 1.9 0.0 8.0 10.0
rlgdgr 11 0 5.1 3.3 0.0 5.0 10.0
essround 2 0 6.5 0.5 6.0 7.0 7.0
yrbrn 80 0 1967.0 18.9 1921.0 1970.0 2000.0
rlgdnm 8 0 5.5 5.2 1.0 5.0 66.0
imprich 6 0 3.5 1.5 1.0 4.0 6.0
weight 724 0 0.2 0.1 0.0 0.2 0.8
year 2 0 2013.1 1.0 2012.0 2014.0 2014.0
table(israel_clean$inprdsc)
## 
##       0-3 friends       4-6 friends 7 or more friends 
##              2177               614               301
table(israel_clean$eduail1)
## 
##                    Up to Secondary School 
##                                      1759 
## Post-Secondary School (Non-Tetiary) or BA 
##                                      1028 
##                                 MA or PHD 
##                                       305
table(israel_clean$hinctnta)
## 
##  1st-3rd Deciles  4th-6th Deciles 7th-10th Deciles 
##             1008             1136              948

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] 18.02712

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] 12.28687

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

Calculating test statistic for income decile

income_stat <- israel_clean %>%
  specify(explanatory = hinctnta, 
          response = happy) %>%
  hypothesize(null = "independence") %>%
  calculate(stat = "F")

print(income_stat$stat)
## [1] 59.38862

Simulating the null distribution for income decile

null_income <- israel_clean %>%
  specify(explanatory = hinctnta,
          response = happy) %>%
  hypothesize(null = "independence") %>%
  generate(reps = 1000, type = "permute") %>% 
  calculate(stat = "F")

Calculating the p-value of sample for income decile

p_income <- null_income %>% 
  get_pvalue(obs_stat = income_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_income
## # A tibble: 1 × 1
##   p_value
##     <dbl>
## 1       0

Visualising for income decile

null_income %>%
  visualize() +
  shade_p_value(obs_stat = income_stat, direction = "greater")
## Warning in min(diff(unique_loc)): no non-missing arguments to min; returning
## Inf

Modelling (Education Attainment and Number of Intimate Friends)

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.446 7.367 7.335
(0.046) (0.049) (0.052)
eduail1Post-Secondary School (Non-Tetiary) or BA 0.401 0.375 0.462
(0.075) (0.075) (0.090)
eduail1MA or PHD 0.462 0.412 0.467
(0.118) (0.119) (0.152)
inprdsc4-6 friends 0.251 0.314
(0.088) (0.124)
inprdsc7 or more friends 0.436 0.685
(0.117) (0.163)
eduail1Post-Secondary School (Non-Tetiary) or BA × inprdsc4-6 friends −0.154
(0.189)
eduail1MA or PHD × inprdsc4-6 friends −0.094
(0.277)
eduail1Post-Secondary School (Non-Tetiary) or BA × inprdsc7 or more friends −0.574
(0.255)
eduail1MA or PHD × inprdsc7 or more friends −0.362
(0.371)
Num.Obs. 3092 3092 3092
R2 0.012 0.018 0.019
R2 Adj. 0.011 0.016 0.017
AIC 12777.2 12762.3 12764.9
BIC 12801.4 12798.5 12825.2
Log.Lik. −6384.604 −6375.137 −6372.440
RMSE 1.91 1.90 1.90
## 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)
eduail1Post-Secondary School (Non-Tetiary) or BA 0.4 (0.1)*** 0.4 (0.1)*** 0.5 (0.1)***
eduail1MA or PHD 0.5 (0.1)*** 0.4 (0.1)*** 0.5 (0.2)**
inprdsc4-6 friends 0.3 (0.1)** 0.3 (0.1)*
inprdsc7 or more friends 0.4 (0.1)*** 0.7 (0.2)***
eduail1Post-Secondary School (Non-Tetiary) or BA × inprdsc4-6 friends −0.2 (0.2)
eduail1MA or PHD × inprdsc4-6 friends −0.1 (0.3)
eduail1Post-Secondary School (Non-Tetiary) or BA × inprdsc7 or more friends −0.6 (0.3)*
eduail1MA or PHD × inprdsc7 or more friends −0.4 (0.4)
Num.Obs. 3092 3092 3092
R2 0.012 0.018 0.019
R2 Adj. 0.011 0.016 0.017
AIC 12777.2 12762.3 12764.9
BIC 12801.4 12798.5 12825.2
Log.Lik. −6384.604 −6375.137 −6372.440
RMSE 1.91 1.90 1.90

Interaction Plot (Education Attainment and Number of Intimate Friends)

edufriend_interaction <- effect("eduail1*inprdsc", model3, na.rm=TRUE)

plot(edufriend_interaction,
     main="Interaction effect Education Level x Number of Intimate Friends",
     xlab="Education Level",
     ylab="Happiness Levels")

Modelling (Education Attainment and Income Decile)

model1 <- lm(happy ~ eduail1, data = israel_clean)
modelinc2 <- lm(happy ~ eduail1 + hinctnta, data = israel_clean)
modelinc3 <- lm(happy ~ eduail1 + hinctnta + eduail1*hinctnta, data = israel_clean)
modelsummary(
  list(model1, modelinc2, modelinc3))
 (1)   (2)   (3)
(Intercept) 7.446 7.140 7.091
(0.046) (0.061) (0.067)
eduail1Post-Secondary School (Non-Tetiary) or BA 0.401 0.187 0.385
(0.075) (0.077) (0.153)
eduail1MA or PHD 0.462 0.093 0.563
(0.118) (0.124) (0.375)
hinctnta4th-6th Deciles 0.406 0.480
(0.083) (0.100)
hinctnta7th-10th Deciles 0.863 0.984
(0.092) (0.124)
eduail1Post-Secondary School (Non-Tetiary) or BA × hinctnta4th-6th Deciles −0.233
(0.193)
eduail1MA or PHD × hinctnta4th-6th Deciles −0.647
(0.439)
eduail1Post-Secondary School (Non-Tetiary) or BA × hinctnta7th-10th Deciles −0.328
(0.207)
eduail1MA or PHD × hinctnta7th-10th Deciles −0.539
(0.411)
Num.Obs. 3092 3092 3092
R2 0.012 0.039 0.040
R2 Adj. 0.011 0.038 0.038
AIC 12777.2 12694.6 12698.1
BIC 12801.4 12730.8 12758.5
Log.Lik. −6384.604 −6341.305 −6339.053
RMSE 1.91 1.88 1.88
## Adding Stars 
modelsummary(
  list(model1, modelinc2, modelinc3),
  fmt = 1,
  estimate  = c( "{estimate} ({std.error}){stars}",
                "{estimate} ({std.error}){stars}",
                "{estimate} ({std.error}){stars}"),
  statistic = NULL,
  coef_omit = "Intercept")
 (1)   (2)   (3)
eduail1Post-Secondary School (Non-Tetiary) or BA 0.4 (0.1)*** 0.2 (0.1)* 0.4 (0.2)*
eduail1MA or PHD 0.5 (0.1)*** 0.1 (0.1) 0.6 (0.4)
hinctnta4th-6th Deciles 0.4 (0.1)*** 0.5 (0.1)***
hinctnta7th-10th Deciles 0.9 (0.1)*** 1.0 (0.1)***
eduail1Post-Secondary School (Non-Tetiary) or BA × hinctnta4th-6th Deciles −0.2 (0.2)
eduail1MA or PHD × hinctnta4th-6th Deciles −0.6 (0.4)
eduail1Post-Secondary School (Non-Tetiary) or BA × hinctnta7th-10th Deciles −0.3 (0.2)
eduail1MA or PHD × hinctnta7th-10th Deciles −0.5 (0.4)
Num.Obs. 3092 3092 3092
R2 0.012 0.039 0.040
R2 Adj. 0.011 0.038 0.038
AIC 12777.2 12694.6 12698.1
BIC 12801.4 12730.8 12758.5
Log.Lik. −6384.604 −6341.305 −6339.053
RMSE 1.91 1.88 1.88

Interaction Plot (Education Attainment and Income Decile)

eduinc_interaction <- effect("eduail1*hinctnta", modelinc3, na.rm=TRUE)

plot(eduinc_interaction,
     main="Interaction effect Education Level x Income Decile",
     xlab="Education Level",
     ylab="Happiness Levels")

GLM Modelling

glm1 <- glm(happy ~ eduail1, family = gaussian, data = israel_clean, weights = round(weight))
glm2 <- glm(happy ~ inprdsc, family = gaussian, data = israel_clean, weights = round(weight))
glm3 <- glm(happy ~ hinctnta, family = gaussian, data = israel_clean, weights = round(weight))

modelsummary(list(glm1, glm2, glm3),
  fmt = 1,
  estimate  = c( "{estimate} ({std.error}){stars}",
                "{estimate} ({std.error}){stars}",
                "{estimate} ({std.error}){stars}"),
  statistic = NULL,
  coef_omit = "Intercept")
 (1)   (2)   (3)
eduail1Post-Secondary School (Non-Tetiary) or BA 0.8 (0.7)
eduail1MA or PHD 1.4 (1.2)
inprdsc4-6 friends 0.8 (0.7)
inprdsc7 or more friends 1.0 (1.1)
hinctnta4th-6th Deciles 1.1 (0.8)
hinctnta7th-10th Deciles 1.8 (0.7)*
Num.Obs. 43 43 43
R2 −0.030 −0.043 −0.064
AIC 184.9 185.3 181.2
BIC 191.9 192.4 188.3
Log.Lik. −88.426 −88.665 −86.614
RMSE 1.95 1.96 1.98
glm_models <- list(glm1, glm2, glm3)
modelplot(glm_models, coef_omit = 'Interc')

## GLM interaction between Education Attainment and Number of Close Friends

glm4 <- glm(happy ~ eduail1 + inprdsc + happy*inprdsc, family = gaussian, data = israel_clean, weights = round(weight))
## Warning in model.matrix.default(mt, mf, contrasts): the response appeared on
## the right-hand side and was dropped
## Warning in model.matrix.default(mt, mf, contrasts): problem with term 3 in
## model.matrix: no columns are assigned
modelsummary(
  list(glm1, glm2, glm4),
  fmt = 1,
  estimate  = c( "{estimate} ({std.error}){stars}",
                "{estimate} ({std.error}){stars}",
                "{estimate} ({std.error}){stars}"),
  statistic = NULL,
  coef_omit = "Intercept")
 (1)   (2)   (3)
eduail1Post-Secondary School (Non-Tetiary) or BA 0.8 (0.7) 0.7 (0.7)
eduail1MA or PHD 1.4 (1.2) 1.2 (1.3)
inprdsc4-6 friends 0.8 (0.7) −6.7 (3.8)+
inprdsc7 or more friends 1.0 (1.1) −5.0 (8.0)
happy × inprdsc4-6 friends 1.0 (0.5)*
happy × inprdsc7 or more friends 0.7 (1.0)
Num.Obs. 43 43 43
R2 −0.030 −0.043 −2.948
AIC 184.9 185.3 185.6
BIC 191.9 192.4 199.7
Log.Lik. −88.426 −88.665 −84.788
RMSE 1.95 1.96 1.74

GLM interaction between Education Attainment and Income Decile

glm5 <- glm(happy ~ eduail1 + hinctnta + happy*hinctnta, family = gaussian, data = israel_clean, weights = round(weight))
## Warning in model.matrix.default(mt, mf, contrasts): the response appeared on
## the right-hand side and was dropped
## Warning in model.matrix.default(mt, mf, contrasts): problem with term 3 in
## model.matrix: no columns are assigned
modelsummary(
  list(glm1, glm3, glm5),
  fmt = 1,
  estimate  = c( "{estimate} ({std.error}){stars}",
                "{estimate} ({std.error}){stars}",
                "{estimate} ({std.error}){stars}"),
  statistic = NULL,
  coef_omit = "Intercept")
 (1)   (2)   (3)
eduail1Post-Secondary School (Non-Tetiary) or BA 0.8 (0.7) 0.5 (0.6)
eduail1MA or PHD 1.4 (1.2) 0.2 (1.0)
hinctnta4th-6th Deciles 1.1 (0.8) −6.0 (1.9)**
hinctnta7th-10th Deciles 1.8 (0.7)* −7.3 (3.4)*
happy × hinctnta4th-6th Deciles 1.0 (0.3)***
happy × hinctnta7th-10th Deciles 1.1 (0.4)**
Num.Obs. 43 43 43
R2 −0.030 −0.064 −9.188
AIC 184.9 181.2 167.0
BIC 191.9 188.3 181.0
Log.Lik. −88.426 −86.614 −75.479
RMSE 1.95 1.98 1.39

Getting the equation output (TO BE CONFIRMED)

# 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]
## cli      (3.6.1  -> 3.6.2 ) [CRAN]
## sass     (0.4.7  -> 0.4.8 ) [CRAN]
## jsonlite (1.8.7  -> 1.8.8 ) [CRAN]
## later    (1.3.1  -> 1.3.2 ) [CRAN]
## vctrs    (0.6.4  -> 0.6.5 ) [CRAN]
## fansi    (1.0.5  -> 1.0.6 ) [CRAN]
## stringi  (1.8.2  -> 1.8.3 ) [CRAN]
## dplyr    (1.1.3  -> 1.1.4 ) [CRAN]
## yaml     (2.3.7  -> 2.3.8 ) [CRAN]
## httpuv   (1.6.12 -> 1.6.13) [CRAN]
## Installing 12 packages: xfun, rlang, cli, sass, jsonlite, later, vctrs, fansi, stringi, dplyr, yaml, httpuv
## 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'
## package 'cli' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'cli'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\yi en\AppData\Local\R\win-library\4.2\00LOCK\cli\libs\x64\cli.dll to
## C:\Users\yi en\AppData\Local\R\win-library\4.2\cli\libs\x64\cli.dll: Permission
## denied
## Warning: restored 'cli'
## package 'sass' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'sass'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\yi en\AppData\Local\R\win-library\4.2\00LOCK\sass\libs\x64\sass.dll to
## C:\Users\yi en\AppData\Local\R\win-library\4.2\sass\libs\x64\sass.dll:
## Permission denied
## Warning: restored 'sass'
## package 'jsonlite' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'jsonlite'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\yi
## en\AppData\Local\R\win-library\4.2\00LOCK\jsonlite\libs\x64\jsonlite.dll to
## C:\Users\yi en\AppData\Local\R\win-library\4.2\jsonlite\libs\x64\jsonlite.dll:
## Permission denied
## Warning: restored 'jsonlite'
## package 'later' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'later'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\yi en\AppData\Local\R\win-library\4.2\00LOCK\later\libs\x64\later.dll
## to C:\Users\yi en\AppData\Local\R\win-library\4.2\later\libs\x64\later.dll:
## Permission denied
## Warning: restored 'later'
## package 'vctrs' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'vctrs'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\yi en\AppData\Local\R\win-library\4.2\00LOCK\vctrs\libs\x64\vctrs.dll
## to C:\Users\yi en\AppData\Local\R\win-library\4.2\vctrs\libs\x64\vctrs.dll:
## Permission denied
## Warning: restored 'vctrs'
## package 'fansi' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'fansi'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\yi en\AppData\Local\R\win-library\4.2\00LOCK\fansi\libs\x64\fansi.dll
## to C:\Users\yi en\AppData\Local\R\win-library\4.2\fansi\libs\x64\fansi.dll:
## Permission denied
## Warning: restored 'fansi'
## package 'stringi' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'stringi'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\yi
## en\AppData\Local\R\win-library\4.2\00LOCK\stringi\libs\x64\stringi.dll to
## C:\Users\yi en\AppData\Local\R\win-library\4.2\stringi\libs\x64\stringi.dll:
## Permission denied
## Warning: restored 'stringi'
## package 'yaml' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'yaml'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\yi en\AppData\Local\R\win-library\4.2\00LOCK\yaml\libs\x64\yaml.dll to
## C:\Users\yi en\AppData\Local\R\win-library\4.2\yaml\libs\x64\yaml.dll:
## Permission denied
## Warning: restored 'yaml'
## package 'httpuv' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'httpuv'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\yi
## en\AppData\Local\R\win-library\4.2\00LOCK\httpuv\libs\x64\httpuv.dll to
## C:\Users\yi en\AppData\Local\R\win-library\4.2\httpuv\libs\x64\httpuv.dll:
## Permission denied
## Warning: restored 'httpuv'
## 
## The downloaded binary packages are in
##  C:\Users\yi en\AppData\Local\Temp\RtmpcfdJeC\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\RtmpcfdJeC\remotes362c33d46076\datalorax-equatiomatic-39e8c3e/DESCRIPTION' ...
  
✔  checking for file 'C:\Users\yi en\AppData\Local\Temp\RtmpcfdJeC\remotes362c33d46076\datalorax-equatiomatic-39e8c3e/DESCRIPTION' (430ms)
## 
  
  
  
─  preparing 'equatiomatic': (2.5s)
##    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}} = 7.37 + 0.38(\operatorname{eduail1}_{\operatorname{Post-Secondary\ School\ (Non-Tetiary)\ or\ BA}}) + 0.41(\operatorname{eduail1}_{\operatorname{MA\ or\ PHD}}) + 0.25(\operatorname{inprdsc}_{\operatorname{4-6\ friends}}) + 0.44(\operatorname{inprdsc}_{\operatorname{7\ or\ more\ friends}}) \]