---
title: "Zero-sum social identity, not zero-sum economic beliefs, explain voting preference in 2024 U.S. presidential election"
format:
  html:
    toc: true
    code-fold: true
    code-summary: "Show the code"
author:
  - name: Aashia Khan
    corresponding: true
    affiliations: 
      - Binghamton University
  - name: Zihan Hei
    orcid: 0009-0007-1617-2666
    affiliations:
      - Binghamton University
  - name: Jeff John
    affiliations:
      - Binghamton University
  - name: Shane McCarty
    orcid: 0000-0001-8930-7049
    corresponding: true
    affiliations:
      - Binghamton University
keywords:
  - zero sum beliefs
  - social identities
  - political affiliation
  - racial identity
abstract: |
  *Background:* Zero-sum beliefs—the perception that one group's gains necessarily result in another group's losses—are important predictors of political attitudes. However, the referents for zero-sum beliefs as economic or social identity remain underexplored in relation to political ideology, party affiliation, and voting behavior in contemporary elections. *Method:* We conducted a comprehensive analysis examining three dimensions of zero-sum beliefs (general, economic, and social identity). We conducted eleven Kruskal-Wallis tests on each of the zero-sum belief statements to investigate whether endorsement of zero-sum beliefs across multiple domains beliefs could be partially explained by political party affiliation and racial/ethnic identity. Subsequently, we examined whether zero-sum belief patterns -- compared with politicial beliefs, gender, racial identity, and competition beliefs -- are most associated with self-reported voting for Donald Trump versus Kamala Harris in the 2024 presidential election. *Results:* All eight zero-sum social identity belief items differed by political party affiliation. However, there was no effect for economic or general beliefs. Republican voters provided significantly higher endorsement of zero-sum social identity beliefs compared to Independents and Democrats. A logistic regression shows that political beliefs and zero-sum social identity beliefs are independently and significant explanatory variables for voting behavior in the 2024 presidential election. Specifically, more conservative political beliefs and higher endorsement of zero-sum social identity are associated with Trump support and more liberal political beliefs and lower zero-sum social identity beliefs are associated with Harris support. *Discussion:* Zero-sum social identity beliefs may represent a competitive core belief underlying contemporary political party affiliation and candidate preference. These findings affirm prior work that zero-sum thinking about economics differ from social identities, with similar levels of agreement on zero-sum economic beliefs across political parties but significantly different levels of agreement on zero-sum social identity beliefs by party affiliation. To the best of our knowledge, this study is the first to show that zero-sum thinking about social identities predicts voter preference in the 2024 election. Ultimately, future work needs to examine how to reduce zero-sum social identity thinking. 

plain-language-summary: |
  Many people hold "zero-sum beliefs" that when one group succeeds, another group must fail. This study examined how these beliefs relate to politics and voting in the 2024 presidential election. We found that while people across political parties hold similar zero-sum beliefs about economic issues, Republicans were much more likely than Democrats to hold zero-sum beliefs about social identities (such as undocumented vs. citizens, White vs. Black, LGBTQ vs. Religious). These social identity zero-sum beliefs were strong predictors of self-reported voting behavior: people with stronger zero-sum social identity beliefs were more likely to vote for Donald Trump, while those with weaker zero-sum social identity beliefs were more likely to vote for Kamala Harris. Post election discourse suggests the economy -- and not social identity -- was the primary driver of voters. Yet, our results show that zero-sum beliefs about economics didn't predict candidate preference. But, the social identity zero-sum beliefs were the second most important factor after accounting for political ideology as the primary predictor. This suggests that how people think about competition between different social groups is a key psychological factor driving political divisions and voting choices in America today.
  
bibliography: references.bib
csl: apa.csl
---

# Introduction

Zero-sum beliefs – the subjective view that as one person gains, others inevitably lose – have been linked to numerous beneficial and harmful social, political, and psychological outcomes (@andrews-fearonStatusZerosumGame2023). A zero-sum mindset can be conceptualized as a specific type of competitive belief regarding the relative distribution of limited resources (e.g., physical resources and social status) between two (or more) groups (e.g., racial groups). Although the origin of this mindset can be traced to inter-generational scarcity and high-competition environments for resources, other important factors may partially explain the adoption and centrality of zero-sum beliefs in the current U.S. political discourse.

In a seminal article, @nortonWhitesSeeRacism2011b demonstrates that White respondents' perceptions of anti-Black bias has decreased markedly since the 1950s while their perception of anti-White bias has increased significantly during that same period. A decade later, @rasmussenWhiteNotBlack2022 provide a conceptual replication of these findings and find that "Whites now believe that anti-White bias is more prevalent than anti-Black bias" (p. 1806). The line of research and related studies have led to the modern-day conception of zero-sum racial beliefs, which has been measured frequently using this item: "Less discrimination against minorities means more discrimination against Whites" (@davidaiPoliticsZerosumThinking2019b). @chinoyZerosumThinkingRoots2023 argue that zero-sum thinking is the root of political differences, demonstrating zero-sum beliefs partially explain a range of social and political views: pro-redistribution policy support, racial attitudes, gender attitudes, and anti-immigration attitudes beyond traditional sociodemographic measures of age, gender, race, income, and state of residence.

Zero-sum racial beliefs are a domain-specific belief about social groups, which differ from zero-sum economic beliefs. For example, people may hold beliefs that policies aimed at reducing income inequality unfairly disadvantage the wealthy, or that initiatives promoting equity give disadvantaged communities an ‘unearned’ advantage over advantaged individuals (@brownIfYouRise2022). A plethora of research lines show zero-sum domain-specific beliefs about economic groups and social groups, such as ethnic, citizenship, trade, and income (@chinoyZerosumThinkingRoots2023), as well as perceived competition between immigrants and native-born individuals (@essesImmigrationDilemmaRole2001). Though domain-specific beliefs can emerge across many areas, they are often most potent when tied to social identities, when political and interpersonal behavior is strongly influenced by perceived group competition.

## **Zero-Sum Social Identity Beliefs**

Many domain-specific zero-sum beliefs focus on social identity. For example, @wongLinkMensZerosum2017a found that men with zero-sum gender beliefs have poorer mental health outcomes, worse romantic relationships, and a more polarized view of domestic chores. On the other hand, according to @bolandZerosumBeliefsAvoidance2024a, zero-sum political beliefs can lead to a disinterest in political discussion because they lead individuals to antagonize other political ideologies and reduce openness to different ideas, leading to less cross-grup discussion and more polarization. In addition to examining one's actual standing as a member of a social group, understanding beliefs about one's own and other social groups may be key for addressing bias and assumptions about resource distribution.

## **Zero-Sum Economic Beliefs**

Zero-sum thinking takes on a unique form when viewed through a more hierarchical lens such as economics. @andrews-fearonStatusZerosumGame2023 posits that individuals who view the United States as more economically unequal were more likely to hold economic zero-sum beliefs. These individuals were therefore more likely to view the world as unjust. This falls in line with the aforementioned argument that zero-sum ideology often leads to prioritizing dominance and competition. On a more global scale, @hornborgCornucopiaZeroSumGame2003 explains dependency theory, a zero-sum economic framework that acknowledges how certain nations benefit at others expense. This theory claims that some nations exploit the resources of others, leading to the prosperity of the former while the latter suffers. Although economic zero-sum beliefs are much like other types of zero-sum thinking, they differ in that individuals belonging to one's own group are also thought of as competition. Both on a global and individual scale, the less wealthy are left competing with one another, rather than with the wealthy individuals or groups that oppress them.

## **Political Party Affiliation**

Political party affiliation is a key determinant of voter preference in elections. Several models aim to expain party affiliation and voting based on sociodemographics. For example, @nadeemChangingPartisanCoalitions2024 found that young people, women, and Black and Hispanic voters tend to be more Democratic leaning. Alternatively, White voters without 4 year degrees tend to lean Republican. However, this methodology struggles in that the social dynamics that determine the relationship between sociodemographics and political affiliation are subject to change. For example, a report by Pew Research Center ([2025](https://www.pewresearch.org/politics/2025/06/26/voting-patterns-in-the-2024-election/)) examines voting pattern shifts across the 2016, 2020, and 2024 Presidential elections by sociodemographic groups. While some groups tend to be more reliably associated with certain political parties at certain times, these relationships can change meaningfully in a few election cycles. In fact, such drastic shifts have led journalists and academics to speculate on whether a political realignment has taken shape in U.S. politics based on race, class, gender, education and their intersections [@barberCrucialRoleRace2024; @meyerDemocratsEmbraceDealignment2025]. However, many prior models have failed to capture the complex arrangements of sociodemographic variables and beliefs influencing voter preference. A ([2021](https://www.pewresearch.org/politics/2021/11/09/beyond-red-vs-blue-the-political-typology/%5D)) Pew Research Center report provided evidence of 9 different typology groups making up the Republican and Democrat coalitions in 2020. The 6 typology groups within the Republican coalition and 5 typology groups within the Democrat coalition hold very different views on racial bias and social groups, economics, climate change, and health, among others. Prior research demonstrates that zero-sum belief scores differ by political party affiliation, but there is significant variation *within* political parties (@davisZeroSumThinkingRoots2024), suggesting zero-sum beliefs might explain overall differences between parties and identification with specific factions within the Republican and Democrat coalition.

## Study Aims

The objective of the study is to: 1) examine domain-specific zero-sum beliefs: zero-sum economic beliefs and zero-sum social identity beliefs, 2) test whether zero-sum beliefs differ based on political party and/or racial identity, 3) test the association between sociodemographic variables and zero-sum beliefs in a logistic regression to explain voter preference for Donald Trump vs. Kamala Harris, and 4) use machine learning models to validate explanatory predict voter preference models.

# Methods

## Participants and Sampling

More than fifty-five thousand people who are active on the Prolific platform were eligible to complete a 45-minute health beliefs survey with measures on various beliefs associated with politics and health. Using a quota sample by gender (50% Man/ 50% Woman), political affiliation (33% Republican, 33% Democrat, and 34% Independent), and race/RACIALIDENTITY.4 (White 40%, Black 20%, Asian 20%, Mixed 10%, and Other 10%), Prolific recruited one hundred and twenty-five people to complete the survey.

A total of 135 individuals were recruited through Prolific, and 10 were excluded due to incomplete responses, yielding a final analytic sample of 125 participants. The final sample was roughly balanced in terms of gender (50.4% male), racial identity (White: 39.2%, Black: 22.4%, Asian: 18.4%, Mixed/Other: 17.6%), and political affiliation (Democrats: 34.4%, Independents: 32.0%, Republicans: 31.2%).

![Figure 1. Participant Flowchart showing exclusions for missing data and final analytic samples used in Kruskal_Wallis and Logistic Regression analyses](Participants_Flowchart.png){#fig-flowchart fig-align="center"}

## Measures

### Gender

Respondents reported their gender using the following options: Girl or woman, boy or man, nonbinary/genderfluid/genderqueer, I am not sure/questioning).

### Racial Identity

Respondents reported their racial identity using the following options: American Indian or Alaska Native, Asian, Black or African American, Hispanic or Latine, Middle Eastern or North African, Native Hawaiian/Pacific Islander, White, Other).

### Subjective Social Status

Respondents reported their subjective social status using MacArthur's Ladder with the following options Lowest 1 to Highest 10).

### Political Beliefs

Respondents reported their political beliefs using the following options: far left/leftist, very liberal, liberal, moderate, conservative, very conservative, alt-right/far-right.

### Education

Respondents reported their highest educational attainment using the following options: Some high school or less, High school diploma or GED, Some college, but no degree, Associates or technical degree, Bachelor’s degree, Graduate or professional degree (MA, MS, MBA, PhD, JD, MD, DDS).

### Political Party Affiliation

Respondents reported their political party affilaition using the following options: Conservative Party, Democratic Party, Libertarian Party, Republican Party, Socialist or Green Party.

### Self-Reported Voting in 2024 U.S. Presidential Election

Respondents reported their voter preference in the 2024 voting behavior using the following selections: Donald Trump, Kamala Harris, Jill Stein, Robert Kennedy Jr., Chase Oliver, Claudia De La Cruz, Cornel West, and DID NOT VOTE IN 2024.

### Zero-Sum Beliefs

Respondents were asked to report their level of agreement using a 7-point Likert scale (1: Strongly Disbelieve, 2: Disbelieve, 3: Somewhat Disbelieve, 4: Neither, 5: Somewhat Believe, 6: Believe, 7: Strongly Believe) with .

Respondents were asked to report their level of agreement to 11 zero-sum belief statements using items from two measures and unvalidated, self-generated statements on a 7-point Likert scale (1: Strongly Disbelieve, 2: Disbelieve, 3: Somewhat Disbelieve, 4: Neither Believe Nor Disbelieve, 5: Somewhat Believe, 6: Believe, 7: Strongly Believe). The first three items were selected from the *Belief in a Zero Sum Game* (BZSG) scale: 1) "Life is so devised that when somebody gains, others have to lose"; 2) "When some people are getting poorer, it means that other people are getting richer"; and 3) "The wealth of a few is acquired at the expense of many" ([@wojciszkeWiaraZycieJako2009; @rozycka-tranBeliefZeroSumGame2015]). An additional item was selected from a validated measure capturing beliefs about social identities: 4) "As women face less sexism, men end up facing more sexism" (@wilkinsYouCanWin2015). Using this sentence construction to examine a gain and loss for two specific groups, seven additional statement were created by the lead author with input from co-authors.

## Data Analysis Plan

Data was exported from the qualtrics platform in numerical format and imported into Posit Cloud. R code provided in the data science workflow (@wickhamWholeGameData2016) was modified to install R packages (see `install.R`), *import* data (`alldata.csv`) using `readr`, *transform* sociodemographic variables, such as gender identity (`GENDER`) to a binary variable (`GENDER_MAN`) using `dplyr`, *visualize* data in a raincloud plot using `ggplot2`, and *model* for inferential statistical tests from the `stats` package along with decision tree and random forest models from the `tidymodels` package. An exploratory factor analysis using promax rotation was used to examine the factor structure of the eleven items capturing zero sum beliefs.

We analyzed 11 zero-sum belief items (`ZEROSUM_`), covering both economic and social identity dimensions. Our primary goal was to assess differences across political affiliation (`POLITICALPARTY`) and racial/ethnic identity (`RACIALIDENTITY.4`).

First, we conducted a two-way Analysis of Variance (ANOVA) for each zero-sum belief item. To assess whether ANOVA assumptions were met, we used the Shapiro-Wilk test to evaluate the normality of residuals. All 11 items showed non-normality. However, given ANOVA's robustness to moderate deviations from normality, we further examined residuals using Q-Q plots (qqnorm and qqline) to visualize distributional shape. These plots indicated that the violations were not severe, so we proceeded with ANOVA while noting the assumption limitations.

To address the non-normality more formally and ensure result accuracy, we complemented the ANOVA with a non-parametric Kruskal-Wallis test for each zero-sum belief. This approach allowed us to evaluate group differences without relying on normality assumptions.

Next, an explanatory logistic regression was conducted to classy voter's preference for Donald Trump (1) vs. Kamala Harris (0) ( `TRUMPVOTE` ) using `POLITICALBELIEFS`, `ZEROSUM_ECONOMIC`, `ZEROSUM_IDENTITY`, `ZEROSUM_1`, `GENDER_MALE`, `RELIGIOUS_YES`, `RACE_BLACK`, `RACE_ASIAN`, `RACE_OTHER`, `EDUCATION_HIGH`, and `SOCIALSTATUS`.

Additionally, we used the `tidymodels` package in R to classify using a predictive modeling approach. Specifically, both decision tree and random forest classifiers were implemented to predict voter preference.

Using this integrative modeling framework, we provide both explanatory and predictive models for classifying voter preferences.

# Results

```{r}
#| label: run install scripts
#| echo: false
#| output: false
#| warning: false
#| error: false
#| results: false

# Run install.R to ensure packages are installed
source("install.R")
```

```{r}
#| label: run cleaning scripts
#| echo: false
#| output: false
#| warning: false
#| error: false
#| results: false
# Run clean.R to prepare data
source("clean.R")
```

```{r}
#| label: load library
#| echo: false
#| output: false
#| warning: false
#| error: false
#| results: false

library(ggplot2)
library(dplyr)
library(tidyr)
library(ggrain)  
library(rmarkdown)
library(readr)
library(patchwork)
library(codebookr)
library(dplyr, warn.conflicts = FALSE)
library(haven)
library(codebook)
library(rempsyc)
library(car)
library(knitr)
library(broom)
library(ggdist)
library(patchwork)
library(car)
library(dataMaid)
library(devtools)
library(MBESS)
library(apaTables)
library(ggpubr)
library(psych)
library(forcats)
library(corrplot)
library(kableExtra)
```

```{r}
#| label: import alldata
#| error: true
#| warning: true
alldata <- read.csv("data/alldata.csv")
```

```{r}
#| label: import select_data
#| error: true
#| warning: true
select_data <- read.csv("data/select_data.csv")
```

```{r}
#| label: import select data form clean.R file
#| error: true
#| warning: true
#str(select_data)
```

## Descriptive Statistics

```{r}
# Define Categorical Variables
categorical_vars <- c(
  "EDUCATION_LEVEL", "INCOME", "RELIGIOUS_Identity", "STREETRACE",
  "SEXUAL_IDENTITY", "POLITICALAFFIL", "VOTE_2024", "SERIOUS", "POLITICALPARTY",
  "SEX", "ETHNICITY", "Nationality", "Student.status", "Employment.status",
  "RACIALIDENTITY.6", "RACIALIDENTITY.4"
)

# Define Continuous Variables
continuous_vars <- c(
  "AGE", "SOCIALSTATUS", "POLITICALBELIEFS", "ATTENTION3",
  paste0("ZEROSUM_", 1:11),
  paste0("NEOLIB_", 1:3)
)
```

```{r}
#| label: Categorical-Frequencies-Statistics-Table
#| echo: false
#| message: false
#| warning: false

library(stringr)

levels_list <- list(
    EDUCATION_LEVEL = c(
      "Some high school or less",
      "High school diploma or GED",
      "Some college, but no degree",
      "Associates or technical degree",
      "Bachelor’s degree",
      "Graduate or professional degree"
    ),
    ETHNICITY = c(
      "Asian",
      "Black",
      "White",
      "Mixed/Other"
    ),
    Employment.status = c(
      "Part-Time",
      "Full-Time",
      "Unemployed (and job seeking)",
      "Not in paid work (e.g. homemaker', 'retired or disabled)",
      "Due to start a new job within the next month",
      "Other",
      "DATA_EXPIRED"
    ),
    INCOME = c(
      "Less than $25,000",
      "$25,000-$49,999",
      "$50,000-$74,999",
      "$75,000-$99,999",
      "$100,000-$149,999",
      "$150,000 or more",
      "Prefer not to say"
    ),
    Nationality = c(
      "United States"
    ),
    POLITICALAFFIL = c(
      "Conservative Party",
      "Democratic Party",
      "Libertarian Party",
      "Republican Party",
      "Socialist or Green Party",
      "None of the above"
    ),
    POLITICALPARTY = c(
      "Democrat",
      "Republican",
      "Independent"
    ),
    RACIALIDENTITY.4 = c(
      "Asian",
      "Black",
      "White",
      "Mixed/Other"
    ),
    RACIALIDENTITY.6 = c(
      "Asian",
      "Black",
      "White",
      "Latine",
      "Mixed",
      "Other"
    ),
    RELIGIOUS_Identity = c(
      "Christian", "Muslim", "Hindu", "Buddhist", "Jewish", "Folk Religion", "Other", "Religiously Unaffiliated", "Decline to answer"
    ),
    SERIOUS = c("No", "Not Sure", "Yes"),
    SEX = c("Female", "Male"),
    SEXUAL_IDENTITY = c(
      "Straight or heterosexual",
      "Gay or lesbian",
      "Bisexual, pansexual, or queer",
      "Asexual",
      "Not sure"
    ),
    STREETRACE = c(
      "Asian American",
      "Native American/American Indian",
      "White",
      "Latine",
      "Black",
      "Arab",
      "Mexican",
      "Some other race"
    ),
    Student.status = c(
      "Yes",
      "No",
      "DATA_EXPIRED"
    ),
    VOTE_2024 = c(
      "Donald Trump",
      "Kamala Harris",
      "Jill Stein",
      "Robert Kennedy Jr.",
      "Chase Oliver",
      "Claudia De La Cruz",
      "Cornel West",
      "DID NOT VOTE IN 2024"
    )
  )

categorical_table <- select_data %>%
  select(all_of(categorical_vars)) %>%
  pivot_longer(
    cols = everything(),
    names_to = "Variable",
    values_to = "Value"
  ) %>%
  mutate(Value = if_else(is.na(Value), "Missing", Value)) %>%
  group_by(Variable, Value) %>%
  summarise(n = n(), .groups = "drop") %>%
  group_by(Variable) %>%
  mutate(
    percent = paste0(round(100 * n / sum(n), 1), "%"),
    Value = factor(
      Value,
      levels = c(levels_list[[cur_group()$Variable]], "Missing")
    )
  ) %>%
  arrange(Variable, Value) %>%
  ungroup()


kable(
  categorical_table,
  col.names = c("Variable", "Category", "Count", "Percent"),
  caption = "Table 1. Frequencies and Percentages for Categorical Variables",
  booktabs = TRUE,
  row.names = FALSE,
  format = ifelse(knitr::is_latex_output(), "latex", "html")
) %>%
kable_styling(
  latex_options = c("hold_position", "scale_down"),
  bootstrap_options = c("striped", "condensed"),
  full_width = FALSE
  )

```

```{r}
#| label: Continuous Descriptive Statistics Table
#| echo: false
#| message: false
#| warning: false

desc_table_continuous <- select_data %>%
  select(all_of(continuous_vars)) %>%
  psych::describe() %>%
  tibble::rownames_to_column(var = "Variable") %>%
  select(Variable, n, mean, median, sd, min, max)

if (knitr::is_html_output()) {
  knitr::kable(
    desc_table_continuous,
    digits = 2,
    escape = TRUE,
    format = "html",
    caption = "Table 2. Descriptive Statistics Table for Continuous Variables"
  ) %>%
    kableExtra::kable_styling(bootstrap_options = c("striped", "hover", "condensed"))
} else if (knitr::is_latex_output()) {
  knitr::kable(
    desc_table_continuous,
    digits = 2,
    escape = TRUE,
    format = "latex",
    longtable = TRUE,
    caption = "Descriptive Statistics Table for Continuous Variables"
  ) %>%
    kableExtra::kable_styling(latex_options = "scale_down")
}
```

Table 2 reports the descriptive statistics (count, mean, median, sd, min, max) for all study variables. The sample consisted of `r nrow(select_data)` participants, with a mean age of `r round(desc_table_continuous$mean[desc_table_continuous$Variable == "AGE"], 2)` years (SD = `r round(desc_table_continuous$sd[desc_table_continuous$Variable == "AGE"], 2)`, range = `r desc_table_continuous$min[desc_table_continuous$Variable == "AGE"]`–`r desc_table_continuous$max[desc_table_continuous$Variable == "AGE"]`). Participants reported a mean social status of `r round(desc_table_continuous$mean[desc_table_continuous$Variable == "SOCIALSTATUS"], 2)` (SD = `r round(desc_table_continuous$sd[desc_table_continuous$Variable == "SOCIALSTATUS"], 2)`) on a 10-point scale.

Zero-sum belief items were rated on a 7-point scale with neutral in the middle. For Item 1, the average score was close to neutral (M = 3.92). Items 2 and 3 had higher average scores (M = 4.75 and M = 4.92, respectively), indicating general agreement. Items 4 through 11 showed lower average scores (ranging from 2.59 to 3.52), reflecting more disagreement than agreement with those statements.

```{r}
#| label: Average Rating per Zero Sum Item
#| fig-width: 8
#| fig-height: 5
#| fig-align: "center"
#| fig-cap: "Average ratings for each ZEROSUM item with 95% confidence intervals. Items 2 (“poor-rich”) and 3 (“wealthfew-many”) received the highest ratings. Item 1 (“gain-lose”) was rated moderately, while items 4 through 11 had the lowest average ratings."


# select and reshape the ZEROSUM items
zerosum_long <- select_data %>%
  select(starts_with("ZEROSUM_")) %>%
  pivot_longer(
    cols = everything(),
    names_to = "Item",
    values_to = "Score"
  )

# mean score per item
zerosum_summary <- zerosum_long %>%
  group_by(Item) %>%
  summarise(
    MeanScore = mean(Score, na.rm = TRUE),
    n = sum(!is.na(Score)),
    sd = sd(Score, na.rm = TRUE),
    se = sd / sqrt(n),                           
    CI_lower = MeanScore - qt(0.975, n - 1) * se, # Lower 95% CI
    CI_upper = MeanScore + qt(0.975, n - 1) * se, # Upper 95% CI
    .groups = "drop"
  ) %>%
  mutate(Item = factor(Item, levels = paste0("ZEROSUM_", 1:11)))


item_labels <- c(
  "gain-lose",
  "poor-rich",
  "wealthfew-many",
  "women-men",
  "minorities-whites",
  "trans-cis",
  "undoc-citizens",
  "paywomen-men",
  "LGBTQ-religious",
  "disabilities-non",
  "healthcare-private insurance"
)


ggplot(zerosum_summary, aes(x = Item, y = MeanScore)) +
  geom_pointrange(aes(ymin = CI_lower, ymax = CI_upper),
                  color = "#2c3e50", size = 0.8) +
  geom_line(aes(group = 1), color = "#3498db", linewidth = 1) +
  theme_minimal(base_size = 14) +
  scale_x_discrete(labels = item_labels) +
  labs(
    title = "Average Rating per ZEROSUM Item with 95% CI",
    x = "ZEROSUM Item",
    y = "Average Score"
  ) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

```

