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);
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 / towns_fund.R
Created September 24, 2020 19:44
Towns fund analysis
### Start by plotting stuff
library(tidyverse)
library(hrbrthemes)
## Download data from https://docs.google.com/spreadsheets/d/1inaI17l9Q-XADRNE_toXLp65sKu4_x4bXUFjZ3qjB2o/edit?usp=sharing
dat <- read.csv("wherever_i_saved_the_google_docs.csv")
### Estimate a simple model which we'll use when graphing
mod <- glm(I(Funded == "Yes") ~ Rank2 + ConWinner1,
family = binomial,
library(tidyverse)
library(rdrobust)
library(hrbrthemes)
library(cowplot)
library(Cairo)
dat <- readRDS("tidy_data.rds")
dat <- dat %>%
mutate(count_satisfied.20 = round(pct_satisfied.20 * response.20),