Skip to content

Instantly share code, notes, and snippets.

@chrishanretty
chrishanretty / irregular.R
Created February 22, 2014 19:28
Irregular changes of leader
library(foreign)
library(gdata)
### Load archigos data
### from http://www.rochester.edu/college/faculty/hgoemans/data.htm
archi <- read.dta("Archigos_2.9-Public.dta")
### Only `irregular' leader transitions with popular support
### with or without foreign support
archi <- subset(archi,exit==3 & exitcode %in% c(1,2))
@chrishanretty
chrishanretty / multivariate_normal_approximation.md
Created February 27, 2014 15:27
Stan pickiness re covariance matrices

% Constrained multivariate normals in Stan % Chris Hanretty % 2014/02/27

Suppose we are interested in recovered the location parameters of a multivariate normal distribution with a known covariance matrix. Specifically, suppose we are interested in recovering the location parameters given a covariance matrix that forces draws from this multivariate normal to sum to one. This would be useful for recovering information about `true' latent levels of party support given manifest polling data. In this case, we could use polling figures ($y_i$ for $i=1,...,I$) to construct a covariance matrix of the following form:

$$

### Libraries
library(pscl)
library(rjags)
library(rstan)
data(AustralianElectionPolling,package="pscl")
dat <- AustralianElectionPolling
dat$startDate <- as.Date(dat$startDate)
library(rgeos)
library(sp) ## the best package for handling shapefiles
library(spdep) ## for the function poly2nb
library(maptools) ## for the function readShapePoly
library(RColorBrewer) ## for pretty colour palettes
### Read in the shapefile
x <- readShapePoly("data/westminster_const_region.shp")
## Add the Press Association number
name2pa <- read.csv("data/name2pa.csv",header=T)
@chrishanretty
chrishanretty / eastminster_post.R
Created September 12, 2014 14:52
Small area estimates of Scottish independence referendum vote intention
## ----loadlibs, echo = FALSE----------------------------------------------
library(foreign)
library(lme4)
library(maptools)
library(car)
library(R2WinBUGS)
library(spdep)
library(ggplot2)
library(scales)
@chrishanretty
chrishanretty / Henderson replication.R
Created September 15, 2014 18:05
Are Scots more left-wing?
library(foreign)
library(car)
library(broom)
library(reshape)
library(ggplot2)
dat <- read.spss("BES2015_W2_Panel_v2.0.sav",to.data.frame = TRUE)
### First, left-right values
### Here, positive values -> more left wing
@chrishanretty
chrishanretty / onthenight.R
Last active August 29, 2015 14:06
On the night projections of #indyref results
## ----Setup, include=FALSE, results="hide", warning=FALSE, eval = TRUE----
## This chunk is for setting nice options for the output. Notice how
## we create both png and pdf files by default, so that we can easily process
## to both HTML and LaTeX/PDF files later.
opts_chunk$set(fig.path='figures/paper-', cache.path='cache/report-', dev=c("png","pdf"), fig.width=14, fig.height=7, fig.show='hold', fig.lp="fig:", cache=FALSE, par=TRUE, echo=FALSE, results="hide", message=FALSE, warning=FALSE, dpi = 300)
knit_hooks$set(par=function(before, options, envir){
if (before && options$fig.show!='none') par(mar=c(4,4,2,.1),cex.lab=.95,cex.axis=.9,mgp=c(2,.7,0),tcl=-.3)
}, crop=hook_pdfcrop)
@chrishanretty
chrishanretty / debate_analysis.R
Last active August 29, 2015 14:17
Did the 2010 debates increase political engagement?
## ----libraries, echo = FALSE, message = FALSE, warning = FALSE-----------
library(foreign)
library(cem)
library(MatchIt)
## ----datain, echo = FALSE, message = FALSE, warning = FALSE--------------
## Requires you to download the file from http://bes2009-10.org/bes-data.php
dat <- read.spss("CIPSDEC2311.SAV",to.data.frame = T)
@chrishanretty
chrishanretty / cses_import.R
Created June 23, 2015 09:01
Greece turnout analysis
dat <- read.fwf("cses4.dat",
widths = c(13, 14, 35, 8, 8, 18, 4, 1, 4, 10, 18, 18, 18, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 4, 2, 2, 4, 1, 1,
6, 1, 2, 2, 4, 3, 3, 2, 4, 1, 2, 1, 1, 1, 1, 1, 1, 2, 3, 1, 1, 1, 2, 3, 1, 1, 1, 1, 2, 2, 2, 1, 1, 4, 3, 2, 3, 3, 1, 5, 3, 4,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 1, 2, 2, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 2, 1, 1, 1, 1,
1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 4, 3, 16,
16, 16, 17, 17, 17, 18, 17, 18, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 16, 16, 17, 17, 16, 16, 17, 17, 17,
16, 16, 16, 16, 17, 16, 16, 17, 16, 15, 16, 16, 16, 16, 17, 17, 3, 3, 16, 16, 17, 17, 18, 16, 16, 16, 3, 16,
16, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 1, 2, 2, 2,
@chrishanretty
chrishanretty / imf.R
Created October 13, 2012 18:42
Replicates Chris Giles' analysis of IMF WEO
library(gdata)
library(countrycode)
infile <- "IMFmultipliers.xls"
imf_rep <- read.xls(infile, sheet = 2,
skip = 3,
header=F)
names(imf_rep) <- c("Country",
"gdp_4cast_2011","gdp_4cast_2012",