David Barker Figures

NOTE: DON’T WORRY ABOUT FIGURES LOOKING “SQUISHED.” THAT IS JUST AN ARTIFACT OF THE WEBSITE.

Preparation

Show the code
# libraries
library(tidyverse)
library(haven)
library(survey)
library(patchwork)
library(sjPlot)
library(MASS)
library(margins)

# load data
setwd("~/Downloads")
data <- read_dta('barkerdata2.dta')

data21 <- read_dta('ces2021.dta')

# uniform aesthestics
david_func <- function() {
  list(
    theme_bw(),
    theme(panel.grid = element_blank(),
        axis.text = element_text(size = 10),
        axis.title = element_text(size = 12),
        legend.text = element_text(size = 10),
        legend.title = element_text(size = 12)) 

  )
}

# Creating a few variables
data <- data %>% 
  mutate(pidfac_d = case_when(pid3lean01 == 0 ~ 1,
                                           T ~ 0),
          pidfac_i = case_when(pid3lean01 == 0.5 ~ 1,
                                           T ~ 0),
          pidfac_r = case_when(pid3lean01 == 1 ~ 1,
                                           T ~ 0))

# Creating survey design objects to include (1) sampling weights, and (2) clustered SEs at the state level
mydesign <- svydesign(ids = ~inputstate, weights = ~weight, data = data)
data21a <- data21 %>% drop_na(teamweight)
mydesign21 <- svydesign(ids = ~inputstate, weights = ~teamweight, data = data21a)

Chapter #2

Figure 2.1

NOTE: THE ORIGINAL NUMBERS IN-TEXT DID NOT APPEAR TO USE SURVEY-WEIGHTS?? WHEN I RUN THE CORS W/ WEIGHTS, THE NUMBERS VARY SLIGHTLY

Show the code
library(weights)

# The code is very long because because we need to bootstrap the confidence intervals with weights :)

#  Core ----------------------------------------------------
# for Ds/Rs
cor1 <- data %>% 
  drop_na(intel901march23newest, 
          antiintelcore601newest) %>% 
  filter(pid3lean01 != 0.5) %>%
  group_by(pid3lean01) %>% 
  dplyr::summarize(cor = weights::wtd.cor(x = intel901march23newest, 
                                   y = antiintelcore601newest,
                                   weight = weight,
                                   bootse = T,
                                   bootn = 1000)) %>% 
  mutate(type = 'Core anti-intellectualism')

# Full sample
cor2 <- data %>% 
  drop_na(intel901march23newest, 
          antiintelcore601newest) %>% 
  dplyr::summarize(cor = weights::wtd.cor(x = intel901march23newest, 
                                   y = antiintelcore601newest,
                                   weight = weight,
                                   bootse = T,
                                   bootn = 1000)) %>% 
  mutate(pid3lean01 = 0.5,
         type = 'Core anti-intellectualism')

# indignant ----------------------------------------------------
# for Ds/Rs
cor3 <- data %>% 
  drop_na(intel901march23newest, 
          antiintpop2pluspolitic3_501) %>% 
  filter(pid3lean01 != 0.5) %>%
  group_by(pid3lean01) %>% 
  dplyr::summarize(cor = weights::wtd.cor(x = intel901march23newest, 
                                   y = antiintpop2pluspolitic3_501,
                                   weight = weight,
                                   bootse = T,
                                   bootn = 1000)) %>% 
  mutate(type = 'Indignant anti-intellectualism')

# Full sample
cor4 <- data %>% 
  drop_na(intel901march23newest, 
          antiintpop2pluspolitic3_501) %>% 
  dplyr::summarize(cor = weights::wtd.cor(x = intel901march23newest, 
                                   y = antiintpop2pluspolitic3_501,
                                   weight = weight,
                                   bootse = T,
                                   bootn = 1000)) %>% 
  mutate(pid3lean01 = 0.5,
         type = 'Indignant anti-intellectualism')

# combine into one df + calculate CIs ----------------------------------
cors <- bind_rows(cor1, cor2, cor3, cor4)
cors$cor1 <- cors$cor[, 1] # core
cors$cor2 <- cors$cor[, 3] # boostrapped se

cors2 <- data.frame(pid3lean01 = cors$pid3lean01, 
                            cor = cors$cor1, 
                            se = cors$cor2, 
                            type = cors$type)

cors2 <- cors2 %>% 
  mutate(min = cor - 1.96*se, # CIs
         max = cor + 1.96*se) %>% 
  mutate(group = factor(pid3lean01,
                        levels = c(0.5, 0, 1),
                        labels = c('Republicans', 'Democrats', 'Full sample')),
         cor = round(cor,2))

# time to plot ---------------------------------------------------------
cors2 %>% 
  ggplot(aes(y = cor, 
             x = factor(type, 
                        levels = c('Indignant anti-intellectualism',
                                   'Core anti-intellectualism')), 
             fill = group,
             ymin = min, 
             ymax = max)) +
  geom_col(color = 'black', position = position_dodge()) +
  geom_errorbar(position = position_dodge(width = 0.9), width = 0.2) +
  geom_text(aes(label = paste(cor), 
                vjust = ifelse(cor >= 0,  0, 0.5), 
                hjust = ifelse(cor >= 0, 0, 1.9)), 
            position = position_dodge(width = 0.9)) +
  scale_fill_grey(start = 0.8, end = 0.3) +
  labs(fill = 'Sample',
       x = '', y = 'Pearson\'s Correlation with Intellectual Identity') +
  david_func() +
  coord_flip() +
  ylim(-1, .15) +
  guides(fill = guide_legend(reverse = TRUE))

