Skip to content

Instantly share code, notes, and snippets.

Avatar
😐

Thomas Lumley tslumley

😐
View GitHub Profile
@tslumley
tslumley / svygofchisq.R
Created Oct 21, 2020
Chisquared goodness of fit test
View svygofchisq.R
svygofchisq<-function(formula, p, design,...){
means<-svytotal(formula, design,...)
rval<-chisq.test(means,p=p)
nm<-names(coef(means))
ncat<-length(coef(means))
means<-svycontrast(means, list(N=rep(1,ncat)), add=TRUE)
pN<-split(cbind(p,0*diag(p)), paste0("p_",nm))
names(pN)<-paste0("p_",nm)
means<-svycontrast(means, pN,add=TRUE)
for(i in 1:length(nm)){
@tslumley
tslumley / proflike.R
Created Aug 5, 2020
Conditional profile likelihood for balanced accuracy
View proflike.R
llprofci<-function(tpos,fpos,tneg,fneg){
ndis<-tpos+fneg
nnot<-tneg+fpos
lljoint<-function(a,d){
k<-length(a)
@tslumley
tslumley / polls.Rmd
Created Jul 27, 2020
Rmarkdown for 'rogue polls' post
View polls.Rmd
---
title: "Rogue polls"
author: "Thomas Lumley"
date: "27/07/2020"
output: word_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
@tslumley
tslumley / svgam-example.do
Created Jul 20, 2020
Examples for svy VGAMs
View svgam-example.do
use zip-merged.dta
svyset SDMVPSU [pw=WTINT2YR], stra(SDMVSTRA)
svy: zip malepartners RIDAGEYR i.RIDRETH1 DMDEDUC, infl(RIDAGEYR i.RIDRETH1 DMDEDUC)
@tslumley
tslumley / proflike.R
Created Jul 7, 2020
Profile likelihood for 'balanced accuracy'
View proflike.R
nnot<-100
ndis<-50
tpr<-0.9
fpr<-0.5
rr<-replicate(100,{
tpos <-rbinom(1,ndis,tpr)
fpos<-rbinom(1,nnot,fpr)
@tslumley
tslumley / assoc.R
Last active Nov 10, 2019
Associative transformation of ggplot
View assoc.R
setClass("ggthing", representation(content="call"))
setClass("ggthings",representation(contents="list"))
bracket<-function(e1) NULL
setGeneric("bracket")
setMethod("bracket",c("ggthing"),
function(e1){
new("ggthings",contents=list(e1@content))
@tslumley
tslumley / cars.R
Last active Nov 6, 2019
Glyph maps for NZ District Health Boards
View cars.R
d<-strsplit("Northland
No Motor Vehicle 3792 4077
One Motor Vehicle 20229 22161
Two Motor Vehicles 19578 20652
Three or More Motor Vehicles 7398 7407
Waitemata
No Motor Vehicle 8871 8658
One Motor Vehicle 54753 57492
Two Motor Vehicles 66411 72768
Three or More Motor Vehicles 29757 33219
@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){