Twitter

Friday, November 02, 2007

Fast permuting r by c two-way tables

Today, I need to permute many two-way tables of the same dimension (say 2 by 3) to carry out a test of independence. My data looks like a big matrix with each row corresponding to a two-way table (arranged by row). The permutation is just to draw a random 2 by 3 table given independence between the two dimensions, conditioning on the observed marginal distributions. I didn't find (or didn't have time to find) a function to do that in R. Therefore I wrote the following codes:

f.perm.tab<-function(x){

x.r<-rep(1:nrow(x), times=rowSums(x))

x.c<-rep(1:ncol(x), times=colSums(x))

temp.mat<-expand.grid(1:nrow(x), 1:ncol(x))

return(table(c(x.r, temp.mat[,1]), c(sample(x.c),temp.mat[,2])) -matrix(1, nrow(x), ncol(x)))

}


I also made a function to do the permutation for each line of my huge
matrix and used the apply() function to speed up things. It works pretty well
for me.

3 comments:

Unknown said...

I think the following code work as well, and do not understand why you need temp.mat then substrated an identity matrix from table.

f.perm.tab<-function(x){

x.r<-rep(1:nrow(x),times=rowSums(x))
x.c<-rep(1:ncol(x),times=colSums(x))

return(table(c(x.r), c(sample(x.c))))

}

Unknown said...

great idea. never thought about that
beltoprent

Unknown said...
This comment has been removed by the author.