Skip to content

Instantly share code, notes, and snippets.

@ProbablePattern
Created August 4, 2013 18:53
Show Gist options
  • Save ProbablePattern/6151434 to your computer and use it in GitHub Desktop.
Save ProbablePattern/6151434 to your computer and use it in GitHub Desktop.
require(foreach); require(xtable)
doublesort=function(data,x,y,q) {
breaks1=quantile(data[,x],probs=seq(0,1,1/q))
tiles=foreach(i=1:q,.combine=cbind) %dopar% {
Q=data[data[,x]>=breaks1[i] & data[,x]<breaks1[i+1],y]
breaks2=quantile(Q,probs=seq(0,1,1/q))
foreach(j=1:q,.combine=rbind) %dopar% mean(Q[Q>=breaks2[j] & Q<breaks2[j+1]])
}
colnames(tiles)=foreach(i=1:q) %do% paste(x,i)
row.names(tiles)=foreach(i=1:q) %do% paste(y,i)
tiles
}
xtable(doublesort(data,"CDS","lp",5),digits=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment