normalize: Normalizes a conditional probability table.

View source: R/Tables.R

normalizeR Documentation

Normalizes a conditional probability table.

Description

A conditional probability table (CPT) represents a collection of probability distribution, one for each configuration of the parent variables. This function normalizes the CPT, insuring that the probabilities in each conditional distribution sum to 1.

Usage

normalize(cpt)
## S3 method for class 'CPF'
normalize(cpt)
## S3 method for class 'data.frame'
normalize(cpt)
## S3 method for class 'CPA'
normalize(cpt)
## S3 method for class 'array'
normalize(cpt)
## S3 method for class 'matrix'
normalize(cpt)
## S3 method for class 'table'
normalize(cpt)
## Default S3 method:
normalize(cpt)

Arguments

cpt

A conditional probability table stored in either array (CPA format) or data frame (CPF format). A general data vector is treated like an unconditional probability vector.

Details

The normalize function is a generic function which attempts to normalize a conditional probability distribution.

A conditional probability table in RNetica is represented in one of two ways. In the conditional probability array (CPA) the table is represented as a p+1 dimensional array. The first p dimensions correspond to configurations of the parent variables and the last dimension the child value. The normalize.CPA method adjusts the data value so that the sum across all of the child states is 1. Thus, apply(result,1:p,sum) should result in a matrix of 1's. The method normalize.array first coerces its argument into a CPA and then applies the normalize.CPA method.

The second way to represent a conditional probability table in RNetica is to use a data frame (CPF). Here the factor variables correspond to a configuration of the parent states, and the numeric columns correspond to states of the child variable. Each row corresponds to a particular configuration of parent variables and the numeric values should sum to one. The normalize.CPF function makes sure this constraint holds. The method normalize.data.frame first applies as.CPF() to make the data frame into a CPF.

The method normalize.matrix ensures that the row sums are 1. It does not change the class.

The default method only works for numeric objects. It ensures that the total sum is 1.

NA's are not allowed and will produce a result that is all NAs.

Value

An object with similar properties to cpt, but adjusted so that probabilities sum to one.

For normalize.CPA and normalize.array an normalized CPA array.

For normalize.CPF and normalize.data.fram an normalized CPF data frame.

For normalize.matrix an matrix whose row sums are 1.

For normalize.default a numeric vector whose values sum to 1.

Note

May be other functions for CPTs later.

Author(s)

Russell Almond

See Also

NodeProbs()

Examples

n14 <- normalize(1:4)


normalize(matrix(1:6,2,3))
normalize(array(1:24,c(4,3,2)))
arr <- array(1:24,c(4,3,2),
             list(a=c("A1","A2","A3","A4"),
                  b=c("B1","B2","B3"),
                  c=c("C1","C2")))
arr <- as.CPA(arr)
normalize(arr)

arf <- as.CPF(arr)
normalize(arf)

df2 <- data.frame(parentval=c("a","b"),
                  prob.true=c(1,1),prob.false=c(1,1))
normalize(df2)


## Admit is the response variable, so force it to be the last dimension.
UCBA1 <- aperm(UCBAdmissions,3:1)
normalize(UCBA1)

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