Skip to content

Instantly share code, notes, and snippets.

View tslumley's full-sized avatar
😐

Thomas Lumley tslumley

😐
View GitHub Profile
@tslumley
tslumley / app.R
Created January 4, 2019 01:13
Shiny app for exploring posterior distributions given surprising data
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Bayesian Surprise"),
# Sidebar with a slider input for number of bins
@tslumley
tslumley / read-glove.R
Last active September 23, 2018 06:38
Read GloVe word embeddings
# Based on https://gist.github.com/tjvananne/8b0e7df7dcad414e8e6d5bf3947439a9
# Rewritten to work chunk by chunk, so I can read the 42B file with only 8GB memory
# input .txt file, exports list of list of values and character vector of names (words)
proc_pretrained_vec <- function(filename, chunksize=1000, guess_size=100000) {
size<-guess_size
here<-0
# initialize space for values and the names of each word in vocab
vals <- vector(mode = "list", length(size))
@tslumley
tslumley / wellybus.R
Last active August 13, 2018 22:48
Slowly accumulating map of Wellington buses (rate-limited to 1 route per 40sec)
library(tidyverse)
library(leaflet)
library(jsonlite)
library(ratelimitr)
library(htmlwidgets)
library(htmltools)
allroutes<-read_csv("~/Downloads/WLG-google-transit/routes.txt") %>% filter(route_type==3)
download_route<-limit_rate(
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>Epiphany Clock</title>
<link rel='stylesheet prefetch' href='http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css'>
@tslumley
tslumley / README.md
Created December 3, 2017 20:58
Non-transitive dice

An example of the non-transitivity of the Wilcoxon test, using Efron's non-transitive dice.

refron() generates numbers from a set of four non-transitive dice, so that a beats b beats c beats d beats a two-thirds of the time, with optional Gaussian smoothing.

example.R does Wilcoxon rank-sum tests to show that a > b > c > d > a

@tslumley
tslumley / README.md
Last active October 13, 2017 21:24
Simple Bayesian model for road death trends

This is a model for NZ road deaths per billion km travelled, using data complied by Sam Warburton (@economissive on Twitter)

The model has an underlying smoothed trend which is a random walk with Gaussian increments, and a year-specific deviation that is also Gaussian. That is, it's a state space model with a random walk in the latent variable and a measurement model that's a Poisson-logNormal mixture to get overdispersed counts.

The variances of the random walk steps and the year-specific deviations have diffuse hyperpriors and the means are set up so that the mean of the exponential of the increment is 1 -- ie, so mu is genuinely the trend in means.

@tslumley
tslumley / ga.R
Created September 22, 2017 02:39
pal<-function(mat) rgb(mat[,1],mat[,2],mat[,3],maxColorValue=255)
showpal<-function(pal) {
n<-length(pal)
image(1:n, 1, as.matrix(1:n), col = pal,
xlab = deparse(substitute(pal)), ylab = "", xaxt = "n", yaxt = "n",
bty = "n")
}
@tslumley
tslumley / unit-examples.R
Created August 17, 2016 23:14
Arithmetic and logical operators for mks and furlong/firkin/fortnight units
gravity<-mks(c(3.7,8.9,9.8,3.7,23.1,9.0,8.7,11.0,0.6), m=1, kg=0, s=-2)
density<-mks(c(5427,5243,5515,3933,1326,687,1270,1638,2390), m=-3,kg=1,s=0)
speed.of.light<-fff(1.8026175e12,fur=1,fir=0,ftn=-1)
setMethod("mean","mks",function(x,...){
u<-getUnits(x)
mks(mean(getValues(x)),u[1],u[2],u[3])
})
@tslumley
tslumley / README.md
Last active June 13, 2016 08:35
'Reasonable cause' strip searches by Corrections Officers in NZ prisons