Skip to content

Instantly share code, notes, and snippets.

@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) |>
@chrishanretty
chrishanretty / ipu_pct_female.R
Created April 6, 2023 08:01
Plot proportion of female parliamentarians over time
library(tidyverse)
### Write a function to download the data from the IPU
dl_data <- function(y, m) {
url <- paste0("https://data.ipu.org/api/women-ranking.csv?load-entity-refs=taxonomy_term%2Cfield_collection_item&max-depth=2&langcode=en&month=",
m,
"&year=",
y)
download.file(url, destfile = paste0("ipu_", y, m, ".csv"))
}
@chrishanretty
chrishanretty / longlat2lad.R
Created January 18, 2023 12:04
Match long/lat to local authority
### Load the necessary libraries
library(sf)
library(tidyverse)
### Read in the shapefile
### Downloaded from https://geoportal.statistics.gov.uk/datasets/ons::local-authority-districts-december-2022-boundaries-uk-bfc/explore?location=55.215430%2C-3.316939%2C6.61
lad_shp <- st_read("LAD_DEC_2022_UK_BFC.shp")
### Read in the list of coordinates
###
@chrishanretty
chrishanretty / wc_groups.R
Created November 20, 2022 13:10
Plot average levels of democracy per 2022 World Cup group
library(vdemdata)
library(tidyverse)
library(ggflags)
library(countrycode)
library(hrbrthemes)
### Get the V-Dem data in
data("vdem")
dat <- vdem |>
distinct(country_name, country_text_id, year,
v2x_polyarchy, v2x_polyarchy_sd) |>
@chrishanretty
chrishanretty / 2022-10-20-nc11.R
Created October 20, 2022 10:35
Analysis of House of Commons abortion clinics vote
library(tidyverse)
library(hrbrthemes)
### This section reads in the data -- scroll down for the code that
### generates the figures and the graphics
tibble::tribble(
~aye_vote, ~ONSConstID, ~Constituency, ~MP, ~Party, ~ConstyOpposition, ~ConstyOpposition_sd,
1, "E14000530", "Aldershot", "Leo Docherty", "Conservative", 1.67320394423204, 0.0170138236227471,
@chrishanretty
chrishanretty / cses5_canplace.R
Created September 20, 2022 14:37
Proportion of CSES wave 5 respondents who can't place themselves on a left-right scale or don't know what it is
### Purpose of this code: plot the proportion of people in each CSES
### wave 5 study who can place themselves on a left-right scale.
### You will need to have downloaded the CSES 5 data somewhere
library(tidyverse)
library(ggtext)
library(RColorBrewer)
library(countrycode)
library(hrbrthemes)
@chrishanretty
chrishanretty / pm_party_rank.R
Created September 12, 2022 06:46
How often is the Prime Minister supplied by the third (or fourth-, or fifth-) ranked party?
library(tidyverse)
library(hrbrthemes)
library(ggtext)
dat <- read.csv("view_cabinet.csv")
dat <- dat %>%
group_by(country_name_short,
country_id,
election_date,