Tables

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
## Warning: package 'rje' was built under R version 4.4.3
tab <- rprobMat(10, 2, 3)
tab
## Group of 10 numeric tables of dimension 2x2x2
## First entry:
## , , 1
## 
##           [,1]       [,2]
## [1,] 0.1098242 0.05435763
## [2,] 0.1124133 0.32103032
## 
## , , 2
## 
##           [,1]       [,2]
## [1,] 0.1250595 0.06001563
## [2,] 0.1062683 0.11103104

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.1098242 0.05435763
## [2,] 0.1124133 0.32103032
## 
## , , 2
## 
##           [,1]       [,2]
## [1,] 0.1250595 0.06001563
## [2,] 0.1062683 0.11103104

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.1098242 0.1250595

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.1098242
## 
## , , 2
## 
##           [,1]
## [1,] 0.1250595

We can also combine tables using tbind, provided that their tdim attributes match.

tbind(tab, tab)
## Group of 20 numeric tables of dimension 2x2x2
## First entry:
## , , 1
## 
##           [,1]       [,2]
## [1,] 0.1098242 0.05435763
## [2,] 0.1124133 0.32103032
## 
## , , 2
## 
##           [,1]       [,2]
## [1,] 0.1250595 0.06001563
## [2,] 0.1062683 0.11103104

Basic numerical 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.2222376 0.2313278
## [2,] 0.3753879 0.1710467
conditional(tab, 2, 1)  # second dimension conditional on first
## Group of 10 numeric tables of dimension 2x2
## First entry:
##           [,1]      [,2]
## [1,] 0.6725241 0.3360491
## [2,] 0.3274759 0.6639509

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.2222376 0.3753879
## [2,] 0.2222376 0.3753879
## 
## , , 2
## 
##           [,1]      [,2]
## [1,] 0.2313278 0.1710467
## [2,] 0.2313278 0.1710467
conditional2(tab, 2, 1)  
## Group of 10 numeric tables of dimension 2x2x2
## First entry:
## , , 1
## 
##           [,1]      [,2]
## [1,] 0.6725241 0.3274759
## [2,] 0.3360491 0.6639509
## 
## , , 2
## 
##           [,1]      [,2]
## [1,] 0.6725241 0.3274759
## [2,] 0.3360491 0.6639509

Functions of Distributions

Some built-in functions are available. For example:

tab2 <- rprobMat(10,2,3)
kl(tab, tab2)   # pairwise Kullback-Leibler divergence
##  [1] 0.3107779 2.0265353 1.3130295 0.5780725 0.9955802 1.2118646 1.1205125
##  [8] 0.5658850 1.0351757 0.4008486
                       # mutual information between
mutualInf(tab, 2, 3)   # second and third dimensions
##  [1] 0.0200581111 0.0307571298 0.0118994865 0.0060247267 0.0545747031
##  [6] 0.0688975518 0.0075588513 0.1059115952 0.0329237468 0.0001860831
mutualInf(tab, 2, 3, cond=1)   # conditional mutual information
##  [1] 0.01676985 0.03988476 0.12455547 0.14770752 0.05180658 0.05374509
##  [7] 0.02581832 0.19045396 0.06791479 0.06303117