## Factor Analysis

### Factor Analysis of Zero-Sum Beliefs

The output below displays the **correlation matrix** of the *Zero-Sum Beliefs* items. Each cell represents the Pearson correlation between pairs of items. **ZEROSUM_1** is moderately positively correlated with most of the other zero-sum items (r = .092 - .462), suggesting a **general** zero-sum thinking. **ZEROSUM_2** and **ZEROSUM_3** also show a moderate positive correlation with each other (r = .479). Most notably, **ZEROSUM_4** through **ZEROSUM_11** are consistently and positively correlated, with moderately positive correlations (ranging from **0.46 to 0.68**), indicating a strong internal consistency among these items. This pattern supports the idea that these items are likely capturing a common latent construct.

```{r}
#| label: factor-analysis
#| fig.width: 6
#| fig.height: 4

library(psych)
# create data frame of ZEROSUM variables for factor analysis
df.ZEROSUM <- select_data[, c("ZEROSUM_1", "ZEROSUM_2", "ZEROSUM_3", "ZEROSUM_4", 
                                  "ZEROSUM_5", "ZEROSUM_6", "ZEROSUM_7", "ZEROSUM_8", 
                                  "ZEROSUM_9", "ZEROSUM_10", "ZEROSUM_11")]

# Or using dplyr to select variables
# zerosum_vars <- select_data %>% select(ZEROSUM_1:ZEROSUM_11)

# Check the correlation matrix first
cor_matrix <- cor(df.ZEROSUM, use = "complete.obs")

kable(
  round(cor_matrix, 2),
  caption = "Table 27. Correlation Matrix of Zero-Sum Belief Items",
  booktabs = TRUE
) %>%
  kable_styling(
    latex_options = c("hold_position", "scale_down"),
    full_width = FALSE
  )

# Determine number of factors using scree plot and parallel analysis
scree(df.ZEROSUM)
wrapped_fa_parallel <- paste(strwrap(capture.output(fa.parallel(df.ZEROSUM, fa = "fa")), width = 80), collapse = "\n")
cat(wrapped_fa_parallel, "\n")

# Run 2-factor factor analysis (adjust nfactors based on scree plot/parallel analysis)
fa_result <- fa(df.ZEROSUM, 
                nfactors = 2,  # adjust this number based on your analysis
                rotate = "promax",
                fm = "ml")  # maximum likelihood

# View results
print(fa_result)
fa_result$loadings

# Get factor scores
factor_scores <- fa_result$scores
```

The results of the factor analysis – an unsupervised machine learning technique – support a two factor model with a promax rotation. The first item loads equally on each factor and will not be included in the composite construction. Based on the items, we named the first factor as `ZEROSUM_ECONOMIC` and the second factor as `ZEROSUM_IDENTITY` to correspond with the two different referents of economic (e.g., wealth vs. poor) and social identity (e.g., racial minorities vs. white people), respectively. The IDENTITY factor showed high reliability, with a Cronbach’s alpha of 0.91. This means the items in the `ZEROSUM_IDENTITY` scale consistently measure the same social identity concept.

```{r}
#| label: factor-composite

select_data <- select_data %>%
  mutate(
    ZEROSUM_ECONOMIC = (ZEROSUM_2 + ZEROSUM_3)/2,
    ZEROSUM_IDENTITY = (ZEROSUM_4 + ZEROSUM_5 + ZEROSUM_6 + ZEROSUM_7 + ZEROSUM_8 + ZEROSUM_9 + ZEROSUM_10 + ZEROSUM_11)/8
  )
```

### Reliability of Zero Sum Social Identity Beliefs

```{r}
#| label: alpha-identity-tables
#| results: asis
#| echo: false

library(psych)
library(knitr)

alpha_identity <- psych::alpha(
  select_data[, c(
    "ZEROSUM_4", "ZEROSUM_5", "ZEROSUM_6",
    "ZEROSUM_7", "ZEROSUM_8", "ZEROSUM_9",
    "ZEROSUM_10", "ZEROSUM_11"
  )]
)

kable(
    as.data.frame(alpha_identity$total),
    caption = "Table 4. Overall Reliability Statistics (Cronbach’s Alpha)",
    digits = 3
)

ci_df <- data.frame(
  Method = "Feldt",
  Lower = alpha_identity$feldt$lower.ci$raw_alpha,
  Alpha = alpha_identity$feldt$alpha$raw_alpha,
  Upper = alpha_identity$feldt$upper.ci$raw_alpha
)

kable(
  ci_df,
  caption = "Table 5. 95% confidence boundaries",
  digits = 2,
  booktabs = TRUE
)


kable(
    alpha_identity$alpha.drop,
    caption = "Table 6. Reliability if an item is dropped",
    digits = 2
)

kable(
    alpha_identity$item.stats,
    caption = "Table 7. Item Statistics",
    digits = 2
)


kable(as.data.frame.matrix(alpha_identity$response.freq),
      digits = 2,
      caption = "Table 8. Non missing response frequency for each item")

```

### Reliability of Zero Sum Economic Beliefs

```{r}
#| label: alpha-economic-table

library(psych)

# Alpha for ZEROSUM_ECONOMIC (2 items)
alpha_economic <- psych::alpha(select_data[, c("ZEROSUM_2", "ZEROSUM_3")])

kable(
    as.data.frame(alpha_economic$total),
    caption = "Table 9. Overall Reliability Statistics (Cronbach’s Alpha)",
    digits = 3
)


ci_df_eco <- data.frame(
  Method = "Feldt",
  Lower = alpha_economic$feldt$lower.ci$raw_alpha,
  Alpha = alpha_economic$feldt$alpha$raw_alpha,
  Upper = alpha_economic$feldt$upper.ci$raw_alpha
)

kable(
  ci_df_eco,
  caption = "Table 10. 95% confidence boundaries",
  digits = 2,
  booktabs = TRUE
)


kable(
    alpha_economic$alpha.drop,
    caption = "Table 11. Reliability if an item is dropped",
    digits = 2
)

kable(
    alpha_economic$item.stats,
    caption = "Table 12. Item Statistics",
    digits = 2
)


kable(as.data.frame.matrix(alpha_economic$response.freq),
      digits = 2,
      caption = "Table 13. Non missing response frequency for each item")
```

The **zero-sum economic beliefs** factor showed lower reliability, with a Cronbach’s alpha of 0.65. A lower reliability is expected in a two-item measure.

### Factor Analysis of Neoliberal Mindset

```{r}
#| label: neolib-check-table
#| fig.width: 6
#| fig.height: 4

library(psych)

# create data frame of NEOLIB variables
df.NEOLIB <- select_data[, c("NEOLIB_1", "NEOLIB_2", "NEOLIB_3")]

# correlation matrix
cor_matrix_neolib <- cor(df.NEOLIB, use = "complete.obs")
kable(
  round(cor_matrix_neolib, 3),
  caption = "Table 14. Correlation Matrix for NEOLIB Scale",
  booktabs = TRUE
)

# Cronbach’s alpha for internal consistency
alpha_neolib <- psych::alpha(df.NEOLIB)

kable(
    as.data.frame(alpha_neolib$total),
    caption = "Table 15. Overall Reliability Statistics (Cronbach’s Alpha)",
    digits = 3
)

ci_df_neo <- data.frame(
  Method = "Feldt",
  Lower = alpha_neolib$feldt$lower.ci$raw_alpha,
  Alpha = alpha_neolib$feldt$alpha$raw_alpha,
  Upper = alpha_neolib$feldt$upper.ci$raw_alpha,
  r_bar = alpha_neolib$feldt$r.bar$raw_alpha
)

kable(
  ci_df_neo,
  caption = "Table 16. 95% confidence boundaries",
  digits = 2,
  booktabs = TRUE
)


kable(
    alpha_neolib$alpha.drop,
    caption = "Table 17. Reliability if an item is dropped",
    digits = 2
)

kable(
    alpha_neolib$item.stats,
    caption = "Table 18. Item Statistics",
    digits = 2
)


kable(as.data.frame.matrix(alpha_neolib$response.freq),
      digits = 2,
      caption = "Table 19. Non missing response frequency for each item")

# factor analysis (1 factor)
fa_neolib <- fa(df.NEOLIB,
                nfactors = 1,
                rotate = "none",
                fm = "ml")

kable(
  round(fa_neolib$loadings[1:3, 1, drop = FALSE], 3),
  caption = "Table 20. Factor Loadings for NEOLIB Scale",
  booktabs = TRUE
)

```

All 3 items load strongly on a single factor: `NEOLIB_2` and `NEOLIB_3` are especially strong (\~.80) `NEOLIB_1` is OK (.50 is acceptable for a 3–item scale)

```{r}
#| label: average NEOLIB score as COMPETITION_SCORE

select_data <- select_data %>%
  mutate(COMPETITION_SCORE = round(rowMeans(select(., NEOLIB_1, NEOLIB_2, NEOLIB_3)), 3))
```

## Inferential Tests

### Comparing Zero-Sum Beliefs

```{r}
#| label: Paired t-test comparing ZEROSUM_ECONOMIC and ZEROSUM_IDENTITY table
library(broom)
library(knitr)

t_test_zerosum <- t.test(select_data$ZEROSUM_ECONOMIC,
                         select_data$ZEROSUM_IDENTITY,
                         paired = TRUE)

# View results
kable(
  tidy(t_test_zerosum),
  caption = "Table 21. Paired t-Test Comparing ZEROSUM Economic and ZEROSUM Identity",
  digits = 16,
  booktabs = TRUE
)
```

There is a significant difference between participants' scores on ZEROSUM_ECONOMIC and ZEROSUM_IDENTITY. On average, participants rated economic zero-sum beliefs 1.75 points higher than identity zero-sum beliefs, 95% CI \[1.38, 2.12\].

```{r}
#| label: plot-comparing-ZEROSUM-ECONOMIC-and-ZEROSUM-IDENTITY

# Create long data with participant ID
paired_data_long <- select_data %>%
  select(ZEROSUM_ECONOMIC, ZEROSUM_IDENTITY) %>%
  filter(!is.na(ZEROSUM_ECONOMIC) & !is.na(ZEROSUM_IDENTITY)) %>%  # Remove missing pairs
  mutate(ID = row_number()) %>%
  pivot_longer(cols = c(ZEROSUM_ECONOMIC, ZEROSUM_IDENTITY), 
               names_to = "Composite", 
               values_to = "Score")

# Plot: boxplot + paired lines
ggplot(paired_data_long, aes(x = Composite, y = Score, group = ID)) +
  geom_boxplot(aes(group = Composite), width = 0.5, alpha = 0.3, fill = "white", outlier.shape = NA) +
  geom_line(color = "gray70", alpha = 0.6) +  
  geom_point(data = paired_data_long %>% 
             filter(Composite == "ZEROSUM_ECONOMIC"), 
             shape = 16, size = 3, color = "blue") +
  geom_point(data = paired_data_long %>% 
             filter(Composite == "ZEROSUM_IDENTITY"),
             shape = 17, size = 3, color = "red") +   
  labs(title = "Paired Differences: Economic vs. Identity Zero-Sum Beliefs",
       x = "Composite Type", y = "Score") +
  theme_minimal()

```

The final sample reflected racial diversity, with participants identifying as White (n = 48), Black (n = 25), Mixed or Other (n = 25), and Asian (n = 24).

```{r}
#| label: show the ETHNICITY

table(select_data$ETHNICITY)
## notice that the groups are unequal which is a problem for the ANOVA.
## TO DO: consider whether we should do a non-parametric approach

# SOURCE FOR DUMMY CODE
## https://stats.oarc.ucla.edu/other/mult-pkg/faq/general/faqwhat-is-dummy-coding/
## https://www.nathanwhudson.com/courses/methods/resources/10.%20Dummy%20Coding.pdf
## can you find an R specific example? 
```

```{r}
#select_data[, c("RACE", "RACIALIDENTITY.4", "ETHNICITY")]

## Race (RACIALIDENTITY.4) was derived from the RACE item. Ethnicity was taken directly from the ETHNICITY item. As expected, these variables were not mutually exclusive, some respondents who identified their race as Mixed/Other reported ethnicity as White. 

## So race and ethnicity maybe are not forced to be logically consistent in many people’s minds, I think that's not an error, just like “mixed / biracial” people have multiple identities, and “White” in ethnicity sometimes is interpreted as “White Hispanic” or simply culturally white.
```

```{r}
#| label: Cross-tab RACIALIDENTITY.4 vs. ETHNICITY

table(select_data$RACIALIDENTITY.4, select_data$ETHNICITY, useNA = "ifany")

```

```{r}
#| label: party colors for plots

party_colors <- c("Democrat" = "#0015BC", "Republican" = "#E9141D", "Independent" = "#732C7B")
```

### Zero-Sum Beliefs by Gender

```{r}
#| label: t-tests between zerosum_1 & gender table

# Run t-tests using the dummy variable
zerosum_1_gender <- t.test(ZEROSUM_1 ~ GENDER_MALE, data = select_data)

kable(
  data.frame(
    t = zerosum_1_gender$statistic,
    df = zerosum_1_gender$parameter,
    p_value = zerosum_1_gender$p.value,
    mean_group_0 = zerosum_1_gender$estimate[1],
    mean_group_1 = zerosum_1_gender$estimate[2],
    mean_difference = zerosum_1_gender$estimate[1] - zerosum_1_gender$estimate[2],
    CI_lower = zerosum_1_gender$conf.int[1],
    CI_upper = zerosum_1_gender$conf.int[2]
  ),
  caption = "Table 22. Independent Samples t-Test: ZEROSUM_1 by Gender",
  digits = 3,
  booktabs = TRUE
)
```

There is no significant difference in ZEROSUM_1 scores between men and women (*p* = .63). The difference in means is small and not statistically meaningful. The 95% CI (-0.48, 0.80) also includes zero, supporting the lack of difference.

```{r}
#| label: t-tests between ZEROSUM_ECONOMIC & gender table

ZEROSUM_ECONOMIC_gender <- t.test(ZEROSUM_ECONOMIC ~ GENDER_MALE, data = select_data)

kable(
  data.frame(
    t = ZEROSUM_ECONOMIC_gender$statistic,
    df = ZEROSUM_ECONOMIC_gender$parameter,
    p_value = ZEROSUM_ECONOMIC_gender$p.value,
    mean_group_0 = ZEROSUM_ECONOMIC_gender$estimate[1],
    mean_group_1 = ZEROSUM_ECONOMIC_gender$estimate[2],
    mean_difference = ZEROSUM_ECONOMIC_gender$estimate[1] - ZEROSUM_ECONOMIC_gender$estimate[2],
    CI_lower = ZEROSUM_ECONOMIC_gender$conf.int[1],
    CI_upper = ZEROSUM_ECONOMIC_gender$conf.int[2]
  ),
  caption = "Table 23. Independent Samples t-Test: ZEROSUM_ECONOMIC by Gender",
  digits = 3,
  booktabs = TRUE
)
```

There is no significant difference in ZEROSUM_ECONOMIC beliefs between men and women (*p* = .27). Although women’s mean score appears slightly higher, this difference is not statistically significant. The 95% CI (-0.21, 0.75) includes zero.

```{r}
#| label: t-tests between ZEROSUM_IDENTITY & gender table

ZEROSUM_IDENTITY_gender <- t.test(ZEROSUM_IDENTITY ~ GENDER_MALE, data = select_data)

kable(
  data.frame(
    t = ZEROSUM_IDENTITY_gender$statistic,
    df = ZEROSUM_IDENTITY_gender$parameter,
    p_value = ZEROSUM_IDENTITY_gender$p.value,
    mean_group_0 = ZEROSUM_IDENTITY_gender$estimate[1],
    mean_group_1 = ZEROSUM_IDENTITY_gender$estimate[2],
    mean_difference = ZEROSUM_IDENTITY_gender$estimate[1] - ZEROSUM_IDENTITY_gender$estimate[2],
    CI_lower = ZEROSUM_IDENTITY_gender$conf.int[1],
    CI_upper = ZEROSUM_IDENTITY_gender$conf.int[2]
  ),
  caption = "Table 24. Independent Samples t-Test: ZEROSUM_IDENTITY by Gender",
  digits = 3,
  booktabs = TRUE
)
```

There is no significant difference in ZEROSUM_IDENTITY beliefs between men and women (*p* = .92). The near-zero difference and very wide p-value indicate no group difference at all.

```{r}
#| label: boxplot for t-test
# Reshape the data to long format for easier plotting
long_data <- select_data %>%
  select(GENDER_MALE, ZEROSUM_1, ZEROSUM_ECONOMIC, ZEROSUM_IDENTITY) %>%
  mutate(GENDER = ifelse(GENDER_MALE == 1, "Man", "Woman")) %>%
  pivot_longer(cols = c(ZEROSUM_1, ZEROSUM_ECONOMIC, ZEROSUM_IDENTITY),
               names_to = "Variable",
               values_to = "Score")

# Create boxplots
ggplot(long_data, aes(x = GENDER, y = Score, fill = GENDER)) +
  geom_boxplot(width = 0.5, alpha = 0.7) +
  facet_wrap(~ Variable, scales = "free_y") +
  labs(title = "Gender Differences in Zero-Sum Beliefs",
       x = "Gender",
       y = "Score") +
  scale_y_continuous(limits = c(1, 7), breaks = 1:7) +
  theme_minimal() +
  theme(legend.position = "none")

```

Across all three variables (`ZEROSUM_1`, `ZEROSUM_ECONOMIC`, `ZEROSUM_IDENTITY`), gender is not associated with significant differences in zero-sum beliefs in our sample. As shown in the box plots, the median scores are nearly identical across genders, and the range from the median to the 3rd quartile (i.e., the upper half of the middle 50%) is also highly similar. The overall range of scores tends to span from approximately 1 to 7 for both female and male participants, further indicating that the distribution of zero-sum beliefs is comparable across gender groups.

### Zero-Sum Beliefs by Political Party Affiliation

#### Gain vs. Loss

*Do zero-sum beliefs regarding gains and losses differ by racial/ethnic identity and political party affiliation?*

Below we present descriptive statistics and visualizations for ZEROSUM_1 ("Life is so devised that when somebody gains, others have to lose"), examining how responses vary across racial identity and political affiliation.

```{r}
#| label: groupstats for ZERO_SUM1 
#| echo: false
#| message: false
#| warning: false

library(dplyr)
group_stats_1 <- select_data %>%
  group_by(POLITICALPARTY, RACIALIDENTITY.4) %>%
  summarise(
    mean_ZEROSUM_1 = mean(ZEROSUM_1, na.rm = TRUE),
    se = sd(ZEROSUM_1, na.rm = TRUE) / sqrt(n()),
    .groups = 'drop'
  )

# Check the structure
#str(group_stats_1)
```

```{r}
#| label: show groupstats for ZERO_SUM1 

head(group_stats_1)
```

```{r}
#| label: zerosum1 pointrange
#| fig-cap: "Mean agreement with the belief that “life is so devised that when somebody gains, others have to lose,” by political party and racial/ethnic identity."
#| fig-align: center
#| message: false
#| warning: false

# Define the zesty_four palette
zesty_four <- c("#E69F00", "#009E73", "#999999", "#CC79A7") 

# Create the plot with pointrange
plot.zerosum1 <- ggplot(group_stats_1, aes(POLITICALPARTY, mean_ZEROSUM_1)) +
  geom_pointrange(aes(color = RACIALIDENTITY.4, 
                      ymin = mean_ZEROSUM_1 - se,  # Use standard error instead of sd
                      ymax = mean_ZEROSUM_1 + se), # Use standard error instead of sd
                  position = position_dodge(0.3)) +
  theme_bw() +
  labs(title = "Life is so devised that when somebody gains, others have to lose.",
       x = "Political Affiliation",
       y = "Agreement with Zero Sum Beliefs",
       color = "RACIALIDENTITY.4") + 
  theme(
    plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
    axis.title = element_text(size = 10),
    axis.text = element_text(size = 8),
    legend.title = element_text(size = 10),
    legend.text = element_text(size = 8)
  ) +
  scale_y_continuous(
    breaks = 1:7,
    labels = c("1: Strongly Disbelieve", 
               "2: Disbelieve", 
               "3: Somewhat Disbelieve", 
               "4: Neither", 
               "5: Somewhat Believe", 
               "6: Believe", 
               "7: Strongly Believe"),
    limits = c(1, 7)  # Set the y-axis limits to match the scale
  )+
  scale_color_manual(values = zesty_four)

# Print and save the plots
print(plot.zerosum1)
ggsave("plots/plot1:gain-lose.png", 
       plot = plot.zerosum1, 
       width = 10, 
       height = 8, 
       dpi = 300)

```

This pointrange plot shows that Black respondents exhibit higher agreement with the belief that “life is so devised that when somebody gains, others have to lose” among Republicans and Democrats, highlighting group-based differences in zero-sum beliefs.

```{r}
#| label: ZEROSUM_1 average score with 95% CI

# ZEROSUM_1 average score with 95% CI
group_stats_1_avg <- select_data %>%
  group_by(POLITICALPARTY) %>%
  summarise(
    mean_ZEROSUM_1 = mean(ZEROSUM_1, na.rm = TRUE),
    se = sd(ZEROSUM_1, na.rm = TRUE) / sqrt(n()),
    n = n(),
    .groups = 'drop'
  ) %>%
  mutate(
    # Calculate 95% confidence interval using t-distribution
    t_value = qt(0.975, df = n - 1),  # 95% CI, two-tailed
    ci_lower = mean_ZEROSUM_1 - t_value * se,
    ci_upper = mean_ZEROSUM_1 + t_value * se
  )
```

*Do zero-sum beliefs regarding gains and losses differ by political party affiliation?*

```{r}
#| label: r plot.zerosum1_enhanced and Raincloud plot
#| fig-cap: "Distribution of agreement with 'life is so devised that when somebody gains, others have to lose' by political affiliation. White dots represent means; colored dots are individual responses."
#| fig-align: center

#| echo: true
#| message: false
#| warning: false

library(ggdist)
library(patchwork)

plot.zerosum1_raincloud <- select_data %>%
  filter(!is.na(ZEROSUM_1), !is.na(POLITICALPARTY)) %>%
  ggplot(aes(x = POLITICALPARTY, y = ZEROSUM_1, fill = POLITICALPARTY, color = POLITICALPARTY)) +
  stat_halfeye(
    adjust = 0.5,
    width = 0.6,
    .width = 0,
    justification = -0.2,
    point_colour = NA,
    alpha = 0.7
  ) +
  geom_boxplot(
    width = 0.15,
    outlier.shape = NA,
    alpha = 0.5
  ) +
  geom_point(
    size = 1.3,
    alpha = 0.3,
    position = position_jitter(
      seed = 1, width = 0.1
    )
  ) +
  stat_summary(
    fun = mean,
    geom = "point",
    size = 3,
    color = "white",
    stroke = 1.5,
    shape = 21
  ) +
  scale_fill_manual(values = party_colors) +
  scale_color_manual(values = party_colors) +
  theme_bw() +
  labs(
    title = "Life is so devised that when somebody gains, others have to lose.",
    x = "Political Affiliation",
    y = "Level of Agreement",
    caption = "White dots = means; colored dots = individual responses"
  ) +
  theme(
    plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
    plot.caption = element_text(size = 9, color = "gray40", hjust = 0.5),
    axis.title = element_text(size = 12, face = "bold"),
    axis.text = element_text(size = 10),
    legend.position = "none",
    panel.grid.major.y = element_line(color = "gray90", linewidth = 0.3)  # Fixed
  ) +
  scale_y_continuous(
    breaks = 1:7,
    labels = c("1: Strongly Disbelieve", 
               "2: Disbelieve", 
               "3: Somewhat Disbelieve", 
               "4: Neither", 
               "5: Somewhat Believe", 
               "6: Believe", 
               "7: Strongly Believe"),
    limits = c(1, 7) 
  )

plot.zerosum1_raincloud
```

This raincloud plot shows that mean agreement with the zero-sum belief (indicated by white dots) varies by political affiliation. Among the three groups, Republicans exhibit higher average agreement with the statement “Life is so devised that when somebody gains, others have to lose.” The distribution (via density and individual dots) suggests greater clustering of high agreement scores among partisans, reflecting perceived competition between social groups. The relatively narrow IQRs and tight clustering around high values also indicate consistent endorsement of this belief within parties.

```{r}
#| label: shapiro.test zerosum1 table

shapiro_zerosum1 <- select_data %>%
  group_by(RACIALIDENTITY.4) %>%
  summarise(p = shapiro.test(ZEROSUM_1)$p.value)

kable(
  shapiro_zerosum1,
  caption = "Table 25. Shapiro–Wilk Normality Test for ZEROSUM_1 by Racial Identity",
  digits = 4,
  booktabs = TRUE
)
## White, Mixed/Other (p<0.05) fail the normality assumption for ANOVA, maybe use a non-parametric test (Kruskal-Wallis)
```

