RNTAB
Generates a pseudorandom two‑way table.
Required Arguments
NRTOT — Vector of length NROW containing the row totals. (Input)
NCTOT — Vector of length NCOL containing the column totals. (Input)
The elements of NRTOT and NCTOT must be nonnegative and must sum to the same quantity.
ITAB — NROW by NCOL random matrix with the given row and column totals. (Output)
Optional Arguments
IDO — Generator option. (Input)
Default: IDO = 0.
IDO |
Action |
0 |
This is the only invocation of RNTAB with these input specifications of the two‑way table. |
1 |
This is the first invocation, and additional calls to RNTAB will be made to generate random tables with the same specifications. |
2 |
This is an intermediate invocation of RNTAB. The work vectors have been set up in a previous call, but they are not to be released because additional calls will be made. |
3 |
This is the final invocation of RNTAB. The work vectors have been set up in a previous call and they are to be released. |
NROW — Number of rows in the table. (Input)
Default: NROW = size (ITAB,1).
NCOL — Number of columns in the table. (Input)
Default: NCOL = size (ITAB,2).
LDITAB — Leading dimension of ITAB exactly as specified in the dimension statement in the calling program. (Input)
Default: LDITAB = size (ITAB,1).
FORTRAN 90 Interface
Generic: CALL RNTAB (NRTOT, NCTOT, ITAB [, …])
Specific: The specific interface name is S_RNTAB.
FORTRAN 77 Interface
Single: CALL RNTAB (IDO, NROW, NCOL, NRTOT, NCTOT, ITAB, LDITAB)
Description
Routine RNTAB 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.
On the first call to RNTAB with a given set of row and column totals, certain checking is done, and the work vector is allocated and initialized. On the final call, the work vector is released. The argument IDO indicates the nature of the call. In a simulation study, RNTAB would typically be called first with IDO = 1, then would be called several times with IDO = 2, and then finally would be called with IDO = 3. If only one table is needed, IDO should be set to 0.
Comments
1. Let IRSUM = the sum of the elements in NRTOT. If IRSUM + 1 is less than 2 * NROW * NCOL, automatic workspace usage is IRSUM; otherwise, automatic workspace usage is 2 * IRSUM + 1 because a different algorithm is used. Workspace may be explicitly provided, if desired, by use of R2TAB. R2TAB allows selection of the algorithm to be used and it allows alternating calls for two different problems (see Comment 3). The reference is:
CALL R2TAB (IDO, NROW, NCOL, NRTOT, NCTOT, ITAB, LDITAB, IOPT, IRSUM, IWK, WK).
The additional arguments are as follows:
IOPT — Option indicator. (Input)
If IOPT = 1, Boyette’s method is used.
If IOPT = 2, Patefield’s method is used.
IRSUM — Sum of the elements in NRTOT. (Output)
IWK — Work vector of length equal to the sum of the elements in NRTOT.
WK — Work vector of length equal to the sum of the elements in NRTOT plus one, used only if IOPT = 2.
WARNING: R2TAB does no error checking.
2. Informational error
Type |
Code |
Description |
3 |
1 |
The values of NRTOT and/or of NCTOT are such that the probability distribution of tables is degenerate, that is, only one such table is possible. |
3. When more than one table with the same marginal totals is to be generated, IDO should be set to 1 for the first call, to 2 for all subsequent calls except the last one, and to 3 for the last call. If several tables of different sizes or with different marginal totals are to be generated, it is necessary to generate all of each type together because of the data stored in the work vectors. If the user provides work vectors for each type of table to be generated, R2TAB can be used to generate different types of tables alternatively.
4. The routine RNSET can be used to initialize the seed of the random number generator. The routine RNOPT can be used to select the form of the generator.
Example
In this example, RNTAB is used to generate a two by three table with row totals 3 and 5, and column totals 2, 4, and 2.
USE RNTAB_INT
USE UMACH_INT
USE RNSET_INT
IMPLICIT NONE
INTEGER I, ISEED, ITAB(2,3), IWK, J, NCTOT(3), NOUT,&
NRTOT(2), NROW, NCOL
!
CALL UMACH (2, NOUT)
NROW = 2
NCOL = 3
NRTOT(1) = 3
NRTOT(2) = 5
NCTOT(1) = 2
NCTOT(2) = 4
NCTOT(3) = 2
ISEED = 123457
CALL RNSET (ISEED)
CALL RNTAB (NRTOT, NCTOT, ITAB)
WRITE (NOUT,99999) ((ITAB(I,J),J=1,NCOL),I=1,NROW)
99999 FORMAT (' A random contingency table with fixed marginal totals:' &
, /, (5X,3I5))
END
Output
A random contingency table with fixed marginal totals:
0 2 1
2 2 1