Skip to content

Instantly share code, notes, and snippets.

@chrishanretty
chrishanretty / cogsd7.R
Created November 20, 2012 14:26
Create replacement risk using ParlGov data
library(plyr)
library(zoo)
startyr <- 1992
endyr <- 2012
pg <- read.csv("http://www.parlgov.org/stable/static/data/stable-utf-8/view_cabinet.csv",as.is=T)
## Exclude GDR
pg <- pg[which(pg$country_name_short!="GDR"),]
#!/usr/bin/perl -w
use strict;
use Switch;
binmode STDOUT, ":utf8";
use WWW::Mechanize;
use HTML::TokeParser;
use Data::Dumper;
my $start = 'http://www.sondaggipoliticoelettorali.it/';
@chrishanretty
chrishanretty / pooling_spe.R
Created February 10, 2013 18:21
Pooling the polls for Italy, final version
### Takes as input a Google Docs spreadsheet
### Outputs national and regional level estimates of support
### over time
### Load libraries
library(R2WinBUGS)
library(rjags)
library(RCurl)
library(reshape)
library(foreach)
@chrishanretty
chrishanretty / plot_comuni.R
Created March 1, 2013 17:39
Plot municipal levels of support for Italian parties
## Load libraries
library(sp)
library(RColorBrewer)
library(car)
library(stringdist)
## Read in the shapefile
shape <- readOGR("../shapefiles/",layer="Comuni_11")
shape$NOME <- iconv(shape$NOME, "latin1", "UTF8")
shape$NOME.lc <- tolower(shape$NOME)
@chrishanretty
chrishanretty / plot_votes.R
Created April 7, 2013 19:02
Plot votes for Italian presidential election candidates
dat <- read.csv("votazioni.csv",header=T)
## Exclude schede bianche, schede null
dat <- subset(dat,!is.element(Candidato,c("Schede bianche","Schede nulle","Voti dispersi")))
## Iterate over years
for (my.year in unique(dat$Year)) {
my.subset <- subset(dat,Year == my.year)
## top eight candidates
max.per.candidate <- tapply(my.subset$Voti,my.subset$Candidato,max)
max.per.candidate <- sort(max.per.candidate,decreasing=TRUE)[1:8]
@chrishanretty
chrishanretty / ages.R
Last active December 16, 2015 13:08
Ages of heads of state and government
## Assumes you have the Samuels/Shugart data from http://laderafrutal.com/academic/samuels-shugart.html
## and the CCP data from http://www.comparativeconstitutionsproject.org/
library(countrycode)
library(foreign)
library(plyr)
library(pwt)
@chrishanretty
chrishanretty / golder_letta.R
Created April 24, 2013 12:58
Predictions for duration of government formation, Letta government
library(foreign)
library(survival)
dat <- read.dta("delay.dta")
dat$bargainingdays [which(dat$bargainingdays == 0)] <- .01
dat$post_legislative_parties <- dat$postelection * dat$legislative_parties
dat$post_polariz <- dat$postelection * dat$polarization
dat$post_positive <- dat$postelection * dat$positive_parl
@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')