Assignment: 2016 US Senate Montana election: Tester/Rosendale
The Senate election may be an even better test of these two hypotheses, because the election was even closer to 50-50, but this time Tester the Democrat won.
Run through the senate election as we did above with the house election. You can get the data from the same spreadsheet, but it’s on sheet = 1.
library(tidyverse)
library(leaflet)
library(sf)
library(readxl)
library(DT)
library(plotly)
library(broom)
library(tidycensus)
census_api_key("3d4c45181796a28eee4974c4c41568433a7d229f")
To install your API key for use in future sessions, run this function with `install = TRUE`.
senate_counties <- read_xlsx("Statewide Results.xlsx", sheet = 1, range = "B7:E63")
senate_counties <- senate_counties %>%
rename(Republican = "MATT ROSENDALE\r\nRepublican") %>%
rename(Democrat = "JON TESTER\r\nDemocrat") %>%
rename(Libertarian = "RICK BRECKENRIDGE\r\nLibertarian")
For this assignment, we are looking at the 2016 Montana Senate race between Jon Tester (Democrat) and Matt Rosendale (Republican). Rick Breckenridge ran as a Libertarian, but we aren’t going to analyze his contribution to the race.
senate_counties <- senate_counties %>%
mutate(total_votes = Republican + Democrat + Libertarian) %>%
mutate(Repub_advantage = Republican/total_votes - Democrat/total_votes) %>%
mutate(Repub_advantage = round(Repub_advantage*100, 1))
senate_counties %>%
arrange(-Repub_advantage)
Here are the counties’ senate results organized by Democrat, Republican, and Libertarian votes. The Republican-Democrat split is shown in column “Repub_advantage” with a higher number indicating a Republican advantage, and a lower number (all the way through negative) indicating a Democrat advantage.
mt_counties <- get_acs(geography = "county",
variables = "B01003_001",
state = "MT",
geometry = TRUE)
Getting data from the 2014-2018 5-year ACS
Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
Using FIPS code '30' for state 'MT'
|
| | 0%
|
|= | 0%
|
|= | 1%
|
|== | 1%
|
|== | 2%
|
|=== | 2%
|
|=== | 3%
|
|==== | 3%
|
|==== | 4%
|
|===== | 4%
|
|===== | 5%
|
|====== | 5%
|
|====== | 6%
|
|======= | 6%
|
|======= | 7%
|
|======== | 7%
|
|========= | 8%
|
|========== | 8%
|
|========== | 9%
|
|=========== | 9%
|
|=========== | 10%
|
|============ | 10%
|
|============ | 11%
|
|============= | 11%
|
|============= | 12%
|
|============== | 12%
|
|============== | 13%
|
|=============== | 13%
|
|================ | 14%
|
|================= | 14%
|
|================= | 15%
|
|================== | 15%
|
|================== | 16%
|
|=================== | 16%
|
|=================== | 17%
|
|==================== | 17%
|
|==================== | 18%
|
|===================== | 18%
|
|===================== | 19%
|
|====================== | 19%
|
|====================== | 20%
|
|======================= | 20%
|
|======================== | 21%
|
|========================= | 21%
|
|========================= | 22%
|
|========================== | 22%
|
|========================== | 23%
|
|=========================== | 23%
|
|=========================== | 24%
|
|============================ | 24%
|
|============================ | 25%
|
|============================= | 25%
|
|============================= | 26%
|
|============================== | 26%
|
|=============================== | 27%
|
|================================ | 27%
|
|================================ | 28%
|
|================================= | 28%
|
|================================= | 29%
|
|================================== | 29%
|
|================================== | 30%
|
|=================================== | 30%
|
|=================================== | 31%
|
|==================================== | 31%
|
|==================================== | 32%
|
|===================================== | 32%
|
|===================================== | 33%
|
|====================================== | 33%
|
|======================================= | 34%
|
|======================================== | 34%
|
|======================================== | 35%
|
|========================================= | 35%
|
|========================================= | 36%
|
|========================================== | 36%
|
|========================================== | 37%
|
|=========================================== | 37%
|
|=========================================== | 38%
|
|============================================ | 38%
|
|============================================ | 39%
|
|============================================= | 39%
|
|============================================== | 40%
|
|=============================================== | 41%
|
|================================================ | 41%
|
|================================================ | 42%
|
|================================================= | 42%
|
|================================================= | 43%
|
|================================================== | 43%
|
|================================================== | 44%
|
|=================================================== | 44%
|
|=================================================== | 45%
|
|==================================================== | 45%
|
|==================================================== | 46%
|
|===================================================== | 46%
|
|====================================================== | 47%
|
|======================================================= | 47%
|
|======================================================= | 48%
|
|======================================================== | 48%
|
|======================================================== | 49%
|
|========================================================= | 49%
|
|========================================================= | 50%
|
|========================================================== | 50%
|
|========================================================== | 51%
|
|=========================================================== | 51%
|
|=========================================================== | 52%
|
|============================================================ | 52%
|
|============================================================ | 53%
|
|============================================================= | 53%
|
|============================================================== | 54%
|
|=============================================================== | 54%
|
|=============================================================== | 55%
|
|================================================================ | 55%
|
|================================================================ | 56%
|
|================================================================= | 56%
|
|================================================================= | 57%
|
|================================================================== | 57%
|
|================================================================== | 58%
|
|=================================================================== | 58%
|
|=================================================================== | 59%
|
|==================================================================== | 59%
|
|==================================================================== | 60%
|
|===================================================================== | 60%
|
|====================================================================== | 60%
|
|====================================================================== | 61%
|
|======================================================================= | 61%
|
|======================================================================= | 62%
|
|======================================================================== | 62%
|
|======================================================================== | 63%
|
|========================================================================= | 63%
|
|========================================================================= | 64%
|
|========================================================================== | 64%
|
|========================================================================== | 65%
|
|=========================================================================== | 65%
|
|=========================================================================== | 66%
|
|============================================================================ | 66%
|
|============================================================================= | 67%
|
|============================================================================== | 67%
|
|============================================================================== | 68%
|
|=============================================================================== | 68%
|
|=============================================================================== | 69%
|
|================================================================================ | 69%
|
|================================================================================ | 70%
|
|================================================================================= | 70%
|
|================================================================================= | 71%
|
|================================================================================== | 71%
|
|================================================================================== | 72%
|
|=================================================================================== | 72%
|
|=================================================================================== | 73%
|
|==================================================================================== | 73%
|
|===================================================================================== | 73%
|
|===================================================================================== | 74%
|
|====================================================================================== | 74%
|
|====================================================================================== | 75%
|
|======================================================================================= | 75%
|
|======================================================================================= | 76%
|
|======================================================================================== | 76%
|
|======================================================================================== | 77%
|
|========================================================================================= | 77%
|
|========================================================================================= | 78%
|
|========================================================================================== | 78%
|
|========================================================================================== | 79%
|
|=========================================================================================== | 79%
|
|=========================================================================================== | 80%
|
|============================================================================================ | 80%
|
|============================================================================================= | 80%
|
|============================================================================================= | 81%
|
|============================================================================================== | 81%
|
|============================================================================================== | 82%
|
|=============================================================================================== | 82%
|
|=============================================================================================== | 83%
|
|================================================================================================ | 83%
|
|================================================================================================ | 84%
|
|================================================================================================= | 84%
|
|================================================================================================= | 85%
|
|================================================================================================== | 85%
|
|================================================================================================== | 86%
|
|=================================================================================================== | 86%
|
|==================================================================================================== | 87%
|
|===================================================================================================== | 87%
|
|===================================================================================================== | 88%
|
|====================================================================================================== | 88%
|
|====================================================================================================== | 89%
|
|======================================================================================================= | 89%
|
|======================================================================================================= | 90%
|
|======================================================================================================== | 90%
|
|======================================================================================================== | 91%
|
|========================================================================================================= | 91%
|
|========================================================================================================= | 92%
|
|========================================================================================================== | 92%
|
|========================================================================================================== | 93%
|
|=========================================================================================================== | 93%
|
|============================================================================================================ | 93%
|
|============================================================================================================ | 94%
|
|============================================================================================================= | 94%
|
|============================================================================================================= | 95%
|
|============================================================================================================== | 95%
|
|============================================================================================================== | 96%
|
|=============================================================================================================== | 96%
|
|=============================================================================================================== | 97%
|
|================================================================================================================ | 97%
|
|================================================================================================================ | 98%
|
|================================================================================================================= | 98%
|
|================================================================================================================= | 99%
|
|================================================================================================================== | 99%
|
|================================================================================================================== | 100%
|
|===================================================================================================================| 100%
senate_counties[25, "County"] <- "Lewis and Clark"
mt_counties <- mt_counties %>%
mutate(County = gsub(" County, Montana", "", NAME)) %>%
rename(Population = estimate)
senate_election <- mt_counties %>%
full_join(senate_counties)
Joining, by = "County"
senate_election %>%
as_tibble() %>%
select(County, Population, Democrat, Republican, Libertarian, total_votes, Repub_advantage) %>%
datatable()
Here is a datatable of all the Senate votings throughout Montana counties. Glacier county has the highest Democrat advantage, and Garfield county has the highest Republican advantage.
vote_colors <- colorNumeric(palette = "viridis", domain = senate_election$Repub_advantage)
senate_election %>%
leaflet() %>%
addTiles() %>%
addPolygons(weight = 1,
fillColor = ~vote_colors(Repub_advantage),
label = ~paste0(County, ", Republican advantage = ", Repub_advantage),
highlight = highlightOptions(weight = 2)) %>%
setView(-110, 47, zoom = 6) %>%
addLegend(pal = vote_colors, values = ~Repub_advantage)
sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
Need '+proj=longlat +datum=WGS84'
Here’s a choreopleth of the senate voting in Montana counties, distinguished by color according to Republican or Democrat advantage. Not surprisingly, Missoula county has a much higher Democrat advantage over Republicans.
pop_model <- lm(Repub_advantage ~ Population, data = senate_election)
tidy(pop_model)
glance(pop_model)
sqrt(.1778615)
[1] 0.4217363
Here is a small table of a linear regression of the Republican advantage due to population. The r squared is 0.42, with a statistically significant p-value. This means that there is a positive (and significant) correlation between having a higher population and having a higher Democrat advantage.
senate_election %>%
plot_ly(x = ~Population,
y = ~Repub_advantage,
hoverinfo = "text",
text = ~paste("County:",
County, "<br>",
"Population: ", Population, "<br>",
"Republican advantage: ", Repub_advantage)) %>%
add_markers(showlegend = F, marker = list(opacity = 0.7)) %>%
layout(title = "Predicting Republican Vote Advantage from Population, by County",
xaxis = list(title = "County population"),
yaxis = list(title = "Republican vote advantage")) %>%
add_lines(y = ~fitted(pop_model))
NA
Here is a graph depicting the population by county, and the Republican/Democrat advantage as shown by the line. Most of the counties in Montana don’t have many people, so the graph is skewed. However, the linear regression clearly demonstrates a pattern between the population of a county and it’s political advantage. Eastern counties favored Matt Rosendale, while western counties voted for Tester.
senate_election <- senate_election %>%
mutate(Longitude = as_tibble(st_coordinates(st_centroid(senate_election$geometry)))$X) %>%
mutate(Latitude = as_tibble(st_coordinates(st_centroid(senate_election$geometry)))$Y)
st_centroid does not give correct centroids for longitude/latitude datast_centroid does not give correct centroids for longitude/latitude data
senate_election %>%
leaflet() %>%
addTiles() %>%
addPolygons(weight = 1) %>%
setView(-110, 47, zoom = 6) %>%
addCircleMarkers(~Longitude, ~Latitude)
sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
Need '+proj=longlat +datum=WGS84'
Here’s a graph illustrating the “center” of each county, geographically.
longitude_lm <- lm(Repub_advantage ~ Longitude, data = senate_election)
tidy(longitude_lm)
glance(longitude_lm)
sqrt(0.1732821)
[1] 0.4162717
This table shows a linear regression of population by geography. The r squared is 0.42 with a significant p-value, indicating that there is a significant, positive correlation between western counties having a higher population compared to eastern counties.
senate_election %>%
plot_ly(x = ~Longitude,
y = ~Repub_advantage,
hoverinfo = "text",
text = ~paste("County:", County, "<br>", "Longitude: ", Longitude, "<br>", "Republican advantage: ", Repub_advantage)) %>%
add_markers(marker = list(opacity = 0.7), showlegend = F) %>%
layout(title = "Predicting Republican Vote Advantage from Longitude, by County",
xaxis = list(title = "County longitude"),
yaxis = list(title = "Republican vote advantage")) %>%
add_lines(y = ~fitted(longitude_lm))
This graph illustrates the population density of Montana by geography, with the linear regression indicating a positive correlation between eastern counties (with lower populations) voting more Republican in the senate election. According to the table, Tester did better than Rosendale in the western counties.
multiple_lm <- lm(Repub_advantage ~ Population + Longitude, data = senate_election)
tidy(multiple_lm)
glance(multiple_lm)
sqrt(0.273999 )
[1] 0.5234491
Here is a multiple regression predicting the geographic area (west or east) and population. With an r squared value of 0.52 and a significant p-value, one can predict a positive correlation between population amount and geographic region.
senate_election %>%
plot_ly(x = ~Longitude, y = ~Population, z = ~Repub_advantage,
text = ~County, hoverinfo = "text") %>%
add_markers(opacity = .7, showlegend = F)
Here is a 3D graph showing the relationship between longitude, population, and voting behavior in Montana. The farther east you get, the more votes Matt Rosendale received. The farther west you get, the more votes Jon Tester received. A very interesting result with lots of implications for future politicians campaigning in Montana.