Ray Fair’s Data

url <- "https://raw.githubusercontent.com/jfcross4/data/master/ray_fair_slim.csv"
elec <- read.csv(url)

Plots and Bootstrapping

library(ggplot2)
elec %>% ggplot(aes(GDP_growth_elec_year, vote_share_incumbent))+geom_point()
elec %>% ggplot(aes(GDP_growth_elec_year, vote_share_incumbent, label=year))+geom_text()
elec %>% ggplot(aes(GDP_growth_elec_year, vote_share_incumbent, label=year))+geom_text()+geom_smooth(method="lm", se=FALSE)
lm(vote_share_incumbent~ GDP_growth_elec_year, data=elec)

m <- lm(vote_share_incumbent~ GDP_growth_elec_year, data=elec)
coef(m)


sample_frac(elec, 1, replace = TRUE)

m <- lm(vote_share_incumbent~ GDP_growth_elec_year, data=sample_frac(elec, 1, replace = TRUE))
coef(m)


bootstrapped_equations <- replicate(1e3,
{m <- lm(vote_share_incumbent~ GDP_growth_elec_year, data=sample_frac(elec, 1, replace = TRUE));
coef(m)})

View(bootstrapped_equations)

bootstrapped_equations[1,]
bootstrapped_equations[2,]

elec %>% ggplot(aes(GDP_growth_elec_year, vote_share_incumbent))+geom_point() + geom_abline(intercept = bootstrapped_equations[1,1], slope=bootstrapped_equations[2,1])

elec %>% ggplot(aes(GDP_growth_elec_year, vote_share_incumbent))+geom_point() + geom_abline(intercept = bootstrapped_equations[1,], slope=bootstrapped_equations[2,], color="blue", alpha=0.1, size=0.5)