Skip to content

Instantly share code, notes, and snippets.

Thomas Lumley tslumley

Block or report user

Report or block tslumley

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@tslumley
tslumley / multiresponse.Rmd
Created Jul 23, 2019
Some ideas towards a multiple-response class
View multiresponse.Rmd
---
title: "Multiple response"
author: "Thomas Lumley"
date: "7/23/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
@tslumley
tslumley / svyivreg.R
Created Jul 15, 2019
survey-weighted two-stage least squares for instrumental variables
View svyivreg.R
library(AER)
svyivreg<-function(formula, design, ...) UseMethod("svyivreg",design)
svyivreg.survey.design<-function(formula, design){
.data<-model.frame(design)
.data$.weights<-weights(design,"sampling")
model<- ivreg(formula, data=.data, weights=.weights)
@tslumley
tslumley / hoffci.R
Last active Jun 7, 2019
Perverting Burris-Hoff confidence intervals for fun and profit.
View hoffci.R
## https://doi.org/10.1093/jssam/smz010
make.s<-function(mu, sigma,tau2,alpha){
g<-make.g(alpha)
invg<-make.ginv(alpha)
function(theta) invg(2*sigma*(theta-mu)/tau2)
}
make.g<-function(alpha){
@tslumley
tslumley / bus-bot-school.R
Created May 3, 2019
Real-time board for Auckland school buses
View bus-bot-school.R
## startup
library(jsonlite)
library(httr)
library(knitr)
library(kableExtra)
options(stringsAsFactors=FALSE)
## These two files are part of the static GTFS information
routes<-read.csv("./routes.txt")
routes$route_id<-substr(as.character(routes$route_id),1,5) ## remove versioning info
@tslumley
tslumley / net-real-estate.R
Created May 2, 2019
Gross and net transfers of NZ real estate.
View net-real-estate.R
## from: https://www.stats.govt.nz/information-releases/property-transfer-statistics-march-2019-quarter
re<-read.table(text=
"year quarter citbuy citsell resbuy ressell fornbuy fornsell corpbuy corpsell knowbuy knowsell unkbuy unksell total
2017 Mar 23592 23490 2130 1101 621 375 3060 4407 29406 29370 4287 4320 33690
2017 Jun 30414 30678 3063 1512 930 486 4188 5946 38595 38625 450 426 39048
2017 Sep 27123 27021 2703 1458 783 441 3618 5316 34230 34239 126 117 34356
2017 Dec 28632 28125 2862 1491 1038 468 3663 6111 36195 36192 84 87 36279
2018 Mar 25881 25947 2625 1401 1083 501 3255 4998 32841 32847 39 36 32880
2018 Jun 31044 31173 3171 1656 1116 492 4281 6285 39606 39606 21 21 39627
2018 Sep 28284 27684 2982 1557 717 378 3630 5997 35613 35613 21 21 35634
@tslumley
tslumley / withPV.R
Created Apr 21, 2019
Plausible values in surveys
View withPV.R
withPV<-function(mapping, design, action, ...) UseMethod("withPV",design)
withPV.survey.design<-function(mapping, design, action,...){
if(inherits(mapping,"formula")) mapping<-list(mapping)
if (!is.list(mapping))
stop("'mapping' must be a list of formulas")
if (!all(sapply(mapping, length)==3))
@tslumley
tslumley / app.R
Created Jan 4, 2019
Shiny app for exploring posterior distributions given surprising data
View app.R
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 Sep 23, 2018
Read GloVe word embeddings
View read-glove.R
# 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 Aug 13, 2018
Slowly accumulating map of Wellington buses (rate-limited to 1 route per 40sec)
View wellybus.R
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(
View index.html
<!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'>
You can’t perform that action at this time.