Author: Robin J. Evans
Date: 2022-11-18

# Tables

#### Robin J. Evans

#### 2022-11-18

This package contains methods for storing and manipulating collections of contingency tables, and for easily vectorizing functions which apply to a contingency table.

The basis of this is the class of object `tables`, which contains a collection of numerical tables all of the same dimension. Let’s create a collection of 10 contingency tables (in this case probability tables), each of dimension 2x2x2.

    library(contingency)

    ## Loading required package: rje

    tab <- rprobMat(10, 2, 3)
    tab

    ## Group of 10 numeric tables of dimension 2x2x2
    ## First entry:
    ## , , 1
    ##
    ##            [,1]       [,2]
    ## [1,] 0.17618992 0.02182497
    ## [2,] 0.04537741 0.36982432
    ##
    ## , , 2
    ##
    ##            [,1]        [,2]
    ## [1,] 0.06995074 0.100333207
    ## [2,] 0.21492410 0.001575333

The print method shows the first table in the list.
The tables are stored as a matrix as can be seen by using the `dim()` function. Accessing particular rows of this matrix return the appropriate tables:

    tab[c(1,4,5),]

    ## Group of 3 numeric tables of dimension 2x2x2
    ## First entry:
    ## , , 1
    ##
    ##            [,1]       [,2]
    ## [1,] 0.17618992 0.02182497
    ## [2,] 0.04537741 0.36982432
    ##
    ## , , 2
    ##
    ##            [,1]        [,2]
    ## [1,] 0.06995074 0.100333207
    ## [2,] 0.21492410 0.001575333

However we can also specific elements of the tables using their co-ordinates, and (optionally) leaving the first entry blank:

    tab[,1,1,]

    ## Group of 10 numeric tables of dimension 2
    ## First entry:
    ## [1] 0.17618992 0.06995074

The `drop` argument can be set to `FALSE` if dimensions of length 1 should be retained:

    tab[,1,1,,drop=FALSE]

    ## Group of 10 numeric tables of dimension 1x1x2
    ## First entry:
    ## , , 1
    ##
    ##           [,1]
    ## [1,] 0.1761899
    ##
    ## , , 2
    ##
    ##            [,1]
    ## [1,] 0.06995074

## Basic Manipulations

Some basic operations are predefined, such as taking the margin of each table, or calculating a conditional distribution.

    margin(tab, 2:3)         # margin of second and third dimensions

    ## Group of 10 numeric tables of dimension 2x2
    ## First entry:
    ##           [,1]      [,2]
    ## [1,] 0.2215673 0.2848748
    ## [2,] 0.3916493 0.1019085

    conditional(tab, 2, 1)  # second dimension conditional on first

    ## Group of 10 numeric tables of dimension 2x2
    ## First entry:
    ##           [,1]      [,2]
    ## [1,] 0.6683178 0.4120643
    ## [2,] 0.3316822 0.5879357

These can also be applied on an ordinary numerical array with the expected effect. It can also be useful to calcuate conditional or other functions but retain the placement of values in the same point as the original table. For this purpose the functions `margin2()` and `conditional2()` are available.

                             # as above but sequence of cells
    margin2(tab, 2:3)        # in table is retained

    ## Group of 10 numeric tables of dimension 2x2x2
    ## First entry:
    ## , , 1
    ##
    ##           [,1]      [,2]
    ## [1,] 0.2215673 0.3916493
    ## [2,] 0.2215673 0.3916493
    ##
    ## , , 2
    ##
    ##           [,1]      [,2]
    ## [1,] 0.2848748 0.1019085
    ## [2,] 0.2848748 0.1019085

    conditional2(tab, 2, 1)

    ## Group of 10 numeric tables of dimension 2x2x2
    ## First entry:
    ## , , 1
    ##
    ##           [,1]      [,2]
    ## [1,] 0.6683178 0.3316822
    ## [2,] 0.4120643 0.5879357
    ##
    ## , , 2
    ##
    ##           [,1]      [,2]
    ## [1,] 0.6683178 0.3316822
    ## [2,] 0.4120643 0.5879357

## Functions of Distributions

Some built-in functions are available. For example:

    tab2 <- rprobMat(10,2,3)
    kl(tab, tab2)   # pairwise Kullback-Leibler divergence

    ##  [1] 1.0618544 0.3342060 0.9182598 1.2890683 0.8684732 0.3680129 0.8647746
    ##  [8] 1.5426562 0.6994782 0.2843636

                           # mutual information between
    mutualInf(tab, 2, 3)   # second and third dimensions

    ##  [1] 0.0688704660 0.0005936809 0.0103282607 0.0211609209 0.0112977252
    ##  [6] 0.0209483129 0.0120756909 0.0009720517 0.0135569948 0.0376785865

    mutualInf(tab, 2, 3, cond=1)   # conditional mutual information

    ##  [1] 0.325450964 0.014315978 0.008692991 0.112810197 0.021386995 0.141698816
    ##  [7] 0.059593111 0.034561512 0.020817647 0.085863122