Skip to content

Instantly share code, notes, and snippets.

View jonrobinson2's full-sized avatar

Jonathan Robinson jonrobinson2

View GitHub Profile
require(tabulizer)
require(dplyr)
require(datapasta)
require(ggplot2)
require(gridExtra)
jr=theme(panel.background = element_rect(fill = NA, colour = "black", size = 0.25),
panel.border = element_blank(),
panel.grid.major = element_line(colour = "black", size = 0.05),
panel.grid.minor = element_line(colour = "black", size = 0.05),
@jonrobinson2
jonrobinson2 / cces_absentee_val.R
Last active December 15, 2020 21:43
Mark B CCES 2016 Mail Intention Validation
require(rio)
require(sqldf)
cces = import('~/Downloads/CCES16_Common_OUTPUT_Feb2018_VV.dta')
.cces = (cces[,c('CL_voterstatus','CC16_364','CC16_403','CL_E2016GVM','commonweight_vv','V101','CL_state')])
.cces = .cces[.cces$CL_voterstatus %in% c('active','inactive'),]
.cces$VOTE_LIKELIHOOD = car::recode(var = as.numeric(.cces$CC16_364),
recodes = "1='Yes'; 2='Probably' ; 3='Already voted' ; 4 = 'No'; else = NA")
.cces$VOTE_METHOD_SELFREPORT = car::recode(var = as.numeric(.cces$CC16_403),
wasserman_rejections = function(by_mail=.8,rejection=.04,
total_votes=2000000,
dem_pct=.65,pre_d_2way=.52){
rejections = (((total_votes*by_mail)*rejection))
pre_rej_vote = c(total_votes*pre_d_2way,total_votes*(1-pre_d_2way))
post_rej_vote = c((total_votes*pre_d_2way) - ((rejections * dem_pct)),
(total_votes*(1-pre_d_2way)) - ((rejections * (1-dem_pct))))

Rockville tracts for CVAP checks

  • 701007
  • 701006
  • 701002
  • 701210
  • 701001
  • 700905
  • 700904
  • 701102
## LOAD LIBRARIES
library(dplyr)
library(tidyr)
library(httr)
library(readxl)
## CUSTOM FUNCTION TO READ ALL SHEETS OF AN EXCEL FILE INTO A LIST OF DATA FRAMES OR TIBBLES THANKS STACK OVERFLOW!!
## https://stackoverflow.com/questions/12945687/read-all-worksheets-in-an-excel-workbook-into-an-r-list-with-data-frames
read_excel_allsheets <- function(filename, tibble = FALSE) {
sheets <- excel_sheets(filename)
remove(list=ls())
require(jsonlite)
require(ggplot2)
require(reshape2)
require(scales)
jr=theme(panel.background = element_rect(fill = NA, colour = "black", size = 0.25),
panel.border = element_blank(),
panel.grid.major = element_line(colour = "black", size = 0.05),
panel.grid.minor = element_line(colour = "black", size = 0.05),
require(data.table)
require(sqldf)
require(rvest)
require(dplyr)
fairvote=read_html('https://infogram.com/cong-primaries-by-party-1g0n2owgxvyrp4y') %>%
html_nodes('script') %>%
html_text()
fairvote=fairvote[[7]]
fairvote=unlist(strsplit(x = fairvote,fixed = T, split = c('[\"','\"]')))
require(arm)
set.seed(987654321)
.tmp=(rnorm(100000, mean=0))
.tmp=invlogit(.tmp)
shift_param=.3
par(mfrow=c(1,3))
hist(x=.tmp, main=paste0('Mean=',round(mean(.tmp),3)))
remove(list=ls())
require(googlesheets)
require(dplyr)
require(stringr)
#options(error = expression(NULL))
list_sheets=gs_ls()
wedding_list=gs_title("Jonarobin Master Wedding List")
@jonrobinson2
jonrobinson2 / cavs.R
Created May 6, 2016 00:54
Quick and Dirty not too pretty season stats
require(httr)
require(jsonlite)
require(ggplot2)
require(gridExtra)
require(directlabels)
cavs_reg=fromJSON('http://stats.nba.com/stats/teamgamelog?LeagueID=00&Season=2015-16&SeasonType=Regular+Season&TeamID=1610612739')
cavs_playoffs=fromJSON('http://stats.nba.com/stats/teamgamelog?LeagueID=00&Season=2015-16&SeasonType=Playoffs&TeamID=1610612739')
cavs=data.frame(cavs_reg$resultSets$rowSet)