Skip to content

Instantly share code, notes, and snippets.

View andybega's full-sized avatar

Andreas Beger andybega

View GitHub Profile
@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.
@andybega
andybega / duke-grade-infl.R
Last active December 17, 2015 02:09
Plot Duke average GPA from 1932 on
library(XML)
library(plyr)
# Get and format html file
duke.html <- readLines("http://www.gradeinflation.com/Duke.html")
duke.doc <- htmlParse(duke.html)
# Get table as data frame
duke <- readHTMLTable(duke.doc, header=F, as.data.frame=F)
duke <- data.frame(duke, stringsAsFactors=F)
@andybega
andybega / plotMissing.R
Created May 24, 2013 15:36
Function to visualize missing values in a data frame or in a data frame representing panel data (or any other 2 dimensions).
plotMissing <- function(data, dim1=NULL, dim2=NULL, na.id=NULL) {
require(ggplot2)
require(grid)
# Function to plot missing values in a data frame, or to plot missing values
# in a data frame representing panel data (i.e. against 2 dimensions)
# Leave dim1 and dim2 NULL to plot missing values in data frame.
if (xor(is.null(dim1), is.null(dim2))) stop("Specify either both or no dims.")
# Set colors for possible data values
myCol <- c("#DCDCDC", "#32CD32", "#CD4F39")
@andybega
andybega / subset.sql
Created June 4, 2013 18:24
My first real SQL query in PostGIS. It subsets the geo-referenced ethnic groups (GREG) data to only groups that are present in Afghanistan (instead of including hundreds of groups for the entire world).
CREATE TABLE afg_greg AS
SELECT *
FROM greg
WHERE ( ST_contains((SELECT the_geom FROM afg_adm0), geom) OR
ST_overlaps((SELECT the_geom FROM afg_adm0), geom) );
@andybega
andybega / count-model-bug.R
Created August 16, 2013 18:01
Protest models from Mike, produces count plots that look really weird.
#
#
# Original code from Mike:
#
#
# mdw attempts at modeling Protests & Demonstrations
# August 13, 2013
library(wicews)
@andybega
andybega / egy.points.R
Created September 25, 2013 20:06
Point data for Egypt ggmap example
egy.points <-
structure(list(Latitude = c(30.0626, 30.0626, 30.0626, 25, 30.5,
30.1219, 29.0744, 31.4044, 29.9737, 30.0626, 31.4, 31.2, 29,
30.6, 28.1, 26.1642, 24.0934, 25.6989, 27, 30.0453, 30.6043,
30.6803, 30.4658, 31.2565, 30.1219, 27.7667, 31.0364, 30.0273,
31.4044, 29.7, 27.2, 26.5, 29.5, 26, 28.501, 27.9333, 30.5, 27.2574,
27, 30.2986, 30.5333, 29.7, 27.2167, 30.7885, 25.0757, 30.9,
30.7, 30.9, 24.0934, 29.9978, 31.1, 30.15, 22.3457, 25.9141,
30.6, 31.35, 26.1642, 28.3121, 26.55, 30.9251, 31.0392, 30.5,
27, 29.3, 26.0495, 31, 26.0549, 26.1247, 30.3, 26.7292, 30.0626,
@andybega
andybega / egy-ggmap-example.R
Created September 25, 2013 21:21
Quick map in R using ggmap
library(ggmap)
library(source.gist)
# Get data frame with coordinates for points
source.gist("6705264")
head(egy.points)
# Get background map
egy.map <- get_map(location=c(lon=30, lat=26), zoom=6, maptype="terrain", filename="~/Desktop/ggmapTemp")
p <- ggmap(egy.map) +
@andybega
andybega / turkey-prot-points.R
Last active December 24, 2015 07:39
GDELT protest data for Turkey from January 2011 to September 2013.
turkey.prot <-
structure(list(source = c("icews", "icews", "icews", "icews",
"icews", "icews", "icews", "icews", "icews", "icews", "icews",
"icews", "icews", "icews", "icews", "icews", "icews", "icews",
"icews", "icews", "icews", "icews", "icews", "icews", "icews",
"icews", "icews", "icews", "icews", "icews", "icews", "icews",
"icews", "icews", "icews", "icews", "icews", "icews", "icews",
"icews", "icews", "icews", "icews", "icews", "icews", "icews",
"icews", "icews", "icews", "icews", "icews", "icews", "icews",
"icews", "icews", "icews", "icews", "icews", "icews", "icews",
@andybega
andybega / turkey-protests.R
Last active December 24, 2015 07:48
Map of protests in Turkey from January 2011 to September 2013, from GDELT event data.
library(ggmap)
library(source.gist)
# Get data frame with coordinates for points
source.gist("6765348")
head(turkey.prot)
# Get background map
tr.map <- get_map(location=c(lon=34, lat=37), zoom=5, maptype="terrain", filename="~/Desktop/ggmapTemp")
p <- ggmap(tr.map) +