```{r}
#| label: kable format ZEROSUM_1 Kruskal-Wallis
#| echo: true
#| message: true
#| warning: false

library(tibble)
library(rempsyc)
library(kableExtra)

kw.ZEROSUM_1.party <- kruskal.test(ZEROSUM_1 ~ POLITICALPARTY, data = select_data)

kw_table <- tibble(
  Predictor = c("POLITICALPARTY"),
  df = c(length(unique(select_data$POLITICALPARTY)) - 1),
  `Chi-squared` = c(round(kw.ZEROSUM_1.party$statistic, 2)),
  p = c(round(kw.ZEROSUM_1.party$p.value, 3))
)

if (knitr::is_html_output()) {
  kw_table %>%
    knitr::kable(
      digits = 3,
      escape = TRUE,
      format = "html",
      caption = "Table 26. Kruskal-Wallis Test Results for ZEROSUM_1"
    ) %>%
    kableExtra::kable_styling(bootstrap_options = c("striped", "hover", "condensed")) %>%
    kableExtra::row_spec(
      which(kw_table$p < 0.05), 
      background = "gray"
    )
} else if (knitr::is_latex_output()) {
  kw_table %>%
    knitr::kable(
      digits = 3,
      escape = TRUE,
      format = "latex",
      longtable = TRUE,
      caption = "Kruskal-Wallis Test Results for ZEROSUM\\_1"
    ) %>%
    kableExtra::kable_styling(full_width = FALSE, position = "left") %>%
    kableExtra::row_spec(
      which(kw_table$p < 0.05), 
      background = "gray!20"
    )
}
```

There was no significant difference in ZEROSUM_1 scores across political party groups, Kruskal-Wallis χ²(`r kw.ZEROSUM_1.party$parameter`) = `r round(kw.ZEROSUM_1.party$statistic, 2)`, *p* = `r signif(kw.ZEROSUM_1.party$p.value, 3)`.

#### Poor vs. Rich

*Do zero-sum beliefs regarding poor and rich differ by racial/ethnic identity and political party affiliation?*

Below we present descriptive statistics and visualizations for ZEROSUM_2 ("When some people are getting poorer, it means that other people are getting richer."), examining how responses vary across racial identity and political affiliation.

```{r}
#| label: groupstats for ZERO_SUM2 
#| echo: false
#| message: false
#| warning: false

library(dplyr)
group_stats_2 <- select_data %>%
  group_by(POLITICALPARTY, RACIALIDENTITY.4) %>%
  summarise(
    mean_ZEROSUM_2 = mean(ZEROSUM_2, na.rm = TRUE),
    se = sd(ZEROSUM_2, na.rm = TRUE) / sqrt(n()),
    .groups = 'drop'
  )

# Check the structure
#str(group_stats_2)
```

```{r}
#| label: show groupstats for ZERO_SUM2 

head(group_stats_2)
```

```{r}
#| label: zerosum2 pointrange

# Define the zesty_four palette
zesty_four <- c("#E69F00", "#009E73", "#999999", "#CC79A7") 

# Create the plot with pointrange
plot.zerosum2 <- ggplot(group_stats_2, 
                        aes(POLITICALPARTY, mean_ZEROSUM_2)) +
  geom_pointrange(aes(color = RACIALIDENTITY.4,
                      ymin = mean_ZEROSUM_2 - se,
                      # Use standard error instead of sd
                      ymax = mean_ZEROSUM_2 + se),
                      # Use standard error instead of sd
                      position = position_dodge(0.3)) +
  theme_bw() +
  labs(title = "When some people are getting poorer, \n it means that other people are getting richer.",
       x = "Political Affiliation",
       y = "Agreement with Zero Sum Beliefs",
       color = "RACIALIDENTITY.4") + 
  theme(
    plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
    axis.title = element_text(size = 10),
    axis.text = element_text(size = 8),
    legend.title = element_text(size = 10),
    legend.text = element_text(size = 8)
  ) +
  scale_y_continuous(
    breaks = 1:7,
    labels = c("1: Strongly Disbelieve", 
               "2: Disbelieve", 
               "3: Somewhat Disbelieve", 
               "4: Neither", 
               "5: Somewhat Believe", 
               "6: Believe", 
               "7: Strongly Believe"),
    limits = c(1, 7)  # Set the y-axis limits to match the scale
  )+
  scale_color_manual(values = zesty_four)

# Print and save the plots
print(plot.zerosum2)
ggsave("plots/plot2:poor-rich.png", 
       plot = plot.zerosum2, 
       width = 10, 
       height = 8, 
       dpi = 300)
```

This pointrange plot shows that White respondents exhibit higher agreement with the belief that “when some people are getting poorer, it means that other people are getting richer” among Republicans and Independents, highlighting group-based differences in zero-sum beliefs.

```{r}
#| label: ZEROSUM_2 average score with 95% CI

# ZEROSUM_1 average score with 95% CI
group_stats_2_avg <- select_data %>%
  group_by(POLITICALPARTY) %>%
  summarise(
    mean_ZEROSUM_2 = mean(ZEROSUM_2, na.rm = TRUE),
    se = sd(ZEROSUM_2, na.rm = TRUE) / sqrt(n()),
    n = n(),
    .groups = 'drop'
  ) %>%
  mutate(
    # Calculate 95% confidence interval using t-distribution
    t_value = qt(0.975, df = n - 1),  # 95% CI, two-tailed
    ci_lower = mean_ZEROSUM_2 - t_value * se,
    ci_upper = mean_ZEROSUM_2 + t_value * se
  )
```

*Do zero-sum beliefs regarding poor and rich differ by political party affiliation?*

```{r}
#| label: plot.zerosum2_enhanced and Raincloud plot

library(ggdist)
library(patchwork)

plot.zerosum2_raincloud <- select_data %>%
  filter(!is.na(ZEROSUM_2), !is.na(POLITICALPARTY)) %>%
  ggplot(aes(x = POLITICALPARTY, y = ZEROSUM_2, fill = POLITICALPARTY, color = POLITICALPARTY)) +
  stat_halfeye(
    adjust = 0.5,
    width = 0.6,
    .width = 0,
    justification = -0.2,
    point_colour = NA,
    alpha = 0.7
  ) +
  geom_boxplot(
    width = 0.15,
    outlier.shape = NA,
    alpha = 0.5
  ) +
  geom_point(
    size = 1.3,
    alpha = 0.3,
    position = position_jitter(
      seed = 1, width = 0.1
    )
  ) +
  stat_summary(
    fun = mean,
    geom = "point",
    size = 3,
    color = "white",
    stroke = 1.5,
    shape = 21
  ) +
  scale_fill_manual(values = party_colors) +
  scale_color_manual(values = party_colors) +
  theme_bw() +
  labs(
    title = "When some people are getting poorer, \n it means that other people are getting richer.",
    x = "Political Affiliation",
    y = "Level of Agreement",
    caption = "White dots = means; colored dots = individual responses"
  ) +
  theme(
    plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
    plot.caption = element_text(size = 9, color = "gray40", hjust = 0.5),
    axis.title = element_text(size = 12, face = "bold"),
    axis.text = element_text(size = 10),
    legend.position = "none",
    panel.grid.major.y = element_line(color = "gray90", linewidth = 0.3)  # Fixed
  ) +
  scale_y_continuous(
    breaks = 1:7,
    labels = c("1: Strongly Disbelieve", 
               "2: Disbelieve", 
               "3: Somewhat Disbelieve", 
               "4: Neither", 
               "5: Somewhat Believe", 
               "6: Believe", 
               "7: Strongly Believe"),
    limits = c(1, 7) 
  )
plot.zerosum2_raincloud

```

This raincloud plot shows that mean agreement with the zero-sum belief (indicated by white dots) varies by political affiliation. Among the three groups, Democrat exhibit higher average agreement with the statement “When some people are getting poorer, it means that other people are getting richer.” ßThe distribution (via density and individual dots) reveals different patterns across groups. Democrats show greater clustering of high agreement scores (concentrated in the 5-7 range). Republicans show the most dispersed distribution with responses spread across nearly the full scale. Independents are more concentrated in the middle-to-lower range.

```{r}
#| label: shapiro.test zerosum2 table

shapiro_zerosum2 <- select_data %>%
  group_by(RACIALIDENTITY.4) %>%
  summarise(p = shapiro.test(ZEROSUM_2)$p.value)

kable(
  shapiro_zerosum2,
  caption = "Table 27. Shapiro–Wilk Normality Test for ZEROSUM_2 by Racial Identity",
  digits = 4,
  booktabs = TRUE
)
## Asian, Black, White (p<0.05) fail the normality assumption for ANOVA, maybe use a non-parametric test (Kruskal-Wallis)
```

```{r}
#| label: Kable format ZEROSUM_2 Kruskal-Wallis
#| echo: false
#| message: false
#| warning: false

library(tibble)
library(knitr)
library(kableExtra)

kw.ZEROSUM_2.party <- kruskal.test(ZEROSUM_2 ~ POLITICALPARTY, data = select_data)

kw_table <- tibble(
  Predictor = c("POLITICALPARTY"),
  df = c(length(unique(select_data$POLITICALPARTY)) - 1),
  `Chi-squared` = c(round(kw.ZEROSUM_2.party$statistic, 2)),
  p = c(round(kw.ZEROSUM_2.party$p.value, 3))
)

if (knitr::is_html_output()) {
  kw_table %>%
    knitr::kable(
      digits = 3,
      escape = TRUE,
      format = "html",
      caption = "Table 28. Kruskal-Wallis Test Results for ZEROSUM_2"
    ) %>%
    kableExtra::kable_styling(bootstrap_options = c("striped", "hover", "condensed")) %>%
    kableExtra::row_spec(
      which(kw_table$p < 0.05),
      background = "gray"
    )
} else if (knitr::is_latex_output()) {
  kw_table %>%
    knitr::kable(
      digits = 3,
      escape = TRUE,
      format = "latex",
      longtable = TRUE,
      caption = "Kruskal-Wallis Test Results for ZEROSUM\\_2"
    ) %>%
    kableExtra::kable_styling(full_width = FALSE, position = "left") %>%
    kableExtra::row_spec(
      which(kw_table$p < 0.05),
      background = "gray!20"
    )
}
```

There was no significant difference in ZEROSUM_2 scores across political party groups, Kruskal-Wallis χ²(`r kw.ZEROSUM_2.party$parameter`) = `r round(kw.ZEROSUM_2.party$statistic, 2)`, *p* = `r signif(kw.ZEROSUM_2.party$p.value, 3)`.

#### Wealth few vs. many

*Do zero-sum beliefs regarding wealth concentration differ by racial/ethnic identity and political party affiliation?*

Below we present descriptive statistics and visualizations for ZEROSUM_3 ("The wealth of a few is acquired at the expense of many."), examining how responses vary across racial identity and political affiliation.

```{r}
#| label: groupstats for ZERO_SUM3 
#| echo: false
#| message: false
#| warning: false

library(dplyr)
group_stats_3 <- select_data %>%
  group_by(POLITICALPARTY, RACIALIDENTITY.4) %>%
  summarise(
    mean_ZEROSUM_3 = mean(ZEROSUM_3, na.rm = TRUE),
    se = sd(ZEROSUM_3, na.rm = TRUE) / sqrt(n()),
    .groups = 'drop'
  )

# Check the structure
#str(group_stats_3)
```

```{r}
#| label: show groupstats for ZERO_SUM3 

head(group_stats_3)
```

```{r}
#| label: zerosum3 pointrange

# Define the zesty_four palette
zesty_four <- c("#E69F00", "#009E73", "#999999", "#CC79A7") 

# Create the plot with pointrange
plot.zerosum3 <- ggplot(group_stats_3,
                        aes(POLITICALPARTY, mean_ZEROSUM_3)) +
  geom_pointrange(aes(color = RACIALIDENTITY.4,
                      ymin = mean_ZEROSUM_3 - se,
                      # Use standard error instead of sd
                      ymax = mean_ZEROSUM_3 + se),
                      # Use standard error instead of sd
                      position = position_dodge(0.3)) +
  theme_bw() +
  labs(title = "The wealth of a few is acquired at the expense of many.",
       x = "Political Affiliation",
       y = "Agreement with Zero Sum Beliefs",
       color = "RACIALIDENTITY.4") + 
  theme(
    plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
    axis.title = element_text(size = 10),
    axis.text = element_text(size = 8),
    legend.title = element_text(size = 10),
    legend.text = element_text(size = 8)
  ) +
  scale_y_continuous(
    breaks = 1:7,
    labels = c("1: Strongly Disbelieve", 
               "2: Disbelieve", 
               "3: Somewhat Disbelieve", 
               "4: Neither", 
               "5: Somewhat Believe", 
               "6: Believe", 
               "7: Strongly Believe"),
    limits = c(1, 7)  # Set the y-axis limits to match the scale
  )+
  scale_color_manual(values = zesty_four)

# Print and save the plots
print(plot.zerosum3)
ggsave("plots/plot3:wealthfew-many.png", 
       plot = plot.zerosum3, 
       width = 10, 
       height = 8, 
       dpi = 300)

```

This pointrange plot shows that White respondents exhibit higher agreement with the belief that “the wealth of a few is acquired at the expense of many” among all three political groups, highlighting group-based differences in zero-sum beliefs.

```{r}
#| label: ZEROSUM_3 average score with 95% CI

# ZEROSUM_3 average score with 95% CI
group_stats_3_avg <- select_data %>%
  group_by(POLITICALPARTY) %>%
  summarise(
    mean_ZEROSUM_3 = mean(ZEROSUM_3, na.rm = TRUE),
    se = sd(ZEROSUM_3, na.rm = TRUE) / sqrt(n()),
    n = n(),
    .groups = 'drop'
  ) %>%
  mutate(
    # Calculate 95% confidence interval using t-distribution
    t_value = qt(0.975, df = n - 1),  # 95% CI, two-tailed
    ci_lower = mean_ZEROSUM_3 - t_value * se,
    ci_upper = mean_ZEROSUM_3 + t_value * se
  )
```

*Do zero-sum beliefs regarding wealth concentration differ by political party affiliation?*

```{r}
#| label: plot.zerosum3_enhanced and Raincloud plot

plot.zerosum3_raincloud <- select_data %>%
  filter(!is.na(ZEROSUM_3), !is.na(POLITICALPARTY)) %>%
  ggplot(aes(x = POLITICALPARTY, y = ZEROSUM_3, fill = POLITICALPARTY, color = POLITICALPARTY)) +
  stat_halfeye(
    adjust = 0.5,
    width = 0.6,
    .width = 0,
    justification = -0.2,
    point_colour = NA,
    alpha = 0.7
  ) +
   geom_boxplot(
    width = 0.15,
    outlier.shape = NA,
    alpha = 0.5
  ) +
  geom_point(
    size = 1.3,
    alpha = 0.3,
    position = position_jitter(
      seed = 1, width = 0.1
    )
  ) +
  stat_summary(
    fun = mean,
    geom = "point",
    size = 3,
    color = "white",
    stroke = 1.5,
    shape = 21
  ) +
  scale_fill_manual(values = party_colors) +
  scale_color_manual(values = party_colors) +
  theme_bw() +
  labs(
    title = "The wealth of a few is acquired at the expense of many.",
    x = "Political Affiliation",
    y = "Level of Agreement",
    caption = "White dots = means; colored dots = individual responses"
  ) +
  theme(
    plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
    plot.caption = element_text(size = 9, color = "gray40", hjust = 0.5),
    axis.title = element_text(size = 12, face = "bold"),
    axis.text = element_text(size = 10),
    legend.position = "none",
    panel.grid.major.y = element_line(color = "gray90", linewidth = 0.3)  # Fixed
  ) +
  scale_y_continuous(
    breaks = 1:7,
    labels = c("1: Strongly Disbelieve", 
               "2: Disbelieve", 
               "3: Somewhat Disbelieve", 
               "4: Neither", 
               "5: Somewhat Believe", 
               "6: Believe", 
               "7: Strongly Believe"),
    limits = c(1, 7) 
  )
plot.zerosum3_raincloud

```

This raincloud plot shows that mean agreement with the zero-sum belief (indicated by white dots) varies by political affiliation. Among the three groups, Democrat exhibit higher average agreement with the statement “the wealth of a few is acquired at the expense of many.” The distribution (via density and individual dots) reveals different patterns across groups. Democrats show strong clustering around high agreement scores (concentrated in the 5-7 range) with a clear rightward skew toward stronger belief. Republicans display a more spread distribution with notable presence across moderate to high agreement levels, though still centered around the middle range. Independents show a relatively concentrated distribution around the moderate-to-high range (4-6), with their mean falling between the other two groups.

```{r}
#| label: shapiro.test zerosum3 table

shapiro_zerosum3 <- select_data %>%
  group_by(RACIALIDENTITY.4) %>%
  summarise(p = shapiro.test(ZEROSUM_3)$p.value)

kable(
  shapiro_zerosum3,
  caption = "Table 29. Shapiro–Wilk Normality Test for ZEROSUM_3 by Racial Identity",
  digits = 6,
  booktabs = TRUE
)
## Black, White, Mixed/Other (p<0.05) fail the normality assumption for ANOVA, maybe use a non-parametric test (Kruskal-Wallis)
```

```{r}
#| label: kable format ZEROSUM_3 Kruskal-Wallis
#| echo: false
#| message: false
#| warning: false

library(tibble)
library(rempsyc)

kw.ZEROSUM_3.party <- kruskal.test(ZEROSUM_3 ~ POLITICALPARTY, data = select_data)

kw_table <- tibble(
  Predictor = c("POLITICALPARTY"),
  df = c(length(unique(select_data$POLITICALPARTY)) - 1),
  `Chi-squared` = c(round(kw.ZEROSUM_3.party$statistic, 2)),
  p = c(round(kw.ZEROSUM_3.party$p.value, 3))
)

if (knitr::is_html_output()) {
  kw_table %>%
    knitr::kable(
      digits = 3,
      escape = TRUE,
      format = "html",
      caption = "Table 30. Kruskal-Wallis Test Results for ZEROSUM_3"
    ) %>%
    kableExtra::kable_styling(bootstrap_options = c("striped", "hover", "condensed")) %>%
    kableExtra::row_spec(
      which(kw_table$p < 0.05),
      background = "gray"
    )
} else if (knitr::is_latex_output()) {
  kw_table %>%
    knitr::kable(
      digits = 3,
      escape = TRUE,
      format = "latex",
      longtable = TRUE,
      caption = "Kruskal-Wallis Test Results for ZEROSUM\\_3"
    ) %>%
    kableExtra::kable_styling(full_width = FALSE, position = "left") %>%
    kableExtra::row_spec(
      which(kw_table$p < 0.05),
      background = "gray!20"
    )
}
```

There was no significant difference in ZEROSUM_3 scores across political party groups, Kruskal-Wallis χ²(`r kw.ZEROSUM_3.party$parameter`) = `r round(kw.ZEROSUM_3.party$statistic, 2)`, *p* = `r signif(kw.ZEROSUM_3.party$p.value, 3)`.

#### Women vs. Men

*Do zero-sum beliefs regarding gender discrimination differ by racial/ethnic identity and political party affiliation?*

Below we present descriptive statistics and visualizations for ZEROSUM_4 ("As women face less sexism, men end up facing more sexism."), examining how responses vary across racial identity and political affiliation.

```{r}
#| label: groupstats for ZERO_SUM4 
#| echo: false
#| message: false
#| warning: false

library(dplyr)
group_stats_4 <- select_data %>%
  group_by(POLITICALPARTY, RACIALIDENTITY.4) %>%
  summarise(
    mean_ZEROSUM_4 = mean(ZEROSUM_4, na.rm = TRUE),
    se = sd(ZEROSUM_4, na.rm = TRUE) / sqrt(n()),
    .groups = 'drop'
  )

# Check the structure
#str(group_stats_4)
```

```{r}
#| label: show groupstats for ZERO_SUM4 

head(group_stats_4)
```

```{r}
#| label: zerosum4 pointrange

# Define the zesty_four palette
zesty_four <- c("#E69F00", "#009E73", "#999999", "#CC79A7") 

# Create the plot with pointrange
plot.zerosum4 <- ggplot(group_stats_4, aes(POLITICALPARTY,
                                           mean_ZEROSUM_4)) +
  geom_pointrange(aes(color = RACIALIDENTITY.4, 
                      ymin = mean_ZEROSUM_4 - se,
                      # Use standard error instead of sd
                      ymax = mean_ZEROSUM_4 + se),
                      # Use standard error instead of sd
                      position = position_dodge(0.3)) +
  theme_bw() +
  labs(title = "As women face less sexism, men end up facing more sexism.",
       x = "Political Affiliation",
       y = "Agreement with Zero Sum Beliefs",
       color = "RACIALIDENTITY.4") + 
  theme(
    plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
    axis.title = element_text(size = 10),
    axis.text = element_text(size = 8),
    legend.title = element_text(size = 10),
    legend.text = element_text(size = 8)
  ) +
  scale_y_continuous(
    breaks = 1:7,
    labels = c("1: Strongly Disbelieve", 
               "2: Disbelieve", 
               "3: Somewhat Disbelieve", 
               "4: Neither", 
               "5: Somewhat Believe", 
               "6: Believe", 
               "7: Strongly Believe"),
    limits = c(1, 7)  # Set the y-axis limits to match the scale
  )+
  scale_color_manual(values = zesty_four)

# Print and save the plots
print(plot.zerosum4)
ggsave("plots/plot4:women-men.png", 
       plot = plot.zerosum4, 
       width = 10, 
       height = 8, 
       dpi = 300)


```

This pointrange plot shows that Mixed/Other respondents exhibit higher agreement with the belief that “As women face less sexism, men end up facing more sexism.” among Republicans. Among political groups, Republicans show the highest overall agreement, while Democrats (particularly Asian and Mixed/Other respondents) show the lowest agreement. The pattern highlights how both political affiliation and racial identity intersect to shape zero-sum beliefs about gender discrimination.

```{r}
#| label: ZEROSUM_4 average score with 95% CI

# ZEROSUM_4 average score with 95% CI
group_stats_4_avg <- select_data %>%
  group_by(POLITICALPARTY) %>%
  summarise(
    mean_ZEROSUM_4 = mean(ZEROSUM_4, na.rm = TRUE),
    se = sd(ZEROSUM_4, na.rm = TRUE) / sqrt(n()),
    n = n(),
    .groups = 'drop'
  ) %>%
  mutate(
    # Calculate 95% confidence interval using t-distribution
    t_value = qt(0.975, df = n - 1),  # 95% CI, two-tailed
    ci_lower = mean_ZEROSUM_4 - t_value * se,
    ci_upper = mean_ZEROSUM_4 + t_value * se
  )
```

*Do zero-sum beliefs regarding gender discrimination differ by political party affiliation?*

```{r}
#| label: plot.zerosum4_enhanced and Raincloud plot

plot.zerosum4_raincloud <- select_data %>%
  filter(!is.na(ZEROSUM_4), !is.na(POLITICALPARTY)) %>%
  ggplot(aes(x = POLITICALPARTY, y = ZEROSUM_4, fill = POLITICALPARTY, color = POLITICALPARTY)) +
  stat_halfeye(
    adjust = 0.5,
    width = 0.6,
    .width = 0,
    justification = -0.2,
    point_colour = NA,
    alpha = 0.7
  ) +
   geom_boxplot(
    width = 0.15,
    outlier.shape = NA,
    alpha = 0.5
  ) +
  geom_point(
    size = 1.3,
    alpha = 0.3,
    position = position_jitter(
      seed = 1, width = 0.1
    )
  ) +
  stat_summary(
    fun = mean,
    geom = "point",
    size = 3,
    color = "white",
    stroke = 1.5,
    shape = 21
  ) +
  scale_fill_manual(values = party_colors) +
  scale_color_manual(values = party_colors) +
  theme_bw() +
  labs(
    title = "As women face less sexism, men end up facing more sexism.",
    x = "Political Affiliation",
    y = "Level of Agreement",
    caption = "White dots = means; colored dots = individual responses"
  ) +
  theme(
    plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
    plot.caption = element_text(size = 9, color = "gray40", hjust = 0.5),
    axis.title = element_text(size = 12, face = "bold"),
    axis.text = element_text(size = 10),
    legend.position = "none",
    panel.grid.major.y = element_line(color = "gray90", linewidth = 0.3)  # Fixed
  ) +
  scale_y_continuous(
    breaks = 1:7,
    labels = c("1: Strongly Disbelieve", 
               "2: Disbelieve", 
               "3: Somewhat Disbelieve", 
               "4: Neither", 
               "5: Somewhat Believe", 
               "6: Believe", 
               "7: Strongly Believe"),
    limits = c(1, 7) 
  )
plot.zerosum4_raincloud

```

This raincloud plot shows that mean agreement with the zero-sum belief (indicated by white dots) varies by political affiliation. Among the three groups, Republican exhibit higher average agreement with the statement “As women face less sexism, men end up facing more sexism.”

The distribution (via density and individual dots) reveals different patterns across groups. Republicans show a relatively spread distribution with responses concentrated in the moderate-to-high range (3-6) and some clustering around the mean. Independents display a more concentrated distribution around the lower-moderate range with their density peaked around 2-4. Democrats show the most pronounced leftward skew with strong clustering in the low agreement range (1-3) and a long tail extending toward higher values, indicating most Democrats disagree with this zero-sum perspective on gender discrimination.

```{r}
#| label: shapiro.test zerosum4 table

shapiro_zerosum4 <- select_data %>%
  group_by(RACIALIDENTITY.4) %>%
  summarise(p = shapiro.test(ZEROSUM_4)$p.value)


kable(
  shapiro_zerosum4,
  caption = "Table 31. Shapiro–Wilk Normality Test for ZEROSUM_4 by Racial Identity",
  digits = 4,
  booktabs = TRUE
)
```