Chapter #3

Figure 3.1

Show the code
# Models (using separate binomial probits because the I can't find a way to run multinomial probits while using complex design elements, e.g., clusters/weights)
R <- svyglm(pidfac_r ~ justbachelorsdegree + graddegree + black + hispanic + male + age01 + faminc_missmedsub01 + churchatt601, design = mydesign, family = quasibinomial(link = "probit"))

I <- svyglm(pidfac_i ~ justbachelorsdegree + graddegree + black + hispanic + male + age01 + faminc_missmedsub01 + churchatt601, design = mydesign, family = quasibinomial(link = "probit"))

D <- svyglm(pidfac_d ~ justbachelorsdegree + graddegree + black + hispanic + male + age01 + faminc_missmedsub01 + churchatt601, design = mydesign, family = quasibinomial(link = "probit"))

# Function to extract predicted probabilities
pred_func <- function(reg, bach, grad, pid, educ) {

    predict_data <- data.frame(
    
    # desired levels of education
    justbachelorsdegree = bach,
    graddegree = grad,
    
    # covariates fixed at their menas
    black = weighted.mean(data$black, na.rm = TRUE, w = data$weight),
    hispanic = weighted.mean(data$hispanic, na.rm = TRUE, w = data$weight),
    male = weighted.mean(data$male, na.rm = TRUE), w = data$weight,
    age01 = weighted.mean(data$age01, na.rm = TRUE, w = data$weight),
    faminc_missmedsub01 = weighted.mean(data$faminc_missmedsub01, na.rm = TRUE, w = data$weight),
    churchatt601 = weighted.mean(data$churchatt601, na.rm = TRUE, w = data$weight)
  )
  
  # tidying
  preds <- predict(reg, newdata = predict_data, type = "response")
  preds <- as.data.frame(preds)
  preds$pid <- pid
  preds$educ <- educ
  
  return(preds)
  
}

# Creating dataframe of predictions
d1 <- pred_func(D, 0, 0, 'Democrat', '< Bachelor\'s degree') 
d2 <- pred_func(D, 1, 0, 'Democrat', 'Bachelor\'s degree') 
d3 <- pred_func(D, 1, 1, 'Democrat', 'Graduate degree') 

i1 <- pred_func(I, 0, 0, 'Independent', '< Bachelor\'s degree') 
i2 <- pred_func(I, 1, 0, 'Independent', 'Bachelor\'s degree') 
i3 <- pred_func(I, 1, 1, 'Independent', 'Graduate degree') 

r1 <- pred_func(R, 0, 0, 'Republican', '< Bachelor\'s degree') 
r2 <- pred_func(R, 1, 0, 'Republican', 'Bachelor\'s degree') 
r3 <- pred_func(R, 1, 1, 'Republican', 'Graduate degree') 

f3.1 <- bind_rows(d1, d2, d3, i1, i2, i3, r1, r2, r3)

f3.1 <- f3.1 %>% 
  mutate(ymax = response + SE*1.96,
         ymin = response - SE*1.96)

# Plot
f3.1 %>% 
  ggplot(aes(x = educ, y = response, group = pid)) +
  geom_line() +
  geom_point() +
  geom_errorbar(aes(ymin = ymin, ymax = ymax), width = 0.1) +
  david_func() +
  facet_wrap(~pid) +
  labs(x = 'Educational Attainment', y = 'Probability of PID') +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  scale_y_continuous(labels = scales::percent,
                     limits = c(0,1))

Figure 3.2

Show the code
# Models
R2 <- svyglm(pidfac_r ~ justbachelorsdegree + graddegree + collegeelitism + black + hispanic + male + age01 + faminc_missmedsub01 + churchatt601, design = mydesign, family = quasibinomial(link = "probit"))

I2 <- svyglm(pidfac_i ~ justbachelorsdegree + graddegree + collegeelitism + black + hispanic + male + age01 + faminc_missmedsub01 + churchatt601, design = mydesign, family = quasibinomial(link = "probit"))

D2 <- svyglm(pidfac_d ~ justbachelorsdegree + graddegree + collegeelitism + black + hispanic + male + age01 + faminc_missmedsub01 + churchatt601, design = mydesign, family = quasibinomial(link = "probit"))

# Function to extract predicted probabilities
pred_func <- function(reg, bach, grad, elite, pid, educ, institution) {

    predict_data <- data.frame(
    
    # desired levels of education
    justbachelorsdegree = bach,
    graddegree = grad,
    collegeelitism = elite,
    
    # covariates fixed at their menas
    black = weighted.mean(data$black, na.rm = TRUE, w = data$weight),
    hispanic = weighted.mean(data$hispanic, na.rm = TRUE, w = data$weight),
    male = weighted.mean(data$male, na.rm = TRUE, w = data$weight),
    age01 = weighted.mean(data$age01, na.rm = TRUE), w = data$weight,
    faminc_missmedsub01 = weighted.mean(data$faminc_missmedsub01, na.rm = TRUE, w = data$weight),
    churchatt601 = weighted.mean(data$churchatt601, na.rm = TRUE, w = data$weight)
  )
  
  # Add labels to dataframes
  preds <- predict(reg, newdata = predict_data, type = "response")
  preds <- as.data.frame(preds)
  preds$pid <- pid
  preds$educ <- educ
  preds$institution <- institution
  
  return(preds)
  
}

