Skip to content

Instantly share code, notes, and snippets.

@elliottmorris
Last active March 23, 2017 23:51
Show Gist options
  • Save elliottmorris/e69f22e8271d31d39d2204fb8f4fca76 to your computer and use it in GitHub Desktop.
Save elliottmorris/e69f22e8271d31d39d2204fb8f4fca76 to your computer and use it in GitHub Desktop.
Code for analyzing public opinion and healthcare CBO impact numbers for the AHCA
## R Code for analysing AHCA CBO outlook and congress data
## Written by G. Elliott Morris
## @gelliottmorris | elliott@thecrosstab.com | TheCrosstab.com
## please do not replicate the analysis without permission.
##
## Additional info:
## DOWNLOAD DATA AT http://bit.ly/2oa0RPa
## Github Gist lives at: http://bit.ly/2nI5bZu
# libraries ####
rm(list = ls()) #reset the environment
library(arm)
library(foreign)
library(tidyverse)
library(stargazer)
library(USAboundaries)
source('~/Desktop/theme_elliott.R')
# load data ####
house <- read.csv("house_and_ahca.csv")
house$state <- substr(house$Code,1,2)
house <- house %>% mutate (Net_Support = Total.Support - Total.Oppose,
GOP_Margin = X2016.GOP - X2016.Dem,
Total_Loss_Nonelderly = as.numeric(Total_Loss_Nonelderly),
Loss_Hundreds = Total_Loss_Nonelderly/100)
house <- house[house$GOP_Margin > -100 & house$GOP_Margin < 100,] # only look at districts from 2016 that had competition
house$state_name <- sub(" .*", "", house$District)
house$cd114fp <- sub('.*\\-', '', house$Code)
house$cd114fp <- sub('AL', '01', house$cd114fp)
# analyze ####
stargazer(lm(Net_Support ~ GOP_Margin, house),
lm(Net_Support ~ Loss_Hundreds,house),
lm(Net_Support ~ GOP_Margin + Loss_Hundreds,house),
type = "text",
title = "Support for the AHCA by Congressional District")
mean(house[house$Party == "D",]$Total_Loss_Nonelderly,na.rm = TRUE)
mean(house[house$Party == "R",]$Total_Loss_Nonelderly,na.rm = TRUE)
mean(house[house$Party == "D",]$Net_Support,na.rm = TRUE)
mean(house[house$Party == "R",]$Net_Support,na.rm = TRUE)
median(house[house$Net_Support >=0,]$GOP_Margin,na.rm = TRUE)
# graphics ####
#map
library(USAboundaries)
library(USAboundariesData)
library(sp)
state_names <- as.list(state.name)
us_congress <- us_congressional(resolution = "low", states = as.character(state_names[!state_names %in% c("Hawaii","Alaska")]))
us_congress@data <- left_join(us_congress@data,house,by=c("state_name","cd114fp"))
#ggplot(us_congress) +
aes(long,lat,group=group) +
geom_polygon(aes(fill=data$Net_Support)) +
geom_path(color="black") +
coord_equal()+
theme_void()
# margin & support - label
gg <- ggplot(data = house, aes(x=GOP_Margin,
y = Net_Support)) +
geom_hline(yintercept=0,linetype=2) +
#geom_point()+
geom_label(aes(label = Code,col = state)) +
geom_smooth(method = "lm",se=FALSE) +
theme_elliott() +
labs(title = "Support for A.H.C.A. by Congressional District",
subtitle = "Net support as a function of gop margin of victory in each district",
x = "GOP Margin of Victory (%)",
y = "Net Support (%)",
caption = "Data: http://bit.ly/2oa0RPa") +
theme(legend.position = "none")
grid.arrange(gg,my_g,heights=c(9, .5))
dev.copy(png,"graphics/AHCA_GOP_Label.png",width=8,height=6,unit="in",res=350)
dev.off()
# margin and support - point
gg <- ggplot(data = house, aes(x=GOP_Margin,
y = Net_Support)) +
geom_hline(yintercept=0,linetype=2) +
geom_point(shape = 1, aes(col = state))+
geom_smooth(method = "lm",se=FALSE) +
theme_elliott() +
labs(title = "Support for A.H.C.A. by Congressional District",
subtitle = "Net support as a function of gop margin of victory in each district",
x = "GOP Margin of Victory (%)",
y = "Net Support (%)",
caption = "Data: http://bit.ly/2oa0RPa") +
theme(legend.position = "none")
grid.arrange(gg,my_g,heights=c(9, .5))
dev.copy(png,"graphics/AHCA_GOP_Point.png",width=8,height=6,unit="in",res=350)
dev.off()
# loss and support - label
gg <- ggplot(data = house, aes(x=Total_Loss_Nonelderly,
y = Net_Support)) +
geom_hline(yintercept=0,linetype=2) +
#geom_point(shape = 1, aes(col = state))+
geom_label(aes(label = Code,col = state)) +
geom_smooth(method = "lm",se=FALSE) +
theme_elliott() +
labs(title = "Support for A.H.C.A. by Congressional District",
subtitle = "Net support as a function of total non-elederly health insurance loss per district",
x = "Total Insurance Loss (Nonelderly)",
y = "Net Support (%)",
caption = "Data: http://bit.ly/2oa0RPa") +
theme(legend.position = "none")
grid.arrange(gg,my_g,heights=c(9, .5))
dev.copy(png,"graphics/AHCA_Loss_Label.png",width=8,height=6,unit="in",res=350)
dev.off()
# loss and support - point
gg <- ggplot(data = house, aes(x=Total_Loss_Nonelderly,
y = Net_Support)) +
geom_hline(yintercept=0,linetype=2) +
geom_point(shape = 1, aes(col = state))+
#geom_label(aes(label = Code,col = state)) +
geom_smooth(method = "lm",se=FALSE) +
theme_elliott() +
labs(title = "Support for A.H.C.A. by Congressional District",
subtitle = "Net support as a function of total non-elederly health insurance loss per district",
x = "Total Insurance Loss (Nonelderly)",
y = "Net Support (%)",
caption = "Data: http://bit.ly/2oa0RPa") +
theme(legend.position = "none")
grid.arrange(gg,my_g,heights=c(9, .5))
dev.copy(png,"graphics/AHCA_Loss_Point.png",width=8,height=6,unit="in",res=350)
dev.off()
# gop shift 2014 -> 2016
house_shift <- house %>% mutate(Shift =(X2016.Dem - X2016.GOP) - (X2014.Dem - X2014.GOP) ) %>% filter(X2016.Dem > 10, X2014.Dem > 10, X2016.GOP > 10, X2016.Dem > 10 ,Party == "R", Shift > 0)
gg <- ggplot(data = house_shift, aes(x=Shift,
y = Net_Support)) +
geom_hline(yintercept=0,linetype=2) +
#geom_point(shape = 1, aes(col = state))+
geom_label(aes(label = Code,col = GOP_Margin)) +
geom_smooth(method = "lm",se=FALSE) +
theme_elliott() +
labs(title = "Support for A.H.C.A. by Congressional District",
subtitle = "Among Republican districs that shifted towards the Democratic house candidate from 2014 to 2016.",
x = "Shift towards Democratic House Candidate (%)",
y = "Net Support (%)",
caption = "Data: http://bit.ly/2oa0RPa") +
theme(legend.position = "right")+
scale_color_gradient2("2016\nGOP Margin",low = "#A569BD",mid = "#E74C3C", high = "#E74C3C",midpoint = 30)
grid.arrange(gg,my_g,heights=c(9, .5))
dev.copy(png,"graphics/AHCA_Supp_SHIFT.png",width=8,height=6,unit="in",res=350)
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment