Title: | Discrete Multivariate Probability Distributions |
---|---|
Description: | Provides an object class for dealing with many multivariate probability distributions at once, useful for simulation. |
Authors: | Robin Evans [aut, cre] |
Maintainer: | Robin Evans <[email protected]> |
License: | GPL-2 |
Version: | 0.1.0 |
Built: | 2025-03-05 04:47:53 UTC |
Source: | https://github.com/rje42/contingency |
Take subset of tables class.
## S3 method for class 'tables' x[i, j, ..., drop = TRUE, keep = FALSE]
## S3 method for class 'tables' x[i, j, ..., drop = TRUE, keep = FALSE]
x |
object of class |
i |
indices of which tables to retain |
j |
which rows of each table to retain (or if |
... |
additional indices up to the dimension of the table |
drop |
usual logical indicating whether to consolidate margins of the table (doesn't apply to |
keep |
if only one table is specified with |
There are two main ways to subset these tables. In both cases the first index
refers to the tables being selected; one of the methods is to additionally specify all the
indices corresponding to the tables, the other is to only specify a single entry.
For example, x[,1,2,2]
specifies the (1,2,2)th entry of each table; x[,7]
will
have the same effect for 2x2x2 tables.
If only one index is specified, then the function behaves just as ordinary subsetting on an array.
A tables object over the specific entries and values selected.
x <- rprobMat(n=10, rep(2,3)) x[1,] x[,1,1:2,1] x[,1,1:2,1,drop=FALSE]
x <- rprobMat(n=10, rep(2,3)) x[1,] x[,1,1:2,1] x[,1,1:2,1,drop=FALSE]
Method for permuting indices of tables object.
## S3 method for class 'tables' aperm(a, perm, ...)
## S3 method for class 'tables' aperm(a, perm, ...)
a |
object of class |
perm |
permutation of 1,...,k, where each table has k dimensions |
... |
other arguments to methods |
A permuted tables
object.
As tables
as_tables(x, tdim, conditional, rev = FALSE, ...)
as_tables(x, tdim, conditional, rev = FALSE, ...)
x |
array or matrix object |
tdim |
dimensions for each table |
conditional |
integer vector of indices that are conditional |
rev |
logical: should output move through each table fastest? |
... |
other arguments for methods |
Transforms a vector, matrix or array into a tables object with
the specified dimensions. Note that if rev = TRUE
then the final dimension
is (by default) used to index the tables objects, and otherwise the first
dimension..
A tables
object.
Convert tables into array
## S3 method for class 'tables' as.array(x, ...)
## S3 method for class 'tables' as.array(x, ...)
x |
|
... |
other arguments |
An array
object
Convert tables into matrix
## S3 method for class 'tables' as.matrix(x, ...)
## S3 method for class 'tables' as.matrix(x, ...)
x |
|
... |
other arguments |
A matrix
object
Apply a function to each contingency table in a tables
object.
capply(x, f, ...)
capply(x, f, ...)
x |
object of class |
f |
function to apply to each table |
... |
additional arguments to |
a vector, matrix or list of outputs from the function f
.
Gives a numerical check that a (conditional) independence holds in a probability distribution.
checkCI(x, A, B, C = integer(0), eps = .Machine$double.eps, ...) ## S3 method for class 'array' checkCI(x, A, B, C = integer(0), eps = .Machine$double.eps, ...) ## S3 method for class 'tables' checkCI(x, A, B, C = integer(0), eps = .Machine$double.eps, ...)
checkCI(x, A, B, C = integer(0), eps = .Machine$double.eps, ...) ## S3 method for class 'array' checkCI(x, A, B, C = integer(0), eps = .Machine$double.eps, ...) ## S3 method for class 'tables' checkCI(x, A, B, C = integer(0), eps = .Machine$double.eps, ...)
x |
an array or object of class |
A , B
|
the sets of variables whose independence is to be tested |
C |
conditioning set (possibly empty) |
eps |
tolerance parameter |
... |
other arguments to methods |
just tests to an appropriate numerical precision that a conditional independence
holds: this is not a statistical test for conditional independence.
If A
and B
overlap with C
then these vertices are ignored. If A
and B
intersect with one another (but not C
) then the solution is always
false.
A logical, or a vector of logicals of the same length as the number of tables provided, indicating whether the conditional independence seems to hold numerically.
checkCI(array)
: method for array
object
checkCI(tables)
: method for tables
object
Calculate entropy of discrete distribution
entropy(p, ...) ## Default S3 method: entropy(p, ...) ## S3 method for class 'array' entropy(p, margin, ...) ## S3 method for class 'tables' entropy(p, margin, ...)
entropy(p, ...) ## Default S3 method: entropy(p, ...) ## S3 method for class 'array' entropy(p, margin, ...) ## S3 method for class 'tables' entropy(p, margin, ...)
p |
non-negative numeric vector |
... |
other arguments to methods |
margin |
margin to consider |
A numeric value of the entopy, or vector of entropies.
entropy(default)
: Default method for vectors
entropy(array)
: Method for arrays
entropy(tables)
: Method for tables
object
Interaction information
interactionInf(p, ...) ## Default S3 method: interactionInf(p, ..., condition)
interactionInf(p, ...) ## Default S3 method: interactionInf(p, ..., condition)
p |
object to find interaction information for |
... |
other arguments to methods |
condition |
variables on which to condition |
Numeric value for interaction information, or a vector of interaction information values.
interactionInf(default)
: Default method for vectors
Checks if the rows of the underlying matrix represent each table or each entry in a table
is_rev(x)
is_rev(x)
x |
object to be tested |
Checks if "tables"
is in the list of class attributes.
is_tables(x)
is_tables(x)
x |
object to be tested |
Get the KL Divergence between two discrete distributions
kl(x, y, ...) ## Default S3 method: kl(x, y, ...) ## S3 method for class 'tables' kl(x, y, ...)
kl(x, y, ...) ## Default S3 method: kl(x, y, ...) ## S3 method for class 'tables' kl(x, y, ...)
x , y
|
vectors (of probabilities) |
... |
other arguments to methods |
a numberic value, vector or matrix of KL-divergences.
kl(default)
: Default method for vectors
kl(tables)
: Method for tables
object
Get margin of a table or tables
margin(x, ...) margin2(x, ...) conditional(x, ...) conditional2(x, ...) intervention(x, ...)
margin(x, ...) margin2(x, ...) conditional(x, ...) conditional2(x, ...) intervention(x, ...)
x |
a contingency table or |
... |
other arguments, not currently used |
margin2
keeps all dimensions, and
hence results will sum to the original sum, times the number of cells summed over.
an object of the same class as x
. The resulting
array, or collection of tables, will contain a marginal, conditional
or interventional distribution.
margin2()
: keep all dimensions
conditional()
: conditional distributions
conditional2()
: conditional distributions with all dimensions kept
intervention()
: interventional distributions
Get the marginal distributions
## S3 method for class 'tables' margin(x, margin = NULL, order = TRUE, ...) ## S3 method for class 'tables' conditional( x, variables, condition = NULL, condition.value = NULL, force = FALSE, undef = NaN, ... ) ## S3 method for class 'tables' conditional2(x, variables, condition = NULL, force = FALSE, undef = NaN, ...) ## S3 method for class 'tables' intervention(x, variables, condition, force = FALSE, ...)
## S3 method for class 'tables' margin(x, margin = NULL, order = TRUE, ...) ## S3 method for class 'tables' conditional( x, variables, condition = NULL, condition.value = NULL, force = FALSE, undef = NaN, ... ) ## S3 method for class 'tables' conditional2(x, variables, condition = NULL, force = FALSE, undef = NaN, ...) ## S3 method for class 'tables' intervention(x, variables, condition, force = FALSE, ...)
x |
an object of class |
margin |
integer vector giving margin to be calculated (1 for rows, etc.) |
order |
logical indicating whether resulting indices
should be in the same order as stated in |
... |
other arguments to function |
condition |
variables to condition upon |
condition.value |
(optionally) values to condition upon |
undef |
value to return for undefined cells |
Calculates marginal distributions for each entry in a probMat
.
An object of class tables
consisting of the required marginal
distribution.
conditional(tables)
: condition in distributions
conditional2(tables)
: condition and keep all variables
intervention(tables)
: intervene on variables in distributions
Get the multiinformation for a discrete distribution
multiInf(x, ...) ## Default S3 method: multiInf(x, margin = NULL, ...) ## S3 method for class 'tables' multiInf(x, margin = NULL, ...)
multiInf(x, ...) ## Default S3 method: multiInf(x, margin = NULL, ...) ## S3 method for class 'tables' multiInf(x, margin = NULL, ...)
x |
vectors (of probabilities) |
... |
other arguments to methods |
margin |
margin to find multiinformation for |
a numberic value, vector or matrix of required multiinformation.
multiInf(default)
: Default method for vectors and arrays
multiInf(tables)
: Method for tables
object
(Conditional) mutual information
mutualInf(p, m1, m2, condition, ...) ## Default S3 method: mutualInf(p, m1, m2, condition, ...) ## S3 method for class 'tables' mutualInf(p, m1, m2, condition, ...)
mutualInf(p, m1, m2, condition, ...) ## Default S3 method: mutualInf(p, m1, m2, condition, ...) ## S3 method for class 'tables' mutualInf(p, m1, m2, condition, ...)
p |
numeric array or |
m1 , m2
|
margins for mutual information |
condition |
conditional margin |
... |
other arguments to methods |
Numeric value for mutual information, or a vector of mutual information values.
mutualInf(default)
: Default method for vectors
mutualInf(tables)
: Method for tables
object
Number of tables
ntables(x)
ntables(x)
x |
an object of class |
Gives the number of tables in an object of class
tables
.
An integer.
k
Currently only works for binary dimensions.
perm_dim(x, k, perm, ...)
perm_dim(x, k, perm, ...)
x |
array or related object |
k |
index to permute |
perm |
permutation to perform |
... |
other arguments (not currently used) |
Permutes the levels of one variable
according to the permutation given in perm
.
Can be applied to matrices, arrays or tables.
A permuted array
or tables
object.
Print method for object of class tables
.
## S3 method for class 'tables' print(x, ...)
## S3 method for class 'tables' print(x, ...)
x |
object of class |
... |
arguments to pass to print method for an array |
The input provided (invisibly).
Turn distributions into tables
repTables(n, f, ..., rev = FALSE)
repTables(n, f, ..., rev = FALSE)
n |
number of distributions to generate |
f |
function that generates a probability distribution |
... |
arguments to |
rev |
logical: should output move through each table fastest? |
a tables object containing the outputs of f
Generates discrete probability distributions in a matrix.
rprobMat(n, dim, d, alpha = 1, rev = FALSE) rcondProbMat(n, dim, d, alpha = 1, condition, rev = FALSE)
rprobMat(n, dim, d, alpha = 1, rev = FALSE) rcondProbMat(n, dim, d, alpha = 1, condition, rev = FALSE)
n |
number of distributions |
dim |
dimension of contingency table for distributions |
d |
number of dimensions of table |
alpha |
parameter to use in dirichlet distribution |
condition |
which dimensions should be conditioned upon |
Returns an object of class tables
consisting of
discrete probability distributions. Each distribution is assumed to be a
contingency table of dimension dim
, and the probabilities
are generated using a Dirichlet distribution with parameters all
equal to alpha
.
A tables
object containing random distributions.
rcondProbMat()
: Random conditional distributions
dat <- rprobMat(10, c(2,2,2))
dat <- rprobMat(10, c(2,2,2))
Wrappers for Dirichlet distribution over a table
dtab_dir(x, alpha, log = FALSE) rtab_dir(n, alpha, rev = FALSE)
dtab_dir(x, alpha, log = FALSE) rtab_dir(n, alpha, rev = FALSE)
x |
|
alpha |
table containing parameters |
n |
number of samples |
rev |
logical: should output move through each table fastest? |
This function obtains the Dirichlet density over a contingency table
structure. In other words, suppose that we have a matrix observation
where
. Then we might choose
to model the vector
as having a Dirichlet distribution, with weights
.
If alpha
is a scalar in dtab_dir
then it is applied to every entry in x
.
dtab_dir()
: density function
rtab_dir()
: sampling function
Create blank tables
tables(n, tdim, rev = FALSE)
tables(n, tdim, rev = FALSE)
n |
number of tables |
tdim |
dimension of each table |
rev |
logical: should output move through each table fastest? |
Bind tables of the same dimension
tbind(x, ..., rev = FALSE)
tbind(x, ..., rev = FALSE)
x |
a |
... |
further |
rev |
logical: should output move through each table fastest? |
Dimension of distributions over contingency tables
tdim(x) tdim(x) <- value
tdim(x) tdim(x) <- value
x |
an object of class |
value |
value to set parameters to |
The class tables
is used to represent a collection of
multidimentional tables; this function
returns the dimension of each table.
an integer vector of the dimensions
the tables
object inputted with the new dimensions
tdim(x) <- value
: assign tables dimension
Dimension names for distributions over contingency tables
tdimnames(x) tdimnames(x) <- value
tdimnames(x) tdimnames(x) <- value
x |
|
value |
value to set dimension names to |
the tables
object inputted with the new dimension names
tdimnames(x) <- value
: assign dimension names