# Creating dataframe of predictions
dd1 <- pred_func(D2, 1, 0, 0,  'Democrat', 'Bachelor\'s degree', 'Non-elite institution') 
dd2 <- pred_func(D2, 1, 0, 1,  'Democrat', 'Bachelor\'s degree', 'Elite institution') 
dd3 <- pred_func(D2, 1, 1, 0,  'Democrat', 'Graduate degree', 'Non-elite institution') 
dd4 <- pred_func(D2, 1, 1, 1,  'Democrat', 'Graduate degree', 'Elite institution') 


ii1 <- pred_func(I2, 1, 0, 0,  'Independent', 'Bachelor\'s degree', 'Non-elite institution') 
ii2 <- pred_func(I2, 1, 0, 1,  'Independent', 'Bachelor\'s degree', 'Elite institution') 

ii3 <- pred_func(I2, 1, 1, 0,  'Independent', 'Graduate degree', 'Non-elite institution') 
ii4 <- pred_func(I2, 1, 1, 1,  'Independent', 'Graduate degree', 'Elite institution') 


rr1 <- pred_func(R2, 1, 0, 0,  'Republican', 'Bachelor\'s degree', 'Non-elite institution') 
rr2 <- pred_func(R2, 1, 0, 1,  'Republican', 'Bachelor\'s degree', 'Elite institution') 

rr3 <- pred_func(R2, 1, 1, 0,  'Republican', 'Graduate degree', 'Non-elite institution') 
rr4 <- pred_func(R2, 1, 1, 1,  'Republican', 'Graduate degree', 'Elite institution') 


f3.2 <- bind_rows(dd1, dd2, dd3, dd4, ii1, ii2, ii3, ii4, rr1, rr2, rr3, rr4)

f3.2 <- f3.2 %>% 
  mutate(ymax = response + SE*1.96,
         ymin = response - SE*1.96)


# Plot

f3.2 %>% 
  ggplot(aes(x = educ, y = response, group = institution, linetype = institution)) +
  geom_line() +
  geom_point() +
  geom_errorbar(aes(ymin = ymin, ymax = ymax), width = 0.1) +
  david_func() +
  facet_wrap(~pid) +
  labs(x = 'Educational attainment', y = 'Probability of PID', linetype = 'Institution type') +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  scale_y_continuous(labels = scales::percent,
                     limits = c(0,1)) +
  scale_linetype_manual(values = c('dashed', 'solid'))

Figure 3.3

NOTE: INCLUDING TWO VERSIONS

Show the code
# Models
R3 <- svyglm(pidfac_r ~ justbachelorsdegree + graddegree + collegeelitism + intel901march23newest + black + hispanic + male + age01 + faminc_missmedsub01 + churchatt601, design = mydesign, family = quasibinomial(link = "probit"))

I3 <- svyglm(pidfac_i ~ justbachelorsdegree + graddegree + collegeelitism + intel901march23newest + black + hispanic + male + age01 + faminc_missmedsub01 + churchatt601, design = mydesign, family = quasibinomial(link = "probit"))

D3 <- svyglm(pidfac_d ~ justbachelorsdegree + graddegree + collegeelitism + intel901march23newest + black + hispanic + male + age01 + faminc_missmedsub01 + churchatt601, design = mydesign, family = quasibinomial(link = "probit"))

# Function to extract predicted probabilities
pred_func <- function(reg, intelid, pid, intelidlab) {

    predict_data <- data.frame(
    
    # intellectual ID
    intel901march23newest = intelid,
    
    # covariates fixed at their menas
    justbachelorsdegree = weighted.mean(data$justbachelorsdegree, na.rm = T,
                                        w = data$weight),
    graddegree = weighted.mean(data$graddegree, na.rm = T,
                                        w = data$weight),
    collegeelitism = weighted.mean(data$collegeelitism, na.rm = T,
                                        w = data$weight),
    black = mean(data$black, na.rm = TRUE,
                                        w = data$weight),
    hispanic = mean(data$hispanic, na.rm = TRUE,
                                        w = data$weight),
    male = mean(data$male, na.rm = TRUE,
                                        w = data$weight),
    age01 = mean(data$age01, na.rm = TRUE,
                                        w = data$weight),
    faminc_missmedsub01 = mean(data$faminc_missmedsub01, na.rm = TRUE,
                                        w = data$weight),
    churchatt601 = mean(data$churchatt601, na.rm = TRUE,
                                        w = data$weight)
  )
  
  # Add labels to dataframes
  preds <- predict(reg, newdata = predict_data, type = "response")
  preds <- as.data.frame(preds)
  preds$pid <- pid
  preds$intelidlab <- intelidlab

  return(preds)
  
}

# create dataframe of predictions
intd1 <- pred_func(D3, 0, 'Democrat', 'Least Intellectual')
intd2 <- pred_func(D3, 1, 'Democrat', 'Most Intellectual')

inti1 <- pred_func(I3, 0, 'Independent', 'Least Intellectual')
inti2 <- pred_func(I3, 1, 'Independent', 'Most Intellectual')

intr1 <- pred_func(R3, 0, 'Republican', 'Least Intellectual')
intr2 <- pred_func(R3, 1, 'Republican', 'Most Intellectual')

