# 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),
hinctnta = ifelse(hinctnta %in% c(77, 88, 99), NA, hinctnta),
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)),
vote_binary = ifelse(vote == 1, "Yes", "No"),
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
hinc_group = case_when(
hinctnta %in% 1 ~ "1",
hinctnta %in% 2 ~ "2",
hinctnta %in% 3 ~ "3",
hinctnta %in% 4 ~ "4",
hinctnta %in% 5 ~ "5",
hinctnta %in% 6 ~ "6",
hinctnta %in% 7 ~ "7",
hinctnta %in% 8 ~ "8",
hinctnta %in% 9 ~ "9",
hinctnta %in% 10 ~ "10",
TRUE ~ as.character(hinctnta) # Keeping other values as character if they do not fit the ranges
),
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, family = binomial, data = uk_data)
model2 <- glm(vote ~ age_group + clsprty, data = uk_data)
model3 <- glm(vote ~ age_group + clsprty + age_group*clsprty, data = uk_data)
model4 <- glm(vote ~ age + clsprty, data = uk_data)
model5 <- glm(vote ~ age + clsprty + age*clsprty, data = uk_data)
modelsummary(
list(model1, model2, model3, model4, model5))
| (1) | (2) | (3) | (4) | (5) | |
|---|---|---|---|---|---|
| (Intercept) | −0.158 | 0.395 | 0.374 | 0.327 | 0.243 |
| (0.041) | (0.009) | (0.010) | (0.009) | (0.012) | |
| age_group30s | 0.658 | 0.143 | 0.140 | ||
| (0.055) | (0.011) | (0.014) | |||
| age_group40s | 1.195 | 0.246 | 0.266 | ||
| (0.057) | (0.011) | (0.014) | |||
| age_group50s | 1.447 | 0.281 | 0.310 | ||
| (0.059) | (0.011) | (0.015) | |||
| age_group60s | 1.834 | 0.324 | 0.366 | ||
| (0.063) | (0.011) | (0.015) | |||
| age_group70s | 2.089 | 0.341 | 0.385 | ||
| (0.073) | (0.012) | (0.018) | |||
| age_group80s | 2.002 | 0.331 | 0.397 | ||
| (0.092) | (0.015) | (0.023) | |||
| age_group90s+ | 2.287 | 0.366 | 0.490 | ||
| (0.276) | (0.036) | (0.062) | |||
| clsprty1 | 0.203 | 0.267 | 0.204 | 0.379 | |
| (0.006) | (0.018) | (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) | |||||
| age | 0.006 | 0.008 | |||
| (0.000) | (0.000) | ||||
| age × clsprty1 | −0.003 | ||||
| (0.000) | |||||
| Num.Obs. | 19629 | 19499 | 19499 | 19574 | 19574 |
| R2 | 0.139 | 0.142 | 0.135 | 0.140 | |
| AIC | 20946.0 | 20438.0 | 20400.3 | 20735.9 | 20633.9 |
| BIC | 21009.0 | 20516.7 | 20534.3 | 20767.4 | 20673.3 |
| Log.Lik. | −10464.984 | −10208.977 | −10183.170 | −10363.948 | −10311.957 |
| RMSE | 0.42 | 0.41 | 0.41 | 0.41 | 0.41 |
exp(coef(model1))
## (Intercept) age_group30s age_group40s age_group50s age_group60s
## 0.853563 1.930177 3.304906 4.252449 6.259476
## age_group70s age_group80s age_group90s+
## 8.073177 7.401500 9.841101
exp(coef(model2))
## (Intercept) age_group30s age_group40s age_group50s age_group60s
## 1.484060 1.153899 1.278731 1.324274 1.383101
## age_group70s age_group80s age_group90s+ clsprty1
## 1.405953 1.392120 1.441401 1.225328
exp(coef(model3))
## (Intercept) age_group30s age_group40s
## 1.4534986 1.1497126 1.3044732
## age_group50s age_group60s age_group70s
## 1.3629075 1.4420817 1.4700344
## age_group80s age_group90s+ clsprty1
## 1.4879262 1.6317661 1.3060649
## age_group30s:clsprty1 age_group40s:clsprty1 age_group50s:clsprty1
## 0.9950927 0.9399068 0.9242494
## age_group60s:clsprty1 age_group70s:clsprty1 age_group80s:clsprty1
## 0.9062475 0.9058922 0.8770137
## age_group90s+:clsprty1
## 0.8066499
exp(coef(model4))
## (Intercept) age clsprty1
## 1.386690 1.005822 1.225773
exp(coef(model5))
## (Intercept) age clsprty1 age:clsprty1
## 1.2752318 1.0075924 1.4608574 0.9966101
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")
test_stat <- uk_data %>%
specify(explanatory = age, # change variable name for explanatory variable
response = vote) %>% # change variable name for outcome of interest
hypothesize(null = "independence") %>%
calculate(stat = "slope") # replace in between quotation marks appropriate test statistic
## Warning: Removed 1273 rows containing missing values.
## Message: The independence null hypothesis does not inform calculation of the
## observed statistic (a slope) and will be ignored.
print(test_stat$stat)
## age
## 0.00710142
null_distribution <- uk_data %>%
specify(explanatory = age,
response = vote) %>%
hypothesize(null = "independence") %>%
generate(reps = 100, type = "permute") %>% # only line we are adding, use reps = 1000 as standard
calculate(stat = "slope")
## Warning: Removed 1273 rows containing missing values.
p_val <- null_distribution %>% # replace name here if you assigned something other than null_distribution above
get_pvalue(obs_stat = test_stat, direction = "two-sided") # would only replace test_stat if assigned another name in Step 1
## 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_val
## # A tibble: 1 × 1
## p_value
## <dbl>
## 1 0
null_distribution %>%
visualize() +
shade_p_value(obs_stat = test_stat, direction = "two-sided")
tyr again
test_stat1 <- uk_data %>%
specify(explanatory = age_group, # change variable name for explanatory variable
response = vote_binary) %>% # change variable name for outcome of interest
hypothesize(null = "independence") %>%
calculate(stat = "Chisq") # replace in between quotation marks appropriate test statistic
## Warning: Removed 1350 rows containing missing values.
print(test_stat1$stat)
## X-squared
## 1750.151
null_distribution1 <- uk_data %>%
specify(explanatory = age_group,
response = vote_binary) %>%
hypothesize(null = "independence") %>%
generate(reps = 100, type = "permute") %>% # only line we are adding, use reps = 1000 as standard
calculate(stat = "Chisq")
## Warning: Removed 1350 rows containing missing values.
p_val <- null_distribution1 %>% # replace name here if you assigned something other than null_distribution above
get_pvalue(obs_stat = test_stat1, direction = "two-sided") # would only replace test_stat if assigned another name in Step 1
## 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_val
## # A tibble: 1 × 1
## p_value
## <dbl>
## 1 0
null_distribution1 %>%
visualize() +
shade_p_value(obs_stat = test_stat1, direction = "two-sided")
## Warning: Chi-Square usually corresponds to right-tailed tests. Proceed with
## caution.
equatiomatic::extract_eq(model3, use_coefs = TRUE)
\[ \widehat{E( \operatorname{vote} )} = 0.37 + 0.14(\operatorname{age\_group}_{\operatorname{30s}}) + 0.27(\operatorname{age\_group}_{\operatorname{40s}}) + 0.31(\operatorname{age\_group}_{\operatorname{50s}}) + 0.37(\operatorname{age\_group}_{\operatorname{60s}}) + 0.39(\operatorname{age\_group}_{\operatorname{70s}}) + 0.4(\operatorname{age\_group}_{\operatorname{80s}}) + 0.49(\operatorname{age\_group}_{\operatorname{90s+}}) + 0.27(\operatorname{clsprty}_{\operatorname{1}}) + 0(\operatorname{age\_group}_{\operatorname{30s}} \times \operatorname{clsprty}_{\operatorname{1}}) - 0.06(\operatorname{age\_group}_{\operatorname{40s}} \times \operatorname{clsprty}_{\operatorname{1}}) - 0.08(\operatorname{age\_group}_{\operatorname{50s}} \times \operatorname{clsprty}_{\operatorname{1}}) - 0.1(\operatorname{age\_group}_{\operatorname{60s}} \times \operatorname{clsprty}_{\operatorname{1}}) - 0.1(\operatorname{age\_group}_{\operatorname{70s}} \times \operatorname{clsprty}_{\operatorname{1}}) - 0.13(\operatorname{age\_group}_{\operatorname{80s}} \times \operatorname{clsprty}_{\operatorname{1}}) - 0.21(\operatorname{age\_group}_{\operatorname{90s+}} \times \operatorname{clsprty}_{\operatorname{1}}) \]