Setting Up

# List of packages
packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer", 
              "fst", "viridis", "knitr", "rmarkdown", "ggridges", "viridis", "questionr", "flextable", "infer","effects", "survey", "MASS", "dplyr", "htmltools", "skimr") 

# 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.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ 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: 'flextable'
## 
## 
## The following object is masked from 'package:purrr':
## 
##     compose
## 
## 
## Loading required package: carData
## 
## lattice theme set by effectsTheme()
## See ?effectsTheme for details.
## 
## Loading required package: grid
## 
## Loading required package: Matrix
## 
## 
## Attaching package: 'Matrix'
## 
## 
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
## 
## 
## Loading required package: survival
## 
## 
## Attaching package: 'survey'
## 
## 
## The following object is masked from 'package:graphics':
## 
##     dotchart
## 
## 
## 
## Attaching package: 'MASS'
## 
## 
## The following object is masked from 'package:dplyr':
## 
##     select
## [[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] "rmarkdown"    "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] "ggridges"     "rmarkdown"    "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"    "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"        
## 
## [[11]]
##  [1] "questionr"    "ggridges"     "rmarkdown"    "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] "flextable"    "questionr"    "ggridges"     "rmarkdown"    "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] "infer"        "flextable"    "questionr"    "ggridges"     "rmarkdown"   
##  [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] "effects"      "carData"      "infer"        "flextable"    "questionr"   
##  [6] "ggridges"     "rmarkdown"    "knitr"        "viridis"      "viridisLite" 
## [11] "fst"          "RColorBrewer" "modelsummary" "lubridate"    "forcats"     
## [16] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [21] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [26] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[15]]
##  [1] "survey"       "survival"     "Matrix"       "grid"         "effects"     
##  [6] "carData"      "infer"        "flextable"    "questionr"    "ggridges"    
## [11] "rmarkdown"    "knitr"        "viridis"      "viridisLite"  "fst"         
## [16] "RColorBrewer" "modelsummary" "lubridate"    "forcats"      "stringr"     
## [21] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [26] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [31] "utils"        "datasets"     "methods"      "base"        
## 
## [[16]]
##  [1] "MASS"         "survey"       "survival"     "Matrix"       "grid"        
##  [6] "effects"      "carData"      "infer"        "flextable"    "questionr"   
## [11] "ggridges"     "rmarkdown"    "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"    "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"        
## 
## [[18]]
##  [1] "htmltools"    "MASS"         "survey"       "survival"     "Matrix"      
##  [6] "grid"         "effects"      "carData"      "infer"        "flextable"   
## [11] "questionr"    "ggridges"     "rmarkdown"    "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] "skimr"        "htmltools"    "MASS"         "survey"       "survival"    
##  [6] "Matrix"       "grid"         "effects"      "carData"      "infer"       
## [11] "flextable"    "questionr"    "ggridges"     "rmarkdown"    "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")
library(fst)

Creating and Cleaning Israel Dataset

# Creating Israel's dataset
israel_dataset <- ess[ess$cntry == "IL", ]
# Selecting Israel's dataset
israel_clean <- israel_dataset %>%
  dplyr::select(edlvail, hinctnta, happy, essround, yrbrn, sclmeet, weight, health, eduail1, stflife) 
# Creating education variable 
israel_clean <- israel_clean %>%
  mutate(
    Education = case_when(
      essround > 5 & eduail1 == 1 ~ "No primary education",
      essround > 5 & eduail1 %in% 2:9 ~ "Education up to High School",
      essround > 5 & eduail1 %in% 10:13 ~ "Post-Secondary School (Non-Tertiary) or BA",
      essround > 5 & eduail1 %in% 14:17 ~ "MA or PHD",
      essround < 5 & edlvail == 0 ~ "No primary education",
      essround < 5 & edlvail %in% 1:5 ~ "Education up to High School",
      essround < 5 & edlvail %in% 6:7 ~ "Post-Secondary School (Non-Tertiary) or BA",
      essround < 5 & edlvail %in% 8:10 ~ "MA or PHD",
      TRUE ~ NA_character_
    )
  )