```{r}
#| label: kable format ZEROSUM_4 Kruskal-Wallis
#| echo: false
#| message: false
#| warning: false

library(tibble)
library(rempsyc)

kw.ZEROSUM_4.party <- kruskal.test(ZEROSUM_4 ~ POLITICALPARTY, data = select_data)

kw_table <- tibble(
  Predictor = c("POLITICALPARTY"),
  df = c(length(unique(select_data$POLITICALPARTY)) - 1),
  `Chi-squared` = c(round(kw.ZEROSUM_4.party$statistic, 2)),
  p = c(round(kw.ZEROSUM_4.party$p.value, 3))
)

if (knitr::is_html_output()) {
  kw_table %>%
    knitr::kable(
      digits = 3,
      escape = TRUE,
      format = "html",
      caption = "Table 32. Kruskal-Wallis Test Results for ZEROSUM_4"
    ) %>%
    kableExtra::kable_styling(bootstrap_options = c("striped", "hover", "condensed")) %>%
    kableExtra::row_spec(
      which(kw_table$p < 0.05),
      background = "gray"
    )
} else if (knitr::is_latex_output()) {
  kw_table %>%
    knitr::kable(
      digits = 3,
      escape = TRUE,
      format = "latex",
      longtable = TRUE,
      caption = "Kruskal-Wallis Test Results for ZEROSUM\\_4"
    ) %>%
    kableExtra::kable_styling(full_width = FALSE, position = "left") %>%
    kableExtra::row_spec(
      which(kw_table$p < 0.05),
      background = "gray!20"
    )
}
```

There was significant difference in ZEROSUM_4 scores across political party groups, Kruskal-Wallis χ²(`r kw.ZEROSUM_4.party$parameter`) = `r round(kw.ZEROSUM_4.party$statistic, 2)`, *p* = `r signif(kw.ZEROSUM_4.party$p.value, 3)`.

#### Minorities vs. Whites

*Do zero-sum beliefs regarding racial discrimination (minorities and whites) differ by racial/ethnic identity and political party affiliation?*

Below we present descriptive statistics and visualizations for ZEROSUM_5 ("Less discrimination against minorities means more discrimination against whites."), examining how responses vary across racial identity and political affiliation.

```{r}
#| label: groupstats for ZERO_SUM5 
#| echo: false
#| message: false
#| warning: false

library(dplyr)
group_stats_5 <- select_data %>%
  group_by(POLITICALPARTY, RACIALIDENTITY.4) %>%
  summarise(
    mean_ZEROSUM_5 = mean(ZEROSUM_5, na.rm = TRUE),
    se = sd(ZEROSUM_5, na.rm = TRUE) / sqrt(n()),
    .groups = 'drop'
  )

# Check the structure
#str(group_stats_5)
```

```{r}
#| label: show groupstats for ZERO_SUM5 

head(group_stats_5)
```

```{r}
#| label: zerosum5 pointrange

# Define the zesty_four palette
zesty_four <- c("#E69F00", "#009E73", "#999999", "#CC79A7") 

# Create the plot with pointrange
plot.zerosum5 <- ggplot(group_stats_5, aes(POLITICALPARTY, mean_ZEROSUM_5)) +
  geom_pointrange(aes(color = RACIALIDENTITY.4, 
                      ymin = mean_ZEROSUM_5 - se,  # Use standard error instead of sd
                      ymax = mean_ZEROSUM_5 + se), # Use standard error instead of sd
                  position = position_dodge(0.3)) +
  theme_bw() +
  labs(title = "Less discrimination against minorities means more \n discrimination against whites.",
       x = "Political Affiliation",
       y = "Agreement with Zero Sum Beliefs",
       color = "RACIALIDENTITY.4") + 
  theme(
    plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
    axis.title = element_text(size = 10),
    axis.text = element_text(size = 8),
    legend.title = element_text(size = 10),
    legend.text = element_text(size = 8)
  ) +
  scale_y_continuous(
    breaks = 1:7,
    labels = c("1: Strongly Disbelieve", 
               "2: Disbelieve", 
               "3: Somewhat Disbelieve", 
               "4: Neither", 
               "5: Somewhat Believe", 
               "6: Believe", 
               "7: Strongly Believe"),
    limits = c(1, 7)  # Set the y-axis limits to match the scale
  )+
  scale_color_manual(values = zesty_four)

# Print and save the plots
print(plot.zerosum5)
ggsave("plots/plot5:minorities-whites.png", 
       plot = plot.zerosum5, 
       width = 10, 
       height = 8, 
       dpi = 300)

```

This pointrange plot shows that White respondents exhibit higher agreement with the belief that “Less discrimination against minorities means more discrimination against whites.” among Republicans. Among political groups, Republicans show the highest overall agreement, while Democrats (particularly Asian and Mixed/Other respondents) show the lowest agreement. The pattern highlights how both political affiliation and racial identity intersect to shape zero-sum beliefs about racial discrimination.

```{r}
#| label: ZEROSUM_5 average score with 95% CI

# ZEROSUM_5 average score with 95% CI
group_stats_5_avg <- select_data %>%
  group_by(POLITICALPARTY) %>%
  summarise(
    mean_ZEROSUM_5 = mean(ZEROSUM_5, na.rm = TRUE),
    se = sd(ZEROSUM_5, na.rm = TRUE) / sqrt(n()),
    n = n(),
    .groups = 'drop'
  ) %>%
  mutate(
    # Calculate 95% confidence interval using t-distribution
    t_value = qt(0.975, df = n - 1),  # 95% CI, two-tailed
    ci_lower = mean_ZEROSUM_5 - t_value * se,
    ci_upper = mean_ZEROSUM_5 + t_value * se
  )
```

*Do zero-sum beliefs regarding racial discrimination (minorities and whites) differ by political party affiliation?*

```{r}
#| label: plot.zerosum5_enhanced and Raincloud plot

plot.zerosum5_raincloud <- select_data %>%
  filter(!is.na(ZEROSUM_5), !is.na(POLITICALPARTY)) %>%
  ggplot(aes(x = POLITICALPARTY, y = ZEROSUM_5, fill = POLITICALPARTY, color = POLITICALPARTY)) +
  stat_halfeye(
    adjust = 0.5,
    width = 0.6,
    .width = 0,
    justification = -0.2,
    point_colour = NA,
    alpha = 0.7
  ) +
   geom_boxplot(
    width = 0.15,
    outlier.shape = NA,
    alpha = 0.5
  ) +
  geom_point(
    size = 1.3,
    alpha = 0.3,
    position = position_jitter(
      seed = 1, width = 0.1
    )
  ) +
  stat_summary(
    fun = mean,
    geom = "point",
    size = 3,
    color = "white",
    stroke = 1.5,
    shape = 21
  ) +
  scale_fill_manual(values = party_colors) +
  scale_color_manual(values = party_colors) +
  theme_bw() +
  labs(
    title = "Less discrimination against minorities means more \n discrimination against whites.",
    x = "Political Affiliation",
    y = "Level of Agreement",
    caption = "White dots = means; colored dots = individual responses"
  ) +
  theme(
    plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
    plot.caption = element_text(size = 9, color = "gray40", hjust = 0.5),
    axis.title = element_text(size = 12, face = "bold"),
    axis.text = element_text(size = 10),
    legend.position = "none",
    panel.grid.major.y = element_line(color = "gray90", linewidth = 0.3)  # Fixed
  ) +
  scale_y_continuous(
    breaks = 1:7,
    labels = c("1: Strongly Disbelieve", 
               "2: Disbelieve", 
               "3: Somewhat Disbelieve", 
               "4: Neither", 
               "5: Somewhat Believe", 
               "6: Believe", 
               "7: Strongly Believe"),
    limits = c(1, 7) 
  )
plot.zerosum5_raincloud

```

This raincloud plot shows that mean agreement with the zero-sum belief (indicated by white dots) varies by political affiliation. Among the three groups, Republican exhibit higher average agreement with the statement “Less discrimination against minorities means more discrimination against whites." The distribution (via density and individual dots) reveals different patterns across groups. Republicans show a broader distribution with notable density across moderate-to-high agreement levels (3-6), indicating more varied responses within the party. Independents display a relatively concentrated distribution around the lower-moderate range with their density peaked around 1-4. Democrats show strong leftward skew with pronounced clustering in the low agreement range (1-3) and a steep drop-off at higher values, indicating most Democrats reject this zero-sum perspective on racial discrimination.

```{r}
#| label: shapiro.test zerosum5 table
shapiro_zerosum5 <- select_data %>%
  group_by(RACIALIDENTITY.4) %>%
  summarise(p = shapiro.test(ZEROSUM_5)$p.value)

kable(
  shapiro_zerosum5,
  caption = "Table 33. Shapiro–Wilk Normality Test for ZEROSUM_5 by Racial Identity",
  digits = 4,
  booktabs = TRUE
)
```

```{r}
#| label: kable format ZEROSUM_5 Kruskal-Wallis
#| echo: false
#| message: false
#| warning: false

library(tibble)
library(rempsyc)

kw.ZEROSUM_5.party <- kruskal.test(ZEROSUM_5 ~ POLITICALPARTY, data = select_data)

kw_table <- tibble(
  Predictor = c("POLITICALPARTY"),
  df = c(length(unique(select_data$POLITICALPARTY)) - 1),
  `Chi-squared` = c(round(kw.ZEROSUM_5.party$statistic, 2)),
  p = c(round(kw.ZEROSUM_5.party$p.value, 5))
)

if (knitr::is_html_output()) {
  kw_table %>%
    knitr::kable(
      digits = 5,
      escape = TRUE,
      format = "html",
      caption = "Table 34. Kruskal-Wallis Test Results for ZEROSUM_5"
    ) %>%
    kableExtra::kable_styling(bootstrap_options = c("striped", "hover", "condensed")) %>%
    kableExtra::row_spec(
      which(kw_table$p < 0.05),
      background = "gray"
    )
} else if (knitr::is_latex_output()) {
  kw_table %>%
    knitr::kable(
      digits = 5,
      escape = TRUE,
      format = "latex",
      longtable = TRUE,
      caption = "Kruskal-Wallis Test Results for ZEROSUM\\_5"
    ) %>%
    kableExtra::kable_styling(full_width = FALSE, position = "left") %>%
    kableExtra::row_spec(
      which(kw_table$p < 0.05),
      background = "gray!20"
    )
}
```

There was significant difference in ZEROSUM_5 scores across political party groups, Kruskal-Wallis χ²(`r kw.ZEROSUM_5.party$parameter`) = `r round(kw.ZEROSUM_5.party$statistic, 2)`, *p* = `r signif(kw.ZEROSUM_5.party$p.value, 3)`.

#### Transgender vs. Cisgender

*Do zero-sum beliefs regarding gender identity (transgender and cisgender) differ by racial/ethnic identity and political party affiliation?*

Below we present descriptive statistics and visualizations for ZEROSUM_6 ("More opportunity for transwomen means less opportunity for people who are assigned female at birth."), examining how responses vary across racial identity and political affiliation.

```{r}
#| label: groupstats for ZERO_SUM6 
#| echo: false
#| message: false
#| warning: false

library(dplyr)
group_stats_6 <- select_data %>%
  group_by(POLITICALPARTY, RACIALIDENTITY.4) %>%
  summarise(
    mean_ZEROSUM_6 = mean(ZEROSUM_6, na.rm = TRUE),
    se = sd(ZEROSUM_6, na.rm = TRUE) / sqrt(n()),
    .groups = 'drop'
  )

# Check the structure
#str(group_stats_6)
```

```{r}
#| label: show groupstats for ZERO_SUM6 

head(group_stats_6)
```

```{r}
#| label: zerosum6 pointrange

# Define the zesty_four palette
zesty_four <- c("#E69F00", "#009E73", "#999999", "#CC79A7") 

# Create the plot with pointrange
plot.zerosum6 <- ggplot(group_stats_6, aes(POLITICALPARTY, mean_ZEROSUM_6)) +
  geom_pointrange(aes(color = RACIALIDENTITY.4, 
                      ymin = mean_ZEROSUM_6 - se,  # Use standard error instead of sd
                      ymax = mean_ZEROSUM_6 + se), # Use standard error instead of sd
                  position = position_dodge(0.3)) +
  theme_bw() +
  labs(title = "More opportunity for transwomen means less opportunity \n for people who are assigned female at birth.",
       x = "Political Affiliation",
       y = "Agreement with Zero Sum Beliefs",
       color = "RACIALIDENTITY.4") + 
  theme(
    plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
    axis.title = element_text(size = 10),
    axis.text = element_text(size = 8),
    legend.title = element_text(size = 10),
    legend.text = element_text(size = 8)
  ) +
  scale_y_continuous(
    breaks = 1:7,
    labels = c("1: Strongly Disbelieve", 
               "2: Disbelieve", 
               "3: Somewhat Disbelieve", 
               "4: Neither", 
               "5: Somewhat Believe", 
               "6: Believe", 
               "7: Strongly Believe"),
    limits = c(1, 7)  # Set the y-axis limits to match the scale
  )+
  scale_color_manual(values = zesty_four)

# Print and save the plots
print(plot.zerosum6)
ggsave("plots/plot6:trans-cis.png", 
       plot = plot.zerosum6, 
       width = 10, 
       height = 8, 
       dpi = 300)
```

This pointrange plot shows that White respondents exhibit higher agreement with the belief that “More opportunity for transwomen means less opportunity for people who are assigned female at birth.” among Republicans. Among political groups, Republicans show the highest overall agreement, while Democrats (particularly Asian and Mixed/Other respondents) show the lowest agreement. The pattern highlights how both political affiliation and racial identity intersect to shape zero-sum beliefs about transgender rights and opportunities.

```{r}
#| label: ZEROSUM_6 average score with 95% CI

# ZEROSUM_6 average score with 95% CI
group_stats_6_avg <- select_data %>%
  group_by(POLITICALPARTY) %>%
  summarise(
    mean_ZEROSUM_6 = mean(ZEROSUM_6, na.rm = TRUE),
    se = sd(ZEROSUM_6, na.rm = TRUE) / sqrt(n()),
    n = n(),
    .groups = 'drop'
  ) %>%
  mutate(
    # Calculate 95% confidence interval using t-distribution
    t_value = qt(0.975, df = n - 1),  # 95% CI, two-tailed
    ci_lower = mean_ZEROSUM_6 - t_value * se,
    ci_upper = mean_ZEROSUM_6 + t_value * se
  )
```

*Do zero-sum beliefs regarding gender identity (transgender and cisgender) differ by political party affiliation?*

```{r}
#| label: plot.zerosum6_enhanced and Raincloud plot

plot.zerosum6_raincloud <- select_data %>%
  filter(!is.na(ZEROSUM_6), !is.na(POLITICALPARTY)) %>%
  ggplot(aes(x = POLITICALPARTY, y = ZEROSUM_6, fill = POLITICALPARTY, color = POLITICALPARTY)) +
  stat_halfeye(
    adjust = 0.5,
    width = 0.6,
    .width = 0,
    justification = -0.2,
    point_colour = NA,
    alpha = 0.7
  ) +
   geom_boxplot(
    width = 0.15,
    outlier.shape = NA,
    alpha = 0.5
  ) +
  geom_point(
    size = 1.3,
    alpha = 0.3,
    position = position_jitter(
      seed = 1, width = 0.1
    )
  ) +
  stat_summary(
    fun = mean,
    geom = "point",
    size = 3,
    color = "white",
    stroke = 1.5,
    shape = 21
  ) +
  scale_fill_manual(values = party_colors) +
  scale_color_manual(values = party_colors) +
  theme_bw() +
  labs(
    title = "More opportunity for transwomen means less opportunity \n for people who are assigned female at birth.",
    x = "Political Affiliation",
    y = "Level of Agreement",
    caption = "White dots = means; colored dots = individual responses"
  ) +
  theme(
    plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
    plot.caption = element_text(size = 9, color = "gray40", hjust = 0.5),
    axis.title = element_text(size = 12, face = "bold"),
    axis.text = element_text(size = 10),
    legend.position = "none",
    panel.grid.major.y = element_line(color = "gray90", linewidth = 0.3)  # Fixed
  ) +
  scale_y_continuous(
    breaks = 1:7,
    labels = c("1: Strongly Disbelieve", 
               "2: Disbelieve", 
               "3: Somewhat Disbelieve", 
               "4: Neither", 
               "5: Somewhat Believe", 
               "6: Believe", 
               "7: Strongly Believe"),
    limits = c(1, 7) 
  )
plot.zerosum6_raincloud

```

This raincloud plot shows that mean agreement with the zero-sum belief (indicated by white dots) varies by political affiliation. Among the three groups, Republican exhibit higher average agreement with the statement “More opportunity for transwomen means less opportunity for people who are assigned female at birth."

The distribution (via density and individual dots) reveals different patterns across groups. Republicans show a relatively concentrated distribution around moderate-to-high agreement levels (3-6) with some spread toward the extremes. Independents display a broad distribution with responses spanning from low to high agreement but concentrated in the moderate range (2-6). Democrats show strong leftward skew with pronounced clustering in the low agreement range (1-3) and a steep drop-off at higher values, indicating most Democrats reject this zero-sum perspective on transgender rights and opportunities.

```{r}
#| label: shapiro.test zerosum6 table

shapiro_zerosum6 <- select_data %>%
  group_by(RACIALIDENTITY.4) %>%
  summarise(p = shapiro.test(ZEROSUM_6)$p.value)

kable(
  shapiro_zerosum6,
  caption = "Table 35. Shapiro–Wilk Normality Test for ZEROSUM_6 by Racial Identity",
  digits = 5,
  booktabs = TRUE
)
```

```{r}
#| label: kable format ZEROSUM_6 Kruskal-Wallis
#| echo: false
#| message: false
#| warning: false

library(tibble)
library(rempsyc)

kw.ZEROSUM_6.party <- kruskal.test(ZEROSUM_6 ~ POLITICALPARTY, data = select_data)

kw_table <- tibble(
  Predictor = c("POLITICALPARTY"),
  df = c(length(unique(select_data$POLITICALPARTY)) - 1),
  `Chi-squared` = c(round(kw.ZEROSUM_6.party$statistic, 2)),
  p = c(round(kw.ZEROSUM_6.party$p.value, 5))
)

if (knitr::is_html_output()) {
  kw_table %>%
    knitr::kable(
      digits = 5,
      escape = TRUE,
      format = "html",
      caption = "Table 36. Kruskal-Wallis Test Results for ZEROSUM_6"
    ) %>%
    kableExtra::kable_styling(bootstrap_options = c("striped", "hover", "condensed")) %>%
    kableExtra::row_spec(
      which(kw_table$p < 0.05),
      background = "gray"
    )
} else if (knitr::is_latex_output()) {
  kw_table %>%
    knitr::kable(
      digits = 5,
      escape = TRUE,
      format = "latex",
      longtable = TRUE,
      caption = "Kruskal-Wallis Test Results for ZEROSUM\\_6"
    ) %>%
    kableExtra::kable_styling(full_width = FALSE, position = "left") %>%
    kableExtra::row_spec(
      which(kw_table$p < 0.05),
      background = "gray!20"
    )
}
```

There was significant difference in ZEROSUM_6 scores across political party groups, Kruskal-Wallis χ²(`r kw.ZEROSUM_6.party$parameter`) = `r round(kw.ZEROSUM_6.party$statistic, 2)`, *p* = `r signif(kw.ZEROSUM_6.party$p.value, 3)`.

#### Undocumented vs. Citizens

*Do zero-sum beliefs about healthcare access—specifically, that undocumented immigration reduces access for U.S. citizens—differ by racial/ethnic identity and political affiliation?*

Below we present descriptive statistics and visualizations for ZEROSUM_7 ("More health care access for undocumented immigrants means less access for U.S. citizens."), examining how responses vary across racial identity and political affiliation.

```{r}
#| label: groupstats for ZERO_SUM7 
#| echo: false
#| message: false
#| warning: false

library(dplyr)
group_stats_7 <- select_data %>%
  group_by(POLITICALPARTY, RACIALIDENTITY.4) %>%
  summarise(
    mean_ZEROSUM_7 = mean(ZEROSUM_7, na.rm = TRUE),
    se = sd(ZEROSUM_7, na.rm = TRUE) / sqrt(n()),
    .groups = 'drop'
  )

# Check the structure
#str(group_stats_7)
```

```{r}
#| label: show groupstats for ZERO_SUM7 

head(group_stats_7)
```

```{r}
#| label: zerosum7 pointrange

# Define the zesty_four palette
zesty_four <- c("#E69F00", "#009E73", "#999999", "#CC79A7") 

# Create the plot with pointrange
plot.zerosum7 <- ggplot(group_stats_7, aes(POLITICALPARTY, mean_ZEROSUM_7)) +
  geom_pointrange(aes(color = RACIALIDENTITY.4, 
                      ymin = mean_ZEROSUM_7 - se,  # Use standard error instead of sd
                      ymax = mean_ZEROSUM_7 + se), # Use standard error instead of sd
                  position = position_dodge(0.3)) +
  theme_bw() +
  labs(title = "More health care access for undocumented immigrants \n means less access for U.S. citizens.",
       x = "Political Affiliation",
       y = "Agreement with Zero Sum Beliefs",
       color = "RACIALIDENTITY.4") + 
  theme(
    plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
    axis.title = element_text(size = 10),
    axis.text = element_text(size = 8),
    legend.title = element_text(size = 10),
    legend.text = element_text(size = 8)
  ) +
  scale_y_continuous(
    breaks = 1:7,
    labels = c("1: Strongly Disbelieve", 
               "2: Disbelieve", 
               "3: Somewhat Disbelieve", 
               "4: Neither", 
               "5: Somewhat Believe", 
               "6: Believe", 
               "7: Strongly Believe"),
    limits = c(1, 7)  # Set the y-axis limits to match the scale
  )+
  scale_color_manual(values = zesty_four)

# Print and save the plots
print(plot.zerosum7)
ggsave("plots/plot7:undoc-citizens.png", 
       plot = plot.zerosum7, 
       width = 10, 
       height = 8, 
       dpi = 300)
```

This pointrange plot shows that Asian respondents exhibit higher agreement with the belief that “More health care access for undocumented immigrants means less access for U.S. citizens.” among Republicans. Among political groups, Republicans show the highest overall agreement, while Democrats (particularly Asian and Mixed/Other respondents) show the lowest agreement. This pattern highlights that political affiliation creates a greater divide in zero-sum beliefs about health care resources within each political group than does racial identity.

```{r}
#| label: ZEROSUM_7 average score with 95% CI

# ZEROSUM_7 average score with 95% CI
group_stats_7_avg <- select_data %>%
  group_by(POLITICALPARTY) %>%
  summarise(
    mean_ZEROSUM_7 = mean(ZEROSUM_7, na.rm = TRUE),
    se = sd(ZEROSUM_7, na.rm = TRUE) / sqrt(n()),
    n = n(),
    .groups = 'drop'
  ) %>%
  mutate(
    # Calculate 95% confidence interval using t-distribution
    t_value = qt(0.975, df = n - 1),  # 95% CI, two-tailed
    ci_lower = mean_ZEROSUM_7 - t_value * se,
    ci_upper = mean_ZEROSUM_7 + t_value * se
  )
```

*Do zero-sum beliefs about healthcare access—specifically, that undocumented immigration reduces access for U.S. citizens—differ by political affiliation?*

```{r}
#| label: plot.zerosum7_enhanced and Raincloud plot

plot.zerosum7_raincloud <- select_data %>%
  filter(!is.na(ZEROSUM_7), !is.na(POLITICALPARTY)) %>%
  ggplot(aes(x = POLITICALPARTY, y = ZEROSUM_7, fill = POLITICALPARTY, color = POLITICALPARTY)) +
  stat_halfeye(
    adjust = 0.5,
    width = 0.6,
    .width = 0,
    justification = -0.2,
    point_colour = NA,
    alpha = 0.7
  ) +
   geom_boxplot(
    width = 0.15,
    outlier.shape = NA,
    alpha = 0.5
  ) +
  geom_point(
    size = 1.3,
    alpha = 0.3,
    position = position_jitter(
      seed = 1, width = 0.1
    )
  ) +
  stat_summary(
    fun = mean,
    geom = "point",
    size = 3,
    color = "white",
    stroke = 1.5,
    shape = 21
  ) +
  scale_fill_manual(values = party_colors) +
  scale_color_manual(values = party_colors) +
  theme_bw() +
  labs(
    title = "More health care access for undocumented immigrants \n means less access for U.S. citizens.",
    x = "Political Affiliation",
    y = "Level of Agreement",
    caption = "White dots = means; colored dots = individual responses"
  ) +
  theme(
    plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
    plot.caption = element_text(size = 9, color = "gray40", hjust = 0.5),
    axis.title = element_text(size = 12, face = "bold"),
    axis.text = element_text(size = 10),
    legend.position = "none",
    panel.grid.major.y = element_line(color = "gray90", linewidth = 0.3)  # Fixed
  ) +
  scale_y_continuous(
    breaks = 1:7,
    labels = c("1: Strongly Disbelieve", 
               "2: Disbelieve", 
               "3: Somewhat Disbelieve", 
               "4: Neither", 
               "5: Somewhat Believe", 
               "6: Believe", 
               "7: Strongly Believe"),
    limits = c(1, 7) 
  )
plot.zerosum7_raincloud

```

This raincloud plot shows that mean agreement with the zero-sum belief (indicated by white dots) varies by political affiliation. Among the three groups, Republican exhibit higher average agreement with the statement “More health care access for undocumented immigrants means less access for U.S. citizens." The distribution (via density and individual dots) reveals different patterns across groups. Republicans show a relatively concentrated distribution around moderate-to-high agreement levels (4-6) with their density peaked around the mean. Independents display a broader, more spread distribution across the full range with notable presence from low to high agreement levels. Democrats show strong leftward skew with pronounced clustering in the low agreement range (1-3) and a steep drop-off at higher values, indicating most Democrats reject this zero-sum perspective on healthcare access.

