Skip to content
Prev 377327 / 398502 Next

list contingency tables

Hi all,
  I am trying to list all the 4 by 2 tables with some fixed margins.
  For example, consider 4 by 2 tables with row margins 1,2,2,1 and
column margins 3,3. I was able to do it using the code below. However,
as seen below, I had to first count the total number of tables with
the specific row margins and column margins in order to create space
to store the tables.
Is there a way to skip the step of counting the number of tables?
  Also, wanted to avoid for loops as much as possible since it can be
extremely slow and inefficient.
   Thanks so much in advance for you insight and help.
       Hanna
+     for (j in 1:ncol(m2)){
+         for (k in 1:ncol(m3)){
+             M <- t(cbind(m1[,i], m2[,j], m3[,k]))
+             M1 <- rbind(M, cmg-apply(M,2,sum))
+             num <- num+(sum(M1[4,] < 0) == 0)
+         }}}
+     for (j in 1:ncol(m2)){
+         for (k in 1:ncol(m3)){
+             M <- t(cbind(m1[,i], m2[,j], m3[,k]))
+             M1 <- rbind(M,cmg-apply(M,2,sum))
+             if (sum(M1[4,] < 0) == 0) {
+                 num <- num+1
+                C[,,num] <- M1
+             }
+         }}}
, , 1

     [,1] [,2]
[1,]    0    1
[2,]    0    2
[3,]    2    0
[4,]    1    0

, , 2

     [,1] [,2]
[1,]    0    1
[2,]    1    1
[3,]    1    1
[4,]    1    0

, , 3

     [,1] [,2]
[1,]    0    1
[2,]    1    1
[3,]    2    0
[4,]    0    1

, , 4

     [,1] [,2]
[1,]    0    1
[2,]    2    0
[3,]    0    2
[4,]    1    0

, , 5

     [,1] [,2]
[1,]    0    1
[2,]    2    0
[3,]    1    1
[4,]    0    1

, , 6

     [,1] [,2]
[1,]    1    0
[2,]    0    2
[3,]    1    1
[4,]    1    0

, , 7

     [,1] [,2]
[1,]    1    0
[2,]    0    2
[3,]    2    0
[4,]    0    1

, , 8

     [,1] [,2]
[1,]    1    0
[2,]    1    1
[3,]    0    2
[4,]    1    0

, , 9

     [,1] [,2]
[1,]    1    0
[2,]    1    1
[3,]    1    1
[4,]    0    1

, , 10

     [,1] [,2]
[1,]    1    0
[2,]    2    0
[3,]    0    2
[4,]    0    1