# Setting Education as numeric
israel_clean <- israel_clean %>%
  mutate(
    edu_num = case_when(
      essround > 5 & eduail1 == 1 ~ 1,   # No primary education
      essround > 5 & eduail1 %in% 2:9 ~ 2,   # Education up to High School
      essround > 5 & eduail1 %in% 10:13 ~ 3, # Post-Secondary School (Non-Tertiary) or BA
      essround > 5 & eduail1 %in% 14:17 ~ 4, # MA or PHD
      essround < 5 & edlvail == 0 ~ 1,       # No primary education
      essround < 5 & edlvail %in% 1:5 ~ 2,    # Education up to High School
      essround < 5 & edlvail %in% 6:7 ~ 3,    # Post-Secondary School (Non-Tertiary) or BA
      essround < 5 & edlvail %in% 8:10 ~ 4,   # MA or PHD
      TRUE ~ NA_real_                        # Assign NA for cases not covered above
    )
  )
# Cleaning dataset
israel_clean <- israel_clean %>%
  mutate(
    happy = ifelse(happy %in% c(77, 88, 99), NA, happy),
    hinctnta = ifelse(hinctnta %in% c(77, 88, 99), NA, hinctnta),
    health = ifelse(health %in% c(7, 8, 9), NA, health),  
    sclmeet = ifelse(sclmeet %in% c(77, 88, 99), NA, sclmeet),
    stflife = ifelse(stflife %in% c(77, 88, 99), NA, stflife),
    yrbrn = ifelse(yrbrn %in% c(7777, 8888, 9999), NA, yrbrn),
    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(hinctnta) & !is.na(health) & !is.na(sclmeet) & !is.na(stflife) & !is.na(Education), !is.na(edu_num))

##Finding what is missing in Happiness

table(israel_clean$happy, useNA = "always")
## 
##    0    1    2    3    4    5    6    7    8    9   10 <NA> 
##   29   23   65   86  172  417  410  707 1271  828  667    0
table(israel_clean$health, useNA = "always")
## 
##    1    2    3    4    5 <NA> 
## 1969 1308  923  368  107    0
# Creating and rescaling the happiness variable
israel_clean$happiness <- scales::rescale(israel_clean$happy + israel_clean$health + israel_clean$stflife, na.rm = TRUE, to = c(0, 100))
table(israel_clean$happiness, useNA = "always")
## 
##                0 4.16666666666667 8.33333333333333             12.5 
##                3                4                8               10 
## 16.6666666666667 20.8333333333333               25 29.1666666666667 
##               15               23               37               45 
## 33.3333333333333             37.5 41.6666666666667 45.8333333333333 
##               72               77              146              168 
##               50 54.1666666666667 58.3333333333333             62.5 
##              243              261              333              393 
## 66.6666666666667 70.8333333333333               75 79.1666666666667 
##              558              611              583              404 
## 83.3333333333333             87.5 91.6666666666667 95.8333333333333 
##              414              156               77               31 
##              100             <NA> 
##                3                0

Visualising happiness output

ggplot(israel_clean, aes(x = happiness)) +
  geom_histogram(bins = 30, fill = "blue", color = "black") +
  theme_minimal() +
  labs(title = "Histogram of Happiness Scale",
       x = "Happiness Scale",
       y = "Count")

Data Summary

table1 <- datasummary_skim(israel_clean  %>% dplyr::select(edu_num, hinctnta, happiness, sclmeet), output = "flextable")
## Warning: The histogram argument is only supported for (a) output types "default",
##   "html", "kableExtra", or "gt"; (b) writing to file paths with extensions
##   ".html", ".jpg", or ".png"; and (c) Rmarkdown, knitr or Quarto documents
##   compiled to PDF (via kableExtra)  or HTML (via kableExtra or gt). Use
##   `histogram=FALSE` to silence this warning.
table1

