CPA: Representation of a conditional probability table as an...

CPAR Documentation

Representation of a conditional probability table as an array.

Description

A conditional probability table for a node can be represented as a array with the first p dimensions representing the parent variables and the last dimension representing the states of the node. Given a set of values for the parent variables, the values in the last dimension contain the conditional probabilities corresponding conditional probabilities. A CPA is a special array object which represents a conditional probability table.

Usage

is.CPA(x)
as.CPA(x)

Arguments

x

Object to be tested or coerced into a CPA.

Details

One way to store a conditional probability table is as an array in which the first p dimensions represent the parent variables, and the p+1 dimension represents the child variable. Here is an example with two parents variables, A and B, and a single child variable, C:

, , C=c1

b1 b2 b3
a1 0.07 0.23 0.30
a2 0.12 0.25 0.31
a3 0.17 0.27 0.32
a4 0.20 0.29 0.33

, , C=c2

b1 b2 b3
a1 0.93 0.77 0.70
a2 0.88 0.75 0.69
a3 0.83 0.73 0.68
a4 0.80 0.71 0.67

[Because R stores (and prints) arrays in column-major order, the last value (in this case tables) is the one that sums to 1.]

The CPA class is a subclass of the array class (formally, it is class c("CPA","array")). The CPA class interprets the dimnames of the array in terms of the conditional probability table. The first p values of names(dimnames(x)) are the input names of the edges (see NodeInputNames() or the variable names (or the parent variable, see NodeParents(), if the input names were not specified), and the last value is the name of the child variable. Each of the elements of dimnames(x) should give the state names (see NodeStates()) for the respective value. In particular, the conversion function as.CPF() relies on the existence of this meta-data, and as.CPA() will raise a warning if an array without the appropriate dimnames is supplied.

Although the intended interpretation is that of a conditional probability table, the normalization constraint is not enforced. Thus a CPA object could be used to store likelihoods, probability potentials, contingency table counts, or other similarly shaped objects. The function normalize scales the values of a CPA so that the normalization constraint is enforced.

The method NodeProbs() returns a CPA object. The function as.CPA() is designed to convert between CPFs (that is, conditional probability tables stored as data frames) and CPAs. It assumes that the factors variables in the data frame represent the parent variables, and the numeric values represent the states of the child variable. It also assumes that the names of the numeric columns are of the form varname.state, and attempts to derive variable and state names from that.

If the argument to as.CPA(x) is an array, then it assumes that the dimnames(x) and names(dimnames(x)) are set to the states of the variables and the names of the variables respectively. A warning is issued if the names are missing.

Value

The function is.CPA() returns a logical value indicating whether or not the is(x,"CPA") is true.

The function as.CPA returns an object of class c("CPA","array"), which is essentially an array with the dimnames set to reflect the variable names and states.

Note

The obvious way to print a CPA would be to always show the child variable as the rows in the individual tables, with the parents corresponding to rows and tables. R, however, internally stores arrays in column-major order, and hence the rows in the printed tables always correspond to the second dimension. A new print method for CPA would be nice.

This is an S3 object, as it just an array with a special interpretation.

Author(s)

Russell Almond

See Also

NodeProbs(), Extract.NeticaNode, CPF, normalize()

Examples

# Note:  in R 4.0, the factor() call is required.
arf <- data.frame(A=factor(rep(c("a1","a2"),each=3)),
                  B=factor(rep(c("b1","b2","b3"),2)),
                  C.c1=1:6, C.c2=7:12, C.c3=13:18, C.c4=19:24)
arfa <- as.CPA(arf)

arr1 <- array(1:24,c(4,3,2),
            dimnames=list(A=c("a1","a2","a3","a4"),B=c("b1","b2","b3"),
                          C=c("c1","c2")))
arr1a <- as.CPA(arr1)


## Not run: 
  ## Requires RNetica
  as.CPA(node[])

## End(Not run)

ralmond/CPTtools documentation built on Dec. 27, 2024, 7:15 a.m.