```{r}
#| label: shapiro.test zerosum7 table

shapiro_zerosum7 <- select_data %>%
  group_by(RACIALIDENTITY.4) %>%
  summarise(p = shapiro.test(ZEROSUM_7)$p.value)

kable(
  shapiro_zerosum7,
  caption = "Table 37. Shapiro–Wilk Normality Test for ZEROSUM_7 by Racial Identity",
  digits = 5,
  booktabs = TRUE
)
```

```{r}
#| label: kable format ZEROSUM_7 Kruskal-Wallis
#| echo: false
#| message: false
#| warning: false

library(tibble)
library(rempsyc)

kw.ZEROSUM_7.party <- kruskal.test(ZEROSUM_7 ~ POLITICALPARTY, data = select_data)

kw_table <- tibble(
  Predictor = c("POLITICALPARTY"),
  df = c(length(unique(select_data$POLITICALPARTY)) - 1),
  `Chi-squared` = c(round(kw.ZEROSUM_7.party$statistic, 2)),
  p = c(round(kw.ZEROSUM_7.party$p.value, 5))
)

if (knitr::is_html_output()) {
  kw_table %>%
    knitr::kable(
      digits = 5,
      escape = TRUE,
      format = "html",
      caption = "Table 38. Kruskal-Wallis Test Results for ZEROSUM_7"
    ) %>%
    kableExtra::kable_styling(bootstrap_options = c("striped", "hover", "condensed")) %>%
    kableExtra::row_spec(
      which(kw_table$p < 0.05),
      background = "gray"
    )
} else if (knitr::is_latex_output()) {
  kw_table %>%
    knitr::kable(
      digits = 5,
      escape = TRUE,
      format = "latex",
      longtable = TRUE,
      caption = "Kruskal-Wallis Test Results for ZEROSUM\\_7"
    ) %>%
    kableExtra::kable_styling(full_width = FALSE, position = "left") %>%
    kableExtra::row_spec(
      which(kw_table$p < 0.05),
      background = "gray!20"
    )
}
```

There was significant difference in ZEROSUM_7 scores across political party groups, Kruskal-Wallis χ²(`r kw.ZEROSUM_7.party$parameter`) = `r round(kw.ZEROSUM_7.party$statistic, 2)`, *p* = `r signif(kw.ZEROSUM_7.party$p.value, 3)`.

#### Paywomen vs. men

*Do zero-sum beliefs regarding gender pay equity differ by racial/ethnic identity and political party affiliation?*

Below we present descriptive statistics and visualizations for ZEROSUM_8 ("If there is equal pay for women, men will get lower wages."), examining how responses vary across racial identity and political affiliation.

```{r}
#| label: groupstats for ZERO_SUM8 
#| echo: false
#| message: false
#| warning: false

library(dplyr)
group_stats_8 <- select_data %>%
  group_by(POLITICALPARTY, RACIALIDENTITY.4) %>%
  summarise(
    mean_ZEROSUM_8 = mean(ZEROSUM_8, na.rm = TRUE),
    se = sd(ZEROSUM_8, na.rm = TRUE) / sqrt(n()),
    .groups = 'drop'
  )

# Check the structure
#str(group_stats_8)
```

```{r}
#| label: show groupstats for ZERO_SUM8 

head(group_stats_8)
```

```{r}
#| label: zerosum8 pointrange

# Define the zesty_four palette
zesty_four <- c("#E69F00", "#009E73", "#999999", "#CC79A7") 

# Create the plot with pointrange
plot.zerosum8 <- ggplot(group_stats_8, aes(POLITICALPARTY, mean_ZEROSUM_8)) +
  geom_pointrange(aes(color = RACIALIDENTITY.4, 
                      ymin = mean_ZEROSUM_8 - se,  # Use standard error instead of sd
                      ymax = mean_ZEROSUM_8 + se), # Use standard error instead of sd
                  position = position_dodge(0.3)) +
  theme_bw() +
  labs(title = "If there is equal pay for women, men will get lower wages.",
       x = "Political Affiliation",
       y = "Agreement with Zero Sum Beliefs",
       color = "RACIALIDENTITY.4") + 
  theme(
    plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
    axis.title = element_text(size = 10),
    axis.text = element_text(size = 8),
    legend.title = element_text(size = 10),
    legend.text = element_text(size = 8)
  ) +
  scale_y_continuous(
    breaks = 1:7,
    labels = c("1: Strongly Disbelieve", 
               "2: Disbelieve", 
               "3: Somewhat Disbelieve", 
               "4: Neither", 
               "5: Somewhat Believe", 
               "6: Believe", 
               "7: Strongly Believe"),
    limits = c(1, 7)  # Set the y-axis limits to match the scale
  )+
  scale_color_manual(values = zesty_four)

# Print and save the plots
print(plot.zerosum8)
ggsave("plots/plot8:paywomen-men.png", 
       plot = plot.zerosum8, 
       width = 10, 
       height = 8, 
       dpi = 300)
```

This pointrange plot shows that White respondents exhibit higher agreement with the belief that “If there is equal pay for women, men will get lower wages.” among Republicans. Among political groups, Republicans show the highest overall agreement, while Democrats across all racial groups showed lower levels of agreement (2-3 range), with white respondents showing the lowest agreement. This pattern highlights how political affiliation creates major divisions in zero-sum beliefs about gender pay equality, with racial differences more pronounced among Republicans than other political groups.

```{r}
#| label: ZEROSUM_8 average score with 95% CI

# ZEROSUM_8 average score with 95% CI
group_stats_8_avg <- select_data %>%
  group_by(POLITICALPARTY) %>%
  summarise(
    mean_ZEROSUM_8 = mean(ZEROSUM_8, na.rm = TRUE),
    se = sd(ZEROSUM_8, na.rm = TRUE) / sqrt(n()),
    n = n(),
    .groups = 'drop'
  ) %>%
  mutate(
    # Calculate 95% confidence interval using t-distribution
    t_value = qt(0.975, df = n - 1),  # 95% CI, two-tailed
    ci_lower = mean_ZEROSUM_8 - t_value * se,
    ci_upper = mean_ZEROSUM_8 + t_value * se
  )
```

*Do zero-sum beliefs regarding gender pay equity differ by political party affiliation?*

```{r}
#| label: plot.zerosum8_enhanced and Raincloud plot

plot.zerosum8_raincloud <- select_data %>%
  filter(!is.na(ZEROSUM_8), !is.na(POLITICALPARTY)) %>%
  ggplot(aes(x = POLITICALPARTY, y = ZEROSUM_8, fill = POLITICALPARTY, color = POLITICALPARTY)) +
  stat_halfeye(
    adjust = 0.5,
    width = 0.6,
    .width = 0,
    justification = -0.2,
    point_colour = NA,
    alpha = 0.7
  ) +
   geom_boxplot(
    width = 0.15,
    outlier.shape = NA,
    alpha = 0.5
  ) +
  geom_point(
    size = 1.3,
    alpha = 0.3,
    position = position_jitter(
      seed = 1, width = 0.1
    )
  ) +
  stat_summary(
    fun = mean,
    geom = "point",
    size = 3,
    color = "white",
    stroke = 1.5,
    shape = 21
  ) +
  scale_fill_manual(values = party_colors) +
  scale_color_manual(values = party_colors) +
  theme_bw() +
  labs(
    title = "If there is equal pay for women, men will get lower wages.",
    x = "Political Affiliation",
    y = "Level of Agreement",
    caption = "White dots = means; colored dots = individual responses"
  ) +
  theme(
    plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
    plot.caption = element_text(size = 9, color = "gray40", hjust = 0.5),
    axis.title = element_text(size = 12, face = "bold"),
    axis.text = element_text(size = 10),
    legend.position = "none",
    panel.grid.major.y = element_line(color = "gray90", linewidth = 0.3)  # Fixed
  ) +
  scale_y_continuous(
    breaks = 1:7,
    labels = c("1: Strongly Disbelieve", 
               "2: Disbelieve", 
               "3: Somewhat Disbelieve", 
               "4: Neither", 
               "5: Somewhat Believe", 
               "6: Believe", 
               "7: Strongly Believe"),
    limits = c(1, 7) 
  )
plot.zerosum8_raincloud

```

This raincloud plot shows that mean agreement with the zero-sum belief (indicated by white dots) varies by political affiliation. Among the three groups, Republican exhibit higher average agreement with the statement “If there is equal pay for women, men will get lower wages." Independents and Democrats show similar mean agreement levels at lower values. The distribution (via density and individual dots) reveals different patterns across groups. Republicans show a relatively concentrated distribution around moderate agreement levels (3-5) with their density peaked around the mean. Both independents and Democrats show very similar distributions, ranging widely across low to moderate levels of agreement (1-5). This suggests that both groups have similar overall skepticism about this zero-sum view of gender pay equality.

```{r}
#| label: shapiro.test zerosum8 table

shapiro_zerosum8 <- select_data %>%
  group_by(RACIALIDENTITY.4) %>%
  summarise(p = shapiro.test(ZEROSUM_8)$p.value)

kable(
  shapiro_zerosum8,
  caption = "Table 39. Shapiro–Wilk Normality Test for ZEROSUM_8 by Racial Identity",
  digits = 5,
  booktabs = TRUE
)
```

```{r}
#| label: kable format ZEROSUM_8 Kruskal-Wallis
#| echo: false
#| message: false
#| warning: false

library(tibble)
library(rempsyc)

kw.ZEROSUM_8.party <- kruskal.test(ZEROSUM_8 ~ POLITICALPARTY, data = select_data)

kw_table <- tibble(
  Predictor = c("POLITICALPARTY"),
  df = c(length(unique(select_data$POLITICALPARTY)) - 1),
  `Chi-squared` = c(round(kw.ZEROSUM_8.party$statistic, 2)),
  p = c(round(kw.ZEROSUM_8.party$p.value, 5))
)

if (knitr::is_html_output()) {
  kw_table %>%
    knitr::kable(
      digits = 5,
      escape = TRUE,
      format = "html",
      caption = "Table 40. Kruskal-Wallis Test Results for ZEROSUM_8"
    ) %>%
    kableExtra::kable_styling(bootstrap_options = c("striped", "hover", "condensed")) %>%
    kableExtra::row_spec(
      which(kw_table$p < 0.05),
      background = "gray"
    )
} else if (knitr::is_latex_output()) {
  kw_table %>%
    knitr::kable(
      digits = 5,
      escape = TRUE,
      format = "latex",
      longtable = TRUE,
      caption = "Kruskal-Wallis Test Results for ZEROSUM\\_8"
    ) %>%
    kableExtra::kable_styling(full_width = FALSE, position = "left") %>%
    kableExtra::row_spec(
      which(kw_table$p < 0.05),
      background = "gray!20"
    )
}
```

There was significant difference in ZEROSUM_8 scores across political party groups, Kruskal-Wallis χ²(`r kw.ZEROSUM_8.party$parameter`) = `r round(kw.ZEROSUM_8.party$statistic, 2)`, *p* = `r signif(kw.ZEROSUM_8.party$p.value, 3)`.

#### LGBTQ vs. Religious

*Do zero-sum beliefs regarding LGBTQ+ rights and religious freedom differ by racial/ethnic identity and political party affiliation?*

Below we present descriptive statistics and visualizations for ZEROSUM_9 ("LGBTQ+ rights mean less freedom for religious groups."), examining how responses vary across racial identity and political affiliation.

```{r}
#| label: groupstats for ZERO_SUM9 
#| echo: false
#| message: false
#| warning: false

library(dplyr)
group_stats_9 <- select_data %>%
  group_by(POLITICALPARTY, RACIALIDENTITY.4) %>%
  summarise(
    mean_ZEROSUM_9 = mean(ZEROSUM_9, na.rm = TRUE),
    se = sd(ZEROSUM_9, na.rm = TRUE) / sqrt(n()),
    .groups = 'drop'
  )

# Check the structure
#str(group_stats_9)
```

```{r}
#| label: show groupstats for ZERO_SUM9 

head(group_stats_9)
```

```{r}
#| label: zerosum9 pointrange

# Define the zesty_four palette
zesty_four <- c("#E69F00", "#009E73", "#999999", "#CC79A7") 

# Create the plot with pointrange
plot.zerosum9 <- ggplot(group_stats_9, aes(POLITICALPARTY, mean_ZEROSUM_9)) +
  geom_pointrange(aes(color = RACIALIDENTITY.4, 
                      ymin = mean_ZEROSUM_9 - se,  # Use standard error instead of sd
                      ymax = mean_ZEROSUM_9 + se), # Use standard error instead of sd
                  position = position_dodge(0.3)) +
  theme_bw() +
  labs(title = "LGBTQ+ rights mean less freedom for religious groups.",
       x = "Political Affiliation",
       y = "Agreement with Zero Sum Beliefs",
       color = "RACIALIDENTITY.4") + 
  theme(
    plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
    axis.title = element_text(size = 10),
    axis.text = element_text(size = 8),
    legend.title = element_text(size = 10),
    legend.text = element_text(size = 8)
  ) +
  scale_y_continuous(
    breaks = 1:7,
    labels = c("1: Strongly Disbelieve", 
               "2: Disbelieve", 
               "3: Somewhat Disbelieve", 
               "4: Neither", 
               "5: Somewhat Believe", 
               "6: Believe", 
               "7: Strongly Believe"),
    limits = c(1, 7)  # Set the y-axis limits to match the scale
  )+
  scale_color_manual(values = zesty_four)

# Print and save the plots
print(plot.zerosum9)
ggsave("plots/plot9:LGBTQ-religious.png", 
       plot = plot.zerosum9, 
       width = 10, 
       height = 8, 
       dpi = 300)
```

This pointrange plot shows that Black respondents exhibit higher agreement with the belief that “LGBTQ+ rights mean less freedom for religious groups." among Republicans. Among political groups, Republicans show the highest overall agreement, while Democrats across all racial groups showed lower levels of agreement (1-2 range), with Mixed/Other and Asian respondents showing the lowest agreement. This pattern captures how political affiliation creates major divisions in zero-sum beliefs about LGBTQ+ and religious rights, with significant racial differences occurring primarily among Republicans.

```{r}
#| label: ZEROSUM_9 average score with 95% CI

# ZEROSUM_9 average score with 95% CI
group_stats_9_avg <- select_data %>%
  group_by(POLITICALPARTY) %>%
  summarise(
    mean_ZEROSUM_9 = mean(ZEROSUM_9, na.rm = TRUE),
    se = sd(ZEROSUM_9, na.rm = TRUE) / sqrt(n()),
    n = n(),
    .groups = 'drop'
  ) %>%
  mutate(
    # Calculate 95% confidence interval using t-distribution
    t_value = qt(0.975, df = n - 1),  # 95% CI, two-tailed
    ci_lower = mean_ZEROSUM_9 - t_value * se,
    ci_upper = mean_ZEROSUM_9 + t_value * se
  )
```

*Do zero-sum beliefs regarding LGBTQ+ rights and religious freedom differ by political party affiliation?*

```{r}
#| label: plot.zerosum9_enhanced and Raincloud plot

plot.zerosum9_raincloud <- select_data %>%
  filter(!is.na(ZEROSUM_9), !is.na(POLITICALPARTY)) %>%
  ggplot(aes(x = POLITICALPARTY, y = ZEROSUM_9, fill = POLITICALPARTY, color = POLITICALPARTY)) +
  stat_halfeye(
    adjust = 0.5,
    width = 0.6,
    .width = 0,
    justification = -0.2,
    point_colour = NA,
    alpha = 0.7
  ) +
   geom_boxplot(
    width = 0.15,
    outlier.shape = NA,
    alpha = 0.5
  ) +
  geom_point(
    size = 1.3,
    alpha = 0.3,
    position = position_jitter(
      seed = 1, width = 0.1
    )
  ) +
  stat_summary(
    fun = mean,
    geom = "point",
    size = 3,
    color = "white",
    stroke = 1.5,
    shape = 21
  ) +
  scale_fill_manual(values = party_colors) +
  scale_color_manual(values = party_colors) +
  theme_bw() +
  labs(
    title = "LGBTQ+ rights mean less freedom for religious groups.",
    x = "Political Affiliation",
    y = "Level of Agreement",
    caption = "White dots = means; colored dots = individual responses"
  ) +
  theme(
    plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
    plot.caption = element_text(size = 9, color = "gray40", hjust = 0.5),
    axis.title = element_text(size = 12, face = "bold"),
    axis.text = element_text(size = 10),
    legend.position = "none",
    panel.grid.major.y = element_line(color = "gray90", linewidth = 0.3)  # Fixed
  ) +
  scale_y_continuous(
    breaks = 1:7,
    labels = c("1: Strongly Disbelieve", 
               "2: Disbelieve", 
               "3: Somewhat Disbelieve", 
               "4: Neither", 
               "5: Somewhat Believe", 
               "6: Believe", 
               "7: Strongly Believe"),
    limits = c(1, 7) 
  )
plot.zerosum9_raincloud

```

This raincloud plot shows that mean agreement with the zero-sum belief (indicated by white dots) varies by political affiliation. Among the three groups, Republican exhibit higher average agreement with the statement “LGBTQ+ rights mean less freedom for religious groups." The distribution (via density and individual dots) reveals different patterns across groups. Republicans show a broad distribution with responses spanning from low to high agreement (2-7) and notable density across moderate-to-high agreement levels. Independents display leftward skew with clustering in the low agreement range (1-3) and a tail extending toward higher values. Democrats show even stronger leftward skew with pronounced clustering in the very low agreement range (1-3) and a steep drop-off at higher values. This suggests that most Democrats and independents reject this zero-sum view of LGBTQ+ and religious rights, with Democrats showing more extreme opposition.

```{r}
#| label: shapiro.test zerosum9 table

shapiro_zerosum9 <- select_data %>%
  group_by(RACIALIDENTITY.4) %>%
  summarise(p = shapiro.test(ZEROSUM_9)$p.value)

kable(
  shapiro_zerosum9,
  caption = "Table 41. Shapiro–Wilk Normality Test for ZEROSUM_9 by Racial Identity",
  digits = 6,
  booktabs = TRUE
)
```

```{r}
#| label: kable format ZEROSUM_9 Kruskal-Wallis
#| echo: false
#| message: false
#| warning: false

library(tibble)
library(rempsyc)

kw.ZEROSUM_9.party <- kruskal.test(ZEROSUM_9 ~ POLITICALPARTY, data = select_data)

kw_table <- tibble(
  Predictor = c("POLITICALPARTY"),
  df = c(length(unique(select_data$POLITICALPARTY)) - 1),
  `Chi-squared` = c(round(kw.ZEROSUM_9.party$statistic, 2)),
  p = c(round(kw.ZEROSUM_9.party$p.value, 5))
)

if (knitr::is_html_output()) {
  kw_table %>%
    knitr::kable(
      digits = 5,
      escape = TRUE,
      format = "html",
      caption = "Table 42. Kruskal-Wallis Test Results for ZEROSUM_9"
    ) %>%
    kableExtra::kable_styling(bootstrap_options = c("striped", "hover", "condensed")) %>%
    kableExtra::row_spec(
      which(kw_table$p < 0.05),
      background = "gray"
    )
} else if (knitr::is_latex_output()) {
  kw_table %>%
    knitr::kable(
      digits = 5,
      escape = TRUE,
      format = "latex",
      longtable = TRUE,
      caption = "Kruskal-Wallis Test Results for ZEROSUM\\_9"
    ) %>%
    kableExtra::kable_styling(full_width = FALSE, position = "left") %>%
    kableExtra::row_spec(
      which(kw_table$p < 0.05),
      background = "gray!20"
    )
}
```

There was significant difference in ZEROSUM_9 scores across political party groups, Kruskal-Wallis χ²(`r kw.ZEROSUM_9.party$parameter`) = `r round(kw.ZEROSUM_9.party$statistic, 2)`, *p* = `r signif(kw.ZEROSUM_9.party$p.value, 3)`.

#### Disabilities vs. Non-disabilities

*Do zero-sum beliefs regarding disability and non-disability healthcare time differ by racial/ethnic identity and political party affiliation?*

Below we present descriptive statistics and visualizations for ZEROSUM_10 ("Accessible healthcare for people with disabilities means longer wait times for non-disabled patients."), examining how responses vary across racial identity and political affiliation.

```{r}
#| label: groupstats for ZERO_SUM10 
#| echo: false
#| message: false
#| warning: false

library(dplyr)
group_stats_10 <- select_data %>%
  group_by(POLITICALPARTY, RACIALIDENTITY.4) %>%
  summarise(
    mean_ZEROSUM_10 = mean(ZEROSUM_10, na.rm = TRUE),
    se = sd(ZEROSUM_10, na.rm = TRUE) / sqrt(n()),
    .groups = 'drop'
  )

# Check the structure
# str(group_stats_10)
```

```{r}
#| label: show groupstats for ZERO_SUM10 

head(group_stats_10)
```

```{r}
#| label: zerosum10 pointrange

# Define the zesty_four palette
zesty_four <- c("#E69F00", "#009E73", "#999999", "#CC79A7") 

# Create the plot with pointrange
plot.zerosum10 <- ggplot(group_stats_10, aes(POLITICALPARTY, mean_ZEROSUM_10)) +
  geom_pointrange(aes(color = RACIALIDENTITY.4, 
                      ymin = mean_ZEROSUM_10 - se,  # Use standard error instead of sd
                      ymax = mean_ZEROSUM_10 + se), # Use standard error instead of sd
                  position = position_dodge(0.3)) +
  theme_bw() +
  labs(title = "Accessible healthcare for people with disabilities means \n longer wait times for non-disabled patients.",
       x = "Political Affiliation",
       y = "Agreement with Zero Sum Beliefs",
       color = "RACIALIDENTITY.4") + 
  theme(
    plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
    axis.title = element_text(size = 10),
    axis.text = element_text(size = 8),
    legend.title = element_text(size = 10),
    legend.text = element_text(size = 8)
  ) +
  scale_y_continuous(
    breaks = 1:7,
    labels = c("1: Strongly Disbelieve", 
               "2: Disbelieve", 
               "3: Somewhat Disbelieve", 
               "4: Neither", 
               "5: Somewhat Believe", 
               "6: Believe", 
               "7: Strongly Believe"),
    limits = c(1, 7)  # Set the y-axis limits to match the scale
  )+
  scale_color_manual(values = zesty_four)

# Print and save the plots
print(plot.zerosum10)
ggsave("plots/plot10:disabilities-non.png", 
       plot = plot.zerosum10, 
       width = 10, 
       height = 8, 
       dpi = 300)

```

This pointrange plot shows that Black respondents exhibit higher agreement with the belief that “Accessible healthcare for people with disabilities means longer wait times for non-disabled patients." among Republicans. Among political groups, Republicans show the highest overall agreement, while Democrats across all racial groups showed lower levels of agreement (2-3 range), with relatively small differences between groups. This pattern captures how political affiliation creates major divisions in zero-sum beliefs about disability healthcare access, with racial differences being most pronounced among Republicans.

```{r}
#| label: ZEROSUM_10 average score with 95% CI

# ZEROSUM_10 average score with 95% CI
group_stats_10_avg <- select_data %>%
  group_by(POLITICALPARTY) %>%
  summarise(
    mean_ZEROSUM_10 = mean(ZEROSUM_10, na.rm = TRUE),
    se = sd(ZEROSUM_10, na.rm = TRUE) / sqrt(n()),
    n = n(),
    .groups = 'drop'
  ) %>%
  mutate(
    # Calculate 95% confidence interval using t-distribution
    t_value = qt(0.975, df = n - 1),  # 95% CI, two-tailed
    ci_lower = mean_ZEROSUM_10 - t_value * se,
    ci_upper = mean_ZEROSUM_10 + t_value * se
  )
```

*Do zero-sum beliefs regarding disability and non-disability healthcare time differ by political party affiliation?*

```{r}
#| label: plot.zerosum10_enhanced and Raincloud plot

plot.zerosum10_raincloud <- select_data %>%
  filter(!is.na(ZEROSUM_10), !is.na(POLITICALPARTY)) %>%
  ggplot(aes(x = POLITICALPARTY, y = ZEROSUM_10, fill = POLITICALPARTY, color = POLITICALPARTY)) +
  stat_halfeye(
    adjust = 0.5,
    width = 0.6,
    .width = 0,
    justification = -0.2,
    point_colour = NA,
    alpha = 0.7
  ) +
   geom_boxplot(
    width = 0.15,
    outlier.shape = NA,
    alpha = 0.5
  ) +
  geom_point(
    size = 1.3,
    alpha = 0.3,
    position = position_jitter(
      seed = 1, width = 0.1
    )
  ) +
  stat_summary(
    fun = mean,
    geom = "point",
    size = 3,
    color = "white",
    stroke = 1.5,
    shape = 21
  ) +
  scale_fill_manual(values = party_colors) +
  scale_color_manual(values = party_colors) +
  theme_bw() +
  labs(
    title = "Accessible healthcare for people with disabilities means \n longer wait times for non-disabled patients.",
    x = "Political Affiliation",
    y = "Level of Agreement",
    caption = "White dots = means; colored dots = individual responses"
  ) +
  theme(
    plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
    plot.caption = element_text(size = 9, color = "gray40", hjust = 0.5),
    axis.title = element_text(size = 12, face = "bold"),
    axis.text = element_text(size = 10),
    legend.position = "none",
    panel.grid.major.y = element_line(color = "gray90", linewidth = 0.3)  # Fixed
  ) +
  scale_y_continuous(
    breaks = 1:7,
    labels = c("1: Strongly Disbelieve", 
               "2: Disbelieve", 
               "3: Somewhat Disbelieve", 
               "4: Neither", 
               "5: Somewhat Believe", 
               "6: Believe", 
               "7: Strongly Believe"),
    limits = c(1, 7) 
  )
plot.zerosum10_raincloud

```

