Skip to content

Instantly share code, notes, and snippets.

@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
### 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
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
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);
@chrishanretty
chrishanretty / seat_model.R
Created May 20, 2017 13:45
Dirichlet-multinomial seat model
library(dplyr)
library(reshape2)
library(nnet)
library(multinomRob)
library(VGAM)
#type <- "nnet"
type <- "vgam"
sim.multinom <- function(object = mod, newdata = NULL, nSims = 10) {
### Some of this code is hacked from the internals of
library(countrycode)
library(tidyverse)
library(rio)
holder <- list()
### (0) PartyFacts lookup
pf <- read.csv("data/partyfacts-external-parties.csv")
### (1) Manifesto Project
@chrishanretty
chrishanretty / dontsmoothlineardgps.R
Created March 20, 2021 11:33
Smoothing and piecewise linear data-generating processes
### Let's generate some data with a step function
library(mgcv)
set.seed(1116)
nDays <- 200
day <- 1:nDays
y <- rnorm(200, 1, 0.25) + I(day > 100) * -0.5
y <- ifelse(y < 0,
@chrishanretty
chrishanretty / nor_distance.R
Created February 3, 2021 22:21
Distances to the Storting
library(sp)
library(raster)
library(viridis)
library(rgdal)
library(rnaturalearth)
### need two things, presumably
### - a polygon mask
### - a population raster
@chrishanretty
chrishanretty / replace_radar.R
Created February 2, 2021 17:10
Radar plot alternative
library(tidyverse)
### Pivot the table so we have four columns
### L/R, GAL/TAN, issue area, and value
dat <- read.csv("surridge1.csv") %>%
pivot_longer(cols = -c(Column1, Column2),
names_to = "Area")
### Tidy up the factor levels and order
dat <- dat %>%
@chrishanretty
chrishanretty / brm_multiple.R
Created January 27, 2021 21:16
Getting brm_multiple and future to play together
library(brms)
library(mice)
library(tictoc)
### Set a nice long number of iters so we can be sure it's not the
### overhead of spinning up cores, passing data, etc.,
nIters <- 1e5
thin <- nIters / 1000
# Load data
@chrishanretty
chrishanretty / 01_constitution_length.R
Created February 12, 2019 22:33
Plot the length of constitutions over time
### Generate a plot which shows the changing length of constitutions over time
### The plot uses data from the Comparative Constitutions Project
## Load libraries
library(tidyverse)
library(hrbrthemes)
## Set this to true and amend file_locn if you have the data
beast_mode <- FALSE
if (beast_mode) {