Skip to content

Instantly share code, notes, and snippets.

View elliottmorris's full-sized avatar

G. Elliott Morris elliottmorris

View GitHub Profile
@elliottmorris
elliottmorris / rasmussen_v_other_approval.R
Created April 12, 2020 18:29
compare rasmussen polling average vs average of all other polls
library(tidyverse)
library(zoo)
trump_approve <- read_csv("https://projects.fivethirtyeight.com/polls-page/president_approval_polls.csv")
trump_approve <- trump_approve %>%
mutate("date" = mdy(end_date)) %>%
dplyr::select(pollster,
date,
"approve" = yes,
@elliottmorris
elliottmorris / polling_averages.R
Last active March 21, 2020 00:26
historical polling averages + 2020
library(tidyverse)
library(politicaldata)
library(lubridate)
library(zoo)
library(gghighlight)
today_2020_time_difference <- as.numeric(difftime(ymd('2020-11-03'),Sys.Date(),units = 'days'))
# wrangle -----------------------------------------------------------------
# historical
@elliottmorris
elliottmorris / historical_approval.R
Last active July 29, 2023 14:00
historical presidential approval ratings
rm(list = ls()) #reset the environment
library(tidyverse)
library(lubridate)
library(mgcv)
exponent_weight <- function(i) {
exp(-0.04*i)
}
TODAY_DAY <- difftime(Sys.Date(),as.Date("2017-01-21"))
@elliottmorris
elliottmorris / trump_approve_by_mode.R
Last active February 11, 2020 17:00
Trump approval by mode
library(tidyverse)
library(lubridate)
library(colorspace)
# get raw polls
if(!exists("raw_polls")){
raw_polls <- read_csv('https://projects.fivethirtyeight.com/polls-page/president_approval_polls.csv')
}
# selects cols etc
@elliottmorris
elliottmorris / white evangelical pop by state.csv
Created December 19, 2019 21:31
white evangelical population by state
state_name white_evangel n prop
Alabama No 2635716.4303168333 0.7215633301038805
Alabama Yes 1017069.5696831668 0.27843666989611954
Alaska No 408543.06241488626 0.7700455804134742
Alaska Yes 122000.93758511376 0.22995441958652582
Arizona No 3873710.103017639 0.8261500325275896
Arizona Yes 815159.8969823611 0.17384996747241044
Arkansas No 1325152.1454922806 0.6069750351168169
Arkansas Yes 858054.8545077195 0.3930249648831831
California No 22234460.694529217 0.8909648084418956
@elliottmorris
elliottmorris / state_edu_targets_2016.csv
Last active November 21, 2019 18:04
State-level educational attainment among predicted 2016 voters
state_abb pct_bachelors_higher
DC 0.6483020341946732
MA 0.4908509625882079
CT 0.4712702755955542
CO 0.4706548492306638
NJ 0.45938202372319153
NY 0.45520330118475155
MD 0.4496009185101967
CA 0.42358538595803436
VA 0.42333224401155256
@elliottmorris
elliottmorris / buttigeg_rise.R
Last active November 17, 2019 00:50
Analysis of Buttigieg's rise
# This file runs an analysis of 2020 Democratic primary polls to
# assess relationships between candidate's polling averages using
# variance-covariance matrices.
#
# Read more about it here:
# https://thecrosstab.substack.com/p/buttigiegs-rise-has-cost-biden-and
rm(list=ls())
library(tidyverse)
library(lubridate)
@elliottmorris
elliottmorris / .gitignore
Created October 28, 2019 19:20
default gitignore
# History files
.Rhistory
.Rapp.history
# Session Data files
.RData
# User-specific files
.Ruserdata
@elliottmorris
elliottmorris / mrp_cces.R
Last active June 5, 2019 14:18
MrP with CCES example code
vote_model <- brm(mvbind(vote_trump,vote_clinton) ~ 1 +
# pop level
state_clinton + state_black_pct + state_hispanic_pct + state_white_protestant +
# group effects
(1|sex) + (1|race) + (1|age) + (1|edu) + (1|state_name) + (1|region) +
# interactions b/t group effects
(1|race:edu) + (1|age:edu) + (1|race:region),
data = cces,
family = bernoulli(link = "logit"),
# stan stuff
@elliottmorris
elliottmorris / democrats_generic_ballot.R
Created August 31, 2018 15:05
Graph the democratic margin on the generic ballot
library(tidyverse)
polls_2018 <- read_tsv("http://elections.huffingtonpost.com/pollster/api/v2/questions/18-US-House/poll-responses-clean.tsv") %>%
filter(month(end_date)>=6) %>%
mutate(Dem.Margin = Democrat-Republican) %>%
group_by(mode) %>%
mutate(mean_dem = mean(Dem.Margin))
ggplot(polls_2018,aes(x=Dem.Margin,fill=Dem.Margin>0,col=Dem.Margin>0)) +
geom_bar(width=0.8,alpha=0.5) +
scale_color_manual(values=c("TRUE"="blue","FALSE"="red")) +