Skip to content

Instantly share code, notes, and snippets.

chrishanretty

View GitHub Profile
@chrishanretty
chrishanretty / lockdown_survey.json
Created Jun 11, 2020
Learning under lockdown survey
View lockdown_survey.json
{
"class": "Survey",
"id": 609340,
"internal_title": "Learning under lockdown survey",
"l10n": "en_GB",
"runs": [
{
"class": "SurveyRun",
"alert_frequency": 0,
"alert_last_update": 0,
View crazy_lm.R
dat <- read.csv("https://www.dropbox.com/s/e3p6xvjq6desua8/am_i_going_crazy.csv?dl=1")
holder <- list()
for (i in 1:20) {
holder[[i]] <- lm(lasso ~ ols, data = dat)
}
### For me,
lapply(holder, coef)
### gives the following. Note the intercepts in runs 1, 5 and 8 are radically different
@chrishanretty
chrishanretty / party_join.R
Created May 3, 2020
Simulating party membership
View party_join.R
library(tidyverse)
###
nGrp <- 250
grp_ideal_points <- rnorm(nGrp, 0, 1)
grp_sd <- 1
ind_sd <- 1
party_pos <- -1
joinfunc1 <- function(ind_pos, party_pos) {
## Probability joining party
@chrishanretty
chrishanretty / covid_lines.R
Created Apr 16, 2020
Relationship between population and COVID-19 fatalities
View covid_lines.R
library(tidyverse)
library(covdata)
library(pwt9)
library(zoo) # for rolling avg.
library(ggrepel)
library(cowplot)
### Get covid data
data("covnat")
data("pwt9.1")
@chrishanretty
chrishanretty / longest_spell.R
Created Dec 16, 2019
Longest spell of increasing vote share in national elections
View longest_spell.R
library(tidyverse)
longest_increase <- function(sign_d) {
rles <- rle(sign_d)
rle_values <- rles$value
rle_lengths <- rles$length
rle_lengths <- rle_lengths[which(rle_values == 1)]
rle_values <- rle_values[which(rle_values == 1)]
max(rle_lengths)
}
@chrishanretty
chrishanretty / tv2019.R
Created Dec 15, 2019
Tactical voting site analysis
View tv2019.R
### Load libraries
library(tidyverse)
library(rio)
library(rdrobust)
library(hrbrthemes)
## Data from https://docs.google.com/spreadsheets/d/1uNdRzf5-IqnSwNCPCD8eTxsBKBEEaTmlvq4eP_pt7JI/edit?usp=sharing
dat <- read.csv("data.csv")
### Graph things
p1 <- ggplot(dat, aes(x = LD17, y = LD,
@chrishanretty
chrishanretty / bfb.R
Created Nov 4, 2019
Reverse engineer bar charts
View bfb.R
library(rio)
library(tidyverse)
library(pixmap)
res <- rio::import("https://www.britishelectionstudy.com/wp-content/uploads/2017/07/BES-2017-General-Election-results-file-v1.0.xlsx")
res <- res %>%
filter(Country != "Scotland")
for (ons_code in unique(res$ONSConstID)) {
@chrishanretty
chrishanretty / driving_licence.R
Created Oct 13, 2019
Vote intention amongst driving licence holders and all others
View driving_licence.R
library(rio)
library(tidyverse)
library(survey)
### Read in the data in. This assumes that you have the cumulative
### file downloaded to the working directory. The mapping function
### iterates over Understanding Society waves 1-8, and carries forward
### information on driving.
data <- map(1:8, function(i) {
### Get the file loaded for each wave
@chrishanretty
chrishanretty / 3darr.R
Created Aug 27, 2019
Getting a grand sum of a 3d array quickly in R
View 3darr.R
### This generates an array which takes up around 600 Mb in memory
input_sizes <- c(100, .1e6, 8)
names(input_sizes) <- c("draws", "nobs", "categories")
my_arr <- array(rnorm(prod(input_sizes)),
dim = input_sizes)
## Returns a 2D matrix of dimensions draws by nobs
## i.e., sums over categories
### This version takes around 17-18s on my laptop
@chrishanretty
chrishanretty / choroplot.R
Created Jun 12, 2019
Customizing choropleths in R
View choroplot.R
### Load libraries
library(ggplot2)
### Load data
dat <- read.csv("choroData.csv")
### Set up the bare plot
p <- ggplot(data = dat,
aes(x = long, y = lat, group = group,
fill = factor(Voucher.count.June.2019)))
You can’t perform that action at this time.