r2dtable
Random Two-way Tables with Given Marginals
Description
Generates random two-way matrices with given marginal totals.
Usage
r2dtable(n, r, c)
Arguments
n |
a non-negative numeric value that specifies the number of matrices to
generate. Numeric values are coerced to integer values.
|
r |
a non-negative vector, where the vector length is greater than
1, that specifies the row sum of each generated matrix. Values
in the vector are coerced to integer values and then r
is tested to make sure that the sum of r equals the sum of
c.
|
c |
a non-negative vector, where the vector length is greater than
1, that specifies the column sum of each generated matrix.
Values in the vector are coerced to integer values.
|
Value
returns a list object with length n, the components of the list are generated n random matrices. The elements of the matrices are integers.
Each generated random matrix, where
x is a generated random matrix, conforms to the following:
- The number of rows equals the length of the vector r:
nrow(x) == length(r)
- The number of columns equals the length of the vector c: ncol(x) == length(c)
- The sum of each row is equivalent to the input vector r: rowSums(x) == r
- The sum of each column is equivalent to the length of the vector c: colSums(x) == c
References
Patefield, W. M. 1981. Algorithm AS159. An efficient method of generating r x c tables with given row and column totals. Applied Statistics. Volume 30. 91-97.
Examples
r2dtable(5, c(4, 2, 3), c(5, 4))
r2dtable(5.6, c(4.4, 2, 3), c(5.2, 4.2)) # Same as above
r2dtable(5.6, c(5, 2, 3), c(1, 4)) # Error: arguments 'r' and 'c' must have the same sums
r2dtable(5.6, c(5), c(1, 4)) # Error: invalid argument 'r'