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

example

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

example

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)
        % 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]
    
        12     5     3
        17     8     5
    
        11     5     4
        14    10     6
    
    

  • 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)
    

    Input Arguments

    expand all

    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

    expand all

    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.

    See Also

    |

    This page has been automatically generated by our routine publishFS