cptable: Create conditional probability tables (CPTs)

Description Usage Arguments Details Value Author(s) References See Also Examples

View source: R/cptable-create.R

Description

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

Usage

1
2
cptable(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.

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.

Value

A cptable object (a list).

Author(s)

S<c3><b8>ren H<c3><b8>jsgaard, sorenh@math.aau.dk

References

S<c3><b8>ren H<c3><b8>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, extractCPT, compileCPT, extractPOT, compilePOT, grain

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
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)

gRain documentation built on May 30, 2017, 8:20 a.m.

Search within the gRain package
Search all R packages, documentation and source code