Unique (#)

Missing (%)

Mean

SD

Min

Median

Max

edu_num

4

0

2.5

0.7

1.0

2.0

4.0

hinctnta

10

0

4.9

2.6

1.0

4.0

10.0

happiness

25

0

65.7

15.5

0.0

66.7

100.0

sclmeet

7

0

5.0

1.5

1.0

5.0

7.0

Recode predictor variables

israel_clean <- israel_clean %>%
# Recoding Variables  
  mutate(
    sclmeet = factor(
      case_when(
        sclmeet == 1 ~ "Never",
        sclmeet %in% 2:4 ~ "Up to Several Times Per Month",
        sclmeet %in% 5:7 ~ "At least Once A Week",
        TRUE ~ as.character(sclmeet)  # Convert to character for consistency
      ),
      levels = c("Never", "Up to Several Times Per Month", "At least Once A Week")
    )) %>%
    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
      ),
  )) 

Scaling Output of Happiness

# Happiness scale (high levels of high self-reported happiness, good self-reported health levels, few worries about being a victim of crime, and high satisfaction with life as an indicator of happiness)

# Differentiating happiness values 
israel_clean$happy <- ifelse(israel_clean$happy < 5, NA, israel_clean$happy)
israel_clean$health <- ifelse(israel_clean$health > 3, NA, israel_clean$health)
israel_clean$stflife <- ifelse(israel_clean$stflife < 5, NA, israel_clean$stflife) 
# Creating and rescaling the happiness variable
israel_clean$happiness <- scales::rescale(israel_clean$happy + israel_clean$health + israel_clean$stflife, na.rm = TRUE, to = c(0, 100))

Visualising happiness output

ggplot(israel_clean, aes(x = happiness)) +
  geom_histogram(bins = 30, fill = "blue", color = "black") +
  theme_minimal() +
  labs(title = "Histogram of Happiness Scale",
       x = "Happiness Scale",
       y = "Count")
## Warning: Removed 910 rows containing non-finite values (`stat_bin()`).

Data Summary

table1 <- datasummary_skim(
  israel_clean %>% dplyr::select(edu_num, hinctnta, happiness, sclmeet),
  output = "flextable",
  na_summary = "hide"
)
## Warning: The histogram argument is only supported for (a) output types "default",
##   "html", "kableExtra", or "gt"; (b) writing to file paths with extensions
##   ".html", ".jpg", or ".png"; and (c) Rmarkdown, knitr or Quarto documents
##   compiled to PDF (via kableExtra)  or HTML (via kableExtra or gt). Use
##   `histogram=FALSE` to silence this warning.
table1

Unique (#)

Missing (%)

Mean

SD

Min

Median

Max

edu_num

4

0

2.5

0.7

1.0

2.0

4.0

happiness

14

19

54.9

22.1

0.0

58.3

100.0

Recode predictor variables

israel_clean <- israel_clean %>%
# Recoding Variables  
  mutate(
    sclmeet = factor(
      case_when(
        sclmeet == 1 ~ "Never",
        sclmeet %in% 2:4 ~ "Up to Several Times Per Month",
        sclmeet %in% 5:7 ~ "At least Once A Week",
        TRUE ~ as.character(sclmeet)  # Convert to character for consistency
      ),
      levels = c("Never", "Up to Several Times Per Month", "At least Once A Week")
    )) %>%
    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
      ),
  )) 
# Convert "Education" to factor with specified levels
israel_clean$Education <- factor(israel_clean$Education, levels = c(
  "No primary education",
  "Education up to High School",
  "Post-Secondary School (Non-Tertiary) or BA",
  "MA or PHD"
))

