library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.1 ✔ readr 2.2.0
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.3 ✔ tibble 3.3.1
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.2
## ✔ purrr 1.2.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
library(readxl)
library(pastecs)
##
## Attaching package: 'pastecs'
##
## The following objects are masked from 'package:dplyr':
##
## first, last
##
## The following object is masked from 'package:tidyr':
##
## extract
library(dplyr)
select <- dplyr::select
library(psych)
##
## Attaching package: 'psych'
##
## The following objects are masked from 'package:ggplot2':
##
## %+%, alpha
Suspense<-read_xls("D7 Suspense Data.xls")
Suspense2026<- (Suspense[-1,])
#Suspense2026$`2024`<- parse_number(Suspense2026$`2024`)
#Suspense2026$`2022`<- parse_number(Suspense2026$`2022`)
#Suspense2026$`2020`<- parse_number(Suspense2026$`2020`)
#Suspense2026$`2018`<- parse_number(Suspense2026$`2018`)
#Suspense2026$`2016`<- parse_number(Suspense2026$`2016`)
#Suspense2026$VotedStatus<- parse_number(Suspense2026$VotedStatus)
#Didn't need to Parse any columns for data due to data being cleaned
D7Cor <- Suspense2026%>%select(VotedStatus,`2016`,`2018`,`2020`,`2022`,`2024`) %>%arrange(VotedStatus,`2016`,`2018`,`2020`,`2022`,`2024`)
cor(D7Cor)
## VotedStatus 2016 2018 2020 2022 2024
## VotedStatus 1.0000000 0.2396771 0.2875087 0.3152934 0.4035985 0.5282914
## 2016 0.2396771 1.0000000 0.5867286 0.4703121 0.3600546 0.2872361
## 2018 0.2875087 0.5867286 1.0000000 0.5442521 0.4255316 0.2988541
## 2020 0.3152934 0.4703121 0.5442521 1.0000000 0.4431008 0.3641353
## 2022 0.4035985 0.3600546 0.4255316 0.4431008 1.0000000 0.4494039
## 2024 0.5282914 0.2872361 0.2988541 0.3641353 0.4494039 1.0000000
pairs(D7Cor)
# Returns correlation coefficients, sample sizes, and p-values
corr.test(D7Cor)$p
## VotedStatus 2016 2018 2020
## VotedStatus 0.000000e+00 1.052755e-82 2.996856e-119 3.585056e-144
## 2016 1.052755e-82 0.000000e+00 0.000000e+00 0.000000e+00
## 2018 9.989521e-120 0.000000e+00 0.000000e+00 0.000000e+00
## 2020 7.170113e-145 0.000000e+00 0.000000e+00 0.000000e+00
## 2022 1.454874e-244 1.918109e-191 1.649023e-274 3.724948e-300
## 2024 0.000000e+00 1.708453e-119 1.166208e-129 4.409776e-196
## 2022 2024
## VotedStatus 1.163899e-243 0.000000e+00
## 2016 1.150865e-190 3.416906e-119
## 2018 1.484121e-273 4.664834e-129
## 2020 3.724948e-299 3.086843e-195
## 2022 0.000000e+00 1.087324e-308
## 2024 9.884767e-310 0.000000e+00
round(corr.test(D7Cor)$p, 4)
## VotedStatus 2016 2018 2020 2022 2024
## VotedStatus 0 0 0 0 0 0
## 2016 0 0 0 0 0 0
## 2018 0 0 0 0 0 0
## 2020 0 0 0 0 0 0
## 2022 0 0 0 0 0 0
## 2024 0 0 0 0 0 0
D7model <- glm(VotedStatus ~ `2016` + `2018` + `2020` + `2022` + `2024`,
data = D7Cor, family = binomial)
D7_table <- summary(D7model)$coefficients
round(D7_table, 4)
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -2.9141 0.0673 -43.3097 0.0000
## `2016` -0.0845 0.1054 -0.8014 0.4229
## `2018` 0.3918 0.1083 3.6194 0.0003
## `2020` 0.4639 0.0955 4.8550 0.0000
## `2022` 0.8793 0.0931 9.4478 0.0000
## `2024` 2.2598 0.0818 27.6166 0.0000
df <- read_xls("D7 Suspense Data.xls")
# 2. Rename election year columns and map EthnicName codes to labels
df <- df %>%
mutate(
EthnicGroup = factor(
EthnicName,
levels = c(0, 1, 2, 3),
labels = c("White", "Latino", "Indian", "Asian")
)
)
# 3. Check unadjusted turnout & significance across ethnic groups (Chi-Square Test)
turnout_table <- table(df$EthnicGroup, df$VotedStatus)
chisq.test(turnout_table)
##
## Pearson's Chi-squared test
##
## data: turnout_table
## X-squared = 3.5524, df = 3, p-value = 0.314
# 4. Run Logistic Regression controlling for past voting history (White = baseline)
model_eth <- glm(
VotedStatus ~ EthnicGroup + `2024` + `2022` + `2020` + `2018` + `2016`,
data = df,
family = binomial
)
# View rounded coefficients, z-values, and p-values
round(summary(model_eth)$coefficients, 4)
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -3.1141 0.0849 -36.7010 0.0000
## EthnicGroupLatino 0.3180 0.0793 4.0131 0.0001
## EthnicGroupIndian 0.2573 0.4383 0.5871 0.5571
## EthnicGroupAsian 0.4340 0.2794 1.5533 0.1203
## `2024` 2.2569 0.0821 27.5063 0.0000
## `2022` 0.8820 0.0934 9.4447 0.0000
## `2020` 0.4806 0.0959 5.0091 0.0000
## `2018` 0.4122 0.1087 3.7914 0.0001
## `2016` -0.0661 0.1060 -0.6243 0.5324
# Test if EthnicGroup as a whole significantly improves the model
anova(model_eth, test = "Chisq")
## Analysis of Deviance Table
##
## Model: binomial, link: logit
##
## Response: VotedStatus
##
## Terms added sequentially (first to last)
##
##
## Df Deviance Resid. Df Resid. Dev Pr(>Chi)
## NULL 6276 6360.5
## EthnicGroup 3 3.56 6273 6357.0 0.3128850
## `2024` 1 1624.28 6272 4732.7 < 2.2e-16 ***
## `2022` 1 203.64 6271 4529.1 < 2.2e-16 ***
## `2020` 1 52.02 6270 4477.0 5.482e-13 ***
## `2018` 1 14.94 6269 4462.1 0.0001108 ***
## `2016` 1 0.39 6268 4461.7 0.5317949
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# 5. Calculate Voter Propensity Scores (0 to 1) and summarize by group
df$Propensity <- predict(model_eth, type = "response")
df %>%
group_by(EthnicGroup) %>%
summarise(
Voters = n(),
Actual_Turnout_Pct = round(mean(VotedStatus, na.rm = TRUE) * 100, 2),
Avg_Propensity_Pct = round(mean(Propensity, na.rm = TRUE) * 100, 2),
.groups = "drop"
)
## # A tibble: 4 × 4
## EthnicGroup Voters Actual_Turnout_Pct Avg_Propensity_Pct
## <fct> <int> <dbl> <dbl>
## 1 White 2785 19.4 19.4
## 2 Latino 3327 21.3 21.3
## 3 Indian 43 20.9 20.9
## 4 Asian 122 20.5 20.5
# 1. Individual p-values comparing each group against White voters (controlling for 2016-2024)
round(summary(model_eth)$coefficients, 4)
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -3.1141 0.0849 -36.7010 0.0000
## EthnicGroupLatino 0.3180 0.0793 4.0131 0.0001
## EthnicGroupIndian 0.2573 0.4383 0.5871 0.5571
## EthnicGroupAsian 0.4340 0.2794 1.5533 0.1203
## `2024` 2.2569 0.0821 27.5063 0.0000
## `2022` 0.8820 0.0934 9.4447 0.0000
## `2020` 0.4806 0.0959 5.0091 0.0000
## `2018` 0.4122 0.1087 3.7914 0.0001
## `2016` -0.0661 0.1060 -0.6243 0.5324
# 2. Overall p-value for EthnicGroup as a whole
anova(model_eth, test = "Chisq")
## Analysis of Deviance Table
##
## Model: binomial, link: logit
##
## Response: VotedStatus
##
## Terms added sequentially (first to last)
##
##
## Df Deviance Resid. Df Resid. Dev Pr(>Chi)
## NULL 6276 6360.5
## EthnicGroup 3 3.56 6273 6357.0 0.3128850
## `2024` 1 1624.28 6272 4732.7 < 2.2e-16 ***
## `2022` 1 203.64 6271 4529.1 < 2.2e-16 ***
## `2020` 1 52.02 6270 4477.0 5.482e-13 ***
## `2018` 1 14.94 6269 4462.1 0.0001108 ***
## `2016` 1 0.39 6268 4461.7 0.5317949
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Sex control
# 1. Format Sex as a factor with "F" (Female) as the reference baseline
df <- df %>%
mutate(
Sex = relevel(factor(Sex), ref = "F")
)
# 2. Fit the logistic regression controlling for EthnicGroup, Sex, and past elections
model_eth_sex <- glm(
VotedStatus ~ EthnicGroup + Sex + `2024` + `2022` + `2020` + `2018` + `2016`,
data = df,
family = binomial
)
# 3. View rounded coefficients, z-values, and p-values
round(summary(model_eth_sex)$coefficients, 4)
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -2.9890 0.0924 -32.3502 0.0000
## EthnicGroupLatino 0.3100 0.0794 3.9059 0.0001
## EthnicGroupIndian 0.2682 0.4382 0.6122 0.5404
## EthnicGroupAsian 0.4295 0.2800 1.5338 0.1251
## SexM -0.2454 0.0776 -3.1647 0.0016
## SexU -0.4844 0.6614 -0.7325 0.4639
## `2024` 2.2534 0.0821 27.4377 0.0000
## `2022` 0.8812 0.0934 9.4353 0.0000
## `2020` 0.4751 0.0960 4.9471 0.0000
## `2018` 0.4095 0.1088 3.7639 0.0002
## `2016` -0.0786 0.1061 -0.7403 0.4591
# 4. Run Likelihood Ratio Test (Type II/III equivalent) for each predictor
drop1(model_eth_sex, test = "Chisq")
## Single term deletions
##
## Model:
## VotedStatus ~ EthnicGroup + Sex + `2024` + `2022` + `2020` +
## `2018` + `2016`
## Df Deviance AIC LRT Pr(>Chi)
## <none> 4451.3 4473.3
## EthnicGroup 3 4467.6 4483.6 16.30 0.0009845 ***
## Sex 2 4461.7 4479.7 10.38 0.0055627 **
## `2024` 1 5260.3 5280.3 809.02 < 2.2e-16 ***
## `2022` 1 4538.9 4558.9 87.54 < 2.2e-16 ***
## `2020` 1 4475.4 4495.4 24.09 9.205e-07 ***
## `2018` 1 4465.4 4485.4 14.05 0.0001784 ***
## `2016` 1 4451.9 4471.9 0.55 0.4583391
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
df$Propensity_Sex <- predict(model_eth_sex, type = "response")
df %>%
filter(Sex %in% c("F", "M")) %>%
group_by(EthnicGroup, Sex) %>%
summarise(
Voters = n(),
Actual_Turnout_Pct = round(mean(VotedStatus) * 100, 2),
Avg_Propensity_Pct = round(mean(Propensity_Sex) * 100, 2),
.groups = "drop"
)
## # A tibble: 8 × 5
## EthnicGroup Sex Voters Actual_Turnout_Pct Avg_Propensity_Pct
## <fct> <fct> <int> <dbl> <dbl>
## 1 White F 1404 21.2 22.0
## 2 White M 1360 17.7 16.9
## 3 Latino F 1746 25.4 24.7
## 4 Latino M 1565 16.9 17.8
## 5 Indian F 21 28.6 21.2
## 6 Indian M 20 15 22.4
## 7 Asian F 66 18.2 23.1
## 8 Asian M 54 24.1 17.8
# 2. Controlled comparison: Predicted probability for a voter who ONLY voted in 2024
profile_grid <- expand.grid(
EthnicGroup = levels(df$EthnicGroup),
Sex = c("F", "M"),
`2024` = 1, `2022` = 0, `2020` = 0, `2018` = 0, `2016` = 0
)
profile_grid$Predicted_Prob_Pct <- round(
predict(model_eth_sex, newdata = profile_grid, type = "response") * 100, 2
)
profile_grid
## EthnicGroup Sex 2024 2022 2020 2018 2016 Predicted_Prob_Pct
## 1 White F 1 0 0 0 0 32.40
## 2 Latino F 1 0 0 0 0 39.52
## 3 Indian F 1 0 0 0 0 38.52
## 4 Asian F 1 0 0 0 0 42.41
## 5 White M 1 0 0 0 0 27.27
## 6 Latino M 1 0 0 0 0 33.83
## 7 Indian M 1 0 0 0 0 32.90
## 8 Asian M 1 0 0 0 0 36.55