This raincloud plot shows that mean agreement with the zero-sum belief (indicated by white dots) varies by political affiliation. Among the three groups, Republican exhibit higher average agreement with the statement “Accessible healthcare for people with disabilities means longer wait times for non-disabled patients." The distribution (via density and individual dots) reveals different patterns across groups. Republicans show a broad distribution with responses spanning from low to high agreement (1-7) and notable density across moderate-to-high agreement levels. Independents display leftward skew with clustering in the low-to-moderate agreement range (1-4) and their density peaked in the lower range. Democrats show strong leftward skew with pronounced clustering in the low agreement range (1-3) and a steep drop-off at higher values. This suggests that most Democrats reject this zero-sum perspective on disability healthcare access.

```{r}
#| label: shapiro.test zerosum10 table

shapiro_zerosum10 <- select_data %>%
  group_by(RACIALIDENTITY.4) %>%
  summarise(p = shapiro.test(ZEROSUM_10)$p.value)

kable(
  shapiro_zerosum10,
  caption = "Table 43. Shapiro–Wilk Normality Test for ZEROSUM_10 by Racial Identity",
  digits = 5,
  booktabs = TRUE
)
```

```{r}
#| label: kable format ZEROSUM_10 Kruskal-Wallis
#| echo: false
#| message: false
#| warning: false

library(tibble)
library(rempsyc)

kw.ZEROSUM_10.party <- kruskal.test(ZEROSUM_10 ~ POLITICALPARTY, data = select_data)

kw_table <- tibble(
  Predictor = c("POLITICALPARTY"),
  df = c(length(unique(select_data$POLITICALPARTY)) - 1),
  `Chi-squared` = c(round(kw.ZEROSUM_10.party$statistic, 2)),
  p = c(round(kw.ZEROSUM_10.party$p.value, 5))
)

if (knitr::is_html_output()) {
  kw_table %>%
    knitr::kable(
      digits = 5,
      escape = TRUE,
      format = "html",
      caption = "Table 44. Kruskal-Wallis Test Results for ZEROSUM_10"
    ) %>%
    kableExtra::kable_styling(bootstrap_options = c("striped", "hover", "condensed")) %>%
    kableExtra::row_spec(
      which(kw_table$p < 0.05),
      background = "gray"
    )
} else if (knitr::is_latex_output()) {
  kw_table %>%
    knitr::kable(
      digits = 5,
      escape = TRUE,
      format = "latex",
      longtable = TRUE,
      caption = "Kruskal-Wallis Test Results for ZEROSUM\\_10"
    ) %>%
    kableExtra::kable_styling(full_width = FALSE, position = "left") %>%
    kableExtra::row_spec(
      which(kw_table$p < 0.05),
      background = "gray!20"
    )
}
```

There was significant difference in ZEROSUM_10 scores across political party groups, Kruskal-Wallis χ²(`r kw.ZEROSUM_10.party$parameter`) = `r round(kw.ZEROSUM_10.party$statistic, 2)`, *p* = `r signif(kw.ZEROSUM_10.party$p.value, 3)`.

#### Healthcare vs. Private

*Do zero-sum beliefs about universal healthcare differ by racial/ethnic identity and political party affiliation?*

Below we present descriptive statistics and visualizations for ZEROSUM_11 ("Universal healthcare means worse healthcare for those who can afford private insurance."), examining how responses vary across racial identity and political affiliation.

```{r}
#| label: groupstats for ZERO_SUM11 
#| echo: false
#| message: false
#| warning: false

library(dplyr)
group_stats_11 <- select_data %>%
  group_by(POLITICALPARTY, RACIALIDENTITY.4) %>%
  summarise(
    mean_ZEROSUM_11 = mean(ZEROSUM_11, na.rm = TRUE),
    se = sd(ZEROSUM_11, na.rm = TRUE) / sqrt(n()),
    .groups = 'drop'
  )

# Check the structure
# str(group_stats_11)
```

```{r}
#| label: show groupstats for ZERO_SUM11 

head(group_stats_11)
```

```{r}
#| label: zerosum11 pointrange

# Define the zesty_four palette
zesty_four <- c("#E69F00", "#009E73", "#999999", "#CC79A7") 

# Create the plot with pointrange
plot.zerosum11 <- ggplot(group_stats_11, aes(POLITICALPARTY, mean_ZEROSUM_11)) +
  geom_pointrange(aes(color = RACIALIDENTITY.4, 
                      ymin = mean_ZEROSUM_11 - se,  # Use standard error instead of sd
                      ymax = mean_ZEROSUM_11 + se), # Use standard error instead of sd
                  position = position_dodge(0.3)) +
  theme_bw() +
  labs(title = "Universal healthcare means worse healthcare for those who can afford private insurance.",
       x = "Political Affiliation",
       y = "Agreement with Zero Sum Beliefs",
       color = "RACIALIDENTITY.4") + 
  theme(
    plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
    axis.title = element_text(size = 10),
    axis.text = element_text(size = 8),
    legend.title = element_text(size = 10),
    legend.text = element_text(size = 8)
  ) +
  scale_y_continuous(
    breaks = 1:7,
    labels = c("1: Strongly Disbelieve", 
               "2: Disbelieve", 
               "3: Somewhat Disbelieve", 
               "4: Neither", 
               "5: Somewhat Believe", 
               "6: Believe", 
               "7: Strongly Believe"),
    limits = c(1, 7)  # Set the y-axis limits to match the scale
  )+
  scale_color_manual(values = zesty_four)

# Print and save the plots
print(plot.zerosum11)
ggsave("plots/plot11:healthcare.png", 
       plot = plot.zerosum11, 
       width = 10, 
       height = 8, 
       dpi = 300)


```

This pointrange plot shows that White respondents exhibit higher agreement with the belief that “Universal healthcare means worse healthcare for those who can afford private insurance." among Republicans. Among political groups, Republicans show the highest overall agreement, while Democrats (particularly Asian and Mixed/Other respondents) show the lowest agreement. This pattern captures how political affiliation creates major divisions in zero-sum beliefs about universal healthcare, with racial differences being most pronounced among Republicans.

```{r}
#| label: ZEROSUM_11 average score with 95% CI

# ZEROSUM_11 average score with 95% CI
group_stats_11_avg <- select_data %>%
  group_by(POLITICALPARTY) %>%
  summarise(
    mean_ZEROSUM_11 = mean(ZEROSUM_11, na.rm = TRUE),
    se = sd(ZEROSUM_11, na.rm = TRUE) / sqrt(n()),
    n = n(),
    .groups = 'drop'
  ) %>%
  mutate(
    # Calculate 95% confidence interval using t-distribution
    t_value = qt(0.975, df = n - 1),  # 95% CI, two-tailed
    ci_lower = mean_ZEROSUM_11 - t_value * se,
    ci_upper = mean_ZEROSUM_11 + t_value * se
  )
```

*Do zero-sum beliefs about universal healthcare differ by political party affiliation?*

```{r}
#| label: plot.zerosum11_enhanced and Raincloud plot

plot.zerosum11_raincloud <- select_data %>%
  filter(!is.na(ZEROSUM_11), !is.na(POLITICALPARTY)) %>%
  ggplot(aes(x = POLITICALPARTY, y = ZEROSUM_11, fill = POLITICALPARTY, color = POLITICALPARTY)) +
  stat_halfeye(
    adjust = 0.5,
    width = 0.6,
    .width = 0,
    justification = -0.2,
    point_colour = NA,
    alpha = 0.7
  ) +
   geom_boxplot(
    width = 0.15,
    outlier.shape = NA,
    alpha = 0.5
  ) +
  geom_point(
    size = 1.3,
    alpha = 0.3,
    position = position_jitter(
      seed = 1, width = 0.1
    )
  ) +
  stat_summary(
    fun = mean,
    geom = "point",
    size = 3,
    color = "white",
    stroke = 1.5,
    shape = 21
  ) +
  scale_fill_manual(values = party_colors) +
  scale_color_manual(values = party_colors) +
  theme_bw() +
  labs(
    title = "Universal healthcare means worse healthcare for those \n who can afford private insurance.",
    x = "Political Affiliation",
    y = "Level of Agreement",
    caption = "White dots = means; colored dots = individual responses"
  ) +
  theme(
    plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
    plot.caption = element_text(size = 9, color = "gray40", hjust = 0.5),
    axis.title = element_text(size = 12, face = "bold"),
    axis.text = element_text(size = 10),
    legend.position = "none",
    panel.grid.major.y = element_line(color = "gray90", linewidth = 0.3)  # Fixed
  ) +
  scale_y_continuous(
    breaks = 1:7,
    labels = c("1: Strongly Disbelieve", 
               "2: Disbelieve", 
               "3: Somewhat Disbelieve", 
               "4: Neither", 
               "5: Somewhat Believe", 
               "6: Believe", 
               "7: Strongly Believe"),
    limits = c(1, 7) 
  )
plot.zerosum11_raincloud

```

This raincloud plot shows that mean agreement with the zero-sum belief (indicated by white dots) varies by political affiliation. Among the three groups, Republican exhibit higher average agreement with the statement “Universal healthcare means worse healthcare for those who can afford private insurance." The distribution (via density and individual dots) reveals different patterns across groups. **Republicans show a broad distribution** with responses spanning from low to high agreement (2-7) and notable density across moderate-to-high agreement levels. **Independents display leftward skew** with clustering in the low-to-moderate agreement range (1-5) and their density peaked in the lower-moderate range. **Democrats show strong leftward skew** with pronounced clustering in the low-to-moderate agreement range (1-4) and a steep drop-off at higher values. This suggest that most Democrats reject this zero-sum perspective on universal healthcare policy.

```{r}
#| label: shapiro.test zerosum11 table

shapiro_zerosum11 <- select_data %>%
  group_by(RACIALIDENTITY.4) %>%
  summarise(p = shapiro.test(ZEROSUM_11)$p.value)

kable(
  shapiro_zerosum11,
  caption = "Table 45. Shapiro–Wilk Normality Test for ZEROSUM_11 by Racial Identity",
  digits = 5,
  booktabs = TRUE
)
```

```{r}
#| label: kable format ZEROSUM_11 Kruskal-Wallis
#| echo: false
#| message: false
#| warning: false

library(tibble)
library(rempsyc)

kw.ZEROSUM_11.party <- kruskal.test(ZEROSUM_11 ~ POLITICALPARTY, data = select_data)

kw_table <- tibble(
  Predictor = c("POLITICALPARTY"),
  df = c(length(unique(select_data$POLITICALPARTY)) - 1),
  `Chi-squared` = c(round(kw.ZEROSUM_11.party$statistic, 2)),
  p = c(round(kw.ZEROSUM_11.party$p.value, 5))
)

if (knitr::is_html_output()) {
  kw_table %>%
    knitr::kable(
      digits = 3,
      escape = TRUE,
      format = "html",
      caption = "Table 46. Kruskal-Wallis Test Results for ZEROSUM_11"
    ) %>%
    kableExtra::kable_styling(bootstrap_options = c("striped", "hover", "condensed")) %>%
    kableExtra::row_spec(
      which(kw_table$p < 0.05),
      background = "gray"
    )
} else if (knitr::is_latex_output()) {
  kw_table %>%
    knitr::kable(
      digits = 3,
      escape = TRUE,
      format = "latex",
      longtable = TRUE,
      caption = "Kruskal-Wallis Test Results for ZEROSUM\\_11"
    ) %>%
    kableExtra::kable_styling(full_width = FALSE, position = "left") %>%
    kableExtra::row_spec(
      which(kw_table$p < 0.05),
      background = "gray!20"
    )
}
```

There was significant difference in ZEROSUM_11 scores across political party groups, Kruskal-Wallis χ²(`r kw.ZEROSUM_11.party$parameter`) = `r round(kw.ZEROSUM_11.party$statistic, 2)`, *p* = `r signif(kw.ZEROSUM_11.party$p.value, 3)`.

### Explaining Zero-Sum Economic Beliefs (multiple linear regression)

*What sociodemographic factors explain zero-sum economic beliefs?*

```{r}
#| label: kable multiple linear regression ZEROSUM_ECONOMIC
#| echo: false
#| message: false
#| warning: false

model_economic <- lm(ZEROSUM_ECONOMIC ~ GENDER_MALE + RELIGIOUS_YES + RACE_BLACK +
                       RACE_ASIAN + RACE_OTHER + EDUCATION_HIGH + SOCIALSTATUS,
                     data = select_data)

reg_table_economic <- rempsyc::nice_lm(model_economic, standardized = FALSE)

# add correct 95% CI for coefficients (b)
ci <- confint(model_economic)
reg_table_economic$CI.lower <- round(ci[-1, 1], 2)
reg_table_economic$CI.upper <- round(ci[-1, 2], 2)

# remove sr2 confidence interval columns
reg_table_clean <- reg_table_economic %>%
  select(-c(`CI_lower`, `CI_upper`))  # those are for sr²

# rename CI columns to "95% CI"
reg_table_clean <- reg_table_clean %>%
  mutate(`95% CI` = paste0("[", CI.lower, ", ", CI.upper, "]")) %>%
  select(-CI.lower, -CI.upper)

sig_rows <- which(as.numeric(reg_table_clean$p) < 0.05)

if (knitr::is_html_output()) {
  knitr::kable(
    reg_table_clean,
    digits = 2,
    escape = TRUE,
    format = "html",
    align = "c",
    caption = "Table 47. Multiple Linear Regression Predicting Economic Zero-Sum Beliefs"
  ) %>%
    kableExtra::kable_styling(bootstrap_options = c("striped", "hover", "condensed")) %>%
    kableExtra::row_spec(sig_rows, background = "gray")
} else if (knitr::is_latex_output()) {
  knitr::kable(
    reg_table_clean,
    digits = 2,
    escape = TRUE,
    format = "latex",
    longtable = TRUE,
    align = "c",
    caption = "Multiple Linear Regression Predicting Economic Zero-Sum Beliefs"
  ) %>%
    kableExtra::kable_styling(full_width = FALSE, position = "left") %>%
    kableExtra::row_spec(sig_rows, background = "gray!20")
}
```

```{r}
#| label: plot linear regression ZEROSUM_ECONOMIC.RACE_ASIAN
# Create prediction data
pred_data_economic <- with(select_data, 
  data.frame(
    RACE_ASIAN = seq(min(RACE_ASIAN, na.rm = TRUE),
                       max(RACE_ASIAN, na.rm = TRUE),
                       length = 100),
    GENDER_MALE = mean(GENDER_MALE, na.rm = TRUE),
    RELIGIOUS_YES = mean(RELIGIOUS_YES, na.rm = TRUE),
    RACE_BLACK = mean(RACE_BLACK, na.rm = TRUE),
    RACE_OTHER = mean(RACE_OTHER, na.rm = TRUE),
    EDUCATION_HIGH = mean(EDUCATION_HIGH, na.rm = TRUE),
    SOCIALSTATUS = mean(SOCIALSTATUS, na.rm = TRUE)
  ))

# Get predictions
pred_data_economic$predicted <- predict(model_economic, pred_data_economic)
pred_data_economic$se <- predict(model_economic, pred_data_economic, se.fit = TRUE)$se.fit

# Confidence intervals
pred_data_economic$lower_ci <- pred_data_economic$predicted - 1.96 * pred_data_economic$se
pred_data_economic$upper_ci <- pred_data_economic$predicted + 1.96 * pred_data_economic$se

# Plot
plot.ZEROSUM_ECONOMIC.RACE_ASIAN <- ggplot(pred_data_economic, aes(x = RACE_ASIAN, y = predicted)) +
  geom_ribbon(aes(ymin = lower_ci, ymax = upper_ci), fill = "green", alpha = 0.2) +
  geom_line(color = "green", size = 1) +
  labs(title = "Predicted Identity Zero-Sum Beliefs by Race Asian",
       subtitle = "Holding Other Variables Constant",
       x = "Race Asian",
       y = "Predicted Economic Zero-Sum Beliefs") +
  theme_minimal()

print(plot.ZEROSUM_ECONOMIC.RACE_ASIAN)
ggsave("plots/plot12:ZEROSUM_ECONOMIC.RACE_ASIAN.png", 
       plot = plot.ZEROSUM_ECONOMIC.RACE_ASIAN, 
       width = 10, 
       height = 8, 
       dpi = 300)

```

Asian participants reported significantly lower levels of economic zero-sum beliefs compared to White participants (β = -0.90, p = .006), indicating a negative association between identifying as Asian and the belief that economic resources are fixed and must be competed for. This suggests that Asian individuals may be less likely to perceive economic outcomes as a zero-sum competition between groups.

```{r}
#| label: plot linear regression ZEROSUM_ECONOMIC.SOCIALSTATUS
# Create prediction data
pred_data_economic <- with(select_data, 
  data.frame(
    SOCIALSTATUS = seq(min(SOCIALSTATUS, na.rm = TRUE),
                       max(SOCIALSTATUS, na.rm = TRUE),
                       length = 100),
    GENDER_MALE = mean(GENDER_MALE, na.rm = TRUE),
    RELIGIOUS_YES = mean(RELIGIOUS_YES, na.rm = TRUE),
    RACE_BLACK = mean(RACE_BLACK, na.rm = TRUE),
    RACE_ASIAN = mean(RACE_ASIAN, na.rm = TRUE),
    RACE_OTHER = mean(RACE_OTHER, na.rm = TRUE),
    EDUCATION_HIGH = mean(EDUCATION_HIGH, na.rm = TRUE)
  ))

# Get predictions
pred_data_economic$predicted <- predict(model_economic, pred_data_economic)
pred_data_economic$se <- predict(model_economic, pred_data_economic, se.fit = TRUE)$se.fit

# Confidence intervals
pred_data_economic$lower_ci <- pred_data_economic$predicted - 1.96 * pred_data_economic$se
pred_data_economic$upper_ci <- pred_data_economic$predicted + 1.96 * pred_data_economic$se

# Plot
plot.ZEROSUM_ECONOMIC.SOCIALSTATUS <- ggplot(pred_data_economic, aes(x = SOCIALSTATUS, y = predicted)) +
  geom_ribbon(aes(ymin = lower_ci, ymax = upper_ci), fill = "red", alpha = 0.2) +
  geom_line(color = "red", size = 1) +
  labs(title = "Predicted Economic Zero-Sum Beliefs by Social Status",
       subtitle = "Holding Other Variables Constant",
       x = "Social Status",
       y = "Predicted Economic Zero-Sum Beliefs") +
  theme_minimal()

print(plot.ZEROSUM_ECONOMIC.SOCIALSTATUS)
ggsave("plots/plot13:ZEROSUM_ECONOMIC.SOCIALSTATUS.png", 
       plot = plot.ZEROSUM_ECONOMIC.SOCIALSTATUS, 
       width = 10, 
       height = 8, 
       dpi = 300)

```

As social status increased, participants reported lower levels of economic zero-sum beliefs (β = -0.15, p = .043), indicating a negative association between perceived social standing and belief in fixed economic resources. Individuals with higher perceived social status may be less likely to view the economy as a zero-sum system.

### Explaining Zero-Sum Identity Beliefs (multiple linear regression)

*What sociodemographic factors explain zero-sum identity beliefs?*

```{r}
#| label: kable multiple linear regression ZEROSUM_IDENTITY
#| echo: false
#| message: false
#| warning: false

model_identity <- lm(ZEROSUM_IDENTITY ~ GENDER_MALE + RELIGIOUS_YES + RACE_BLACK +
                       RACE_ASIAN + RACE_OTHER + EDUCATION_HIGH + SOCIALSTATUS,
                     data = select_data)

reg_table_identity <- rempsyc::nice_lm(model_identity, standardized = FALSE)

# add correct 95% confidence intervals for coefficients
ci_identity <- confint(model_identity)
reg_table_identity$CI.lower <- round(ci_identity[-1, 1], 2)
reg_table_identity$CI.upper <- round(ci_identity[-1, 2], 2)

# remove incorrect sr2 CI columns
reg_table_identity_clean <- reg_table_identity %>%
  select(-c(`CI_lower`, `CI_upper`))

# rename CI columns to "95% CI"
reg_table_identity_clean <- reg_table_identity_clean %>%
  mutate(`95% CI` = paste0("[", CI.lower, ", ", CI.upper, "]")) %>%
  select(-CI.lower, -CI.upper)

sig_rows <- which(as.numeric(reg_table_identity_clean$p) < 0.05)

if (knitr::is_html_output()) {
  knitr::kable(
    reg_table_identity_clean,
    digits = 2,
    escape = TRUE,
    format = "html",
    align = "c",
    caption = "Table 48. Multiple Linear Regression Predicting Identity Zero-Sum Beliefs"
  ) %>%
    kableExtra::kable_styling(bootstrap_options = c("striped", "hover", "condensed")) %>%
    kableExtra::row_spec(sig_rows, background = "gray")
} else if (knitr::is_latex_output()) {
  knitr::kable(
    reg_table_identity_clean,
    digits = 2,
    escape = TRUE,
    format = "latex",
    longtable = TRUE,
    align = "c",
    caption = "Multiple Linear Regression Predicting Identity Zero-Sum Beliefs"
  ) %>%
    kableExtra::kable_styling(full_width = FALSE, position = "left") %>%
    kableExtra::row_spec(sig_rows, background = "gray!20")
}
```

```{r}
#| label: plot linear regression ZEROSUM_IDENTITY.SOCIALSTATUS
# Create prediction data
pred_data_identity <- with(select_data, 
  data.frame(
    SOCIALSTATUS = seq(min(SOCIALSTATUS, na.rm = TRUE),
                       max(SOCIALSTATUS, na.rm = TRUE),
                       length = 100),
    GENDER_MALE = mean(GENDER_MALE, na.rm = TRUE),
    RELIGIOUS_YES = mean(RELIGIOUS_YES, na.rm = TRUE),
    RACE_BLACK = mean(RACE_BLACK, na.rm = TRUE),
    RACE_ASIAN = mean(RACE_ASIAN, na.rm = TRUE),
    RACE_OTHER = mean(RACE_OTHER, na.rm = TRUE),
    EDUCATION_HIGH = mean(EDUCATION_HIGH, na.rm = TRUE)
  ))

# Get predictions
pred_data_identity$predicted <- predict(model_identity, pred_data_identity)
pred_data_identity$se <- predict(model_identity, pred_data_identity, se.fit = TRUE)$se.fit

# Confidence intervals
pred_data_identity$lower_ci <- pred_data_identity$predicted - 1.96 * pred_data_identity$se
pred_data_identity$upper_ci <- pred_data_identity$predicted + 1.96 * pred_data_identity$se

# Plot
plot.ZEROSUM_IDENTITY.SOCIALSTATUS <- ggplot(pred_data_identity, aes(x = SOCIALSTATUS, y = predicted)) +
  geom_ribbon(aes(ymin = lower_ci, ymax = upper_ci), fill = "blue", alpha = 0.2) +
  geom_line(color = "blue", size = 1) +
  labs(title = "Predicted Identity Zero-Sum Beliefs by Social Status",
       subtitle = "Holding Other Variables Constant",
       x = "Social Status",
       y = "Predicted Identity Zero-Sum Beliefs") +
  theme_minimal()

print(plot.ZEROSUM_IDENTITY.SOCIALSTATUS)
ggsave("plots/plot14:ZEROSUM_IDENTITY.SOCIALSTATUS.png", 
       plot = plot.ZEROSUM_IDENTITY.SOCIALSTATUS, 
       width = 10, 
       height = 8, 
       dpi = 300)

```

As social status increased, participants reported higher levels of identity zero-sum beliefs (β = 0.23, p = .003), indicating a positive association between perceived social standing and belief in fixed identity resources. This suggests that individuals who perceive themselves as having higher social status may be more likely to endorse the view that one group’s gain comes at another’s expense.

## Predicting Voting Behavior for 2024 Presidential Candidate

### Logistic Regression

```{r}
#| label: transform VOTE2024 to TRUMPVOTE

# create new variable
select_data <- select_data %>%
  mutate(TRUMPVOTE = case_when(
    VOTE2024 == 1 ~ 1,
    VOTE2024 == 2 ~ 0,
    TRUE ~ NA
  ))

```

```{r}
#| label: kable format logistic regression v1
#| echo: false
#| message: false
#| warning: false

# Fit the logistic regression model
logregmodel.v1 <- glm(TRUMPVOTE ~ POLITICALBELIEFS + ZEROSUM_ECONOMIC + ZEROSUM_IDENTITY + ZEROSUM_1 +
                      GENDER_MALE + RELIGIOUS_YES + RACE_BLACK + RACE_ASIAN + RACE_OTHER + 
                      EDUCATION_HIGH + SOCIALSTATUS + COMPETITION_SCORE,
             data = select_data, 
             family = binomial)

# Tidy model output
logit_table <- broom::tidy(logregmodel.v1, conf.int = TRUE) %>%
  rename(Predictor = term,
         B = estimate,
         SE = std.error,
         z = statistic,
         p = p.value,
         CI_lower = conf.low,
         CI_upper = conf.high)

if (knitr::is_html_output()) {
  logit_table %>%
    knitr::kable(
      digits = 3,
      escape = TRUE,
      format = "html",
      caption = "Table 49. Logistic Regression Predicting Trump Vote"
    ) %>%
    kableExtra::kable_styling(bootstrap_options = c("striped", "hover", "condensed")) %>%
    kableExtra::row_spec(which(logit_table$p < 0.05), background = "gray")
} else if (knitr::is_latex_output()) {
  logit_table %>%
    knitr::kable(
      digits = 3,
      escape = TRUE,
      format = "latex",
      longtable = TRUE,
      caption = "Logistic Regression Predicting Trump Vote"
    ) %>%
    kableExtra::kable_styling(full_width = FALSE, position = "left") %>%
    kableExtra::row_spec(which(logit_table$p < 0.05), background = "gray!20")
}
```

