Skip to content

Instantly share code, notes, and snippets.

@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",
@chrishanretty
chrishanretty / cl-jags.R
Created October 27, 2012 17:50
Conditional logit in R + JAGS
## Load libraries
library(mclogit)
library(reshape)
library(rjags)
library(R2WinBUGS) ## for write.model
## Load the data file from mclogit
data(Transport)
## Do the mclogit model
@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"),]
@chrishanretty
chrishanretty / pooling_termometro.R
Last active January 20, 2016 07:04
Pooling the polls for Italy
## Please note, this script is entirely derivative of work done
## by Simon Jackman for his article `Pooling the Polls...'
## Aust. J. Pol. Science, 40(4): 499-517
## Libraries
library(reshape)
library(rjags)
library(R2WinBUGS)
library(ggplot2)
library(scales)
#!/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