Create Bayesian network (grain objects (graphical independence network)).

grain(x, ...)

# S3 method for class 'cpt_spec'
grain(x, control = list(), smooth = 0, compile = TRUE, details = 0, ...)

# S3 method for class 'CPTspec'
grain(x, control = list(), smooth = 0, compile = TRUE, details = 0, ...)

# S3 method for class 'pot_spec'
grain(x, control = list(), smooth = 0, compile = TRUE, details = 0, ...)

# S3 method for class 'igraph'
grain(
  x,
  control = list(),
  smooth = 0,
  compile = TRUE,
  details = 0,
  data = NULL,
  ...
)

# S3 method for class 'dModel'
grain(
  x,
  control = list(),
  smooth = 0,
  compile = TRUE,
  details = 0,
  data = NULL,
  ...
)

Arguments

x

An argument to build an independence network from. Typically a list of conditional probability tables, a DAG or an undirected graph. In the two latter cases, data must also be provided.

...

Additional arguments, currently not used.

control

A list defining controls, see 'details' below.

smooth

A (usually small) number to add to the counts of a table if the grain is built from a graph plus a dataset.

compile

Should network be compiled.

details

Debugging information.

data

An optional data set (currently must be an array/table)

Value

An object of class "grain"

Details

If 'smooth' is non-zero then entries of 'values' which a zero are replaced by the value of 'smooth' - BEFORE any normalization takes place.

Note

A change from earlier versions of this package is that grain objects are now compiled upon creation.

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


## Create network from conditional probability tables CPTs:

yn   <- c("yes", "no")
a    <- cpt(~asia,                  values=c(1,99), levels=yn)
t.a  <- cpt(~tub + asia,            values=c(5,95,1,99), levels=yn)
s    <- cpt(~smoke,                 values=c(5,5), levels=yn)
l.s  <- cpt(~lung + smoke,          values=c(1,9,1,99), levels=yn)
b.s  <- cpt(~bronc + smoke,         values=c(6,4,3,7), levels=yn)
e.lt <- cpt(~either + lung + tub,   values=c(1,0,1,0,1,0,0,1), levels=yn)
x.e  <- cpt(~xray + either,         values=c(98,2,5,95), levels=yn)
d.be <- cpt(~dysp + bronc + either, values=c(9,1,7,3,8,2,1,9), levels=yn)
cpt_list  <- list(a, t.a, s, l.s, b.s, e.lt, x.e, d.be)
chest_cpt <- compileCPT(cpt_list)
## Alternative: chest_cpt <- compileCPT(a, t.a, s, l.s, b.s, e.lt, x.e, d.be)

chest_bn  <- grain(chest_cpt)

## Create network from data and graph specification.

data(lizard, package="gRbase")

## From a DAG: height <- species -> diam
daG <- dag(~species + height:species + diam:species, result="igraph")

## From an undirected graph UG : [height:species][diam:species]
uG  <- ug(~height:species + diam:species, result="igraph")

liz.ug   <- grain(uG, data=lizard)
liz.dag  <- grain(daG, data=lizard)