# 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)
## 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) = 3 > 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 '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 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
## Warning: package 'skimr' 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] "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")
## Warning: package 'fstcore' was built under R version 4.2.3
library(fst)
# Creating Israel's dataset
israel_dataset <- ess[ess$cntry == "IL", ]
# Creating Israel's dataset
table(israel_dataset$ipsuces)
##
## 1 2 3 4 5 6 7 8 9
## 4086 4859 2745 1359 694 154 312 427 274
# 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))
# 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)
# Before removing rows
str(israel_clean)
## 'data.frame': 4675 obs. of 13 variables:
## $ edlvail : num 2 7 2 6 7 10 8 9 2 2 ...
## $ hinctnta : num 3 8 6 2 2 3 10 8 10 9 ...
## $ happy : num NA 6 9 7 NA 6 8 8 8 7 ...
## $ essround : num 4 4 4 4 4 4 4 4 4 4 ...
## $ yrbrn : num 1944 1959 1969 1976 1959 ...
## $ sclmeet : num 2 5 6 5 7 7 5 6 6 5 ...
## $ weight : num NA NA NA NA NA NA NA NA NA NA ...
## $ health : num 3 2 1 1 NA NA 1 2 1 2 ...
## $ eduail1 : num NA NA NA NA NA NA NA NA NA NA ...
## $ stflife : num NA 6 8 6 NA 5 8 8 7 8 ...
## $ Education: chr "Education up to High School" "Post-Secondary School (Non-Tertiary) or BA" "Education up to High School" "Post-Secondary School (Non-Tertiary) or BA" ...
## $ edu_num : num 2 3 2 3 3 4 4 4 2 2 ...
## $ year : num 2008 2008 2008 2008 2008 ...
summary(israel_clean)
## edlvail hinctnta happy essround
## Min. : 0.000 Min. : 1.000 Min. : 5.000 Min. :4.00
## 1st Qu.: 2.000 1st Qu.: 3.000 1st Qu.: 7.000 1st Qu.:4.00
## Median : 3.000 Median : 4.000 Median : 8.000 Median :6.00
## Mean : 4.135 Mean : 4.868 Mean : 7.857 Mean :5.57
## 3rd Qu.: 7.000 3rd Qu.: 7.000 3rd Qu.: 9.000 3rd Qu.:7.00
## Max. :10.000 Max. :10.000 Max. :10.000 Max. :7.00
## NA's :2948 NA's :375
## yrbrn sclmeet weight health
## Min. :1915 Min. :1.000 Min. : 38.00 Min. :1.000
## 1st Qu.:1949 1st Qu.:4.000 1st Qu.: 62.00 1st Qu.:1.000
## Median :1965 Median :5.000 Median : 72.00 Median :2.000
## Mean :1964 Mean :4.951 Mean : 95.62 Mean :1.751
## 3rd Qu.:1980 3rd Qu.:6.000 3rd Qu.: 82.00 3rd Qu.:2.000
## Max. :2000 Max. :7.000 Max. :888.00 Max. :3.000
## NA's :20 NA's :3230 NA's :475
## eduail1 stflife Education edu_num
## Min. : 1.000 Min. : 5.000 Length:4675 Min. :1.000
## 1st Qu.: 4.000 1st Qu.: 7.000 Class :character 1st Qu.:2.000
## Median : 6.000 Median : 8.000 Mode :character Median :2.000
## Mean : 7.787 Mean : 7.817 Mean :2.487
## 3rd Qu.:12.000 3rd Qu.: 9.000 3rd Qu.:3.000
## Max. :17.000 Max. :10.000 Max. :4.000
## NA's :1727 NA's :456
## year
## Min. :2008
## 1st Qu.:2008
## Median :2012
## Mean :2011
## 3rd Qu.:2014
## Max. :2014
##
table(israel_clean$happiness, useNA = "always")
##
## <NA>
## 0
table(israel_clean$Education)
##
## Education up to High School
## 2542
## MA or PHD
## 459
## No primary education
## 158
## Post-Secondary School (Non-Tertiary) or BA
## 1516
table(israel_clean$happiness)
## < table of extent 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))
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()`).
# Cleaning dataset part2
israel_clean <- israel_clean %>%
mutate(
happiness = ifelse(happiness %in% c(77, 88, 99), NA, happiness),
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(happiness) & !is.na(hinctnta) & !is.na(health) & !is.na(sclmeet) & !is.na(stflife) & !is.na(Education), !is.na(edu_num))
table1 <- datasummary_skim(israel_clean %>% dplyr::select(edu_num, 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 |
happiness | 13 | 0 | 54.9 | 22.1 | 0.0 | 58.3 | 100.0 |
sclmeet | 7 | 0 | 5.1 | 1.4 | 1.0 | 5.0 | 7.0 |
# Create year variable
table(israel_clean$essround)
##
## 4 6 7
## 1355 1254 1156
israel_clean$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:10){
israel_clean$year[israel_clean$essround == i] <- replacements[i]
}
#Average Happiness Levels by Birth Year in Israel
happiness_bybirth_IL <- israel_clean %>%
group_by(yrbrn) %>%
summarize(mean_happyIL = mean(happiness, na.rm = TRUE)) %>%
filter(!is.na(yrbrn))
happiness_bybirth_IL
## # A tibble: 82 × 2
## yrbrn mean_happyIL
## <dbl> <dbl>
## 1 1917 66.7
## 2 1918 75
## 3 1921 60.4
## 4 1922 56.2
## 5 1923 58.3
## 6 1924 42.9
## 7 1925 57.1
## 8 1926 54.5
## 9 1927 52.6
## 10 1928 53.3
## # ℹ 72 more rows
# Visualisation
ggplot(happiness_bybirth_IL, aes(x = yrbrn, y = mean_happyIL)) +
geom_line(color = "skyblue", size = 1, breaks = seq(1910, 2000, by = 5)) + # Line to show the trend
geom_point(color = "orange", size = 2, breaks = seq(1910, 2000, by = 5)) + # Points to highlight each year's value
labs(title = " Mean Happiness Levels by Birth Year" ,
x = "Birth Year",
y = "Mean Happiness (0-100 scale)") +
ylim(0, 100) +
theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning in geom_line(color = "skyblue", size = 1, breaks = seq(1910, 2000, :
## Ignoring unknown parameters: `breaks`
## Warning in geom_point(color = "orange", size = 2, breaks = seq(1910, 2000, :
## Ignoring unknown parameters: `breaks`
# Zooming In
ggplot(happiness_bybirth_IL, aes(x = yrbrn, y = mean_happyIL)) +
geom_line(color = "skyblue", size = 1, breaks = seq(1910, 2000, by = 5)) + # Line to show the trend
geom_point(color = "orange", size = 2, breaks = seq(1910, 2000, by = 5)) +
labs(title = " Mean Happiness Levels by Birth Year" ,
x = "Birth Year",
y = "Mean Happiness (0-100 scale)") +
scale_x_continuous() +
ylim(30, 80) +
theme_minimal()
## Warning in geom_line(color = "skyblue", size = 1, breaks = seq(1910, 2000, : Ignoring unknown parameters: `breaks`
## Ignoring unknown parameters: `breaks`
# Mean Happiness Levels by Education Attainment
mean_edu_happy <- israel_clean %>%
group_by(edu_num) %>%
summarize(mean_happy_byedu = mean(happiness, na.rm = TRUE))
# Visualisation of Mean Happiness Levels By Education Levels
ggplot(mean_edu_happy, aes(x = edu_num, y = mean_happy_byedu)) +
geom_line(color = "skyblue", size = 1) + # Line to show the trend
geom_point(color = "orange", size = 3) + # Points to highlight each year's value
labs(title = "Mean Happiness Levels By Education Attainment (2008-2020)" ,
x = "Education Attainment",
y = "Mean Happiness (0-100 scale)") +
ylim(0,100) +
theme_minimal()
# Zoom in
ggplot(mean_edu_happy, aes(x = edu_num, y = mean_happy_byedu)) +
geom_line(color = "skyblue", size = 1) + # Line to show the trend
geom_point(color = "orange", size = 3) + # Points to highlight each year's value
labs(title = "Mean Happiness Levels By Education Attainment (2008-2020)" ,
x = "Education Attainment",
y = "Mean Happiness (0-100 scale)") +
ylim(50,60) +
theme_minimal()
# Against Average
mean_edu_happy %>%
ggplot(aes(x = edu_num, y = mean_happy_byedu)) +
geom_smooth(method = "loess", se = FALSE, color = "grey50") +
geom_point(aes(color = mean_happy_byedu), size = 2.5) +
scale_color_gradient(low = "darkblue", high = "lightblue") +
labs(title = "Average Happiness Levels by Education Attainment",
x = "Education Level", y = "Average Happiness Level (0-100)") +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
## : span too small. fewer data values than degrees of freedom.
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
## : pseudoinverse used at 0.985
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
## : neighborhood radius 2.015
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
## : reciprocal condition number 0
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
## : There are other near singularities as well. 4.0602
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")
))
# 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"
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"
test_stat <- israel_clean %>%
specify(explanatory = Education,
response = happiness) %>%
hypothesize(null = "independence") %>%
calculate(stat = "F")
print(test_stat$stat)
## [1] 0.7459508
null_distribution <- israel_clean %>%
specify(explanatory = Education,
response = happiness) %>%
hypothesize(null = "independence") %>%
generate(reps = 1000, type = "permute") %>%
calculate(stat = "f")
p_val <- null_distribution %>%
get_pvalue(obs_stat = test_stat, direction = "greater")
p_val
## # A tibble: 1 × 1
## p_value
## <dbl>
## 1 0.504
null_distribution %>%
visualize() +
shade_p_value(obs_stat = test_stat, direction = "greater")
# 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)