# List of packages
packages <- c("infer", "fst", "modelsummary", "effects", "tidyr", "dplyr", "ggplot2") # 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)
## Loading required package: carData
## lattice theme set by effectsTheme()
## See ?effectsTheme for details.
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
## [[1]]
## [1] "infer" "stats" "graphics" "grDevices" "utils" "datasets"
## [7] "methods" "base"
##
## [[2]]
## [1] "fst" "infer" "stats" "graphics" "grDevices" "utils"
## [7] "datasets" "methods" "base"
##
## [[3]]
## [1] "modelsummary" "fst" "infer" "stats" "graphics"
## [6] "grDevices" "utils" "datasets" "methods" "base"
##
## [[4]]
## [1] "effects" "carData" "modelsummary" "fst" "infer"
## [6] "stats" "graphics" "grDevices" "utils" "datasets"
## [11] "methods" "base"
##
## [[5]]
## [1] "tidyr" "effects" "carData" "modelsummary" "fst"
## [6] "infer" "stats" "graphics" "grDevices" "utils"
## [11] "datasets" "methods" "base"
##
## [[6]]
## [1] "dplyr" "tidyr" "effects" "carData" "modelsummary"
## [6] "fst" "infer" "stats" "graphics" "grDevices"
## [11] "utils" "datasets" "methods" "base"
##
## [[7]]
## [1] "ggplot2" "dplyr" "tidyr" "effects" "carData"
## [6] "modelsummary" "fst" "infer" "stats" "graphics"
## [11] "grDevices" "utils" "datasets" "methods" "base"
ess <- read_fst("All-ESS-Data.fst")
uk_data <- ess %>%
filter(cntry == "GB") %>%
mutate(
age = ifelse(agea == 999, NA, agea),
polintr = ifelse(polintr %in% c(7, 8, 9), NA, polintr),
vote = ifelse(vote == 2, 0, ifelse(vote %in% c(3, 7, 8, 9), NA, vote)),
clsprty = case_when(
clsprty == 1 ~ 1, # Keep 1 as numeric
clsprty == 2 ~ 0, # Recode 2 as numeric 0
clsprty %in% c(7, 8, 9) ~ NA_real_, # Set 7, 8, 9 as NA keeping the type numeric
TRUE ~ clsprty ), # Keep all other values as they are
age_group = case_when(
age %in% 18:29 ~ "1",
age %in% 30:39 ~ "2",
age %in% 40:49 ~ "3",
age %in% 50:59 ~ "4",
age %in% 60:69 ~ "5",
age %in% 70:79 ~ "6",
age %in% 80:89 ~ "7",
age %in% 90:120 ~ "8",
TRUE ~ as.character(age) # Keeping other values as character if they do not fit the ranges
),
# Converting cohort to a factor with labels
age_group = factor(age_group,
levels = c("1", "2", "3", "4", "5", "6", "7", "8"),
labels = c("18-20s", "30s", "40s", "50s", "60s", "70s", "80s", "90s+"))
)
uk_data$clsprty <- as.factor(uk_data$clsprty)
model1 <- glm(vote ~ age_group, data = uk_data)
model2 <- glm(vote ~ age_group + clsprty, data = uk_data)
model3 <- glm(vote ~ age_group + clsprty + age_group*clsprty, data = uk_data)
modelsummary(
list(model1, model2, model3))
|
|
(1)
|
(2)
|
(3)
|
|
(Intercept)
|
0.460
|
0.395
|
0.374
|
|
|
(0.009)
|
(0.009)
|
(0.010)
|
|
age_group30s
|
0.162
|
0.143
|
0.140
|
|
|
(0.011)
|
(0.011)
|
(0.014)
|
|
age_group40s
|
0.278
|
0.246
|
0.266
|
|
|
(0.011)
|
(0.011)
|
(0.014)
|
|
age_group50s
|
0.324
|
0.281
|
0.310
|
|
|
(0.011)
|
(0.011)
|
(0.015)
|
|
age_group60s
|
0.382
|
0.324
|
0.366
|
|
|
(0.011)
|
(0.011)
|
(0.015)
|
|
age_group70s
|
0.413
|
0.341
|
0.385
|
|
|
(0.012)
|
(0.012)
|
(0.018)
|
|
age_group80s
|
0.403
|
0.331
|
0.397
|
|
|
(0.015)
|
(0.015)
|
(0.023)
|
|
age_group90s+
|
0.433
|
0.366
|
0.490
|
|
|
(0.036)
|
(0.036)
|
(0.062)
|
|
clsprty1
|
|
0.203
|
0.267
|
|
|
|
(0.006)
|
(0.018)
|
|
age_group30s × clsprty1
|
|
|
−0.005
|
|
|
|
|
(0.023)
|
|
age_group40s × clsprty1
|
|
|
−0.062
|
|
|
|
|
(0.023)
|
|
age_group50s × clsprty1
|
|
|
−0.079
|
|
|
|
|
(0.023)
|
|
age_group60s × clsprty1
|
|
|
−0.098
|
|
|
|
|
(0.023)
|
|
age_group70s × clsprty1
|
|
|
−0.099
|
|
|
|
|
(0.025)
|
|
age_group80s × clsprty1
|
|
|
−0.131
|
|
|
|
|
(0.031)
|
|
age_group90s+ × clsprty1
|
|
|
−0.215
|
|
|
|
|
(0.077)
|
|
Num.Obs.
|
19629
|
19499
|
19499
|
|
R2
|
0.089
|
0.139
|
0.142
|
|
AIC
|
21684.3
|
20438.0
|
20400.3
|
|
BIC
|
21755.3
|
20516.7
|
20534.3
|
|
Log.Lik.
|
−10833.148
|
−10208.977
|
−10183.170
|
|
RMSE
|
0.42
|
0.41
|
0.41
|
interaction_plot <- effect("age_group*clsprty", model3, na.rm=TRUE)
# We plot here the interaction term
plot(interaction_plot,
main="Interaction effect Age Group x close to party",
xlab="Age Group",
ylab="Vote")