f3.3 <- bind_rows(intd1, intd2, inti1, inti2, intr1, intr2)

f3.3 <- f3.3 %>% 
  mutate(ymax = response + SE*1.96,
         ymin = response - SE*1.96)

# Plot
f3.3 %>% 
  ggplot(., aes(x = pid, y = response, ymin = ymin, ymax = ymax, fill = intelidlab)) +
  geom_col(color = 'black',  position = 'dodge') +
  geom_errorbar(width = 0.2, position = position_dodge(width = 0.9)) +
  david_func() +
  scale_y_continuous(labels = scales::percent) +
  labs(x = '', y = 'Probability of PID', fill = 'Intellectual Identity Index') +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
   scale_fill_grey(start = 0.5, end = 0.8)

Show the code
f3.3 %>% 
  ggplot(., aes(x = intelidlab, y = response, ymin = ymin, ymax = ymax, fill = pid)) +
  geom_col(color = 'black',  position = 'dodge') +
  geom_errorbar(width = 0.2, position = position_dodge(width = 0.9)) +
  david_func() +
  scale_y_continuous(labels = scales::percent) +
  labs(x = 'Intellectual Identity Index', y = 'Probability of PID', fill = 'Party ID') +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
   scale_fill_grey(start = 0.5, end = 0.8)

Figure 3.4

Figure 3.5

Show the code
reg3.5 <- svyglm(demleandic ~ intel901march23newest*noeatlunch01 + ideo501libhi + woke3pca01 + white + male + age01 + faminc_missmedsub01 + churchatt601, design = mydesign, family = quasibinomial(link = "probit"))

sjPlot::plot_model(reg3.5, type = 'pred', terms = c('intel901march23newest', 'noeatlunch01[0,1]')) +
  david_func() +
  labs(x = 'Intellectual Identity',
       y = 'Probability Identify as Democrat',
       title = '',
       color = "Socio-Political Clannishness",
       fill = "Socio-Political Clannishness") +
  scale_color_grey(start = 0.5, end = 0.1, labels = c("Lowest", "Highest")) + 
  scale_fill_grey(start = 0.3, end = 0, labels = c("Lowest", "Highest")) +
  ylim(0,1)

Figure 3.6

Show the code
reg3.6 <- svyglm(iamintelligentdic ~ intel901march23newest + ideo501libhi + pid3lean01 +  justbachelorsdegree + graddegree + collegeelitism + white + male + age01 +  faminc_missmedsub01 + churchatt601, design = mydesign, family = quasibinomial(link = "probit"))

sjPlot::plot_model(reg3.6, type = 'pred', terms = 'intel901march23newest',
                   colors = 'gray20') +
  david_func() +
  labs(x = 'Intellectual Identity',
       y = 'Probability Identify as Democrat',
       title = '') +
  ylim(0,1)

Show the code
# reg3.6a <- glm(iamintelligentdic ~ intel901march23newest + ideo501libhi + pid3lean01 +  justbachelorsdegree + graddegree + collegeelitism + white + male + age01 +  faminc_missmedsub01 + churchatt601, data = data, weights = weight, family = quasibinomial(link = "probit"))
# 
# marg_effects <- margins(reg3.6a)
#marg_effects <- summary(marg_effects)
#marg_effects <- as.data.frame(marg_effects)
#marg_effects

Figure 3.7

Show the code
reg3.7 <- svyglm(faminc_missmedsub01 ~ intel901march23newest  + white + male + age01 + churchatt601, design = mydesign, family = gaussian())

sjPlot::plot_model(reg3.7, type = 'pred', terms = 'intel901march23newest', 
                   colors = 'gray20') +
  david_func() +
  labs(x = 'Intellectual Identity',
       y = 'Gross Family Income',
       title = '') +
  ylim(0,1)

Figure 3.8

Show the code
reg3.8 <- svyglm(faminc_missmedsub01 ~ intel901march23newest + justbachelorsdegree + graddegree + collegeelitism  + white + male + age01 + churchatt601, design = mydesign, family = gaussian())

sjPlot::plot_model(reg3.8, type = 'pred', terms = 'intel901march23newest', 
                   colors = 'gray20') +
  david_func() +
  labs(x = 'Intellectual Identity',
       y = 'Gross Family Income',
       title = '') +
  ylim(0,1)

Figure 3.9

Show the code
reg3.9 <- svyglm(ownsstockdic ~ intel901march23newest + justbachelorsdegree + graddegree + collegeelitism + white + male + age01 + churchatt601, design = mydesign, family = quasibinomial(link = "probit"))

sjPlot::plot_model(reg3.9, type = 'pred', terms = 'intel901march23newest', 
                   colors = 'gray20') +
  david_func() +
  labs(x = 'Intellectual Identity',
       y = 'Stock Ownership',
       title = '') +
  ylim(0,1)

Chapter 6

Figure 6.1

Show the code
# indignant
reg6.1 <- svyglm(antiintpop2pluspolitic3_501 ~ biblebornpluschurchatt6 + pid3lean01 + justbachelorsdegree + graddegree + collegeelitism + intel901march23newest + white + male + age01 + faminc_missmedsub01 + catholic + protestant, design = mydesign, family = gaussian())

points6.1 <- sjPlot::get_model_data(reg6.1, type = 'pred', terms = 'biblebornpluschurchatt6')
points6.1 <- as.data.frame(points6.1)

