bn.fit: Fit the parameters of a Bayesian network

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

Description

Fit the parameters of a Bayesian network conditional on its structure.

Usage

1
2
3
bn.fit(x, data, method = "mle", ..., debug = FALSE)
custom.fit(x, dist, ordinal = FALSE)
bn.net(x, debug = FALSE)

Arguments

x

an object of class bn (for bn.fit and custom.fit) or an object of class bn.fit (for bn.net).

data

a data frame containing the variables in the model.

dist

a named list, with element for each node of x. See below.

method

a character string, either mle for Maximum Likelihood parameter estimation or bayes for Bayesian parameter estimation (currently implemented only for discrete data).

...

additional arguments for the parameter estimation prcoedure, see below.

ordinal

a boolean value. If TRUE, discrete nodes saved as ordinal random variables (bn.fit.onode) instead of unordered factors (bn.fit.dnode).

debug

a boolean value. If TRUE a lot of debugging output is printed; otherwise the function is completely silent.

Details

bn.fit fits the parameters of a Bayesian network given its structure and a data set; bn.net returns the structure underlying a fitted Bayesian network.

An in-place replacement method is available to change the parameters of each node in a bn.fit object; see the examples for both discrete and continuous networks below. For a node in a discrete network, the new parameters must be in a table object. For a node in a continuous network, the new parameters can be defined either by an lm, glm or pensim object (the latter is from the penalized package) or in a list with elements named coef, sd and optionally fitted and resid.

custom.fit takes a set of user-specified distributions and their parameters and uses them to build a bn.fit object. Its purpose is to specify a Bayesian network (complete with the parameters, not only the structure) using knowledge from experts in the field instead of learning it from a data set. The distributions must be passed to the function in a list, with elements named after the nodes of the network structure x. Each element of the list must be in one of the formats described above for in-place replacement.

Value

bn.fit returns an object of class bn.fit, bn.net an object of class bn. See bn class and bn.fit class for details.

Note

Due to the way Bayesian networks are defined it's possible to estimate their parameters only if the network structure is completely directed (i.e. there are no undirected arcs). See set.arc and pdag2dag for two ways of manually setting the direction of one or more arcs.

The only supported additional parameter is the imaginary sample size (iss) for the Dirichlet posterior distribution of discrete networks (see score for details).

Author(s)

Marco Scutari

See Also

bn.fit utilities, bn.fit plots.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
data(learning.test)

# learn the network structure.
res = gs(learning.test)
# set the direction of the only undirected arc, A - B.
res = set.arc(res, "A", "B")
# estimate the parameters of the Bayesian network.
fitted = bn.fit(res, learning.test)
# replace the parameters of the node B.
new.cpt = matrix(c(0.1, 0.2, 0.3, 0.2, 0.5, 0.6, 0.7, 0.3, 0.1),
            byrow = TRUE, ncol = 3,
            dimnames = list(B = c("a", "b", "c"), A = c("a", "b", "c")))
fitted$B = as.table(new.cpt)
# the network structure is still the same.
all.equal(res, bn.net(fitted))

# learn the network structure.
res = hc(gaussian.test)
# estimate the parameters of the Bayesian network.
fitted = bn.fit(res, gaussian.test)
# replace the parameters of the node F.
fitted$F = list(coef = c(1, 2, 3, 4, 5), sd = 3)
# set again the original parameters
fitted$F = lm(F ~ A + D + E + G, data = gaussian.test)

# discrete Bayesian network from expert knowledge.
net = model2network("[A][B][C|A:B]")
cptA = matrix(c(0.4, 0.6), ncol = 2, dimnames = list(NULL, c("LOW", "HIGH")))
cptB = matrix(c(0.8, 0.2), ncol = 2, dimnames = list(NULL, c("GOOD", "BAD")))
cptC = c(0.5, 0.5, 0.4, 0.6, 0.3, 0.7, 0.2, 0.8)
dim(cptC) = c(2, 2, 2)
dimnames(cptC) = list("C" = c("TRUE", "FALSE"), "A" =  c("LOW", "HIGH"),
                   "B" = c("GOOD", "BAD"))
cfit = custom.fit(net, dist = list(A = cptA, B = cptB, C = cptC))
# for ordinal nodes it's nearly the same.
cfit = custom.fit(net, dist = list(A = cptA, B = cptB, C = cptC), ordinal = TRUE)

# Gaussian Bayesian network from expert knowledge.
distA = list(coef = c("(Intercept)" = 2), sd = 1)
distB = list(coef = c("(Intercept)" = 1), sd = 1.5)
distC = list(coef = c("(Intercept)" = 0.5, "A" = 0.75, "B" = 1.32), sd = 04)
cfit = custom.fit(net, dist = list(A = distA, B = distB, C = distC))

vspinu/bnlearn documentation built on May 3, 2019, 7:08 p.m.