user4561672
user4561672

Reputation:

Creating a contingency table with fixed margins

I am trying to create a table with random entries from a central hypergeometric distribution where the column and row totals are fixed.

However I can get the column sums to be fixed and equal but not the row sums. I have read other answers but none seem to talk specifically about how to do it, my R knowledge is pretty basic and could do with some help or a point in the right direction.

To get the values from a central hypergeometric distribution I am using the BiasedUrn package.

For example:

N <- 50
rand <- 10
n1 <- 25
odds0 <- rep(1,K)  
m0 <- rep(N/K,K)
library(BiasedUrn)
i <- as.table(rMFNCHypergeo(nran=rand, n=n1, m=m0, odds=odds0))
addmargins(i) 
             A   B   C   D   E   F   G   H   I   J Sum
       A     5   3   5   7   5   5   6   6   5   5  52    
       B     8   7   4   5   5   6   3   4   5   4  51
       C     3   6   4   4   4   5   6   8   5   4  49
       D     4   4   6   3   6   4   5   3   3   5  43
       E     5   5   6   6   5   5   5   4   7   7  55
       Sum  25  25  25  25  25  25  25  25  25  25 250

Where I'm looking to keep all the column sums equal to 25, and all the row sums equal to another number which I can choose such as 50.

Upvotes: 2

Views: 1299

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226162

Are you looking for the r2dtable function from base R?

set.seed(101)
tt <- r2dtable(n=1,c=rep(25,6),r=rep(50,3))
addmargins(as.table(tt[[1]]))
##       A   B   C   D   E   F Sum
## A     7   9   7  11   9   7  50
## B    10   7  10   6   7  10  50
## C     8   9   8   8   9   8  50
## Sum  25  25  25  25  25  25 150

Upvotes: 4

Related Questions