A couple of exercises with purrr.

A couple of exercises with the presidential election data we messed with in lab. I hope this will advertise the advantage of group_by and nest in managing list of models.

First we’ll load the county presdidental election tables and do some initial manipulations.

library(tidyverse)
library(magrittr)

t20 <- "https://github.com/thomasjwood/ps7160/raw/master/election_analysis/election_context_2020.csv" %>% 
  read_csv %>% 
  mutate(
    gop_20 = trump20 %>% 
      divide_by(
        biden20 %>% add(trump20)
      ),
    gop_16 = trump16 %>%
      divide_by(
        clinton16 %>% add(trump16)
      ),
    gop_12 = romney12 %>% 
      divide_by(
        romney12 %>% add(obama12)
      )
  )

1. What’s the relationship between presidential vote and mean county income, by state and by year?

Regress the variables gop_20, gop_16, and gop_12 on household income, by state. Print a table which reports the state by year income coefficients, sorted by the coefficients

2. Which variable most diminishes the income effect’s magnitude?

Repeat the analysis above, but separately control for percent non white, percent foreign born, percent age 65 or more, and percent less than HS educational attainment. Which control variable most affects the magnitude of the income variable?