```{r}
#| label: logregmodel.v1 coeff
#| fig-cap: "Predicted Probability of Trump Vote by Political Beliefs."
#| fig-align: center
#| echo: true
#| message: false
#| warning: false
#| code-overflow: wrap

# Create prediction data for one variable (holding others at mean)
pred_data <- with(select_data, 
  data.frame(
    POLITICALBELIEFS = seq(min(POLITICALBELIEFS, na.rm = TRUE), 
                          max(POLITICALBELIEFS, na.rm = TRUE), length = 100),
    SOCIALSTATUS = mean(SOCIALSTATUS, na.rm = TRUE),
    ZEROSUM_IDENTITY = mean(ZEROSUM_IDENTITY, na.rm = TRUE),
    ZEROSUM_ECONOMIC = mean(ZEROSUM_ECONOMIC, na.rm = TRUE),
    ZEROSUM_1 = mean(ZEROSUM_1, na.rm = TRUE),
    GENDER_MALE = mean(GENDER_MALE, na.rm = TRUE),
    RELIGIOUS_YES = mean(RELIGIOUS_YES, na.rm = TRUE),
    RACE_BLACK = mean(RACE_BLACK, na.rm = TRUE),
    RACE_ASIAN = mean(RACE_ASIAN, na.rm = TRUE),
    RACE_OTHER = mean(RACE_OTHER, na.rm = TRUE),
    EDUCATION_HIGH = mean(EDUCATION_HIGH, na.rm = TRUE),
    COMPETITION_SCORE = mean(COMPETITION_SCORE, na.rm = TRUE)
  ))

# Get predictions with standard errors
predictions <- predict(logregmodel.v1, pred_data, type = "link", se.fit = TRUE)

# Convert to probabilities and calculate confidence intervals
pred_data$predicted_prob <- plogis(predictions$fit)
pred_data$lower_ci <- plogis(predictions$fit - 1.96 * predictions$se.fit)
pred_data$upper_ci <- plogis(predictions$fit + 1.96 * predictions$se.fit)

# Plot with confidence intervals and proper labels
plot.TRUMPVOTE.POLITICIALBELIEFS <- ggplot(pred_data, 
  aes(x = POLITICALBELIEFS, y = predicted_prob)) +
  geom_ribbon(aes(ymin = lower_ci, ymax = upper_ci), alpha = 0.3, fill = "purple") +
  geom_line(color = "purple", size = 1) +
  scale_x_continuous(
    breaks = 1:7,
    labels = c("Far Left /\nLeftist", "Very Liberal", "Liberal", "Moderate", 
               "Conservative", "Very\nConservative", "Alt-Right /\nFar-Right")
  ) +
  labs(title = "Predicted Probability of Trump Vote by Political Beliefs",
       subtitle = "With 95% Confidence Intervals",
       x = "Political Beliefs", y = "Predicted Probability") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 9))

# Print and save the plots
print(plot.TRUMPVOTE.POLITICIALBELIEFS)
ggsave("plots/plot14:TRUMPVOTE.POLITICIALBELIEFS.png", 
       plot = plot.TRUMPVOTE.POLITICIALBELIEFS, 
       width = 10, 
       height = 8, 
       dpi = 300)

```

This figure shows the relationship between self-reported political ideology (ranging from "far left/leftist" to "very conservative") and the predicted probability of voting for Trump in the 2024 election. The purple curve exhibits a strong S-shaped relationship with a 95% confidence interval. This indicates a sharp increase in the probability of voting for Trump, from near zero among far left voters to almost certainty among very conservative voters, with the largest increase among liberal and moderate voters. The statistical model shows a highly significant positive correlation coefficient (β = 2.39, p \< .001, 95% CI: \[1.29, 4.06\]), confirming that political beliefs are the strongest predictor.

```{r}
#| label: plot ZEROSUM_IDENTITY
#| fig-cap: "Predicted Probability of Trump Vote by Zero-Sum Identity Beliefs."
#| fig-align: center
#| echo: true
#| message: false
#| warning: false

# Create prediction data for ZEROSUM_IDENTITY (holding others at mean)
pred_data_identity <- with(select_data, 
  data.frame(
    ZEROSUM_IDENTITY = seq(min(ZEROSUM_IDENTITY, na.rm = TRUE), 
                          max(ZEROSUM_IDENTITY, na.rm = TRUE), length = 100),
    POLITICALBELIEFS = mean(POLITICALBELIEFS, na.rm = TRUE),
    SOCIALSTATUS = mean(SOCIALSTATUS, na.rm = TRUE),
    ZEROSUM_ECONOMIC = mean(ZEROSUM_ECONOMIC, na.rm = TRUE),
    ZEROSUM_1 = mean(ZEROSUM_1, na.rm = TRUE),
    GENDER_MALE = mean(GENDER_MALE, na.rm = TRUE),
    RELIGIOUS_YES = mean(RELIGIOUS_YES, na.rm = TRUE),
    RACE_BLACK = mean(RACE_BLACK, na.rm = TRUE),
    RACE_ASIAN = mean(RACE_ASIAN, na.rm = TRUE),
    RACE_OTHER = mean(RACE_OTHER, na.rm = TRUE),
    EDUCATION_HIGH = mean(EDUCATION_HIGH, na.rm = TRUE),
    COMPETITION_SCORE = mean(COMPETITION_SCORE, na.rm = TRUE)
  ))

# Get predictions with standard errors
predictions_identity <- predict(logregmodel.v1, pred_data_identity, type = "link", se.fit = TRUE)

# Convert to probabilities and calculate confidence intervals
pred_data_identity$predicted_prob <- plogis(predictions_identity$fit)
pred_data_identity$lower_ci <- plogis(predictions_identity$fit - 1.96 * predictions_identity$se.fit)
pred_data_identity$upper_ci <- plogis(predictions_identity$fit + 1.96 * predictions_identity$se.fit)

# Plot with confidence intervals and proper labels
plot.TRUMPVOTE.ZEROSUM_IDENTITY <- ggplot(pred_data_identity, 
  aes(x = ZEROSUM_IDENTITY, y = predicted_prob)) +
  geom_ribbon(aes(ymin = lower_ci, ymax = upper_ci), alpha = 0.3, fill = "red") +
  geom_line(color = "red", size = 1) +
  scale_x_continuous(
    breaks = 1:7,
    labels = c("Strongly\nDisbelieve", "Disbelieve", "Somewhat\nDisbelieve", 
               "Neither\nDisbelieve\nnor Believe", "Somewhat\nBelieve", 
               "Believe", "Strongly\nBelieve")
  ) +
  labs(title = "Predicted Probability of Trump Vote by Zero-Sum IDENTITY Beliefs",
       subtitle = "With 95% Confidence Intervals",
       x = "Zero-Sum IDENTITY Beliefs", y = "Predicted Probability") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 9))

# Print and save the plots
print(plot.TRUMPVOTE.ZEROSUM_IDENTITY)
ggsave("plots/plot15:TRUMPVOTE.ZEROSUM_IDENTITY.png", 
       plot = plot.TRUMPVOTE.ZEROSUM_IDENTITY, 
       width = 10, 
       height = 8, 
       dpi = 300)

```

This figure shows the relationship between zero-sum thinking about identity issues (ranging from "strongly disbelieve" to "believe") and the predicted probability of voting for Trump in the 2024 election. The red curve exhibits a steady upward trend with a 95% confidence interval. This indicates a consistent increase in the probability of voting for Trump, from approximately 5% among those who strongly disbelieve zero-sum identity concepts to around 95% among those who believe in them. The statistical model shows a significant positive correlation coefficient (β = 1.43, p = .002, 95% CI: \[0.63, 2.51\]), confirming that zero-sum identity beliefs are a meaningful predictor of Trump support beyond traditional political ideology.

```{r}
#| label: plot ZEROSUM_ECONOMIC
#| fig-cap: "Predicted Probability of Trump Vote by Zero-Sum Economic Beliefs."
#| fig-align: center
#| echo: true
#| message: false
#| warning: false

# Create prediction data for ZEROSUM_ECONOMIC (holding others at mean)
pred_data_econ <- with(select_data, 
  data.frame(
    ZEROSUM_ECONOMIC = seq(min(ZEROSUM_ECONOMIC, na.rm = TRUE), 
                          max(ZEROSUM_ECONOMIC, na.rm = TRUE), length = 100),
    POLITICALBELIEFS = mean(POLITICALBELIEFS, na.rm = TRUE),
    SOCIALSTATUS = mean(SOCIALSTATUS, na.rm = TRUE),
    ZEROSUM_IDENTITY = mean(ZEROSUM_IDENTITY, na.rm = TRUE),
    ZEROSUM_1 = mean(ZEROSUM_1, na.rm = TRUE),
    GENDER_MALE = mean(GENDER_MALE, na.rm = TRUE),
    RELIGIOUS_YES = mean(RELIGIOUS_YES, na.rm = TRUE),
    RACE_BLACK = mean(RACE_BLACK, na.rm = TRUE),
    RACE_ASIAN = mean(RACE_ASIAN, na.rm = TRUE),
    RACE_OTHER = mean(RACE_OTHER, na.rm = TRUE),
    EDUCATION_HIGH = mean(EDUCATION_HIGH, na.rm = TRUE),
    COMPETITION_SCORE = mean(COMPETITION_SCORE, na.rm = TRUE)
  ))

# Get predictions with standard errors
predictions_econ <- predict(logregmodel.v1, pred_data_econ, type = "link", se.fit = TRUE)

# Convert to probabilities and calculate confidence intervals
pred_data_econ$predicted_prob <- plogis(predictions_econ$fit)
pred_data_econ$lower_ci <- plogis(predictions_econ$fit - 1.96 * predictions_econ$se.fit)
pred_data_econ$upper_ci <- plogis(predictions_econ$fit + 1.96 * predictions_econ$se.fit)

# Plot with confidence intervals and proper labels
plot.TRUMPVOTE.ZEROSUM_ECONOMIC <- ggplot(pred_data_econ, 
  aes(x = ZEROSUM_ECONOMIC, y = predicted_prob)) +
  geom_ribbon(aes(ymin = lower_ci, ymax = upper_ci), alpha = 0.3, fill = "blue") +
  geom_line(color = "blue", size = 1) +
  scale_x_continuous(
    breaks = 1:7,
    labels = c("Strongly\nDisbelieve", "Disbelieve", "Somewhat\nDisbelieve", 
               "Neither\nDisbelieve\nnor Believe", "Somewhat\nBelieve", 
               "Believe", "Strongly\nBelieve")
  ) +
  labs(title = "Predicted Probability of Trump Vote by Zero-Sum Economic Beliefs",
       subtitle = "With 95% Confidence Intervals",
       x = "Zero-Sum Economic Beliefs", y = "Predicted Probability") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 9))

# Print and save the plots
print(plot.TRUMPVOTE.ZEROSUM_ECONOMIC)
ggsave("plots/plot16:TRUMPVOTE.ZEROSUM_ECONOMIC.png", 
       plot = plot.TRUMPVOTE.ZEROSUM_ECONOMIC, 
       width = 10, 
       height = 8, 
       dpi = 300)

```