p1 <- points6.1 %>% 
  filter(x %in% c(0,1)) %>% 
  ggplot(., aes(x = factor(x, 
                           levels = c(1,0),
                           labels = c('Highest', 'Lowest')), 
                y = predicted, ymin = conf.low, ymax = conf.high)) + 
  geom_col(color = 'black', fill = 'gray60') +
  geom_errorbar(width = 0.2) + 
  labs(x = '', y = 'Indignant Anti-Intellectualism') +
  david_func() +
  ylim(0, .65) +
  coord_flip() 

# core
reg6.1b <- svyglm(antiintelcore601newest  ~ biblebornpluschurchatt6 + pid3lean01 + justbachelorsdegree + graddegree + collegeelitism + intel901march23newest + white + male + age01 + faminc_missmedsub01 + catholic + protestant, design = mydesign, family = gaussian())

points6.1b <- sjPlot::get_model_data(reg6.1b, type = 'pred', terms = 'biblebornpluschurchatt6')
points6.1b <- as.data.frame(points6.1b)

p2 <- points6.1b %>% 
  filter(x %in% c(0,1)) %>% 
  ggplot(., aes(x = factor(x, 
                           levels = c(1,0),
                           labels = c('Highest', 'Lowest')), 
                y = predicted, ymin = conf.low, ymax = conf.high)) + 
  geom_col(color = 'black', fill = 'gray60') +
  geom_errorbar(width = 0.2) + 
  labs(x = '', y = 'Core Anti-Intellectualism') +
  david_func() +
  ylim(0, .65) +
  coord_flip()

# together
pp <- p1 + p2 + plot_layout(ncol = 1)  

wrap_elements(pp)+
  labs(tag = 'Evangelical Religiosity Index') +
  theme(
    plot.tag = element_text(size = 12, angle = 90),
    plot.tag.position = "left",
  )

Figure 6.2

Show the code
# Indignant
reg6.2a <- svyglm(antiintpop2pluspolitic3_501 ~ biblebornpluschurchatt6*pid3lean01 + justbachelorsdegree  + graddegree + collegeelitism + intel901march23newest + white + male + age01 + faminc_missmedsub01 + catholic + protestant, design = mydesign, family = gaussian())

points6.2a <- get_model_data(reg6.2a, type = 'pred', terms = c('pid3lean01[0,1]', 'biblebornpluschurchatt6[0,1]'))

points6.2a <- as.data.frame(points6.2a)

p1 <- points6.2a %>% 
  mutate(party = factor(x, 
                        levels = c(1,0),
                        labels = c('Republicans', 'Democrats'))) %>% 
  ggplot(., aes(x = factor(group, 
                           levels = c(1,0),
                           labels = c('Highest', 'Lowest')), 
                y = predicted, fill = party, ymin = conf.low, ymax = conf.high)) +
  geom_col(position = 'dodge', color = 'black') +
  geom_errorbar(position = position_dodge(width = 0.9), width = 0.2) +
  david_func() +
  scale_fill_grey(start = 0.5, end = 0.8) +
  labs(fill = 'Party',
       y = 'Indignant Anti-Intellectualism',
       x = '') +
  coord_flip() +
  guides(fill = guide_legend(reverse = TRUE))

# Core
reg6.2b <- svyglm(antiintelcore601newest ~ biblebornpluschurchatt6*pid3lean01 + justbachelorsdegree  + graddegree + collegeelitism + intel901march23newest + white + male + age01 + faminc_missmedsub01 + catholic + protestant, design = mydesign, family = gaussian())

points6.2b <- get_model_data(reg6.2b, type = 'pred', terms = c('pid3lean01[0,1]', 'biblebornpluschurchatt6[0,1]'))

points6.2b <- as.data.frame(points6.2b)

p2 <- points6.2b %>% 
  mutate(party = factor(x, 
                        levels = c(1,0),
                        labels = c('Republicans', 'Democrats'))) %>% 
  ggplot(., aes(x = factor(group, 
                           levels = c(1,0),
                           labels = c('Highest', 'Lowest')), 
                y = predicted, fill = party, ymin = conf.low, ymax = conf.high)) +
  geom_col(position = 'dodge', color = 'black') +
  geom_errorbar(position = position_dodge(width = 0.9), width = 0.2) +
  david_func() +
  scale_fill_grey(start = 0.5, end = 0.8) +
  labs(fill = 'Party',
       y = 'Core Anti-Intellectualism',
       x = '') +
  coord_flip() +
  guides(fill = guide_legend(reverse = TRUE))

# together
ppp <- p1 + p2 + plot_layout(ncol = 1, guides = 'collect')  

wrap_elements(ppp)+
  labs(tag = 'Evangelical Religiosity Index') +
  theme(
    plot.tag = element_text(size = 12, angle = 90),
    plot.tag.position = "left",
  )

Figure 6.3

Show the code
# indignant
reg6.7a <- svyglm(antiintelpolpluspop401 ~ antisemitismindex201 + pid3lean01 + bachelorsdegree + postgrad + intelfactrmlpurest601 + white + male + age01 + famincmeansub01, design = mydesign21, family = gaussian())

points6.7a <- get_model_data(reg6.7a, type = 'pred', terms = 'antisemitismindex201[0,1]')
points6.7a <- as.data.frame(points6.7a)

