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
tab <- rprobMat(10, 2, 3)
tab
## Group of 10 numeric tables of dimension 2x2x2
## First entry:
## , , 1
## 
##            [,1]      [,2]
## [1,] 0.09880853 0.1640623
## [2,] 0.11319662 0.1268470
## 
## , , 2
## 
##            [,1]         [,2]
## [1,] 0.30284865 1.612444e-01
## [2,] 0.03295049 4.198198e-05

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.09880853 0.1640623
## [2,] 0.11319662 0.1268470
## 
## , , 2
## 
##            [,1]         [,2]
## [1,] 0.30284865 1.612444e-01
## [2,] 0.03295049 4.198198e-05

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.09880853 0.30284865

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.09880853
## 
## , , 2
## 
##           [,1]
## [1,] 0.3028486

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.09880853 0.1640623
## [2,] 0.11319662 0.1268470
## 
## , , 2
## 
##            [,1]         [,2]
## [1,] 0.30284865 1.612444e-01
## [2,] 0.03295049 4.198198e-05

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.2120051 0.3357991
## [2,] 0.2909093 0.1612864
conditional(tab, 2, 1)  # second dimension conditional on first
## Group of 10 numeric tables of dimension 2x2
## First entry:
##           [,1]      [,2]
## [1,] 0.5525132 0.5352667
## [2,] 0.4474868 0.4647333

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.2120051 0.2909093
## [2,] 0.2120051 0.2909093
## 
## , , 2
## 
##           [,1]      [,2]
## [1,] 0.3357991 0.1612864
## [2,] 0.3357991 0.1612864
conditional2(tab, 2, 1)  
## Group of 10 numeric tables of dimension 2x2x2
## First entry:
## , , 1
## 
##           [,1]      [,2]
## [1,] 0.5525132 0.4474868
## [2,] 0.5352667 0.4647333
## 
## , , 2
## 
##           [,1]      [,2]
## [1,] 0.5525132 0.4474868
## [2,] 0.5352667 0.4647333

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.4327429 0.1048570 0.5841707 0.5010903 0.6625511 0.4164222 1.1994320
##  [8] 0.9228334 0.6396058 0.6042378
                       # mutual information between
mutualInf(tab, 2, 3)   # second and third dimensions
##  [1] 0.032933700 0.004891692 0.145364013 0.014089971 0.022088809 0.033769663
##  [7] 0.098414919 0.003300811 0.001990354 0.003299019
mutualInf(tab, 2, 3, cond=1)   # conditional mutual information
##  [1] 0.04837453 0.04503526 0.30706803 0.04268345 0.07792133 0.07778950
##  [7] 0.09564989 0.22865214 0.05288251 0.01686910