Learn R Programming

rje (version 1.4)

condition.table: Find conditional probability table

Description

Given a numeric array, calculates margins of some dimensions conditional on particular values of others.

Usage

condition.table(x, variables, condition = NULL, condition.value = NULL)
condition.table2(x, variables, condition)

Arguments

x
A numeric array.
variables
An integer vector containing the margins of x interest.
condition
An integer vector containing the dimensions of x to condition on.
condition.value
An integer vector or list of the same length as condition, containing the values to condition with. If NULL, then the full conditional distribution is returned.

Value

  • condition.table returns an array whose first length(variables) corresponds to the dimensions in variables, and the remainder (if any) to dimensions in condition with a corresponding entry in condition.value of length > 1.

    condition.table2 always returns an array of the same dimensions as x, with the variables in the same order.

Details

condition.table calculates the marginal distribution over the dimensions in variables for each specified value of the dimensions in condition. Single or multiple values of each dimension in condition may be specified in condition.value; in the case of multiple values, condition.value must be a list.

The sum over the dimensions in variables is normalized to 1 for each value of condition.

condition.table2 is just a wrapper which returns the conditional distribution as an array of the same dimensions and ordering as the original x.

See Also

margin.table

Examples

Run this code
x = array(1:16, rep(2,4))
x = x/sum(x) # probability distribution on 4 binary variables x1, x2, x3, x4.

# distribution of x2, x3 given x1 = 1 and x4=2.
condition.table(x, c(2,3), c(1,4), c(1,2))
# x2, x3 given x1 = 1,2 and x4 = 2.
condition.table(x, c(2,3), c(1,4), list(1:2,2))

# complete conditional of x2, x3 given x1, x4
condition.table(x, c(2,3), c(1,4))

# condition.table2 leaves dimensions unchanged
tmp = condition.table2(x, c(2,3), c(1,4))
aperm(tmp, c(2,3,1,4))

Run the code above in your browser using DataLab