p1 <- points6.7a %>% 
  ggplot(., aes(x = factor(x, 
                           levels = c(1,0),
                           labels = c('Highest', 'Lowest')),
                y = predicted,
                ymin = conf.low,
                ymax = conf.high)) +
  geom_col(color = 'black', fill = 'gray60') +
  geom_errorbar(width = 0.2) +
  david_func() +
  labs(x = '', 
       y = 'Indignant Anti-Intellectualism') +
  coord_flip() +
  ylim(0,.7)

# core
reg6.7b <- svyglm(antiintelcorefactorpurest501 ~ antisemitismindex201 + pid3lean01 + bachelorsdegree + postgrad + intelfactrmlpurest601 + white + male + age01 + famincmeansub01, design = mydesign21, family = gaussian())

points6.7b <- get_model_data(reg6.7b, type = 'pred', terms = 'antisemitismindex201[0,1]')
points6.7b <- as.data.frame(points6.7b)

p2 <- points6.7b %>% 
  ggplot(., aes(x = factor(x, 
                           levels = c(1,0),
                           labels = c('Highest', 'Lowest')),
                y = predicted,
                ymin = conf.low,
                ymax = conf.high)) +
  geom_col(color = 'black', fill = 'gray60') +
  geom_errorbar(width = 0.2) +
  david_func() +
  labs(x = '', 
       y = 'Core Anti-Intellectualism') +
  coord_flip()+
  ylim(0,.7)

# together
pppp <- p1 + p2 + plot_layout(ncol = 1, guides = 'collect')  

wrap_elements(pppp)+
  labs(tag = 'Anti-Semitism Index') +
  theme(
    plot.tag = element_text(size = 12, angle = 90),
    plot.tag.position = "left",
  )

Figure 6.4 -WAITING FOR NICK (estimates lightly off)

Show the code
# library(lavaan)
# 
# # Define the path model with observed variables
# model <- '
#   # Regression paths
#   antisemitismindex201 ~ biblebornchurchpca01 + pid3lean01 + bachelorsdegree + postgrad + white + male + age01 + famincmeansub01
#   antiintelpolpluspop401 ~ antisemitismindex201 + biblebornchurchpca01 + pid3lean01 + bachelorsdegree + postgrad + white + male + age01 + famincmeansub01
# '
# 
# # Fit the model
# fit <- sem(model, data = data21a, cluster = "inputstate", sampling.weights = 'teamweight')
# 
# # Get the summary of the model fit
# summary(fit)
# 

Figure 6.5

Show the code
# indignant
reg6.5a <- svyglm(antiintelpolpluspop401 ~ antisemitismindex201*pid3lean01 + bachelorsdegree + postgrad + intelfactrmlpurest601 + white + male + age01 + famincmeansub01, design = mydesign21, family = gaussian())

points6.5a <- get_model_data(reg6.5a, type = 'pred', terms = c('antisemitismindex201[0,1]', 'pid3lean01[0,1]'))

points6.5a <- as.data.frame(points6.5a)

p1 <- points6.5a %>% 
  ggplot(.,aes(x = factor(x, 
                          levels = c(1,0),
                          labels = c('Highest', 'Lowest')),
               y = predicted, 
               fill = factor(group, 
                        levels = c(1,0),
                        labels = c('Republicans', 'Democrats')),
               ymin = conf.low,
               ymax = conf.high)) +
  geom_col(color = 'black', position = 'dodge') +
  scale_fill_grey(start = 0.5, end = 0.8) +
  geom_errorbar(position = position_dodge(width = 0.9), width = 0.2) +
  david_func() +
  labs(x = '', 
       y = 'Indignant Anti-Intellectualism Index',
       fill = 'Party ID') +
  ylim(0,.8) +
  coord_flip() +
  guides(fill = guide_legend(reverse = TRUE))

# Core
reg6.5b <- svyglm(antiintelcorefactorpurest501  ~ antisemitismindex201*pid3lean01 + bachelorsdegree + postgrad + intelfactrmlpurest601 + white + male + age01 + famincmeansub01, design = mydesign21, family = gaussian())

points6.5b <- get_model_data(reg6.5b, type = 'pred', terms = c('antisemitismindex201[0,1]', 'pid3lean01[0,1]'))

points6.5b <- as.data.frame(points6.5b)

p2 <- points6.5b %>% 
  ggplot(.,aes(x = factor(x, 
                          levels = c(1,0),
                          labels = c('Highest', 'Lowest')),
               y = predicted, 
               fill = factor(group, 
                        levels = c(1,0),
                        labels = c('Republicans', 'Democrats')),
               ymin = conf.low,
               ymax = conf.high)) +
  geom_col(color = 'black', position = 'dodge') +
  scale_fill_grey(start = 0.5, end = 0.8) +
  geom_errorbar(position = position_dodge(width = 0.9), width = 0.2) +
  david_func() +
  labs(x = '', 
       y = 'Core Anti-Intellectualism Index',
       fill = 'Party ID') +
  ylim(0,.8) +
  coord_flip() +
  guides(fill = guide_legend(reverse = TRUE))



# all together
ppppp <- p1 + p2 + plot_layout(ncol = 1, guides = 'collect')

wrap_elements(ppppp)+
  labs(tag = 'Anti-Semitism Index') +
  theme(
    plot.tag = element_text(size = 12, angle = 90),
    plot.tag.position = "left",
  )

Old (ignore everything below; saving just in case)

Figure 2.1

