Skip to content

Instantly share code, notes, and snippets.

View elliottmorris's full-sized avatar

G. Elliott Morris elliottmorris

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / polls_biden_v_clinton_2020.R
Last active August 16, 2020 15:16
Code to make a chart that compares Biden's polling numbers to Hillary Clinton's 2016 performance
library(tidyverse)
library(janitor)
library(lubridate)
library(zoo)
library(politicaldata)
RUN_DATE <- Sys.Date()
start_date <- ymd("2020-02-01")
election_day <- ymd('2020-11-03')
@elliottmorris
elliottmorris / tipping-point_states.R
Last active August 26, 2020 00:18
Code to generate the tipping-point index from our model's simulations
library(pbapply)
library(tidyverse)
mod <- read_csv('~/Downloads/output/site_data/electoral_college_simulations.csv')
mod <- mod %>% gather(state,vote,4:ncol(.))
evs <- read_csv('data/prior/state_evs.csv')
mod <- mod %>% left_join(evs)
tp <- pblapply(1:max(mod$draw),
cl = 12,
@elliottmorris
elliottmorris / poll_change_trend.gg
Last active October 29, 2020 02:36
Charts the poll-level trend in 2020 polls
library(tidyverse)
library(lubridate)
library(pbapply)
url<- 'https://docs.google.com/spreadsheets/d/e/2PACX-1vQ56fySJKLL18Lipu1_i3ID9JE06voJEz2EXm6JW4Vh11zmndyTwejMavuNntzIWLY0RyhA1UsVEen0/pub?gid=0&single=true&output=csv'
all_polls <- read_csv(url)
# remove any polls if biden or trump blank
all_polls <- all_polls %>% filter(!is.na(biden),!is.na(trump))#, include == "TRUE")
@elliottmorris
elliottmorris / election_night_live_model.R
Last active October 18, 2025 10:10
A live election-night prediction model using The Economist's pre-election forecast
#' Description
#' This file runs a live election-night forecast based on The Economist's pre-election forecasting model
#' available at projects.economist.com/us-2020-forecast/president.
#' It is resampling model based on https://pkremp.github.io/update_prob.html.
#' This script does not input any real election results! You will have to enter your picks/constraints manually (scroll to the bottom of the script).
#'
#' Licence
#' This software is published by *[The Economist](https://www.economist.com)* under the [MIT licence](https://opensource.org/licenses/MIT). The data generated by *The Economist* are available under the [Creative Commons Attribution 4.0 International License](https://creativecommons.org/licenses/by/4.0/).
#' The licences include only the data and the software authored by *The Economist*, and do not cover any *Economist* content or third-party data or content made available using the software. More information about licensing, syndication and the copyright of *Economist* content can be fou