[R] list contingency tables

David Winsemius dwin@emiu@ @ending from comc@@t@net
Fri Nov 9 21:28:58 CET 2018


Seems like you are trying to recreate the calculations needed to perform 
an exact test. Why not look at the code for that or even easier, just 
use the function.

-- 

David.

On 11/8/18 8:05 PM, li li wrote:
> 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
>
>
>
>> library(gtools)
>> A <- permutations(n=4,r=2,v=0:3, repeats.allowed=TRUE)
>> B <- apply(A, 1, sum)
>> rmg <- c(1,2,2,1)
>> cmg <- c(3,3)
>> m1 <- t(A[which(B==1),])
>> m2 <- t(A[which(B==2),])
>> m3 <- t(A[which(B==2),])
>>
>> ##count number of tables with row margins 1,2,2,1 and column margins 3,3.
>> num <- 0
>> for (i in 1:ncol(m1)){
> +     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)
> +         }}}
>>
>> #create space to store the tables
>> C <- array(NA, dim=c(4,2,num))
>>
>> # list all the tables with fixed margins
>> num <- 0
>> for (i in 1:ncol(m1)){
> +     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
> +             }
> +         }}}
>> C
> , , 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
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list