Show the code
# # Creating variables based on text description
# group <- rep(c('Full Sample', 'Democrats', 'Republicans'), 2)
# type <- c('Core anti-intellectualism', 'Core anti-intellectualism', 'Core anti-intellectualism', 'Indignant anti-intellectualism', 'Indignant anti-intellectualism', 'Indignant anti-intellectualism')
# cors <- c(-.57, -.64, -.38, -.34, -.44, 0.01)
# 
# # combining into single dataframe
# f2.1 <- data.frame(group, type, cors)
# f2.1$group <- factor(f2.1$group, levels = c('Full Sample', 'Democrats', 'Republicans'))
# 
# # Plotting
# f2.1 %>% 
#   ggplot(aes(x = type, y = cors, fill = group)) +
#   geom_col(color = 'black', position = 'dodge') +
#   geom_text(aes(label = paste(cors), 
#                 vjust = ifelse(cors >= 0, -0.3, 1.3)), 
#             position = position_dodge(width = 0.9)) +
#   scale_fill_grey(start = 0.3, end = 0.8) +
#   labs(fill = 'Sample',
#        x = '', y = 'Pearson\'s Correlation') +
#   david_func()
Show the code
# 
# data <- data %>% mutate(pidfac_dr = case_when(pid3lean01 == 0 ~ 'Democrat',
#                                            pid3lean01 == 1 ~ 'Republican'),
#                         pidfac_di = case_when(pid3lean01 == 0 ~ 'Democrat',
#                                            pid3lean01 == 0.5 ~ 'Independent'),
#                         pidfac_ir = case_when(pid3lean01 == 1 ~ 'Republican',
#                                            pid3lean01 == 0.5 ~ 'Independent'),
#                         pidfac = case_when(pid3lean01 == 0 ~ 'Democrat',
#                                            pid3lean01 == 0.5 ~ 'Independent',
#                                            pid3lean01 == 1 ~ 'Republican'))
# 
# 
# data$pidfac_dr <- factor(data$pidfac_dr, levels = c('Republican', 'Democrat'))
# data$pidfac_di <- factor(data$pidfac_di, levels = c('Independent', 'Democrat'))
# data$pidfac_ir <- factor(data$pidfac_ir, levels = c('Republican', 'Independent'))
# 
# data$pid3lean01 <- factor(data$pid3lean01)
# 
# mydesign <- svydesign(ids = ~inputstate, weights = ~weight, data = data)
# 
# RtoD <- svyglm(pidfac_dr ~ justbachelorsdegree + graddegree + black + hispanic + male + age01 + faminc_missmedsub01 + churchatt601, design = mydesign, family = quasibinomial(link = "probit"))
# 
# ItoD <- svyglm(pidfac_di ~ justbachelorsdegree + graddegree + black + hispanic + male + age01 + faminc_missmedsub01 + churchatt601, design = mydesign, family = quasibinomial(link = "probit"))
# 
# RtoI <- svyglm(pidfac_ir ~ justbachelorsdegree + graddegree + black + hispanic + male + age01 + faminc_missmedsub01 + churchatt601, design = mydesign, family = quasibinomial(link = "probit"))
# 
# names <- c('t', 't', 't', 't', 't', 't', 't', 't')
# plot_summs(RtoD, ItoD, RtoI,
#            model.names = c('Republican to Democrat',
#                            'Independent to Democrat',
#                            'Republican to Independent'),
#            coefs = c('Bachelor\'s degree' = 'justbachelorsdegree',
#                      'Graduate Degree' = 'graddegree',
#                      'Black'= 'black',
#                      'Hispanic' = 'hispanic',
#                      'Male' = 'male',
#                      'Age' = 'age01',
#                      'Family Income' = 'faminc_missmedsub01',
#                      'Service Attendance' = 'churchatt601'),
#            colors = rep(c('black', 'gray30', 'gray60'),8 ),
#            legend.title = 'Outcome') +
#   david_func() +
#   labs(x = '', y = 'Coefficients')
Show the code
# # Models
# R2 <- svyglm(pidfac_r ~ justbachelorsdegree + graddegree + collegeelitism + black + hispanic + male + age01 + faminc_missmedsub01 + churchatt601, design = mydesign, family = quasibinomial(link = "probit"))
# 
# I2 <- svyglm(pidfac_i ~ justbachelorsdegree + graddegree + collegeelitism + black + hispanic + male + age01 + faminc_missmedsub01 + churchatt601, design = mydesign, family = quasibinomial(link = "probit"))
# 
# D2 <- svyglm(pidfac_d ~ justbachelorsdegree + graddegree + collegeelitism + black + hispanic + male + age01 + faminc_missmedsub01 + churchatt601, design = mydesign, family = quasibinomial(link = "probit"))
# 
# # Function to extract predicted probabilities
# pred_func <- function(reg, bach, grad, elite, pid, educ) {
# 
#     predict_data <- data.frame(
#     
#     # desired levels of education
#     justbachelorsdegree = bach,
#     graddegree = grad,
#     collegeelitism = elite,
#     
#     # covariates fixed at their menas
#     black = mean(data$black, na.rm = TRUE),
#     hispanic = mean(data$hispanic, na.rm = TRUE),
#     male = mean(data$male, na.rm = TRUE),
#     age01 = mean(data$age01, na.rm = TRUE),
#     faminc_missmedsub01 = mean(data$faminc_missmedsub01, na.rm = TRUE),
#     churchatt601 = mean(data$churchatt601, na.rm = TRUE)
#   )
#   
#   # Add labels to dataframes
#   preds <- predict(reg, newdata = predict_data, type = "response")
#   preds <- as.data.frame(preds)
#   preds$pid <- pid
#   preds$educ <- educ
#   return(preds)
#   
# }
# 
# # Creating dataframe of predictions
# dd1 <- pred_func(D2, 1, 0, 0,  'Democrat', 'Bachelor\'s degree (non-elite)') 
# dd2 <- pred_func(D2, 1, 0, 1,  'Democrat', 'Bachelor\'s degree (elite)') 
# dd3 <- pred_func(D2, 1, 1, 0,  'Democrat', 'Graduate degree (non-elite)') 
# dd4 <- pred_func(D2, 1, 1, 1,  'Democrat', 'Graduate degree (elite)') 
# 
# 
# ii1 <- pred_func(I2, 1, 0, 0,  'Independent', 'Bachelor\'s degree (non-elite)') 
# ii2 <- pred_func(I2, 1, 0, 1,  'Independent', 'Bachelor\'s degree (elite)') 
# 
# ii3 <- pred_func(I2, 1, 1, 0,  'Independent', 'Graduate degree (non-elite)') 
# ii4 <- pred_func(I2, 1, 1, 1,  'Independent', 'Graduate degree (elite)') 
# 
# 
# rr1 <- pred_func(R2, 1, 0, 0,  'Republican', 'Bachelor\'s degree (non-elite)') 
# rr2 <- pred_func(R2, 1, 0, 1,  'Republican', 'Bachelor\'s degree (elite)') 
# 
# rr3 <- pred_func(R2, 1, 1, 0,  'Republican', 'Graduate degree (non-elite)') 
# rr4 <- pred_func(R2, 1, 1, 1,  'Republican', 'Graduate degree (elite)') 
# 
# 
# f3.2 <- bind_rows(dd1, dd2, dd3, dd4, ii1, ii2, ii3, ii4, rr1, rr2, rr3, rr4)
# 
# f3.2 <- f3.2 %>% 
#   mutate(ymax = response + SE,
#          ymin = response - SE)
# 
# # Plot
# f3.2 %>% 
#   filter(pid != 'Independent') %>%
#   ggplot(., aes(x = educ, y = response, ymin = ymin, ymax = ymax)) +
#   geom_col(color = 'black', position = 'dodge', fill = 'grey60') +
#   geom_errorbar(color = 'black', position = position_dodge(width = 0.9),
#                 width = 0.2) +
#   facet_wrap(~pid) +
#   david_func() +
#   theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
#   scale_y_continuous(labels = scales::percent)
# 
Show the code
# predict_data <- data.frame(
#   justbachelorsdegree = 0,
#   graddegree = 0,
#   black = mean(data$black, na.rm = TRUE),
#   hispanic = mean(data$hispanic, na.rm = TRUE),
#   male = mean(data$male, na.rm = TRUE),
#   age01 = mean(data$age01, na.rm = TRUE),
#   faminc_missmedsub01 = mean(data$faminc_missmedsub01, na.rm = TRUE),
#   churchatt601 = mean(data$churchatt601, na.rm = TRUE)
# )
# 
# 
# less_than_bachelors <- predict(D, newdata = predict_data, type = "response")
# 
# predict_data <- data.frame(
#   justbachelorsdegree = 1,
#   graddegree = 0,
#   black = mean(data$black, na.rm = TRUE),
#   hispanic = mean(data$hispanic, na.rm = TRUE),
#   male = mean(data$male, na.rm = TRUE),
#   age01 = mean(data$age01, na.rm = TRUE),
#   faminc_missmedsub01 = mean(data$faminc_missmedsub01, na.rm = TRUE),
#   churchatt601 = mean(data$churchatt601, na.rm = TRUE)
# )
# 
# 
# bachelors <- predict(D, newdata = predict_data, type = "response")
# 
# predict_data <- data.frame(
#   justbachelorsdegree = 1,
#   graddegree = 1,
#   black = mean(data$black, na.rm = TRUE),
#   hispanic = mean(data$hispanic, na.rm = TRUE),
#   male = mean(data$male, na.rm = TRUE),
#   age01 = mean(data$age01, na.rm = TRUE),
#   faminc_missmedsub01 = mean(data$faminc_missmedsub01, na.rm = TRUE),
#   churchatt601 = mean(data$churchatt601, na.rm = TRUE)
# )
# 
# 
# graduate <- predict(D, newdata = predict_data, type = "response")
# 
# less_than_bachelors
# bachelors
# graduate
Show the code
# library(nnet)
# multi_mod <- multinom(pidfac ~ justbachelorsdegree + graddegree + black + hispanic + male + age01 + faminc_missmedsub01 + churchatt601, data = data, weights = weight)
# 
# predict_data <- data.frame(
#   justbachelorsdegree = 0,
#   graddegree = 0,
#   black = mean(data$black, na.rm = TRUE),
#   hispanic = mean(data$hispanic, na.rm = TRUE),
#   male = mean(data$male, na.rm = TRUE),
#   age01 = mean(data$age01, na.rm = TRUE),
#   faminc_missmedsub01 = mean(data$faminc_missmedsub01, na.rm = TRUE),
#   churchatt601 = mean(data$churchatt601, na.rm = TRUE)
# )
# 
# 
# predicted_probs <- predict(multi_mod, newdata = predict_data, type = "probs")
# predicted_probs