R packages

# install.packages("tidycensus")
# install.packages("gistr")
# install.packages("DT")
library(dplyr)
library(tibble)
library(tidycensus)
library(DT)
library(gistr)

Load functions and Census API key

census_api_key(Sys.getenv("CENSUS_API_KEY"))

source("R/voting_results.R")
source("R/acs/population.R")
source("R/acs/age.R")
source("R/acs/race.R")
source("R/acs/gini.R")
source("R/acs/income.R")
source("R/acs/unemployment.R")
source("R/acs/education.R")
source("R/acs/industry.R")
source("R/acs/citizenship.R")
source("R/acs/insurance.R")
source("R/acs/veterans.R")

Data

Presidential voting outcomes

votes <- get_voting_results(filename="presidential.csv")

datatable(votes)

American Community Survey (ACS) demographic variables

County-level demographic characteristics taken from the 2018 5-year American Community Survey for use in the association rules analysis:

  • Population counts by age band
  • Population counts by race
  • Gini index (income inequality)
  • Income per capita
  • Unemployment rate
  • Population counts by educational attainment
  • Population counts by industry
  • Population counts by citizenship status
  • Population counts by insurance coverage
  • Population counts by veteran status
population <- get_population(tag = "POPULATION")
age <- get_age(tag = "AGE")
race <- get_race(tag = "RACE")
gini <- get_gini(tag = "GINI")
income <- get_income(tag = "INCOME")
unemployment <- get_unemployment(tag = "UNEMPLOY")
education <- get_education(tag = "EDU_ATTAIN")
industry <- get_industry(tag = "INDUSTRY")
citizenship <- get_citizenship(tag = "CITIZEN")
insurance <- get_insurance(tag = "HEALTH_INSURANCE")
veterans <- get_veterans(tag = "VETERANS")

Merge votes and ACS data

data_joins <- votes %>%
  select(fips, state, name, party_winner, trump_pct, margin) %>%
  inner_join(population, by = c("fips" = "GEOID")) %>%
  inner_join(age, by = c("fips" = "GEOID")) %>%
  inner_join(race, by = c("fips" = "GEOID")) %>%
  inner_join(gini, by = c("fips" = "GEOID")) %>%
  inner_join(income, by = c("fips" = "GEOID")) %>%
  inner_join(unemployment, by = c("fips" = "GEOID")) %>%
  inner_join(education, by = c("fips" = "GEOID")) %>%
  inner_join(industry, by = c("fips" = "GEOID")) %>%
  inner_join(citizenship, by = c("fips" = "GEOID")) %>%
  inner_join(insurance, by = c("fips" = "GEOID")) %>%
  inner_join(veterans , by = c("fips" = "GEOID")) %>%
  drop_na()

data_joins %>%
  head() %>%
  datatable() %>%
  formatRound(
    columns = c("trump_pct", "margin"), 
    digits = 3
  )

Adjust demographics by population size

population_adjusted <- data_joins %>%
  mutate_at(vars(AGE_18_29,
                 AGE_30_44,
                 AGE_45_59,
                 AGE_60_Plus,
                 contains("UNEMPLOY"),
                 contains("HEALTH_INSURANCE"),
                 contains("VETERANS"),
                 contains("EDU_ATTAIN"),
                 contains("INDUSTRY")), 
            list(~./AGE_18_Plus)) %>%
  mutate_at(vars(contains("RACE"),
                 contains("CITIZEN")),
            list(~./POPULATION_Total)) 

population_adjusted %>%
  head() %>%
  datatable() %>%
  formatRound(
    columns = 5:52, 
    digits = 3
  )

Write data to local file

write_csv(population_adjusted, "county_data.csv")

Session info

sessionInfo()
## R version 4.0.3 (2020-10-10)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.2 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/atlas/libblas.so.3.10.3
## LAPACK: /usr/lib/x86_64-linux-gnu/atlas/liblapack.so.3.10.3
## 
## locale:
##  [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8       
##  [4] LC_COLLATE=C.UTF-8     LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8   
##  [7] LC_PAPER=C.UTF-8       LC_NAME=C              LC_ADDRESS=C          
## [10] LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] stringr_1.4.0     tidyr_1.1.3       readr_1.4.0       gistr_0.9.0      
## [5] DT_0.18           tidycensus_0.11.4 tibble_3.1.2      dplyr_1.0.6      
## 
## loaded via a namespace (and not attached):
##  [1] tidyselect_1.1.1   xfun_0.23          purrr_0.3.4        sf_0.9-8          
##  [5] lattice_0.20-41    vctrs_0.3.8        generics_0.1.0     htmltools_0.5.1.1 
##  [9] yaml_2.2.1         utf8_1.2.1         rlang_0.4.11       e1071_1.7-6       
## [13] pillar_1.6.1       foreign_0.8-80     glue_1.4.2         httpcode_0.3.0    
## [17] DBI_1.1.1          rappdirs_0.3.3     sp_1.4-5           uuid_0.1-4        
## [21] lifecycle_1.0.0    rvest_1.0.0        tigris_1.4         htmlwidgets_1.5.3 
## [25] evaluate_0.14      knitr_1.33         crosstalk_1.1.1    maptools_1.1-1    
## [29] curl_4.3.1         class_7.3-17       fansi_0.4.2        Rcpp_1.0.6        
## [33] KernSmooth_2.23-17 classInt_0.4-3     jsonlite_1.7.2     hms_1.1.0         
## [37] digest_0.6.27      stringi_1.6.2      grid_4.0.3         cli_2.5.0         
## [41] rgdal_1.5-23       tools_4.0.3        magrittr_2.0.1     proxy_0.4-25      
## [45] crul_1.1.0         crayon_1.4.1       pkgconfig_2.0.3    ellipsis_0.3.2    
## [49] xml2_1.3.2         rstudioapi_0.13    assertthat_0.2.1   rmarkdown_2.8     
## [53] httr_1.4.2         R6_2.5.0           units_0.7-1        compiler_4.0.3