Skip to content

Instantly share code, notes, and snippets.

@chrishanretty
chrishanretty / gist:2e8e18c50f9ef3ce78544252339b0e7d
Created December 28, 2024 15:37
Replication of Hilbig and Wiedemann (2024), "How Budget Tradeoffs Undermine Electoral Incentives to Build Public Housing"
### Load libraries
library(tidyverse)
library(haven)
library(fixest)
library(modelsummary)
### Download data from https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/GTEBJA#
gwz <- read_rds("data_main.rds") |>
mutate(inc_share_diff = inc_share_diff * 100) |>
filter(main_sample == 1)
@chrishanretty
chrishanretty / le_turnout.csv
Created December 22, 2024 12:24
Turnout in local authorities in English, 1991 to 2023
Year Borough Type Frequency whole_bc_redistr
1991 Adur District council Thirds NA
1991 Allerdale District council Whole NA
1991 Alnwick District council Whole NA
1991 Amber Valley District council Thirds NA
1991 Arun District council Whole NA
1991 Ashfield District council Whole NA
1991 Ashford District council Whole NA
1991 Aylesbury Vale District council Whole NA
1991 Babergh District council Whole NA
@chrishanretty
chrishanretty / clogit_mes.R
Last active September 26, 2024 07:56
Two different ways of calculating "average effects" in conditional logit models
### Purpose of this code: estimate a conditional logit model, and
### calculate the average effect of changing one non-induced
### observation in each stratum to an induced observation.
library(survival)
library(marginaleffects)
data("infert")
## Create a subset, and identify a non-induced observation as a
## "focal" observation for which we will later change the value
@chrishanretty
chrishanretty / welsh_constituencies.R
Last active September 18, 2024 11:16
Analysis of Welsh Senedd constituency proposal
### Purpose of this code:
### - To generate pairwise average travel times between Westminster constituencies
### - To sample contiguous pairings of Westminster constituencies
### - To evaluate the travel times between pairings
### - To evaluate the compactness of pairings
### Inputs
### - Travel time data from https://data.ubdc.ac.uk/datasets/public-transport-travel-time-matrices-for-great-britain-ttm-2023
### - Shapefiles from https://geoportal.statistics.gov.uk/datasets/0d698c9712de4afcac9377367d831c1a_0/explore
### - lookups from https://geoportal.statistics.gov.uk/
@chrishanretty
chrishanretty / ge2024_disprop.R
Created July 5, 2024 07:58
Quick proportionality calculation
library(tidyverse)
dat <- tribble(~party, ~seat_share, ~vote_share,
"Lab", 411/632, .338,
"Con", 119/632, .237,
"LDem", 71/632, .122,
"SNP", 9/632, .024,
"Ind", 6/632, 0.02,
"Ref", 4/632, .143,
"Green", 4/632, .068,
@chrishanretty
chrishanretty / pop_cog.R
Created June 12, 2024 12:41
Cost of governing for populist parties
library(here)
library(tidyverse)
library(colorspace)
library(RColorBrewer)
library(hrbrthemes)
set.seed(2686)
here::i_am("R/rr_cog.R")
@chrishanretty
chrishanretty / changing_fx_of_consty_context.R
Created April 13, 2024 13:13
Separate multinomial regression of vote choice on lagged constituency vote shares for successive waves of the BES
### Load libraries
library(tidyverse)
library(haven) ## for Stata import
library(readxl)
library(marginaleffects) ## for easy predictions and contrasts
library(nnet) ## for nnet::multinom
library(furrr) ## to perform computations in parallel
### Set fewer workers if you have fewer cores
plan(multisession, workers = 12)
@chrishanretty
chrishanretty / bam_preds.R
Created April 4, 2024 11:18
Predictions from predict.bam and marginaleffects::predictions with and without discretization
### PURPOSE OF THIS CODE: estimate a large model in bam and speed-test
### predictions using mgcv::predict.bam and
### marginaleffects::predictions with and without discretization and
### parallelization
### ##################################################################
### load libraries
### ##################################################################
library(mgcv)
library(marginaleffects)
@chrishanretty
chrishanretty / breakfast.R
Created March 7, 2024 10:00
breakfast analysis
library(tidyverse)
library(rstan)
library(cmdstanr)
dat <- read.delim("data_attractiveness_juge_results.csv")
### Set subject IDs using the same factor levels
subject_levels <- unique(c(dat$ID_left,
dat$ID_right))
dat <- dat |>
@chrishanretty
chrishanretty / 20231111-voterid.R
Created November 11, 2023 16:46
Quick analysis of BES wave 25 questions on local election turnout
library(tidyverse)
library(haven)
### Replace your file location here
bes <- read_dta("~/Dropbox/bes_data/BES2019_W25_v25.0.dta")
### What was turnout like in the panel?
bes |>
filter(!is.na(localTurnoutRetro)) |>
group_by(localTurnoutRetro) |>