Learn R Programming

gRain (version 1.3-2)

cptable: Create conditional probability tables (CPTs)

Description

Creates conditional probability tables of the form p(v|pa(v)).

Usage

cptable(vpar, levels = NULL, values = NULL, normalize = TRUE, smooth = 0)

cptab(vpar, levels = NULL, values = NULL, normalize = TRUE, smooth = 0)

Arguments

vpar

Specifications of the names in P(v|pa1,...pak). See section 'details' for information about the form of the argument.

levels

See 'details' below.

values

Probabilities; recycled if necessary. Regarding the order, please see section 'details' and the examples.

normalize

See 'details' below.

smooth

See 'details' below.

Value

A cptable object (a list).

Details

If normalize=TRUE then for each configuration of the parents the probabilities are normalized to sum to one.

If smooth is non--zero then zero entries of values are replaced with smooth before normalization takes place.

Regarding the form of the argument vpar: To specify \(P(a|b,c)\) one may write ~a|b:c, ~a:b:c, ~a|b+c, ~a+b+c or c("a","b","c"). Internally, the last form is used. Notice that the + and : operator is used as a separator only. The order of the variables IS important so the operators DO NOT commute.

If a has levels a1,a2 and likewise for b and c then the order of values corresponds to the configurations (a1,b1,c1), (a2,b1,c1) (a1,b2,c1), (a2,b2,c1) etc. That is, the first variable varies fastest. Hence the first two elements in values will be the conditional probabilities of a given b=b1, c=c1.

References

S<U+00F8>ren H<U+00F8>jsgaard (2012). Graphical Independence Networks with the gRain Package for R. Journal of Statistical Software, 46(10), 1-26. http://www.jstatsoft.org/v46/i10/.

See Also

andtable, ortable, extract_cpt, compile_cpt, extract_pot, compile_pot, grain

Examples

Run this code
# NOT RUN {

yn   <- c("yes", "no")
ynm  <- c("yes", "no", "maybe")
a    <- cptable(~ asia, values=c(1, 99), levels=yn)
t.a  <- cptable(~ tub : asia, values=c(5, 95, 1, 99, 1, 999),  levels=ynm)
d.a  <- cptable(~ dia : asia, values=c(5, 5, 1, 99, 100, 999), levels=ynm)
cptlist <- compileCPT(list(a, t.a, d.a))
grain(cptlist)

## Example: Specifying conditional probabilities as a matrix
bayes.levels  <- c('Enzyme', 'Keratine', 'unknown')
root.node     <- cptable(~ R, values=c( 1, 1, 1 ), levels=bayes.levels)
cond.prob.tbl <- t(matrix(c(1, 0, 0, 0, 1, 0, 0.5, 0.5, 0),
   nrow=3, ncol=3, byrow=TRUE, dimnames=list(bayes.levels, bayes.levels)))
cond.prob.tbl

## Notice above: Columns represent parent states; rows represent child states
query.node    <- cptable(~ Q | R, values=cond.prob.tbl, levels=bayes.levels)
sister.node   <- cptable(~ S | R, values=cond.prob.tbl, levels=bayes.levels)

## Testing 
compile(grain(compileCPT(list(root.node, query.node, sister.node))), propagate=TRUE)

# }

Run the code above in your browser using DataLab