Skip to content

Instantly share code, notes, and snippets.

View aaronschiff's full-sized avatar

Aaron Schiff aaronschiff

View GitHub Profile
@aaronschiff
aaronschiff / Makefile
Created February 11, 2016 20:45
Transformations on LINZ shapefiles for use in Mapbox
DATA_DIR = /Users/aaron/Google\ Drive/Data/Geographic/NZ/Topographic
nzlakes: nzlakes50.shp nzlakes250.shp nzlakes500.shp
nzlakes50.shp: $(DATA_DIR)/50k/nz-lake-polygons-topo-150k/nz-lake-polygons-topo-150k.shp
rm -f temp.*
ogr2ogr temp.shp $(DATA_DIR)/50k/nz-lake-polygons-topo-150k/nz-lake-polygons-topo-150k.shp -nln temp
ogr2ogr nzlakes50.shp temp.shp -sql "SELECT name, OGR_GEOM_AREA/1000 as area FROM temp" -t_srs EPSG:4326
rm temp.*
@aaronschiff
aaronschiff / RUP_overlays_map.R
Created August 7, 2016 23:32
Maps of recommended Auckland Unitary Plan overlays
# Maps of overlays in the IHP recommended Auckland Unitary Plan
# Setup
rm(list = ls())
library(maptools)
crs_nztm = CRS("+proj=tmerc +lat_0=0 +lon_0=173 +k=0.9996 +x_0=1600000 +y_0=10000000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs")
# Map config
line_width = 1
line_colour = rgb(100/255, 100/255, 100/255)
@aaronschiff
aaronschiff / RUP_spiderweb.R
Created August 10, 2016 19:24
Make a map of all the geodata in Auckland's recommended Unitary Plan
# Fun with Unitary Plan data -- all the things!
# Setup
rm(list = ls())
library(maptools)
crs_nztm = CRS("+proj=tmerc +lat_0=0 +lon_0=173 +k=0.9996 +x_0=1600000 +y_0=10000000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs")
# Map config
line_width = 1
point_size = 0.25
@aaronschiff
aaronschiff / random-walks.R
Created June 12, 2017 01:00
Regress pairs of random walks and look at the p-values
# Perform simple regressions on random walks and analyse the results
# Setup
rm(list = ls())
library(broom)
reps = 1000
walk_length = 100
# Generate a random walk of length n
generateRandomWalk = function(n) {
@aaronschiff
aaronschiff / random-walks-2.R
Last active June 12, 2017 01:19
Prediction and spurious regression example
# Regress a random walk on a bunch of others and choose the model with the highest R-squared
# Analyse out-of-sample forecasting performance of that model
# Setup
rm(list = ls())
walk_length = 100
reps = 100
# Generate a random walk of length n
generateRandomWalk = function(n) {
@aaronschiff
aaronschiff / read-infoshare.R
Last active July 12, 2017 05:54
R function to read CSV files exported from Statistics New Zealand's Infoshare system
# Helper function to read csv files exported from Infoshare
# Categories should be a list of names of the categorical variables to be created
read_infoshare <- function(filename, categories) {
num_categories <- length(categories)
dat <- read_csv(filename)
names(dat)[1] <- "date"
# Drop rows where all columns except the first column are NA, to remove junk at the bottom of the file
junk_rows <- apply(dat[, -1], 1, function(x) prod(is.na(x))) == 1
dat <- dat[!junk_rows, ]
@aaronschiff
aaronschiff / analysis-mbie-mfe-data.R
Created August 10, 2017 03:46
Make a simple chart of property prices from MBIE/MfE urban development capacity data
# -----------------------------------------------------------------------------
# Setup
library(magrittr)
library(lubridate)
library(tidyverse)
library(ggplot2)
library(scales)
source("clean-ggplot-theme.R")
# -----------------------------------------------------------------------------
@aaronschiff
aaronschiff / jaggy.R
Created August 15, 2017 01:20
Jaggy label in ggplot
library(ggplot2)
dat <- data.frame(x = 1:10, y = runif(10))
chart <- ggplot(dat) +
geom_bar(aes(x = x, y = y), stat = "identity") +
geom_text(aes(x = 2, y = 1.5, label = "This text is jaggy"), size = 6)
print(chart)
@aaronschiff
aaronschiff / map-chc-property.R
Created August 24, 2017 04:38
Simple graphs and maps of Christchurch property sales data
# Map Christchurch property data
# -----------------------------------------------------------------------------
# Setup
rm(list = ls())
library(magrittr)
library(lubridate)
library(tidyverse)
library(scales)
library(sf)
@aaronschiff
aaronschiff / read-chc-data.R
Created August 24, 2017 04:39
Download all data from Christchurch City Council's property API
# -----------------------------------------------------------------------------
# Setup
rm(list = ls())
library(magrittr)
library(jsonlite)
library(httr)
library(lubridate)
library(tidyverse)
library(scales)
source("clean-ggplot-theme.R")