# Check levels again
levels(israel_clean$Education)
## [1] "No primary education"                      
## [2] "Education up to High School"               
## [3] "Post-Secondary School (Non-Tertiary) or BA"
## [4] "MA or PHD"

Double-checking that dataset is clean

levels(israel_clean$Education)
## [1] "No primary education"                      
## [2] "Education up to High School"               
## [3] "Post-Secondary School (Non-Tertiary) or BA"
## [4] "MA or PHD"
levels(israel_clean$sclmeet)
## [1] "Never"                         "Up to Several Times Per Month"
## [3] "At least Once A Week"
levels(israel_clean$hinctnta)
## [1] "1st-3rd Deciles"  "4th-6th Deciles"  "7th-10th Deciles"

Double-checking that dataset is clean#2

table(israel_clean$Education)
## 
##                       No primary education 
##                                        158 
##                Education up to High School 
##                                       2542 
## Post-Secondary School (Non-Tertiary) or BA 
##                                       1516 
##                                  MA or PHD 
##                                        459
table(israel_clean$happiness)
## 
##                0 8.33333333333333 16.6666666666667               25 
##               43               90              192              206 
## 33.3333333333333 41.6666666666667               50 58.3333333333333 
##              296              356              524              586 
## 66.6666666666667               75 83.3333333333333 91.6666666666667 
##              553              354              375              129 
##              100 
##               61

Calculating test statistic for education level

test_stat <- israel_clean %>%
  specify(explanatory = Education, 
          response = happiness) %>%
  hypothesize(null = "independence") %>%
  calculate(stat = "F")
## Warning: Removed 910 rows containing missing values.
print(test_stat$stat)
## [1] 0.7459508

Simulating the null distribution for education level

null_distribution <- israel_clean %>%
  specify(explanatory = Education,
          response = happiness) %>%
  hypothesize(null = "independence") %>%
  generate(reps = 1000, type = "permute") %>% 
  calculate(stat = "f")
## Warning: Removed 910 rows containing missing values.
##Error investigation
# Identify rows with missing values
rows_with_missing <- complete.cases(israel_clean)

# Count the number of rows with missing values
num_missing_rows <- sum(!rows_with_missing)

Calculating the p-value of sample for education level

p_val <- null_distribution %>% 
  get_pvalue(obs_stat = test_stat, direction = "greater")

p_val
## # A tibble: 1 × 1
##   p_value
##     <dbl>
## 1   0.531

Visualising for education level

null_distribution %>%
  visualize() +
  shade_p_value(obs_stat = test_stat, direction = "greater")

Calculating test statistic for frequency of social engagements

friend_stat <- israel_clean %>%
  specify(explanatory = sclmeet, 
          response = happiness) %>%
  hypothesize(null = "independence") %>%
  calculate(stat = "F")
## Warning: Removed 910 rows containing missing values.
print(friend_stat$stat)
## [1] 14.97584

Simulating the null distribution for frequency of social engagements

null_friend <- israel_clean %>%
  specify(explanatory = sclmeet,
          response = happiness) %>%
  hypothesize(null = "independence") %>%
  generate(reps = 1000, type = "permute") %>% 
  calculate(stat = "F")
## Warning: Removed 910 rows containing missing values.

Calculating the p-value of sample for frequency of social engagements

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 frequency of social engagements

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")
## Warning: Removed 375 rows containing missing values.
print(income_stat$stat)
## [1] 50.69403

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")
## Warning: Removed 375 rows containing missing values.

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 Frequency of Social Engagements)

model1 <- lm(happiness ~ Education, data = israel_clean)
modelfriend2 <- lm(happiness ~ Education + sclmeet, data = israel_clean)
modelfriend3 <- lm(happiness ~ Education + sclmeet + Education*sclmeet, data = israel_clean)
modelsummary(
  list(model1, modelfriend2, modelfriend3))

(1)

(2)

(3)

(Intercept)

54.663

58.522

69.048

(2.406)

(3.648)

