Skip to content

Instantly share code, notes, and snippets.

Avatar

Andreas Beger andybega

View GitHub Profile
@andybega
andybega / add-countries-to-cshapes
Last active Nov 24, 2020
Example of how to add countries (polygons) to cshapes
View add-countries-to-cshapes
# Example of how to add missing countries/territories to cshapes using another source for
# the map data/polygon
#
library(cshapes)
library(rgdal) # need this for projection transform (spTransform)
# this returns all polygons, with start and end dates for indepdence
# we'll have to manually subset for dates below
all <- cshp()
@andybega
andybega / runme.R
Last active Oct 2, 2019
Scrape the Wikipedia terror incidents pages/lists
View runme.R
#
# copy and run this code in R to setup and source `wikipedia-terrorism.R` from this gist
#
packs <- c("dplyr", "rvest", "httr", "XML", "stringr", "purrr", "xml2", "tibble", "devtools")
need <- packs[!packs %in% rownames(installed.packages())]
install.packages(need)
devtools::source_gist("https://gist.github.com/andybega/0f16b6a0de0185c157e2a280b5e05074",
filename = "wikipedia-terrorism.R")
@andybega
andybega / us48-spatial-lag.R
Last active Aug 7, 2019
Fraction of neighboring US states with a policy
View us48-spatial-lag.R
library("splm")
data("usaww")
?usaww
# Only Alabama has this policy
policy_x <- c(1, rep(0, 47))
names(policy_x) <- rownames(usaww)
head(policy_x)
@andybega
andybega / line-segment-colors.R
Created Apr 1, 2019
Line segment colors in base plot
View line-segment-colors.R
plot(c(1, 2), c(1, 1), col = c("blue", "red"), pch= 19, cex=4)
lines(c(1, 2), c(1, 1), col = c("blue", "red"), lwd=3)
View plot-yemen.R
library(cshapes)
library(sf)
states <- cshp(as.Date("2016-01-01"), useGW = TRUE)
states <- st_as_sf(states)
yemen <- states[states$CNTRY_NAME=="Yemen", ]
plot(yemen[, 1])
@andybega
andybega / chart-model.R
Created Jan 11, 2019
RCT-A subset machine/human relative performance by question group
View chart-model.R
library("tidyverse")
arima_qs <- readr::read_csv("/path/to/forecast_rct_a_subset.csv",
col_types = cols(user_id = col_integer())) %>%
mutate(Forecaster = factor(Forecaster))
# Add # of questions to each Data source label
arima_qs <- arima_qs %>%
group_by(Data_source) %>%
mutate(by_Data_source_n_ifps = length(unique(ifp_id))) %>%
@andybega
andybega / example.R
Created Jan 11, 2019
Reprex for win-builder R devel ggplot2-related error
View example.R
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()
View dot-density-map.R
# 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 / predict-ebma.r
Created Mar 18, 2015
Function to calculate EBMA ensemble from stream of input predictions and EBMA fitted model
View predict-ebma.r
# 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 / archigos-polity.r
Created Dec 15, 2014
Descriptive look at Archigos leader entry/exit and tenure length for different regime types, using the Polity IV data and scheme for the latter.
View archigos-polity.r
# Archigos transitions and tenure by regime type
#
# Andreas Beger
# 15 December 2014
setwd("~/Desktop/archigos-polity")
library(foreign)
library(gdata)