library(broom)
7 Visualizing statistical models
A more accurate title, of course, woudl be “visualizing outputs from statistical models”.
We’ll be using this package to help us out:
7.1 Nationscape data
# Read in Nationscape survey data:
library(tidyverse)
library(labelled)
<- readRDS("data_nationscape2019/Nationscape_first10waves.rds") a
7.1.1 Association between sexism and AOC favorability over time
We estimate 10 separate model, aka “rolling regressions”.
We run one model for each value of week
, and create a tibble thanks to the tidy()
function:
%>% filter(
a !is.na(aoc_Favorable),
!is.na(gender_att3_by1SD)) %>%
group_by(week) %>%
do(broom::tidy(lm(aoc_Favorable ~
+
gender_att3_by1SD + college_grad +
age + Black + Hispanic, data = .))) %>%
White # Which coefficient we wish to pull:
filter(term == "gender_att3_by1SD") %>%
ggplot(aes(x=week,y=estimate,
ymax = estimate + 1.96*std.error,
ymin = estimate - 1.96*std.error)) +
geom_pointrange(position = position_dodge(width = .45), size=.6) + labs(x="",y="Coefficient estimate",
color="Subset of respondents") +
ggtitle("Association between sexism and AOC favorability") +
geom_hline(yintercept=0, linetype=2)
It seems that the correlation between sexism and (lower) favorability of AOC is quite stable.
<- a %>% filter(
aoc_Favorable !is.na(aoc_Favorable),
!is.na(gender_att3_by1SD)) %>%
group_by(pid3) %>%
do(broom::tidy(lm(aoc_Favorable ~
+
gender_att3_by1SD + college_grad +
age + Black + Hispanic, data = .))) White
7.1.2 Demographics
We can now plot any set of coefficients, of course.
Let us try to see whether age correlates negatively with AOC’s favorability, as we might perhaps suspect:
%>% filter(pid3 <= 3 & term == "age") %>%
aoc_Favorable ggplot(aes(x=as_factor(pid3),y=estimate,
ymax = estimate + 1.96*std.error,
ymin = estimate - 1.96*std.error,
shape = factor(pid3))) +
geom_pointrange() + labs(x="",y="Coefficient estimate",color="")
Let’s improve the design a little bit:
%>% filter(pid3 <= 3,term == "age") %>%
aoc_Favorable ggplot(aes(x=as_factor(pid3),y=estimate,
ymax = estimate + 1.96*std.error,
ymin = estimate - 1.96*std.error,color=as_factor(pid3))) +
geom_pointrange(position = position_dodge(width = .45), size=.6) + labs(x="Model estimate among respondents who identified as a...",y="Coefficient estimate",
color="Subset of respondents") +
ggtitle("Association between age and AOC favorability") +
geom_hline(yintercept=0, linetype=2)
Need to fix the party colors:
%>% filter(pid3 <= 3,term == "age") %>%
aoc_Favorable ggplot(aes(x=as_factor(pid3),y=estimate,
ymax = estimate + 1.96*std.error,
ymin = estimate - 1.96*std.error,color=as_factor(pid3))) +
geom_pointrange(position = position_dodge(width = .45), size=.6) + labs(x="Model estimate among respondents who identified as a...",y="Coefficient estimate",
color="Subset of respondents") +
ggtitle("Association between age and AOC favorability") +
geom_hline(yintercept=0, linetype=2) +
scale_color_manual(values = c("blue3","darkred","grey40"))
7.2 AJPS (2021) data
library(tidyverse)
library(haven)
library(labelled)
# READ IN RECODED DATA
source("data_AJPS2021/0_ajps_recode.R")
We now turn again to the dataset posted by Uscinski et al. (2021).
# CT seems correlated with Trump FT among Dems
# Pop seems correlated with Trump FT among Reps
# Manich. correlated with Trump FT among all
<- lm(cexaggerate ~ pid*pop_Index, data=d3)
int1 <- lm(trumpft ~ pid*pop_Index, data=d3)
int2 <- lm(trumpft ~ pid*consp_Index, data=d3)
int3 <- lm(trumpft ~ pid*goodevil + consp_Index + pop_Index, data=d3) int4
::ggeffect(int1, terms=c("pid","pop_Index")) %>% plot() +
ggeffectslabs(y="Climate change is exaggerated",
x="PID (7=Strong Republican",color="Populism")