(8.307)

EducationEducation up to High School

-0.243

-0.550

-14.502

(2.457)

(2.458)

(9.146)

EducationPost-Secondary School (Non-Tertiary) or BA

0.810

0.533

-7.937

(2.482)

(2.485)

(10.453)

EducationMA or PHD

0.940

0.455

-5.159

(2.642)

(2.646)

(15.167)

sclmeetUp to Several Times Per Month

-6.560

-15.861

(3.046)

(9.122)

sclmeetAt least Once A Week

-2.352

-15.559

(3.008)

(8.958)

EducationEducation up to High School × sclmeetUp to Several Times Per Month

12.988

(9.934)

EducationPost-Secondary School (Non-Tertiary) or BA × sclmeetUp to Several Times Per Month

6.652

(11.167)

EducationMA or PHD × sclmeetUp to Several Times Per Month

4.778

(15.780)

EducationEducation up to High School × sclmeetAt least Once A Week

16.606

(9.759)

EducationPost-Secondary School (Non-Tertiary) or BA × sclmeetAt least Once A Week

11.379

(11.001)

EducationMA or PHD × sclmeetAt least Once A Week

8.118

(15.583)

Num.Obs.

3765

3765

3765

R2

0.001

0.008

0.010

R2 Adj.

0.000

0.007

0.007

AIC

33984.5

33958.7

33966.8

BIC

34015.7

34002.4

34047.8

Log.Lik.

-16987.256

-16972.370

-16970.402

RMSE

22.04

21.96

21.94

## Adding Stars 
modelsummary(
  list(model1, modelfriend2, modelfriend3),
  fmt = 1,
  estimate  = c( "{estimate} ({std.error}){stars}",
                "{estimate} ({std.error}){stars}",
                "{estimate} ({std.error}){stars}"),
  statistic = NULL,
  coef_omit = "Intercept")

(1)

(2)

(3)

EducationEducation up to High School

-0.2 (2.5)

-0.5 (2.5)

-14.5 (9.1)

EducationPost-Secondary School (Non-Tertiary) or BA

0.8 (2.5)

0.5 (2.5)

-7.9 (10.5)

EducationMA or PHD

0.9 (2.6)

0.5 (2.6)

-5.2 (15.2)

sclmeetUp to Several Times Per Month

-6.6 (3.0)*

-15.9 (9.1)+

sclmeetAt least Once A Week

-2.4 (3.0)

-15.6 (9.0)+

EducationEducation up to High School × sclmeetUp to Several Times Per Month

13.0 (9.9)

EducationPost-Secondary School (Non-Tertiary) or BA × sclmeetUp to Several Times Per Month

6.7 (11.2)

EducationMA or PHD × sclmeetUp to Several Times Per Month

4.8 (15.8)

EducationEducation up to High School × sclmeetAt least Once A Week

16.6 (9.8)+

EducationPost-Secondary School (Non-Tertiary) or BA × sclmeetAt least Once A Week

11.4 (11.0)

EducationMA or PHD × sclmeetAt least Once A Week

8.1 (15.6)

Num.Obs.

3765

3765

3765

R2

0.001

0.008

0.010

R2 Adj.

0.000

0.007

0.007

AIC

33984.5

33958.7

33966.8

BIC

34015.7

34002.4

34047.8

Log.Lik.

-16987.256

-16972.370

-16970.402

RMSE

22.04

21.96

21.94

Model Checks

# set graphical parameters
par(mfrow = c(2,2)) # plot 2 X 2 matrix, by row

# plot diagnostics plots
plot(modelfriend3, pch = 19, col = scales::alpha("grey", .8), cex = .5)

Interaction Plot (Education Attainment and Frequency of Social Engagements)

edusocial_interaction <- effect("Education*sclmeet", modelfriend3, na.rm=TRUE)

plot(edusocial_interaction,
     main="Interaction effect Education Level and Frequency of Social Engagements",
     xlab="Education Attainment",
     ylab="Happiness Levels")

