Skip to content

Instantly share code, notes, and snippets.

@chrishanretty
chrishanretty / letta.R
Created April 30, 2013 08:41
Duration for Letta government
## ############################
## LOAD THE NECESSARY LIBRARIES
## ############################
library(foreign)
library(survival)
## ######################
## READ DATA AND DO DATES
## ######################
@chrishanretty
chrishanretty / aldrich_mckelvey.R
Created June 3, 2013 14:37
Aldrich-McKelvey analysis on British Candidate Surveys
### Load libraries
library(foreign)
library(basicspace)
library(ggplot2)
library(car)
### Demo
data(LC1980)
result <- aldmck(data=LC1980,
@chrishanretty
chrishanretty / atp3.py
Created July 2, 2013 07:39
ATP rankings scraper
import re
import codecs
import urllib.request
from bs4 import BeautifulSoup
from bs4 import SoupStrainer
start = 'http://www.atpworldtour.com/Rankings/Singles.aspx'
outfile = codecs.open('atp_ranks3.csv', 'w','utf-8')
@chrishanretty
chrishanretty / local_disproportionality.R
Created September 14, 2013 20:51
Calculate local indices of disproportionality
## Libraries
library(maptools)
library(rgdal)
library(plyr)
library(rgeos)
library(RColorBrewer)
## Read in the shape
x <- readShapeSpatial("boundaries/westminster_const_region.shp",IDvar="NAME")
## Get centroids
cents <- gCentroid(x,byid=TRUE)@coords
@chrishanretty
chrishanretty / irish_agencies.Rnw
Created November 4, 2013 18:33
R/Sweave code for a talk on the termination of Irish government agencies
\documentclass[14pt]{beamer}
\usetheme{default}
\usefonttheme{professionalfonts}
\usepackage[lm-default]{fontspec} % remove font-spec if maths are required
\setmainfont[Mapping=tex-text]{Fontin Regular}
\setsansfont[Mapping=tex-text]{Museo Sans 300} % Beamer uses sans-serif by default
\setbeamersize{text margin left=.5cm}
\setbeamersize{text margin right=.5cm}
\setbeamertemplate{headline}[authortitle]{}%
@chrishanretty
chrishanretty / times_only.R
Created November 29, 2013 22:45
Classifier for Times headlines
### Load libraries
library(RTextTools)
### Make sure .xlsx file has been converted to CSV properly
media <- read.csv("media1960-2008_websiteversion_111007.csv",
header=T,as.is=T)
media$Date <- as.Date(media$Date,"%d/%m/%Y")
media <- subset(media,Date > as.Date("1996-01-01"))
media <- media[,c("Title","Subtitle","Major_Topic")]
@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)