Skip to content

Instantly share code, notes, and snippets.

chrishanretty

Block or report user

Report or block chrishanretty

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@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)))
@chrishanretty
chrishanretty / irine.R
Created May 28, 2019
Example of models with continuous and categorical coding
View irine.R
## Load packages
library(rio)
library(tidyverse)
library(gtsummary) ## optional
### Read the data
dat <- rio::import("Irine_Data.sav")
summary(dat$Retrospective_)
summary(dat$Party)
@chrishanretty
chrishanretty / ineq_changes.R
Created May 14, 2019
Plot relationship between inequality and democracy
View ineq_changes.R
### This uses code from Marta Kolczynska
### https://martakolczynska.com/post/participation-inequality-indices/
library(rio)
library(tidyverse)
library(vdem)
library(countrycode)
library(hrbrthemes)
vdem_part <- extract_vdem(name_pattern = "v2x_libdem",
@chrishanretty
chrishanretty / ineq_changes.R
Created May 14, 2019
Plot relationship between inequality and democracy
View ineq_changes.R
### This uses code from Marta Kolczynska
### https://martakolczynska.com/post/participation-inequality-indices/
library(rio)
library(tidyverse)
library(vdem)
library(countrycode)
library(hrbrthemes)
vdem_part <- extract_vdem(name_pattern = "v2x_libdem",
@chrishanretty
chrishanretty / unfolding.R
Created Apr 3, 2019
Unfolding models in Stan; problems when used transformed parameters block
View unfolding.R
library(rstan)
library(mudfold)
library(reshape2)
## Simulate data
K <- 12
J <- 100
simulation1 <- mudfoldsim(N=K, n=J, seed = 1)
dat <- simulation1$dat
@chrishanretty
chrishanretty / get_indicative_votes.R
Created Mar 27, 2019
Get indicative votes from Digiminister
View get_indicative_votes.R
### Load libraries
library(tidyverse)
library(purrr)
library(rio)
read_digiminster <- function(url, descr) {
loc <- tempfile()
download.file(url, loc)
df <- read.csv(loc, skip = 9, header = TRUE)
df$description <- descr
@chrishanretty
chrishanretty / plot_revoke.R
Created Mar 21, 2019
Plot signatures to a petition calling for revocation of Article 50
View plot_revoke.R
library(rjson)
library(rio)
library(tidyverse)
library(hrbrthemes)
library(ggrepel)
json_to_df <- function(f) {
json_obj <- tryCatch(fromJSON(file = f),
error = function(e) warning(paste0("No data in ",f)))
You can’t perform that action at this time.