setwd("/Users/raneenzamly/Desktop/tut files and data sets")
packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer",
"fst", "viridis", "knitr", "rmarkdown", "ggridges", "viridis", "questionr", "effects", "vctrs")
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.3 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ 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
##
## Loading required package: carData
##
## lattice theme set by effectsTheme()
## See ?effectsTheme for details.
##
##
## Attaching package: 'vctrs'
##
##
## The following object is masked from 'package:dplyr':
##
## data_frame
##
##
## The following object is masked from 'package:tibble':
##
## data_frame
## [[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] "effects" "carData" "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"
##
## [[13]]
## [1] "vctrs" "effects" "carData" "questionr" "ggridges"
## [6] "rmarkdown" "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"
ess <- read_fst("All-ESS-Data.fst")
netherlands_data <- ess %>%
filter(cntry == "NL") %>%
mutate(happy = ifelse(happy %in% c(77, 88, 99), NA, happy),
health = ifelse(health %in% c(7, 8, 9), NA, health),
rlgatnd = ifelse(rlgatnd %in% c(77, 88, 99), NA, rlgatnd),
rlgdgr = ifelse(rlgdgr %in% c(77, 88, 99), NA, rlgdgr))
datasummary_skim(netherlands_data %>% select(happy, health, rlgatnd, rlgdgr))
## 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.
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max |
|---|---|---|---|---|---|---|---|
happy | 12 | 0 | 7.8 | 1.4 | 0.0 | 8.0 | 10.0 |
health | 6 | 0 | 2.2 | 0.8 | 1.0 | 2.0 | 5.0 |
rlgatnd | 8 | 0 | 5.9 | 1.5 | 1.0 | 7.0 | 7.0 |
rlgdgr | 12 | 0 | 4.6 | 3.1 | 0.0 | 5.0 | 10.0 |
table(netherlands_data$happy)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 32 26 73 155 249 521 1023 3716 7624 3544 1339
netherlands_data$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:10){
netherlands_data$year[netherlands_data$essround == i] <- replacements[i]
}
happy_by_year <- netherlands_data %>%
group_by(year) %>%
filter(cntry == "NL") %>%
summarize(avg_happy = mean(happy, na.rm = TRUE))
happy_by_year
## # A tibble: 10 × 2
## year avg_happy
## <dbl> <dbl>
## 1 2002 7.79
## 2 2004 7.68
## 3 2006 7.64
## 4 2008 7.71
## 5 2010 7.79
## 6 2012 7.81
## 7 2014 7.78
## 8 2016 7.88
## 9 2018 8.02
## 10 2020 7.90
table(ess$essround)
##
## 1 2 3 4 5 6 7 8 9 10
## 42359 47537 43000 56752 52458 54673 40185 44387 49519 59685
ess$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:10){
ess$year[ess$essround == i] <- replacements[i]
}
ess$happy[ess$happy %in% c(77, 88, 99)] <- NA
avg_happy_netherlands <- netherlands_data %>%
group_by(year) %>%
summarize(mean_happy_netherlands = mean(happy, na.rm = TRUE))
avg_happy_all_countries <- ess %>%
group_by(year) %>%
summarize(mean_happy_all = mean(happy, na.rm = TRUE))
combined_data <- left_join(avg_happy_netherlands, avg_happy_all_countries, by = "year")
long_data <- combined_data %>%
pivot_longer(
cols = starts_with("mean_happy"),
names_to = "happy_Type",
values_to = "Mean_happy"
)
ggplot(long_data) +
geom_line(aes(x = year, y = Mean_happy, color = happy_Type), size = 1) +
geom_point(aes(x = year, y = Mean_happy, color = happy_Type), size = 3) +
labs(title = "Happiness in the Netherlands (2002-2020)",
x = "Year",
y = "Happiness (0-10 scale)") +
ylim(6.5, 8.5) +
scale_color_manual(values = c("orange", "pink"),
name = "Dataset",
labels = c("Netherlands", "All ESS"),
breaks = c("mean_happy_netherlands", "mean_happy_all")) +
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.
model1 <- lm(happy ~ rlgdgr, data = netherlands_data)
model2 <- lm(happy ~ rlgdgr + health, data = netherlands_data)
model3 <- lm(happy ~ rlgdgr + rlgatnd + health, data = netherlands_data)
model4 <- lm(happy ~ rlgdgr + rlgatnd + health*rlgdgr, data = netherlands_data)
modelsummary(
list(model1, model2, model3, model4))
| (1) | (2) | (3) | (4) |
|---|---|---|---|---|
(Intercept) | 7.738 | 8.869 | 9.107 | 9.200 |
(0.018) | (0.031) | (0.062) | (0.073) | |
rlgdgr | 0.013 | 0.022 | 0.013 | -0.007 |
(0.003) | (0.003) | (0.004) | (0.009) | |
health | -0.541 | -0.539 | -0.582 | |
(0.013) | (0.013) | (0.022) | ||
rlgatnd | -0.034 | -0.034 | ||
(0.008) | (0.008) | |||
rlgdgr × health | 0.009 | |||
(0.004) | ||||
Num.Obs. | 18247 | 18240 | 18228 | 18228 |
R2 | 0.001 | 0.093 | 0.094 | 0.094 |
R2 Adj. | 0.001 | 0.093 | 0.094 | 0.094 |
AIC | 63251.9 | 61461.3 | 61411.3 | 61407.5 |
BIC | 63275.4 | 61492.5 | 61450.3 | 61454.4 |
Log.Lik. | -31622.959 | -30726.632 | -30700.638 | -30697.744 |
RMSE | 1.37 | 1.30 | 1.30 | 1.30 |
# Recoding Health
netherlands_data <- netherlands_data %>%
mutate(
health = case_when(
lrscale %in% 1:2 ~ "Good",
lrscale %in% 3 ~ "Fair",
lrscale %in% 4:5 ~ "Bad",
lrscale %in% c(7, 8, 9) ~ NA_character_
),
)
model4 <- lm(happy ~ rlgdgr + health + rlgdgr*health, data = netherlands_data, weights = round(weight))
interaction_plot <- effect("rlgdgr*health", model4, na.rm=TRUE)
plot(interaction_plot,
main="Interaction effect",
xlab="Religiosity",
ylab="Happiness")
interaction_plot
##
## rlgdgr*health effect
## health
## rlgdgr Bad Fair Good
## 0 7.669270 7.472697 8.024343
## 2 7.702375 7.519635 7.872624
## 5 7.752034 7.590044 7.645046
## 8 7.801692 7.660452 7.417468
## 10 7.834797 7.707390 7.265749