This figure shows the relationship between zero-sum thinking about economic issues (ranging from "strongly disbelieve" to "strongly believe") and the predicted probability of voting for Trump in the 2024 election. The blue curve exhibits an interesting U-shaped pattern with a 95% confidence interval. This indicates higher Trump support at both extremes: around 70-75% among those who strongly disbelieve or strongly believe in zero-sum economic thinking, while those with neutral positions show the lowest support at around 40-45%. However, the statistical model shows this relationship is not significant (\`β\` = -0.24, p = .582, 95% CI: \[-1.12, 0.63\]), indicating that zero-sum economic beliefs do not reliably predict Trump's voting outcome when accounting for statistical uncertainty.

$$
\begin{aligned}
\log\left(\frac{\hat{P}(\text{TRUMPVOTE})}{1 - \hat{P}(\text{TRUMPVOTE})}\right)
&= \beta_0 + \beta_1 \cdot \text{POLITICALBELIEFS} + \beta_2 \cdot \text{AGE} \\
&\quad + \beta_3 \cdot \text{SOCIALSTATUS} + \beta_4 \cdot \text{ZEROSUM\_ECONOMIC} \\
&\quad + \beta_5 \cdot \text{ZEROSUM\_IDENTITY} + \beta_6 \cdot \text{ZEROSUM\_1}
\end{aligned}
$$ {#eq-logistic-regression-v1}

```{r}
#| label: corr
#| fig-cap: "Correlation Matrix of All Variables."
#| fig-align: center
#| echo: true
#| message: false
#| warning: false

library(corrplot)

# Select the variables for correlation matrix
cor_vars <- select_data %>%
  select(TRUMPVOTE, ZEROSUM_ECONOMIC, ZEROSUM_IDENTITY, ZEROSUM_1:ZEROSUM_11, 
         POLITICALBELIEFS, COMPETITION_SCORE)

# Create correlation matrix (using complete observations)
cor_matrix <- cor(cor_vars, use = "complete.obs")

# Print the correlation matrix
print(cor_matrix)

# Visualize with corrplot
corrplot(cor_matrix, 
         method = "color",
         type = "upper",
         order = "hclust",
         tl.cex = 0.8,
         tl.col = "black",
         tl.srt = 45,
         addCoef.col = "black",
         number.cex = 0.7)

# Alternative visualization with different style
corrplot(cor_matrix, 
         method = "circle",
         type = "full",
         order = "original",
         tl.cex = 0.8,
         tl.col = "black",
         tl.srt = 45,
         col = colorRampPalette(c("blue", "white", "red"))(100))
```

This heat map illustrates the correlation structure between voting for Trump, zero-sum beliefs (including economic and identity beliefs), individual zero-sum items (ZEROSUM_1 through ZEROSUM_11), and overall political beliefs. Red circles indicate positive correlations, blue circles indicate negative correlations, and the size of the circles represents the strength of the correlation. The matrix shows that voting for Trump is strongly positively correlated with both zero-sum identity beliefs and political beliefs.

### Decision Tree and Random Forest Analysis

To further validate these findings and examine the predictive power of our variables using a different analytical approach, we employed a series of machine learning techniques. Our analysis proceeded in three stages:

-   *Stage 1: Initial Decision Tree:* We first constructed a simple decision tree to identify the primary predictors and their splitting thresholds for Trump voting behavior. This provided an interpretable baseline model showing how the algorithm naturally segments voters.

-   *Stage 2: Extended Decision Tree with Cross-Validation:* We then built a more complex decision tree incorporating additional demographic variables and used cross-validation to determine the optimal model complexity. Through this process, we found that the best performing tree is the 2-split model, which achieved a cross-validation error of 0.24. This suggests that despite having access to multiple demographic and ideological variables, the most predictive model requires only two key splits to effectively classify voters.

-   *Stage 3: Random Forest Analysis:* Finally, we employed a Random Forest ensemble method to capture potential non-linear relationships and interactions while providing robust variable importance measures. This approach confirmed our regression findings by identifying `ZEROSUM_IDENTITY` and `POLITICALBELIEFS` as the most important predictors, with substantially higher importance scores than all other variables.

This machine learning approach serves as an independent validation of our regression based findings, using fundamentally different algorithms to examine the same relationships and providing additional confidence in our substantive conclusions about voting behavior predictors.

```{r}
#| label: ensure TRUMPVOTE is a factor
select_data <- select_data %>%
  mutate(TRUMPVOTE = factor(TRUMPVOTE, levels = c(0, 1)))  # 0 = non-Trump, 1 = Trump

```

```{r}
#| label: decision tree
#| fig-cap: "Decision Tree Analysis of Voting Predictors."
#| fig-align: center
#| echo: true
#| message: false
#| warning: false

library(rpart)
library(rpart.plot)

dt_model <- rpart(TRUMPVOTE ~ POLITICALBELIEFS + ZEROSUM_ECONOMIC + 
    ZEROSUM_IDENTITY + ZEROSUM_1 + GENDER_MALE + RELIGIOUS_YES + 
    RACE_BLACK + RACE_ASIAN + RACE_OTHER + EDUCATION_HIGH + SOCIALSTATUS + COMPETITION_SCORE,
                  data = select_data,
                  method = "class")

rpart.plot(dt_model, extra = 104)

```

The only variable the tree uses is zero-sum social identity beliefs (`ZEROSUM_IDENTITY`), suggesting it is the most important predictor in our model. If a respondent scores below 3.3 on `ZEROSUM_IDENTITY`, they are much more likely to be classified as *not* voting for Donald Trump in 2024 (84%). Participants who scored 3.3 or higher are much more likely to be classified as voting for Donald Trump (`TRUMPVOTE`)(87%).

```{r}
#| label: a deeper decision tree
#| fig-cap: "Extended Decision Tree with Demographic Variables."
#| fig-align: center
#| echo: true
#| message: false
#| warning: false

dt_model <- rpart(TRUMPVOTE ~ POLITICALBELIEFS + ZEROSUM_ECONOMIC + 
    ZEROSUM_IDENTITY + ZEROSUM_1 + GENDER_MALE + RELIGIOUS_YES + 
    RACE_BLACK + RACE_ASIAN + RACE_OTHER + EDUCATION_HIGH + SOCIALSTATUS + COMPETITION_SCORE,
  data = select_data,
  method = "class",
  control = rpart.control(
    cp = 0.001,         # smaller = deeper tree
    minsplit = 10,      # smaller = allows more splits
    maxdepth = 5        # allow up to 5 levels deep
  )
)

rpart.plot(dt_model, extra = 104)

```

This expanded decision tree incorporates demographic variables (gender and race) alongside the core predictors. The tree shows how demographic factors interact with ideological variables to refine predictions, with male respondents and those from "other" racial categories showing higher Trump support within similar ideological profiles.

```{r}
#| label: Cross-Validation table

dt_model <- rpart(TRUMPVOTE ~ POLITICALBELIEFS + ZEROSUM_ECONOMIC + 
  ZEROSUM_IDENTITY + ZEROSUM_1 + GENDER_MALE + RELIGIOUS_YES + 
  RACE_BLACK + RACE_ASIAN + RACE_OTHER + EDUCATION_HIGH + SOCIALSTATUS + COMPETITION_SCORE,
  data = select_data,
  method = "class",
  control = rpart.control(cp = 0.001)
)

cp_table <- as.data.frame(dt_model$cptable)

# Print as kable
kable(
  cp_table,
  caption = "Table 50. Decision Tree Cross-Validation Results",
  digits = 4,
  booktabs = TRUE
)
```

The best tree is the 2-split model with Cross-validation error (0.24)

```{r}
#| label: random forest table
#| fig-cap: "Variable Importance in Random Forest Model."
#| fig-align: center
#| echo: true
#| message: false
#| warning: false

library(randomForest)
library(tidyr)

# need to drop NA to get accuracy
select_data <- select_data %>%
  drop_na(TRUMPVOTE, POLITICALBELIEFS, ZEROSUM_ECONOMIC, ZEROSUM_IDENTITY, ZEROSUM_1,
          GENDER_MALE, RELIGIOUS_YES, RACE_BLACK, RACE_ASIAN, RACE_OTHER, 
          EDUCATION_HIGH, SOCIALSTATUS, COMPETITION_SCORE)

# split into training and testing sets
set.seed(123)
train_idx <- sample(seq_len(nrow(select_data)), size = 0.7 * nrow(select_data))
train <- select_data[train_idx, ]
test  <- select_data[-train_idx, ]

# Fit random forest model
rf_model <- randomForest(
  TRUMPVOTE ~ POLITICALBELIEFS + ZEROSUM_ECONOMIC + ZEROSUM_IDENTITY + ZEROSUM_1 + 
  GENDER_MALE + RELIGIOUS_YES + RACE_BLACK + RACE_ASIAN + RACE_OTHER + 
  EDUCATION_HIGH + SOCIALSTATUS + COMPETITION_SCORE,
  data = train,
  na.action = na.roughfix,
  ntree = 500
)

# Variable importance
varImpPlot(rf_model)

pred <- predict(rf_model, newdata = test)

conf_matrix <- table(Predicted = pred, Actual = test$TRUMPVOTE)

# Convert to data frame for kable
conf_matrix_df <- as.data.frame.matrix(conf_matrix) %>%
  rownames_to_column(var = "Predicted")

kable(
  conf_matrix,
  caption = "Table 51. Random Forest Confusion Matrix",
  digits = 3,
  booktabs = TRUE
)

accuracy <- mean(predict(rf_model, newdata = test) == test$TRUMPVOTE)
accuracy_df <- data.frame(Metric = "Accuracy", Value = round(accuracy, 3))

kable(
  accuracy_df,
  caption = "Table 52. Random Forest Model Accuracy",
  digits = 7,
  booktabs = TRUE
)

```

Zero-sum identity beliefs and political beliefs emerge as the most important predictors, with Mean Decrease Gini values around 9-12, substantially higher than other variables. This ranking confirms our regression results that these two variables are the main drivers of Trump’s voting behavior, while demographic and other ideological variables play a secondary role.

```{r}
#| label: visual conf matrix
#| fig-cap: "Random Forest Model Performance."
#| fig-align: center
#| echo: true
#| message: false
#| warning: false

library(yardstick)
library(ggplot2)
library(dplyr)

# Create data frame for predictions and actual values
conf_df <- data.frame(
  truth = test$TRUMPVOTE,
  prediction = pred
)

# Create confusion matrix object
conf_mat_obj <- conf_mat(conf_df, truth = truth, estimate = prediction)

# Visualize it
autoplot(conf_mat_obj, type = "heatmap") +
  scale_fill_gradient(low = "white", high = "steelblue") +
  labs(title = "Confusion Matrix: Random Forest",
       x = "Predicted",
       y = "Actual")

```

The confusion matrix shows the random forest model's prediction accuracy on the test data. The model achieved an overall accuracy of 83.33%, correctly classifying 13 of 16 non-Trump voters and 12 of 14 Trump voters. The model experienced two false negatives (predicting Trump voters as non-Trump voters) and three false positives (predicting non-Trump voters as Trump voters), indicating strong but not perfect prediction performance.

### Participant FlowChart

```{r}
#| label: Participant FlowChart
#| fig-cap: "Participant Flowchart showing exclusions for missing data and final analytic samples used in Kruskal-Wallis and Logistic Regression analyses."
#| fig-align: center
#| echo: true
#| message: false
#| warning: false

library(consort)

# Sample sizes
total_start <- nrow(alldata)
after_select <- 122
excluded_count <- total_start - after_select

# Attention check exclusions
attention_fail <- sum(select_data$ATTENTION3 != 2 | select_data$SERIOUS != "Yes", na.rm = TRUE)
after_attention <- after_select - attention_fail

# Logistic regression sample size
log_regression <- nobs(logregmodel.v1)
excluded_log_reg <- after_select - log_regression

# Build flowchart
consort_plot <- add_box(NULL, txt = paste0("Imported CSV\nN = ", total_start)) %>%
  add_side_box(txt = paste0("Excluded due to missing data\nn = ", excluded_count)) %>%
  
  add_box(txt = paste0("Selected Variables\nN = ", after_select)) %>%

  add_side_box(txt = paste0("Excluded for failing attention checks\nn = ", attention_fail)) %>%
  
  add_box(txt = paste0("Kruskal-Wallis Analyses\nN = ", after_select)) %>%
  
  add_side_box(txt = paste0("Excluded due to missing data\nn = ", excluded_log_reg)) %>%
  
  add_box(txt = paste0("Logistic Regression Analyses\nN = ", log_regression))

# Save flowchart as an object
plot(consort_plot)

```

### Logistic Regression for White vs. Color

*Are explanatory variables for voter preference different for White people compared to People of Color?*

```{r}
# White only
white_data <- select_data %>%
  filter(RACE_BLACK == 0 & RACE_ASIAN == 0 & RACE_OTHER == 0)  # White = reference group

# People of Color (anyone not White)
poc_data <- select_data %>%
  filter(RACE_BLACK == 1 | RACE_ASIAN == 1 | RACE_OTHER == 1)
```

```{r}
# Logistic regression for White only
model_white <- glm(
  TRUMPVOTE ~  ZEROSUM_IDENTITY + ZEROSUM_ECONOMIC, 
  data = white_data, 
  family = binomial
)

# Logistic regression for POC only
model_poc <- glm(
  TRUMPVOTE ~ ZEROSUM_IDENTITY + ZEROSUM_ECONOMIC, 
  data = poc_data, 
  family = binomial
)

tidy_white <- tidy(model_white, conf.int = TRUE, exponentiate = TRUE)

kable_white <- tidy_white %>%
  kable(
    digits = 3,
    caption = "Table 53. Logistic Regression for White Participants",
    booktabs = TRUE
  ) %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed")) %>%
  row_spec(which(tidy_white$p.value < 0.05), background = "gray")

kable_white 


tidy_poc <- tidy(model_poc, conf.int = TRUE, exponentiate = TRUE) 

kable_poc <- tidy_poc %>%
  kable(
    digits = 3,
    caption = "Table 54. Logistic Regression for POC Participants",
    booktabs = TRUE
  ) %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed")) %>%
  row_spec(which(tidy_poc$p.value < 0.05), background = "gray")

kable_poc 
```

`ZEROSUM_IDENTITY` is positively associated with TRUMPVOTE for both groups. The coefficients are numerically similar (1.3 vs 1.59). `ZEROSUM_ECONOMIC` is significant only for POC and negative (-0.9002), meaning higher economic zero-sum beliefs reduce the likelihood of voting Trump for POC. For White participants, this effect is near zero (-0.09855).

```{r}
# Prediction data for White vs. ZEROSUM_IDENTITY
pred_white <- with(white_data,
  data.frame(
    ZEROSUM_IDENTITY = seq(min(ZEROSUM_IDENTITY, na.rm = TRUE),
                           max(ZEROSUM_IDENTITY, na.rm = TRUE), length = 100),
    ZEROSUM_ECONOMIC = mean(ZEROSUM_ECONOMIC, na.rm = TRUE)
  ))

# Predictions
predictions_white <- predict(model_white, pred_white, type = "link", se.fit = TRUE)

# Convert to probabilities
pred_white$predicted_prob <- plogis(predictions_white$fit)
pred_white$lower_ci <- plogis(predictions_white$fit - 1.96 * predictions_white$se.fit)
pred_white$upper_ci <- plogis(predictions_white$fit + 1.96 * predictions_white$se.fit)

# Plot
plot_white <- ggplot(pred_white, aes(x = ZEROSUM_IDENTITY, y = predicted_prob)) +
  geom_ribbon(aes(ymin = lower_ci, ymax = upper_ci), alpha = 0.3, fill = "steelblue") +
  geom_line(color = "steelblue", size = 1) +
  labs(title = "Predicted Probability of Trump Vote (White Only)",
       subtitle = "With 95% Confidence Intervals",
       x = "Zero-Sum Identity Beliefs",
       y = "Predicted Probability") +
  theme_minimal(base_size = 14)

plot_white

```

```{r}
# Prediction data for POC vs. ZEROSUM_IDENTITY
pred_poc <- with(poc_data,
  data.frame(
    ZEROSUM_IDENTITY = seq(min(ZEROSUM_IDENTITY, na.rm = TRUE),
                           max(ZEROSUM_IDENTITY, na.rm = TRUE), length = 100),
    ZEROSUM_ECONOMIC = mean(ZEROSUM_ECONOMIC, na.rm = TRUE)
  ))

# Predictions
predictions_poc <- predict(model_poc, pred_poc, type = "link", se.fit = TRUE)

# Convert to probabilities
pred_poc$predicted_prob <- plogis(predictions_poc$fit)
pred_poc$lower_ci <- plogis(predictions_poc$fit - 1.96 * predictions_poc$se.fit)
pred_poc$upper_ci <- plogis(predictions_poc$fit + 1.96 * predictions_poc$se.fit)

# Plot
plot_poc <- ggplot(pred_poc, aes(x = ZEROSUM_IDENTITY, y = predicted_prob)) +
  geom_ribbon(aes(ymin = lower_ci, ymax = upper_ci), alpha = 0.3, fill = "darkred") +
  geom_line(color = "darkred", size = 1) +
  labs(title = "Predicted Probability of Trump Vote (People of Color)",
       subtitle = "With 95% Confidence Intervals",
       x = "Zero-Sum Identity Beliefs",
       y = "Predicted Probability") +
  theme_minimal(base_size = 14)

plot_poc

```

```{r}
# Prediction data for POC vs. ZEROSUM_ECONOMIC
pred_poc <- with(poc_data,
  data.frame(
    ZEROSUM_ECONOMIC = seq(min(ZEROSUM_ECONOMIC, na.rm = TRUE),
                           max(ZEROSUM_ECONOMIC, na.rm = TRUE), length = 100),
    ZEROSUM_IDENTITY = mean(ZEROSUM_IDENTITY, na.rm = TRUE)
  ))

# Predictions
predictions_poc <- predict(model_poc, pred_poc, type = "link", se.fit = TRUE)

# Convert to probabilities
pred_poc$predicted_prob <- plogis(predictions_poc$fit)
pred_poc$lower_ci <- plogis(predictions_poc$fit - 1.96 * predictions_poc$se.fit)
pred_poc$upper_ci <- plogis(predictions_poc$fit + 1.96 * predictions_poc$se.fit)

# Plot
plot_poc <- ggplot(pred_poc, aes(x = ZEROSUM_ECONOMIC, y = predicted_prob)) +
  geom_ribbon(aes(ymin = lower_ci, ymax = upper_ci), alpha = 0.3, fill = "darkgreen") +
  geom_line(color = "darkgreen", size = 1) +
  labs(title = "Predicted Probability of Trump Vote (People of Color)",
       subtitle = "With 95% Confidence Intervals",
       x = "Zero-Sum Economic Beliefs",
       y = "Predicted Probability") +
  theme_minimal(base_size = 14)

plot_poc

```

```{r}
# Prediction data for White vs. ZEROSUM_ECONOMIC
pred_white <- with(white_data,
  data.frame(
    ZEROSUM_ECONOMIC = seq(min(ZEROSUM_ECONOMIC, na.rm = TRUE),
                           max(ZEROSUM_ECONOMIC, na.rm = TRUE), length = 100),
    ZEROSUM_IDENTITY = mean(ZEROSUM_IDENTITY, na.rm = TRUE)
  ))

# Predictions
predictions_white <- predict(model_white, pred_white, type = "link", se.fit = TRUE)

# Convert to probabilities
pred_white$predicted_prob <- plogis(predictions_white$fit)
pred_white$lower_ci <- plogis(predictions_white$fit - 1.96 * predictions_white$se.fit)
pred_white$upper_ci <- plogis(predictions_white$fit + 1.96 * predictions_white$se.fit)

# Plot
plot_white <- ggplot(pred_white, aes(x = ZEROSUM_ECONOMIC, y = predicted_prob)) +
  geom_ribbon(aes(ymin = lower_ci, ymax = upper_ci), alpha = 0.3, fill = "purple") +
  geom_line(color = "purple", size = 1) +
  labs(title = "Predicted Probability of Trump Vote (White Only)",
       subtitle = "With 95% Confidence Intervals",
       x = "Zero-Sum Economic Beliefs",
       y = "Predicted Probability") +
  theme_minimal(base_size = 14)

plot_white

```

# Discussion

In the modern era, political scientists and mainstream media have pointed to a racial realignment and class dealignment to explain the shifting political landscape, particularly the significant changes in voter behavior based on racial identity, social class, and educational attainment. The current study extends this literature by emphasizing the importance of social identities in influencing voter preference in the 2024 U.S. Presidential election. However, in contrast to decades of existing research demonstrating that social identities explain voter behavior and preference, our findings suggest that a person's *beliefs* about social identity groups—specifically zero-sum social identity beliefs—may matter more than a person's social identity. Results of a logistic regression classifying self-reported voting behavior in the 2024 U.S. Presidential election indicate that the newly developed measure of zero-sum social identity beliefs (`ZEROSUM_IDENTITY`) produced the second largest coefficient estimate, trailing only political ideology and exceeding other sociodemographic variables including gender (`GENDER_MALE`) and racial identity (`RI_Else`). Decision tree and random forest models also emphasize the importance zero-sum social identity beliefs for classifying voter preference. Also, an item-by-item analysis of all zero sum social identity beliefs showed significantly group differences for political party affiliation (`POLITICALPARTY`): democrat, republican, and independent.

The current study also advances the domain-specific conceptualization of zero-sum beliefs. An exploratory factor analysis of zero-sum belief items revealed a three-factor solution comprising general, economic, and social identity dimensions. Furthermore, a paired t-test demonstrated that zero-sum economic beliefs (`ZEROSUM_ECONOMIC`) and zero-sum social identity beliefs (`ZEROSUM_IDENTITY`) differed significantly from one another and functioned as distinct predictors of voter preference in the 2024 election. While several prior studies have examined gender and racial attitudes in relation to voting behavior, this may be the first study to demonstrate that zero-sum social identity beliefs are significant explanatory variables of Donald Trump preference over Kamala Harris in the 2024 U.S. Presidential election, independent of traditional demographic predictors.

# Appendix A: Detailed Factor Analysis Output {.appendix}

### Factor Analysis of Zero-Sum Beliefs

```{r}
#| label: factor-analysis-zerosum
#| fig.width: 6
#| fig.height: 4
#| echo: true

library(psych)
# create data frame of ZEROSUM variables for factor analysis
df.ZEROSUM <- select_data[, c("ZEROSUM_1", "ZEROSUM_2", "ZEROSUM_3", "ZEROSUM_4", 
                                  "ZEROSUM_5", "ZEROSUM_6", "ZEROSUM_7", "ZEROSUM_8", 
                                  "ZEROSUM_9", "ZEROSUM_10", "ZEROSUM_11")]

# Or using dplyr to select variables
# zerosum_vars <- select_data %>% select(ZEROSUM_1:ZEROSUM_11)

# Check the correlation matrix first
cor_matrix <- cor(df.ZEROSUM, use = "complete.obs")
print(cor_matrix)


# Determine number of factors using scree plot and parallel analysis
scree(df.ZEROSUM)
wrapped_fa_parallel <- paste(strwrap(capture.output(fa.parallel(df.ZEROSUM, fa = "fa")), width = 80), collapse = "\n")
cat(wrapped_fa_parallel, "\n")

# Run 2-factor factor analysis (adjust nfactors based on scree plot/parallel analysis)
fa_result <- fa(df.ZEROSUM, 
                nfactors = 2,  # adjust this number based on your analysis
                rotate = "promax",
                fm = "ml")  # maximum likelihood

# View results
print(fa_result)
fa_result$loadings

# View factor loadings
fa_result$loadings

# Get factor scores
factor_scores <- fa_result$scores
```

### Reliability of Zero Sum Social Identity Beliefs

```{r}
#| label: alpha-identity
library(psych)

# Alpha for ZEROSUM_IDENTITY (8 items)
alpha_identity <- psych::alpha(select_data[, c("ZEROSUM_4", "ZEROSUM_5", "ZEROSUM_6", 
                                         "ZEROSUM_7", "ZEROSUM_8", "ZEROSUM_9", 
                                         "ZEROSUM_10", "ZEROSUM_11")])
print(alpha_identity)

```

### Reliability of Zero Sum Economic Beliefs

```{r}
#| label: alpha-economic

library(psych)

# Alpha for ZEROSUM_ECONOMIC (2 items)
alpha_economic <- psych::alpha(select_data[, c("ZEROSUM_2", "ZEROSUM_3")])
print(alpha_economic)

```

### Factor Analysis of Neoliberal Mindset

```{r}
#| label: neolib check
#| fig.width: 6
#| fig.height: 4

library(psych)

# create data frame of NEOLIB variables
df.NEOLIB <- select_data[, c("NEOLIB_1", "NEOLIB_2", "NEOLIB_3")]

# correlation matrix
cor_matrix_neolib <- cor(df.NEOLIB, use = "complete.obs")
print(cor_matrix_neolib)

# Cronbach’s alpha for internal consistency
alpha_neolib <- psych::alpha(df.NEOLIB)
alpha_neolib

# factor analysis (1 factor)
fa_neolib <- fa(df.NEOLIB,
                nfactors = 1,
                rotate = "none",
                fm = "ml")

fa_neolib$loadings

```

# Appendix B: Detailed Inferential Tests Output {.appendix}

### Comparing Zero-Sum Beliefs (Paired t-test)

```{r}
#| label: Paired t-test comparing ZEROSUM_ECONOMIC and ZEROSUM_IDENTITY
t_test_zerosum <- t.test(select_data$ZEROSUM_ECONOMIC,
                         select_data$ZEROSUM_IDENTITY,
                         paired = TRUE)

# View results
t_test_zerosum
```

### Zero-Sum Beliefs by Gender (t-test)

```{r}
#| label: t-tests between zerosum_1 & gender

# Run t-tests using the dummy variable
zerosum_1_gender <- t.test(ZEROSUM_1 ~ GENDER_MALE, data = select_data)

# Wrap the printed t-test output before printing
wrapped <- paste(strwrap(capture.output(zerosum_1_gender), width = 80), collapse = "\n")
cat(wrapped, "\n")
```

```{r}
#| label: t-tests between ZEROSUM_ECONOMIC & gender

ZEROSUM_ECONOMIC_gender <- t.test(ZEROSUM_ECONOMIC ~ GENDER_MALE, data = select_data)
wrapped <- paste(strwrap(capture.output(ZEROSUM_ECONOMIC_gender), width = 80), collapse = "\n")
cat(wrapped, "\n")
```

```{r}
#| label: t-tests between ZEROSUM_IDENTITY & gender

ZEROSUM_IDENTITY_gender <- t.test(ZEROSUM_IDENTITY ~ GENDER_MALE, data = select_data)
wrapped <- paste(strwrap(capture.output(ZEROSUM_IDENTITY_gender), width = 80), collapse = "\n")
cat(wrapped, "\n")
```

### Zero-Sum Beliefs by Political Party Affiliation

#### Gain vs. Loss (shapiro test & kruskal-Wallis test)

```{r}
#| label: shapiro.test zerosum1

select_data %>%
  group_by(RACIALIDENTITY.4) %>%
  summarise(p = shapiro.test(ZEROSUM_1)$p.value)

## White, Mixed/Other (p<0.05) fail the normality assumption for ANOVA, maybe use a non-parametric test (Kruskal-Wallis)
```

```{r}
#| label: Kruskal-Wallis zerosum1

kw.ZEROSUM_1.party <- kruskal.test(ZEROSUM_1 ~ POLITICALPARTY, data = select_data)
kw.ZEROSUM_1.party

## There is no significant difference in ZEROSUM_1 scores across the POLITICALPARTY groups (Kruskal-Wallis chi-squared = 1.59, p = .45).  
```

#### Poor vs. Rich (shapiro test & kruskal-Wallis test)

```{r}
#| label: shapiro.test zerosum2

select_data %>%
  group_by(RACIALIDENTITY.4) %>%
  summarise(p = shapiro.test(ZEROSUM_2)$p.value)

## Asian, Black, White (p<0.05) fail the normality assumption for ANOVA, maybe use a non-parametric test (Kruskal-Wallis)
```

```{r}
#| label: Kruskal-Wallis zerosum2

kw.ZEROSUM_2.party <- kruskal.test(ZEROSUM_2 ~ POLITICALPARTY, data = select_data)
kw.ZEROSUM_2.party

## There is no significant difference in ZEROSUM_2 scores across the POLITICALPARTY groups (Kruskal-Wallis chi-squared = 3.39, p = .18).  
```

#### Wealth few vs. many

```{r}
#| label: shapiro.test zerosum3

select_data %>%
  group_by(RACIALIDENTITY.4) %>%
  summarise(p = shapiro.test(ZEROSUM_3)$p.value)

## Black, White, Mixed/Other (p<0.05) fail the normality assumption for ANOVA, maybe use a non-parametric test (Kruskal-Wallis)
```

```{r}
#| label: Kruskal-Wallis zerosum3

kw.ZEROSUM_3.party <- kruskal.test(ZEROSUM_3 ~ POLITICALPARTY, data = select_data)
kw.ZEROSUM_3.party

## There is no significant difference in ZEROSUM_3 scores across the POLITICALPARTY groups (Kruskal-Wallis chi-squared = 2.23, p = .33).  
```

#### Women vs. Men

```{r}
#| label: shapiro.test zerosum4

select_data %>%
  group_by(RACIALIDENTITY.4) %>%
  summarise(p = shapiro.test(ZEROSUM_4)$p.value)

## Asian, Black, White, Mixed/Other (p<0.05) fail the normality assumption for ANOVA, maybe use a non-parametric test (Kruskal-Wallis)
```

```{r}
#| label: Kruskal-Wallis zerosum4

kw.ZEROSUM_4.party <- kruskal.test(ZEROSUM_4 ~ POLITICALPARTY, data = select_data)
kw.ZEROSUM_4.party

## There is significant difference in ZEROSUM_4 scores across the POLITICALPARTY groups (Kruskal-Wallis chi-squared = 10.45, p = .005).  
```

#### Minorities vs. Whites

```{r}
#| label: shapiro.test zerosum5
select_data %>%
  group_by(RACIALIDENTITY.4) %>%
  summarise(p = shapiro.test(ZEROSUM_5)$p.value)

## Asian, Black, White, Mixed/Other (p<0.05) fail the normality assumption for ANOVA, maybe use a non-parametric test (Kruskal-Wallis)
```

```{r}
#| label: Kruskal-Wallis zerosum5

kw.ZEROSUM_5.party <- kruskal.test(ZEROSUM_5 ~ POLITICALPARTY, data = select_data)
kw.ZEROSUM_5.party

## There is significant difference in ZEROSUM_5 scores across the POLITICALPARTY groups (Kruskal-Wallis chi-squared = 17.86, p = .0001).  
```

#### Transgender vs. Cisgender

```{r}
#| label: shapiro.test zerosum6

select_data %>%
  group_by(RACIALIDENTITY.4) %>%
  summarise(p = shapiro.test(ZEROSUM_6)$p.value)

## Asian, Black, White, Mixed/Other (p<0.05) fail the normality assumption for ANOVA, maybe use a non-parametric test (Kruskal-Wallis)
```

```{r}
#| label: Kruskal-Wallis zerosum6

kw.ZEROSUM_6.party <- kruskal.test(ZEROSUM_6 ~ POLITICALPARTY, data = select_data)
kw.ZEROSUM_6.party

## There is significant difference in ZEROSUM_6 scores across the POLITICALPARTY groups (Kruskal-Wallis chi-squared = 22.82, p = 1.109e-05).  
```

#### Undocumented vs. Citizens

```{r}
#| label: shapiro.test zerosum7

select_data %>%
  group_by(RACIALIDENTITY.4) %>%
  summarise(p = shapiro.test(ZEROSUM_7)$p.value)

## Asian, Black, White, Mixed/Other (p<0.05) fail the normality assumption for ANOVA, maybe use a non-parametric test (Kruskal-Wallis)
```

```{r}
#| label: Kruskal-Wallis zerosum7

kw.ZEROSUM_7.party <- kruskal.test(ZEROSUM_7 ~ POLITICALPARTY, data = select_data)
kw.ZEROSUM_7.party

## There is significant difference in ZEROSUM_7 scores across the POLITICALPARTY groups (Kruskal-Wallis chi-squared = 14.98, p = .0005578).  
```

#### Paywomen vs. men

```{r}
#| label: shapiro.test zerosum8

select_data %>%
  group_by(RACIALIDENTITY.4) %>%
  summarise(p = shapiro.test(ZEROSUM_8)$p.value)

## Asian, Black, White, Mixed/Other (p<0.05) fail the normality assumption for ANOVA, maybe use a non-parametric test (Kruskal-Wallis)
```

```{r}
#| label: Kruskal-Wallis zerosum8

kw.ZEROSUM_8.party <- kruskal.test(ZEROSUM_8 ~ POLITICALPARTY, data = select_data)
kw.ZEROSUM_8.party

## There is significant difference in ZEROSUM_8 scores across the POLITICALPARTY groups (Kruskal-Wallis chi-squared = 15.55, p = .0004211).  
```

#### LGBTQ vs. Religious

```{r}
#| label: shapiro.test zerosum9

select_data %>%
  group_by(RACIALIDENTITY.4) %>%
  summarise(p = shapiro.test(ZEROSUM_9)$p.value)

## Asian, White, Mixed/Other (p<0.05) fail the normality assumption for ANOVA, maybe use a non-parametric test (Kruskal-Wallis)
```

```{r}
#| label: Kruskal-Wallis zerosum9

kw.ZEROSUM_9.party <- kruskal.test(ZEROSUM_9 ~ POLITICALPARTY, data = select_data)
kw.ZEROSUM_9.party

## There is significant difference in ZEROSUM_9 scores across the POLITICALPARTY groups (Kruskal-Wallis chi-squared = 21.63, p = 2.009e-05).  
```

#### Disabilities vs. Non-disabilities

```{r}
#| label: shapiro.test zerosum10

select_data %>%
  group_by(RACIALIDENTITY.4) %>%
  summarise(p = shapiro.test(ZEROSUM_10)$p.value)

## Black, White, Mixed/Other (p<0.05) fail the normality assumption for ANOVA, maybe use a non-parametric test (Kruskal-Wallis)
```

```{r}
#| label: Kruskal-Wallis zerosum10

kw.ZEROSUM_10.party <- kruskal.test(ZEROSUM_10 ~ POLITICALPARTY, data = select_data)
kw.ZEROSUM_10.party

## There is significant difference in ZEROSUM_10 scores across the POLITICALPARTY groups (Kruskal-Wallis chi-squared = 28.20, p = 7.532e-07).  
```

#### Healthcare vs. Private

```{r}
#| label: shapiro.test zerosum11

select_data %>%
  group_by(RACIALIDENTITY.4) %>%
  summarise(p = shapiro.test(ZEROSUM_11)$p.value)

## Asian, Black, White, Mixed/Other (p<0.05) fail the normality assumption for ANOVA, maybe use a non-parametric test (Kruskal-Wallis)
```

```{r}
#| label: Kruskal-Wallis zerosum11

kw.ZEROSUM_11.party <- kruskal.test(ZEROSUM_11 ~ POLITICALPARTY, data = select_data)
kw.ZEROSUM_11.party

## There is significant difference in ZEROSUM_11 scores across the POLITICALPARTY groups (Kruskal-Wallis chi-squared = 19.65, p = 5.403e-05).  
```

## Exploratory Analyses

### Explaining Zero-Sum Economic Beliefs (multiple linear regression)

```{r}
#| label: multiple linear regression ZEROSUM_ECONOMIC
model_economic <- lm(ZEROSUM_ECONOMIC ~ GENDER_MALE + RELIGIOUS_YES + RACE_BLACK + RACE_ASIAN + RACE_OTHER + 
                       EDUCATION_HIGH + SOCIALSTATUS,
                     data = select_data)

summary(model_economic)

```

### Explaining Zero-Sum Identity Beliefs (multiple linear regression)

```{r}
#| label: multiple linear regression ZEROSUM_IDENTITY
model_identity <- lm(ZEROSUM_IDENTITY ~ GENDER_MALE + RELIGIOUS_YES + RACE_BLACK + RACE_ASIAN + RACE_OTHER + 
                       EDUCATION_HIGH + SOCIALSTATUS,
                     data = select_data)

summary(model_identity)

```

## Predicting Voting Behavior

### Logistic Regression

```{r}
#| label: logistic-regression-v1

# Fit the logistic regression model
logregmodel.v1 <- glm(TRUMPVOTE ~ POLITICALBELIEFS + ZEROSUM_ECONOMIC + ZEROSUM_IDENTITY + ZEROSUM_1 +
                      GENDER_MALE +  RACIALIDENTITY.2 + 
                      COMPETITION_SCORE,
             data = select_data, 
             family = binomial)

# View the results
summary(logregmodel.v1)
```

```{r}
#| label: logistic-regression-nosociodems

# Fit the logistic regression model
logregmodel.nosdemo.v1 <- glm(TRUMPVOTE ~ POLITICALBELIEFS + ZEROSUM_ECONOMIC + ZEROSUM_IDENTITY + ZEROSUM_1 +
                      COMPETITION_SCORE + GENDER_MALE,
             data = select_data, 
             family = binomial)

# View the results
summary(logregmodel.nosdemo.v1)
```

### Decision Tree and Random Forest Analysis

```{r}
#| label: Cross-Validation

dt_model <- rpart(TRUMPVOTE ~ POLITICALBELIEFS + ZEROSUM_ECONOMIC + 
  ZEROSUM_IDENTITY + ZEROSUM_1 + GENDER_MALE + RELIGIOUS_YES + 
  RACE_BLACK + RACE_ASIAN + RACE_OTHER + EDUCATION_HIGH + SOCIALSTATUS + COMPETITION_SCORE,
  data = select_data,
  method = "class",
  control = rpart.control(cp = 0.001)
)

printcp(dt_model)

```

```{r}
#| label: random forest
#| fig-cap: "Variable Importance in Random Forest Model."
#| fig-align: center
#| echo: true
#| message: false
#| warning: false

library(randomForest)
library(tidyr)

# need to drop NA to get accuracy
select_data <- select_data %>%
  drop_na(TRUMPVOTE, POLITICALBELIEFS, ZEROSUM_ECONOMIC, ZEROSUM_IDENTITY, ZEROSUM_1,
          GENDER_MALE, RELIGIOUS_YES, RACE_BLACK, RACE_ASIAN, RACE_OTHER, 
          EDUCATION_HIGH, SOCIALSTATUS, COMPETITION_SCORE)

# split into training and testing sets
set.seed(123)
train_idx <- sample(seq_len(nrow(select_data)), size = 0.7 * nrow(select_data))
train <- select_data[train_idx, ]
test  <- select_data[-train_idx, ]

# Fit random forest model
rf_model <- randomForest(
  TRUMPVOTE ~ POLITICALBELIEFS + ZEROSUM_ECONOMIC + ZEROSUM_IDENTITY + ZEROSUM_1 + 
  GENDER_MALE + RELIGIOUS_YES + RACE_BLACK + RACE_ASIAN + RACE_OTHER + 
  EDUCATION_HIGH + SOCIALSTATUS + COMPETITION_SCORE,
  data = train,
  na.action = na.roughfix,
  ntree = 500
)

# Predict on test set
pred <- predict(rf_model, newdata = test)


# Confusion matrix
table(Predicted = pred, Actual = test$TRUMPVOTE)

# Accuracy
mean(pred == test$TRUMPVOTE)

# Variable importance
#varImpPlot(rf_model)

```

### Logistic Regression for White vs. Color

```{r}
# Logistic regression for White only
model_white <- glm(
  TRUMPVOTE ~  ZEROSUM_IDENTITY + ZEROSUM_ECONOMIC, 
  data = white_data, 
  family = binomial
)

# Logistic regression for POC only
model_poc <- glm(
  TRUMPVOTE ~ ZEROSUM_IDENTITY + ZEROSUM_ECONOMIC, 
  data = poc_data, 
  family = binomial
)

summary(model_white)
summary(model_poc)
```
