Skip to content

Instantly share code, notes, and snippets.

View tslumley's full-sized avatar
😐

Thomas Lumley tslumley

😐
View GitHub Profile
@tslumley
tslumley / Why-Random.R
Created October 29, 2015 00:31
Hextri examples
library(hextri)
library(survey)
data(api)
## all hexes same orientation: looks as if middle schools went up more
with(apipop,hextri(api99,api00,stype,c("orange","forestgreen","purple"),nbins=20,diffuse=FALSE,
xlab="1999 Academic Performance Index",ylab="2000 Academic Performance Index",style="size"))
legend("topleft",fill=c("orange","purple","forestgreen"),legend=c("Elementary","Middle","High"),bty="n")
## random orientation: less pretty, but you can see middle schools went up less
rock<-read.csv("~/Downloads/output.csv", as.is=TRUE, header=FALSE, col.names=c("datetime","song","group"))
rock$stime<-substr(rock$datetime,5,9)
rock$timeangle<-with(rock, as.numeric(substr(stime,1,2))*2*pi/24+as.numeric(substr(stime,4,5))*2*pi/24/60)
rock$ty<-cos(rock$timeangle)
rock$tx<-sin(rock$timeangle)
rock$txmean<-mean(rock$tx)
rock$tymean<-mean(rock$ty)
@tslumley
tslumley / hexmap.R
Created April 21, 2016 05:59
Hexmaps for Australian electorates, by state
hexmap<-function(x,y,id,...){
xcent<-x*sqrt(3) - (y%%2) *sqrt(3)/2
ycent<-y*1.5
plot(x,y,type="n",ylim=range(ycent)+c(-2,2),xlim=range(xcent)+c(-2,2))
for(i in 1:length(x)){
polygon(hex_x+xcent[i],hex_y+ycent[i])
text(xcent[i],ycent[i],id[i],cex=0.4)
}
@tslumley
tslumley / README.md
Last active June 13, 2016 08:35
'Reasonable cause' strip searches by Corrections Officers in NZ prisons
@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 / bentime.R
Created April 6, 2016 23:11
Times that don't turn into a pumpkin at midnight
"+.bentime"<-function(e1,e2){
e<-list(hour=e1$hour+e2$hour,min=e1$min+e2$min,sec=e1$sec+e2$sec)
sec_overflow<-e$sec>60L
e$sec<-e$sec %% 60L
e$min<-e$min+sec_overflow
min_overflow<-e$min>60L
e$hour<-e$hour+min_overflow
e$min<-e$min %% 60L
class(e)<-"bentime"
@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 / 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 / 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