Skip to content

Instantly share code, notes, and snippets.

View andybega's full-sized avatar

Andreas Beger andybega

View GitHub Profile
@andybega
andybega / event-pull-for-simon.sql
Created October 23, 2013 19:59
Pull events for a given country between 2001 and 2011, for Simon's party project.
# Log in to event database first, e.g.
# mysql -u xxx -p -h IPaddress -D event_data
SET @countryid = (SELECT id FROM countries WHERE countryname='CYPRUS');
# Pull events and related information, takes 1.5m for Cyprus, and save
# to a csv file
CREATE TABLE temp_results AS
SELECT e.event_ID,
e.event_date,
@andybega
andybega / predictReport-worldmap.R
Created November 26, 2013 18:08
Map code for the prediction report
worldMap <- function(x, id, data, date='2008-01-01', legend.title=NULL,
maxy=1) {
# Input 2-column matrix with unique identifier and data to be mapped for
# state slice in "date", output thematic map.
require(cshapes)
require(maptools)
require(RColorBrewer) # for color palettes
require(plyr)
@andybega
andybega / vector-combinations.R
Last active December 31, 2015 04:19
For Scott, combinations of vectors that are survey answers.
# Change to your data
data <- data.frame(resp=1:5,
w11=sample(1:5, 5, replace=TRUE),
w12=sample(1:5, 5, replace=TRUE),
w13=sample(1:5, 5, replace=TRUE))
data$LoySum <- apply(data[, c("w11", "w12", "w13")], 1, mean)
# Names of the variables we want to look at
q.names <- c("w11", "w12", "w13")
@andybega
andybega / explore-global-wits.R
Created December 12, 2013 21:32
Quick look at the Global WITS data Gary sent us.
setwd("/Volumes/political-science/shared/ICEWS Project/C-IED/Data/Global_WITS")
# Read as txt chunk
# The first 16 lines are meta data
wits.text <- readLines("WITS.csv")
head(wits.text, n=16)
# Get actual data
wits <- read.csv(text=wits.text, header=TRUE, skip=16)
@andybega
andybega / pg-upgrade.sh
Created December 17, 2013 01:02
Updating PostgreSQL and PostGIS (installed via homebrew)
# from here http://blog.55minutes.com/2013/09/postgresql-93-brew-upgrade/
# Create backup of database
/usr/local/cellar/postgresql/9.2.4/bin/pg_dump -h localhost -p 5432 -U ab428 -Fc -b -v -f "/usr/local/var/pg_backup/afghanistan.backup" afghanistan
# Upgrade PostGIS
brew doctor # fix errors
brew update
brew upgrade postgres
brew upgrade gdal --with-postgres
@andybega
andybega / gt-post-correlations.R
Created December 30, 2013 12:24
Get CIDNE/GDELT/ICEWS series in the Ground Truth comparison blog post in order to calculate correlations. This is based on Ben's R code for the plots. There are some small differences because I am subsetting the events in SQL and using the data fields, not with grep of actual event type values.
## Correlations for blog post
library(plyr)
# from GT
# source directories and first few lines of pull-data.R
sql <-
"SELECT dateoccured AS date, count(*) AS sigacts
FROM sigacts
@andybega
andybega / archigos-polity.r
Created December 15, 2014 11:32
Descriptive look at Archigos leader entry/exit and tenure length for different regime types, using the Polity IV data and scheme for the latter.
# Archigos transitions and tenure by regime type
#
# Andreas Beger
# 15 December 2014
setwd("~/Desktop/archigos-polity")
library(foreign)
library(gdata)
@andybega
andybega / predict-ebma.r
Created March 18, 2015 13:26
Function to calculate EBMA ensemble from stream of input predictions and EBMA fitted model
# EBMA ensemble prediction
#
# Aggregate a set of predictions into an ensemble prediction using a fitted
# EBMA model. Recreates the functionality used to calculate test predictions
# during a EBMA calibration call.
#
# ebma - A fitted EBMA object; only works with logit EBMA.
# inputs - Matrix or data frame in which each column is a series of
# probabilities.
# outcome - Vector of observed outcomes. Lenght must match rows in inputs.
# Dot density map
# Eventually should be like http://www.radicalcartography.net/index.html?frenchkisses
library(maptools)
nc_SP <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1],
proj4string=CRS("+proj=longlat +ellps=clrk66"))
## Not run:
pls <- slot(nc_SP, "polygons")
@andybega
andybega / example.R
Created January 11, 2019 09:51
Reprex for win-builder R devel ggplot2-related error
library("ggplot2")
df <- structure(list(date = structure(c(17532, 17897, 18262, 18993,
19358, 19723, 20089), class = "Date"), id = c("9991", "9991",
"9991", "9992", "9992", "9992", "9992"), cowcode = c(999, 999,
999, 999, 999, 999, 999), y = c(1, 1, 1, 2, 2, 2, 2)), row.names = c(NA,
-7L), class = "data.frame")
ggplot(df, aes(x = date, y = y, group = cowcode)) + geom_line()