Skip to content

Instantly share code, notes, and snippets.

@chrishanretty
chrishanretty / 20231111-voterid.R
Created November 11, 2023 16:46
Quick analysis of BES wave 25 questions on local election turnout
View 20231111-voterid.R
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
View ipu_pct_female.R
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
View longlat2lad.R
### 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
View wc_groups.R
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
View 2022-10-20-nc11.R
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
View cses5_canplace.R
### 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?
View pm_party_rank.R
library(tidyverse)
library(hrbrthemes)
library(ggtext)
dat <- read.csv("view_cabinet.csv")
dat <- dat %>%
group_by(country_name_short,
country_id,
election_date,
@chrishanretty
chrishanretty / monarchic_survival.R
Created September 10, 2022 12:01
Update results of Kim (2021) to include binary variable for hereditary head of state
View monarchic_survival.R
### Replicate
###
### Kim, Nam Kyu. "Previous Military Rule and Democratic Survival." Journal of Conflict Resolution 65, no. 2-3 (2021): 534-562
###
### including an additional covariate for a hereditary head of state
###
library(rio)
library(tidyverse)
library(vdemdata)
library(countrycode)
@chrishanretty
chrishanretty / parish_control.R
Created March 14, 2022 11:48
R code to create a plot of parishes by party holding the corresponding Westminster seat
View parish_control.R
library(tidyverse)
library(rio)
library(hrbrthemes)
### (1) Get files in
### Parish to Ward
### https://geoportal.statistics.gov.uk/datasets/ons::parish-to-ward-to-local-authority-district-december-2020-lookup-in-england-and-wales-v2/about
par2ward <- read.csv("Parish_to_Ward_to_Local_Authority_District__December_2020__Lookup_in_England_and_Wales_V2.csv")
par2ward$FID <- NULL
@chrishanretty
chrishanretty / ord_dirichlet.R
Created November 16, 2021 19:45
Likely faulty Ordered Dirichlet R/Stan code
View ord_dirichlet.R
library(tidyverse)
library(cmdstanr)
library(MCMCpack)
### log PDF is based on eqn. 3 in https://www2.seas.gwu.edu/~dorpjr/Publications/Bookchapter/MarcelDekker2003.pdf
stan_code <-
"
functions {
real orddir_lpdf(vector y, real[] alpha) {
int K = size(alpha);