cpt: Create conditional probability tables (CPTs)

View source: R/cptable_create.R

cptR Documentation

Create conditional probability tables (CPTs)

Description

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

Usage

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

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.

Value

An array.

Author(s)

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

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/.

See Also

andtable, ortable, extract_cpt, compileCPT, extract_cpt, compilePOT, grain

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

varNames(t1)
valueLabels(t1)

## Wet grass example
ssp <- list(R=yn, S=yn, G=yn) # state space
p.R    <- cptable(~R,     levels=ssp, values=c(.2, .8))
p.S_R  <- cptable(~S:R,   levels=ssp, values=c(.01, .99, .4, .6))
p.G_SR <- cptable(~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
wet.cpt$S # etc

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


hojsgaard/gRain documentation built on Jan. 10, 2024, 9:27 p.m.