# rcontFS

rcontFS generates a random two-way table with given marginal totals

## Syntax

• out=rcontFS( I, J, nrowt, ncolt)example
• out=rcontFS( I, J, nrowt, ncolt,Name,Value)example

## Description

 out =rcontFS(I, J, nrowt, ncolt) rcontFS with all the default options.

 out =rcontFS(I, J, nrowt, ncolt, Name, Value) rcontFS with option nocheck set to true.

## Examples

expand all

### rcontFS with all the default options.

Generate a random contingency table with 2 rows and 3 columns.

nrow=2;
ncol=3;
% Fix the marginals of the two rows
nrowt=[20; 30];
% Fix the marginals of the three columns
ncolt=[25 15 10];
% Generate the contingency table
out=rcontFS(nrow,ncol,nrowt,ncolt)
% testing row and column marginals
assert(isequal(nrowt, sum(out.m144,2)))
assert(isequal(ncolt, sum(out.m144,1)))
assert(isequal(nrowt, sum(out.m159,2)))
assert(isequal(ncolt, sum(out.m159,1)))
% Random contingency table based on algorthm AS144
disp(out.m144)
% Random contingency table based on algorthm AS159
disp(out.m159)
out =

struct with fields:

m144: [2×3 double]
m159: [2×3 double]

11     6     3
14     9     7

10     8     2
15     7     8



### rcontFS with option nocheck set to true.

Generate a random contingency table with 2 rows and 3 columns and do not check input arguments.

nrow=2;
ncol=3;
% Fix the marginals of the two rows
nrowt=[20 30];
% Fix the marginals of the three columns
ncolt=[25 15 10];
% Generate the contingency table and avoid checks
out=rcontFS(nrow,ncol,nrowt,ncolt,'nocheck',true)
% Random contingency table based on algorthm AS144
disp(out.m144)
% Random contingency table based on algorthm AS159
disp(out.m159)

## Related Examples

expand all

### An exmple with the same number of units in each row and in each column.

n=10000;
% Contingency table with nrow rows and ncol columns
nrow=40;
ncol=5;
% n/nrow units in each row
nrowt=(n/nrow)*ones(nrow,1);
% n/ncol units in each column
ncolt=(n/ncol)*ones(ncol,1);
% Generate the contingency table
out=rcontFS(nrow,ncol,nrowt,ncolt);
% testing row and column marginals
assert(isequal(nrowt, sum(out.m144,2)))
assert(isequal(ncolt, sum(out.m144,1)'))
assert(isequal(nrowt, sum(out.m159,2)))
assert(isequal(ncolt, sum(out.m159,1)'))

## Input Arguments

### I — Number of rows of the simulated contingency table. Scalar.

Scalar which contains the requested number of rows the output matrix must have.

Data Types: single| double

### J — Number of columns of the simulated contingency table. Scalar.

Scalar which contains the requested number of columns the output matrix must have.

Data Types: single| double

### nrowt — Row totals of the simulated contingency table. Vector.

Vector of length I containing the requested row totals the output matrix must have. First element refers to the total number of elements in the first row, ..., $I$-th element refers to the total number of elements in the $I$-th row. In other words, $nrowt=(n_{1.}, n_{2.}, \ldots, n_{I.})$.

Data Types: single| double

### ncolt — Column totals of the simulated contingency table. Vector.

Vector of length J containing the requested column totals the output matrix must have. First element refers to the total number of elements in the first column, ..., $J$-th element refers to the total number of elements in the $J$-th column.

In other words, $ncolt=(n_{.1}, n_{.2}, \ldots, n_{.J})$.

Data Types: single| double

### Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as  Name1,Value1,...,NameN,ValueN.

Example:  'nocheck',true , 'algorithm','144' 

### nocheck —Checks on input arguments.boolean.

If nocheck is false (default) program checks whether 1) nrow and ncol are greater than 1;

2) min(nrowt) and min(ncolt) are strictly greater than 0;

3) length(nrowt)=I, and length(ncol)=J;

4) the sum of the elements of vector nrowt is equal to the sum of the elements of vector ncolt, (in other words, we check whether the row and column sum vectors have the same grand total).

To avoid all the above checks set nocheck to true.

Example:  'nocheck',true 

Data Types: boolean

### algorithm —Algorithm to use to create the random contingency table.character.

Character which specifies which algorithm must be used to create the contingency table.

Possible values for algorithm are: '144' in this case the algorithm due to Boyett (1979) is used and the output structure will contain field out.matrix144.

'159' in this case the algorithm due to Patefield (1979) is used and the output structure will contain field out.matrix159.

'all' in this case the algorithms due to Boyett (1979) and to Patefield (1981) are both used. The output structure out will contain both out.matrix144 and out.matrix159.

Example:  'algorithm','144' 

Data Types: character

## Output Arguments

### out — description Structure

A structure containing the following fields

Value Description
m144

$I$-by-$J$-table containing contingency table generated using algorithm '144' due to Boyett.

The $(i,j)$-th element is equal to $n_{ij}$, $i=1, 2, \ldots, I$ and $j=1, 2, \ldots, J$.

This field is present only if input option algorithm is 'all' or '144'.

Note that sum(out.144,1)=nrowt and that sum(out.144,1)=ncolt.

m159

$I$-by-$J$-table containing contingency table generated using algorithm '159' due to Patefield.

The $(i,j)$-th element is equal to $n_{ij}$, $i=1, 2, \ldots, I$ and $j=1, 2, \ldots, J$.

This field is present only if input option algorithm is 'all' or '159'.

Note that sum(out.159,1)=nrowt and that sum(out.159,1)=ncolt.

## References

Boyett, J. (1979), Algorithm AS 144: Random R x C Tables with Given Row and Column Totals, "Applied Statistics", Vol. 28, pp. 329-332.

Patefield, M. (1981), Algorithm AS 159: An Efficient Method of Generating RXC Tables with Given Row and Column Totals, "Applied Statistics", Vol. 30, pp. 91-97.

## Acknowledgements

This routine is based on the codes written in FORTRAN77 version by Michael Patefield and James Boyett and on the Matlab version by John Burkardt.