Modelling (Education Attainment and Income Decile)

model1 <- lm(happiness ~ Education, data = israel_clean)
modelinc2 <- lm(happiness ~ Education + hinctnta, data = israel_clean)
modelinc3 <- lm(happiness ~ Education + hinctnta + Education*hinctnta, data = israel_clean)
modelsummary(
  list(model1, modelinc2, modelinc3))

(1)

(2)

(3)

(Intercept)

54.663

54.438

51.716

(2.406)

(2.397)

(2.657)

EducationEducation up to High School

-0.243

-1.421

1.525

(2.457)

(2.462)

(2.764)

EducationPost-Secondary School (Non-Tertiary) or BA

0.810

-1.596

1.456

(2.482)

(2.518)

(2.975)

EducationMA or PHD

0.940

-2.528

-1.716

(2.642)

(2.698)

(4.183)

hinctnta4th-6th Deciles

0.517

14.356

(0.886)

(6.431)

hinctnta7th-10th Deciles

5.807

23.284

(0.953)

(15.721)

EducationEducation up to High School × hinctnta4th-6th Deciles

-14.155

(6.527)

EducationPost-Secondary School (Non-Tertiary) or BA × hinctnta4th-6th Deciles

-13.792

(6.643)

EducationMA or PHD × hinctnta4th-6th Deciles

-14.356

(7.487)

EducationEducation up to High School × hinctnta7th-10th Deciles

-17.988

(15.777)

EducationPost-Secondary School (Non-Tertiary) or BA × hinctnta7th-10th Deciles

-18.304

(15.806)

EducationMA or PHD × hinctnta7th-10th Deciles

-14.118

(16.110)

Num.Obs.

3765

3765

3765

R2

0.001

0.013

0.015

R2 Adj.

0.000

0.012

0.012

AIC

33984.5

33941.5

33944.4

BIC

34015.7

33985.1

34025.5

Log.Lik.

-16987.256

-16963.756

-16959.223

RMSE

22.04

21.90

21.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)

EducationEducation up to High School

-0.2 (2.5)

-1.4 (2.5)

1.5 (2.8)

EducationPost-Secondary School (Non-Tertiary) or BA

0.8 (2.5)

-1.6 (2.5)

1.5 (3.0)

EducationMA or PHD

0.9 (2.6)

-2.5 (2.7)

-1.7 (4.2)

hinctnta4th-6th Deciles

0.5 (0.9)

14.4 (6.4)*

hinctnta7th-10th Deciles

5.8 (1.0)***

23.3 (15.7)

EducationEducation up to High School × hinctnta4th-6th Deciles

-14.2 (6.5)*

EducationPost-Secondary School (Non-Tertiary) or BA × hinctnta4th-6th Deciles

-13.8 (6.6)*

EducationMA or PHD × hinctnta4th-6th Deciles

-14.4 (7.5)+

EducationEducation up to High School × hinctnta7th-10th Deciles

-18.0 (15.8)

EducationPost-Secondary School (Non-Tertiary) or BA × hinctnta7th-10th Deciles

-18.3 (15.8)

EducationMA or PHD × hinctnta7th-10th Deciles

-14.1 (16.1)

Num.Obs.

3765

3765

3765

R2

0.001

0.013

0.015

R2 Adj.

0.000

0.012

0.012

AIC

33984.5

33941.5

33944.4

BIC

34015.7

33985.1

34025.5

Log.Lik.

-16987.256

-16963.756

-16959.223

RMSE

22.04

21.90

21.88

Model Checks

# set graphical parameters
par(mfrow = c(2,2)) # plot 2 X 2 matrix, by row

# plot diagnostics plots
plot(modelinc3, pch = 19, col = scales::alpha("grey", .8), cex = .5)

Interaction Plot (Education Attainment and Income Decile)

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

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