library(socsci)
## Loading required package: tidyverse
## -- Attaching packages ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.2.0 v purrr 0.3.2
## v tibble 2.1.3 v dplyr 0.8.1
## v tidyr 0.8.3 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.4.0
## -- Conflicts ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
## Loading required package: rlang
##
## Attaching package: 'rlang'
## The following objects are masked from 'package:purrr':
##
## %@%, as_function, flatten, flatten_chr, flatten_dbl,
## flatten_int, flatten_lgl, flatten_raw, invoke, list_along,
## modify, prepend, splice
## Loading required package: scales
##
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
##
## discard
## The following object is masked from 'package:readr':
##
## col_factor
## Loading required package: broom
## Loading required package: glue
##
## Attaching package: 'glue'
## The following object is masked from 'package:dplyr':
##
## collapse
library(fst)
library(car)
## Loading required package: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
##
## recode
## The following object is masked from 'package:purrr':
##
## some
library(interactions)
cces18 <- read.fst("C://cces18.fst") ### I have my data in a funky format so it loads faster. You probably need to load haven and use read_dta
reg <- cces18 %>%
filter(pid3 < 4) %>% ## Get rid of pid weirdos
mutate(pid3 = frcode(pid3 == 1 ~ "Democrat",
pid3 == 2 ~ "Republican",
pid3 == 3 ~ "Independent")) %>% ## This makes everything a factor
mutate(male = car::recode(gender, "1=1; else=0")) %>%
mutate(male = as.factor(male)) %>% ## Same here
mutate(age = 2018 - birthyr) %>%
mutate(meet = car::recode(CC18_417a_1, "1=1; else=0")) %>% ## just showing you two activities
mutate(sign = car::recode(CC18_417a_2, "1=1; else =0")) %>% ## here's the second one
mutate(activity = sign + meet) %>% ## adding them together
mutate(CC18_412 = as.numeric(CC18_412)) %>%
mutate(house = car::recode(CC18_412, "1 = 1; 2:90 = 0; else = NA")) ## This is one for Democrat vote in the house
gg <- glm(house ~ activity*pid3 + male + age + educ, data = reg, family = "binomial") ## Notice that we are interacting the activity variable with the pid3 variable
interact_plot(gg, pred= activity, modx = pid3, int.width = .76, interval = TRUE)
How do we interpret that? Well, the lines are all pretty flat. However, see the independent line? It tilts up slightly. That means if an independent engaged in both those activities they were slightly more likely to vote for the Democrat. Looks like it jumps about 5% or so, but Iām just eyeballing it.
All you have to do is save the interaction as an object and just add ggplot stuff to it. Like so:
interact <- interact_plot(gg, pred= activity, modx = pid3, int.width = .76, interval = TRUE)
interact +
scale_y_continuous(labels = percent) +
labs(x = "", y = "Predicting a Vote for the Democrat", title = "How Does Political Activity Change Vote Choice?", caption = "Data: CCES 2018")