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

cpt(names, levels, values, normalize = "first", smooth = 0)

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

Arguments

names

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

levels
  1. a list with specification of the levels of the factors in names or 2) a vector with number of levels of the factors in names. See 'examples' below.

values

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

normalize

See 'details' below.

smooth

Should values be smoothed, see 'Details' below.

vpar

node an its parents

Value

An array.

Details

cptable is simply a wrapper for cpt and the functions can hence be used synonymously.

If smooth is non–zero, then this value is added to all cells before normalization takes place.

Regarding the form of the argument names: 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 are used as a separators only. The order of the variables IS important so the operators DO NOT commute.

The first variable in levels varies fastest.

References

Søren Højsgaard (2012). Graphical Independence Networks with the gRain Package for R. Journal of Statistical Software, 46(10), 1-26. https://www.jstatsoft.org/v46/i10/.

Author

Søren Højsgaard, sorenh@math.aau.dk

Examples


## See the wet grass example at
## https://en.wikipedia.org/wiki/Bayesian_network

yn <- c("yes", "no")
ssp <- list(R=yn, S=yn, G=yn) # state space

## Different forms
t1 <- cpt(c("S", "R"), levels=ssp,     values=c(.01, .99, .4, .6))
t2 <- cpt(~S:R,        levels=ssp,     values=c(.01, .99, .4, .6))
t3 <- cpt(~S:R,        levels=c(2, 2), values=c(.01, .99, .4, .6))
t4 <- cpt(~S:R,        levels=yn,      values=c(.01, .99, .4, .6))
t1; t2; t3; t4
#>      R
#> S      yes  no
#>   yes 0.01 0.4
#>   no  0.99 0.6
#>      R
#> S      yes  no
#>   yes 0.01 0.4
#>   no  0.99 0.6
#>    R
#> S      1   2
#>   1 0.01 0.4
#>   2 0.99 0.6
#>      R
#> S      yes  no
#>   yes 0.01 0.4
#>   no  0.99 0.6

varNames(t1)
#> [1] "S" "R"
valueLabels(t1)
#> $S
#> [1] "yes" "no" 
#> 
#> $R
#> [1] "yes" "no" 
#> 

## Wet grass example
ssp <- list(R=yn, S=yn, G=yn) # state space
p.R    <- cpt(~R,     levels=ssp, values=c(.2, .8))
p.S_R  <- cpt(~S:R,   levels=ssp, values=c(.01, .99, .4, .6))
p.G_SR <- cpt(~G:S:R, levels=ssp, values=c(.99, .01, .8, .2, .9, .1, 0, 1))

wet.cpt <- compileCPT(p.R, p.S_R, p.G_SR)
wet.cpt
#>  P( R )
#>  P( S | R )
#>  P( G | S R )
wet.cpt$S # etc
#>      R
#> S      yes  no
#>   yes 0.01 0.4
#>   no  0.99 0.6

# A Bayesian network is created with:
wet.bn <- grain(wet.cpt)