IMSL C Stat Library
random_table_twoway
Generates a pseudorandom two-way table.
Synopsis
#include <imsls.h>
int *imsls_random_table_twoway(int nrow, int ncol, int nrtot[], int nctot[]…, 0)
Required Arguments
int nrow (Input)
Number of rows in the table.
int ncol (Input)
Number of columns in the table.
int nrtot[] (Input)
Array of length nrow containing the row totals.
int nctot[] (Input)
Array of length ncol containing the column totals. The elements of nrtot and nctot must be nonnegative and must sum to the same quantity.
Return Value
nrow by ncol random matrix with the given row and column totals. To release this space, use imsls_free.
Synopsis with Optional Arguments
#include <imsls.h>
int *imsls_random_table_twoway (int nrow, int ncol, int nrtot[], int nctot[],
IMSLS_RETURN_USER, int ir[],
0)
Optional Arguments
IMSLS_RETURN_USER, int ir[] (Output)
User-supplied array of size nrow by ncol containing the random matrix with the given row and column totals.
Description
Function imsls_random_table_twoway generates pseudorandom entries for a two-way contingency table with fixed row and column totals. The method depends on the size of the table and the total number of entries in the table. If the total number of entries is less than twice the product of the number of rows and columns, the method described by Boyette (1979) and by Agresti, Wackerly, and Boyette (1979) is used. In this method, a work vector is filled with row indices so that the number of times each index appears equals the given row total. This vector is then randomly permuted and used to increment the entries in each row so that the given row total is attained.
For tables with larger numbers of entries, the method of Patefield (1981) is used. This method can be considerably faster in these cases. The method depends on the conditional probability distribution of individual elements, given the entries in the previous rows. The probabilities for the individual elements are computed starting from their conditional means.
Example
In this example, imsls_random_table_twoway is used to generate a two by three table with row totals 3 and 5, and column totals 2, 4, and 2.
 
#include <imsls.h>
 
int main()
{
int *itable, nrow = 2, ncol = 3;
int nrtot[2] = {3, 5};
int nctot[3] = {2, 4, 2};
char *title = "A random contingency table with fixed"
" marginal totals";
 
imsls_random_seed_set(123457);
 
itable = imsls_random_table_twoway(nrow, ncol, nrtot, nctot,
0);
 
imsls_i_write_matrix(title, nrow, ncol, itable,
IMSLS_NO_ROW_LABELS,
IMSLS_NO_COL_LABELS,
0);
}
Output
 
A random contingency table with fixed marginal totals
0 2 1
2 2 1