# List of packages
packages <- c("tidyverse", "infer", "fst", "modelsummary", "effects") # add any you need here
# 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.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: carData
##
## lattice theme set by effectsTheme()
## See ?effectsTheme for details.
## [[1]]
## [1] "lubridate" "forcats" "stringr" "dplyr" "purrr" "readr"
## [7] "tidyr" "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [13] "grDevices" "utils" "datasets" "methods" "base"
##
## [[2]]
## [1] "infer" "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [7] "readr" "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [13] "graphics" "grDevices" "utils" "datasets" "methods" "base"
##
## [[3]]
## [1] "fst" "infer" "lubridate" "forcats" "stringr" "dplyr"
## [7] "purrr" "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [13] "stats" "graphics" "grDevices" "utils" "datasets" "methods"
## [19] "base"
##
## [[4]]
## [1] "modelsummary" "fst" "infer" "lubridate" "forcats"
## [6] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [11] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [16] "grDevices" "utils" "datasets" "methods" "base"
##
## [[5]]
## [1] "effects" "carData" "modelsummary" "fst" "infer"
## [6] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [11] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [16] "stats" "graphics" "grDevices" "utils" "datasets"
## [21] "methods" "base"
ess <- read_fst("All-ESS-Data.fst")
israel_dataset <- ess[ess$cntry == "IL", ]
israel_clean <-israel_dataset %>%
select(eduail1, hinctnta, happy, essround) %>%
mutate(
happy = ifelse(happy %in% c(77, 88, 99), NA, happy),
eduail1 =ifelse(eduail1 %in% c(4444,5555,7777,8888),NA, eduail1),
hinctnta = ifelse(hinctnta %in% c(77, 88, 99), NA, hinctnta),
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(eduail1) & !is.na(hinctnta))
In what follows, we will filter to France as our country of interest and re-code a number of outcomes and covariates that can be subsequently used.
Similar to what we saw during the lecture, the first model is one predictor, the second is two predictors, and third incorporates an interaction. The idea explored here is that there is a generational decline in satisfaction with democracy, which further stratifies by educational attainment (BA v. no BA). This is debated, however. There are advocates, and others that say that it is exaggerated or misleading altogether.
The following simple model does not give credence to the stark generational decline idea. Pay attention to the coefficients, standard errors, and adjusted R-squared. The education covariate/predictor is here more interesting, but still to be interpreted with modesty. The interaction model does not add anything.
model1_Israel <- lm(happy ~ eduail1, data = israel_clean)
model2_Israel <- lm(happy ~ eduail1 + hinctnta, data = israel_clean)
model3_Israel <- lm(happy ~ eduail1 + hinctnta + eduail1*hinctnta, data = israel_clean)
modelsummary(
list(model1_Israel, model2_Israel, model3_Israel))
| (1) | (2) | (3) | |
|---|---|---|---|
| (Intercept) | 7.150 | 6.735 | 6.473 |
| (0.068) | (0.079) | (0.133) | |
| eduail1 | 0.058 | 0.020 | 0.057 |
| (0.008) | (0.008) | (0.017) | |
| hinctnta | 0.141 | 0.199 | |
| (0.014) | (0.028) | ||
| eduail1 × hinctnta | −0.007 | ||
| (0.003) | |||
| Num.Obs. | 3301 | 3301 | 3301 |
| R2 | 0.018 | 0.045 | 0.047 |
| R2 Adj. | 0.017 | 0.045 | 0.046 |
| AIC | 13694.3 | 13601.4 | 13597.4 |
| BIC | 13712.6 | 13625.8 | 13627.9 |
| Log.Lik. | −6844.149 | −6796.686 | −6793.718 |
| RMSE | 1.92 | 1.90 | 1.89 |
Note: if the outcome is binary (e.g., clsprty) it needs to be set to a numeric 0 and 1 and then use glm() instead of lm().
Adding “stars” – but remember, don’t become stargazers!
modelsummary(
list(model1_Israel, model2_Israel, model3_Israel),
fmt = 1,
estimate = c( "{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}"),
statistic = NULL,
coef_omit = "Intercept")
| (1) | (2) | (3) | |
|---|---|---|---|
| eduail1 | 0.1 (0.0)*** | 0.0 (0.0)* | 0.1 (0.0)** |
| hinctnta | 0.1 (0.0)*** | 0.2 (0.0)*** | |
| eduail1 × hinctnta | 0.0 (0.0)* | ||
| Num.Obs. | 3301 | 3301 | 3301 |
| R2 | 0.018 | 0.045 | 0.047 |
| R2 Adj. | 0.017 | 0.045 | 0.046 |
| AIC | 13694.3 | 13601.4 | 13597.4 |
| BIC | 13712.6 | 13625.8 | 13627.9 |
| Log.Lik. | −6844.149 | −6796.686 | −6793.718 |
| RMSE | 1.92 | 1.90 | 1.89 |
We can also visualize the interaction term by using the “effects” package. This will give us a better understanding of the differences in effects between someone who has at least a BA and someone who doe not have a BA across all four recoded generations.