Title: | Handling Linear Gaussian Bayesian Networks |
---|---|
Description: | Creation, manipulation, simulation of linear Gaussian Bayesian networks from text files and more... |
Authors: | Jean-Baptiste Denis [aut, cre], Marco Scutari [ctb] |
Maintainer: | Marco Scutari <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.9-6 |
Built: | 2024-12-14 06:39:34 UTC |
Source: | CRAN |
General functions to generate, transform, display general and
particular linear Gaussian Bayesian networks [/nbn/] are provided.
Specific /nbn/ are chain and crossed /nbn/s. Focus is given in getting
joint and conditional probability distributions of the set of
nodes.
rbmn stands for R'eseau Bay'esien MultiNormal.
Some basic concepts:
chain /nbn/s are /nbn/s where all nodes are connected with two other nodes, except the two ending nodes of the chain having only one connection. (This is not the usual terminology in graphical models but I didn't find a more appropriate word: suggestions are welcome.)
crossed /nbn/s are /nbn/s having the node set defined as a
Cartesian product of two series of items, and a DAG based on this
structure. See the crossed4nbn1nbn
function and/or Tian (2014) for
details.
An adjacency matrix is a matrix equivalent to the DAG
associated to a /nbn/. Its rows as well as its columns are associated
to the set of nodes. The (i,j)
cell is one when there is an arc going
from node i
to j
and zero otherwise.
Three equivalent ways can be used to represent the joint probability distribution of a set of nodes respectively associated to the structures /mn/, /nbn/ and /gema/:
/mn/ (for multivariate normal) is just the list of the
expectation ($mu
) and the variance matrix ($gamma
).
/nbn/ (for normal Bayesian network) is a simple list, a component a node described with a list. The names are node names and each list associated to a node provides the conditional expectation and variance, the parent (if any) and the associated regression coefficients.
/gema/ (for generating matrices) is a list of a vector
(mu
) and a matrix (li
) such that the vector of the
nodes can be defined by X = mu + li%*%E
where E
is a
normal random vector with expectation zero and variance matrix
unity.
It is planned to add a fourth one under the name of /gbn/.
To relieve the memory effort, most names of the functions have been given a two (or more) components structure separated with a figure. This idea will be explained and exploited in a package to come named documair. The approximate meaning of the figures are:
0 (similar to 'o') rbmn0chain.01
to indicate an object
example provided by rbmn.
1 (similar to an ~ and) ??? to link different objects or
actions train1car
for train and car.
2 (as usual but only one-to-one) nbn2gema
means
\"transforming a /nbn/ into a /gema/ objects\".
3 (remind the 'belong to' sign) form3repeat
could be
interpreted as "repeat action from the series of 'form' functions".
4 (associated to 'from') adja4nbn
means "get the adjacency
matrix from a /nbn/ object".
7 (upper bar of '7' similar to the hyphen) arc7nb4nbn
means "get the arc-numbers from a /nbn/".
8 (similar to 'a') generate8nbn
or print8nbn
for \"generating or printing a /nbn/ object\".
A number of ancillary functions have not been exported to give a better
access to the main function of /rbmn/. Nevertheless they are available
in the ../rbmn/R/
directory, and with all their comments
(equivalent to Rd
files into ../rbmn/inst/original/
directory). Some of them are visible when defining the default
arguments of some functions.
Generalize the /mn/ object with a regression part like
the output of function condi4joint
when argument
pour
is not of length zero and argument x2
is not
null. With such a structure, every node of a /nbn/ could be
described with a /mn/ comprising a unique variable... Also the two
arguments of function mn4joint1condi
would be just two /mn/
objects... This is also the generalized /mn/ proposed in function
simulate8gmn
under the argument of loi
... Of course
almost all functions dealing with /nbn/ objects will be to rewrite!
Introduce a new object gbn
for Gaussian Bayesian
network similar to the list provided by function nbn2rr
.
Systemize the existence of check8object
functions
Introduce their systematic use conditionned with a
rbmn0check
variable.
Follow the main checking of every functions
Give (and use) class attributes to the main objects.
Introduce the main objects in this short presentation.
Make a true small example in this short presentation.
Make the function nbn4string7dag
.
Add the computation made with /bnlearn/ in the example of
estimate8nbn
.
Check the topological order within nbn2nbn
depending on
rbmn0check
value.
Make a super transformation function from an object associated to a Bayesian network to any other type, including itself.
Correct the ord
option in order4chain
.
Check the topological order in rm8nd4adja
.
Think about removing all rmatrix
transformations to the
benefit of the to-come gbn
object.
Introduce a check of non-negativity of ma
into
cor4var
.
Add examples to all functions without any.
Original author: Jean-Baptiste Denis
Maintainer: Marco Scutari
Scutari M (2010). "Learning Bayesian Networks with the bnlearn R Package". Journal of Statistical Software, 35(3), 1-22.
Tian S, Scutari M & Denis J-B (2014). "Predicting with Crossed Linear Gaussian Bayesian Networks". Journal de la Societe Francaise de Statistique, 155(3), 1-21.
library(rbmn) ## getting the data set data(boco) print(head(boco));
library(rbmn) ## getting the data set data(boco) print(head(boco));
returns the arc matrix from an adjacency matrix.
adja2arcs(adj)
adja2arcs(adj)
adj |
The adjacency matrix. |
a matrix with two columns ("from","to")
adja2arcs(rbmn0adja.02)
adja2arcs(rbmn0adja.02)
Like crossed4nbn1nbn but at the level of adjacency matrices. Must be much efficient when regression coefficients are not needed.
adja2crossed(adj1, adj2, nona=as.vector(outer(dimnames(adj1)[[1]], dimnames(adj2)[[1]], paste, sep="_")))
adja2crossed(adj1, adj2, nona=as.vector(outer(dimnames(adj1)[[1]], dimnames(adj2)[[1]], paste, sep="_")))
adj1 |
The first adjacency matrix. |
adj2 |
The second adjacency matrix. |
nona |
The node names to give to the crossed /nbn/, the nodes
of the |
Just two Kronecker products of matrices.
The resulting crossed adjacency matrix.
print(adja2crossed(rbmn0adja.01, rbmn0adja.01));
print(adja2crossed(rbmn0adja.01, rbmn0adja.01));
returns a nbn
object with O/1 regression coefficients having
adja
as adjacency matrix.
adja2nbn(adja)
adja2nbn(adja)
adja |
The initial adjacency matrix. |
The corresponding standardized nbn
object.
print8nbn(adja2nbn(adja4nbn(rbmn0nbn.03)));
print8nbn(adja2nbn(adja4nbn(rbmn0nbn.03)));
returns a dimnamed matrix indicating with 1 an arc from row to column nodes (0 everywhere else); i.e. the adjacency matrix.
adja4nbn(nbn)
adja4nbn(nbn)
nbn |
The initial |
A dimnamed matrix
adja4nbn(rbmn0nbn.04);
adja4nbn(rbmn0nbn.04);
Returns the list of the 25 adjacency matrices associated to DAGs comprising three nodes. The first character of the name components gives the number of arcs in the DAG.
adja4three(nona=LETTERS[1:3])
adja4three(nona=LETTERS[1:3])
nona |
The three node names. |
Poor filling...
a named list having 25 components, each being a 3x3 matrix.
returns the arc numbers of the node of /nbn/ object.
arc7nb4nbn(nbn, each=FALSE)
arc7nb4nbn(nbn, each=FALSE)
nbn |
The |
each |
When |
Parents associated with a zero regression coefficient are not excluded in the counting.
Either a number or a named vector of numbers (names being the node names).
arc7nb4nbn(rbmn0nbn.05);
arc7nb4nbn(rbmn0nbn.05);
Returns a list of matrices with two columns (as needed by
estimate8constrainednbn
) indicating corresponding arcs for
each arcs/nodes of nbn1
(or nbn2
) of the crossed /nbn/
obtained when crossing /nbn1/ and /nbn2/ with node names given by
nona
.
arcs4nbn1nbn(nbn1, nbn2, type="a1", nona=as.vector(outer(names(nbn1), names(nbn2), paste, sep="_")))
arcs4nbn1nbn(nbn1, nbn2, type="a1", nona=as.vector(outer(names(nbn1), names(nbn2), paste, sep="_")))
nbn1 |
The first generating /nbn/. |
nbn2 |
The second generating /nbn/. |
type |
Must be |
nona |
The node names to give to the crossed /nbn/, the nodes
of the |
The resulting named (after node names) list of matrices.
print(arcs4nbn1nbn(rbmn0nbn.01, rbmn0nbn.04));
print(arcs4nbn1nbn(rbmn0nbn.01, rbmn0nbn.04));
returns a nbn
object from a DAG (bn
object) of
/bnlearn/ package. O and 1 coefficients are introduced...
bn2nbn(bn)
bn2nbn(bn)
bn |
The object to be transformed. |
A list following the nbn
specification
returns a nbn
object from a Gaussian bn.fit
object of
/bnlearn/ package.
bnfit2nbn(bn.fit)
bnfit2nbn(bn.fit)
bn.fit |
The object to be transformed. |
If bn.fit
is not pertinent, a fatal error is issued.
A list following the nbn
specification
Real-world data set extracted from the Nhanes data base comprising nine variables describing the body composition and five easy measurable covariables.
data(boco)
data(boco)
The boco
data set stored in variable boco
comprises 100 individuals with the following variables:
A
the age in years
H
the height in cm
W
the weight in kg
C
the waist circumference in cm
TF
the trunk fat in kg
LF
the leg fat in kg
AF
the arm fat in kg
TL
the trunk lean in kg
LL
the leg lean in kg
AL
the arm lean in kg
TB
the trunk bone in kg
LB
the leg bone in kg
AB
the arm bone in kg
Centers for Disease Control and Prevention. The 1999-2004 dual energy
X-ray absorptiometry (DXA) multiple imputation data files and
technical documentation.
Available from:
http://www.cdc.gov/nchs/about/major/nhanes/dxx/dxa.html (accessed on
13_07_03).
# load the data and build the correct network from the model string. data(boco); print(head(boco)); boco7dag <- "[H][W|H][TF|W;H]"; # to be finished
# load the data and build the correct network from the model string. data(boco); print(head(boco)); boco7dag <- "[H][W|H][TF|W;H]"; # to be finished
returns the correlation matrix of a /chain/ object.
chain2correlation(chain)
chain2correlation(chain)
chain |
The chain object to consider. |
The correlation matrix. It is not sorted to respect a topological
order contrary to chain2mn
function.
chain2correlation(rbmn0chain.03);
chain2correlation(rbmn0chain.03);
From a chain
object returns the gema
using a direct
formulae.
Much precised than to use the /nbn/ way.
chain2gema(chain)
chain2gema(chain)
chain |
the |
The corresponding gema
object.
identical(chain2gema(rbmn0chain.02)$mu, rbmn0gema.02$mu); print(chain2gema(rbmn0chain.02)$li-rbmn0gema.02$li);
identical(chain2gema(rbmn0chain.02)$mu, rbmn0gema.02$mu); print(chain2gema(rbmn0chain.02)$li-rbmn0gema.02$li);
returns the /mn/ object associated to a /chain/ object. Much better
to use this function that the general function nbn2mn
since
exact formulae are applied.
chain2mn(chain, order=TRUE)
chain2mn(chain, order=TRUE)
chain |
The chain object to consider. |
order |
Must a topological order be imposed? |
The resulting /mn/ object. Following the convention of mn
objects, a topological order is given to it. This is necessary to
retrieve the associate /nbn/.
print8mn(chain2mn(rbmn0chain.01));
print8mn(chain2mn(rbmn0chain.01));
From a chain
object returns the nbn
translation.
chain2nbn(chain)
chain2nbn(chain)
chain |
the |
The corresponding nbn
object.
print8nbn(chain2nbn(rbmn0chain.02), ordering=names(rbmn0nbn.02));
print8nbn(chain2nbn(rbmn0chain.02), ordering=names(rbmn0nbn.02));
returns the precision matrix of a chain, that is the inverse of its
variance (correlation) matrix. Much better to use this function that
solve(chain2mn(chain)$gamma)
since exact formulae are applied.
chain2pre(chain, corre=FALSE)
chain2pre(chain, corre=FALSE)
chain |
The chain object to consider. |
corre |
To get the inverse of the correlation matrix instead of. |
A dimnamed matrix
chain2pre(rbmn0chain.02);
chain2pre(rbmn0chain.02);
returns the chain obtained from chain
retaining only nodes
indicated by nodes
and conditioned with nodes indicated in
condi
.
chain4chain(chain, nodes, condi=numeric(0), value=rep(0, length(condi)))
chain4chain(chain, nodes, condi=numeric(0), value=rep(0, length(condi)))
chain |
The chain object to consider. |
nodes |
|
condi |
|
value |
Numerical values associated to |
Integration is done for nodes not belonging to the extracted chain nor being in the conditioning subset. Then the distribution of the retained nodes is left identical to this in the initial chain.
The resulting chain
chain4chain(rbmn0chain.02, c("a", "d"), c("b"), 12);
chain4chain(rbmn0chain.02, c("a", "d"), c("b"), 12);
checks the consistency of chain
as a /chain/ object issues a
fatal error with some clues if inconsistent.
check8chain(chain)
check8chain(chain)
chain |
The |
Looking a the code of this function provides a way to know which are the requirements of a /chain/ object.
TRUE
or a character
containing some clue about the
discovered inconsistency.
check8chain(rbmn0chain.01); res <- check8chain(rbmn0adja.01); if (is.na(as.logical(res))) { print(res);}
check8chain(rbmn0chain.01); res <- check8chain(rbmn0adja.01); if (is.na(as.logical(res))) { print(res);}
checks the consistency of gema
as a /gema/ object issues a
fatal error with some clues if inconsistent.
check8gema(gema)
check8gema(gema)
gema |
The |
Looking a the code of this function provides a way to know which are the requirements of a /chain/ object.
TRUE
or a character
containing some clue about the
discovered inconsistency.
check8gema(rbmn0gema.01); res <- check8gema(rbmn0adja.01); if (is.na(as.logical(res))) { print(res);}
check8gema(rbmn0gema.01); res <- check8gema(rbmn0adja.01); if (is.na(as.logical(res))) { print(res);}
checks the consistency of nbn
as a /nbn/ object issues a fatal
error with some clues if inconsistent.
check8nbn(nbn, sto = FALSE)
check8nbn(nbn, sto = FALSE)
nbn |
The |
sto |
If 'TRUE', the possible error is displayed and the process is stopped. If 'FALSE', the possible error is returned as a character. |
Looking a the code of this function provides a way to know which are the requirements of a /chain/ object.
TRUE
or a character
containing some clue about the
discovered inconsistency.
check8nbn(rbmn0nbn.01); res <- check8nbn(rbmn0adja.01); if (is.na(as.logical(res))) { print(res);}
check8nbn(rbmn0nbn.01); res <- check8nbn(rbmn0adja.01); if (is.na(as.logical(res))) { print(res);}
returns the expectation and variance of a sub-vector conditionned
with another (non overlapping) sub-vector from an initial random
vector described by mn
.
condi4joint(mn, par, pour, x2=NULL)
condi4joint(mn, par, pour, x2=NULL)
mn |
list defining the distribution of the initial vector with
|
par |
names (or indices) of the sub-vector to give the distribution. |
pour |
names (or indices) of the conditionning sub-vector (can
be |
x2 |
values to consider for the conditioning sub-vector. When
|
when no names are given to mn$mu
, par
and pour
are supposed containing indices and default sequential names are
provided.
A list:
when x2
provides the values taken by the
conditioning part, it is a /mn/ object with its two components:
$mu
for the expectation vector and $gamma
for the
variance matrix.
when x2
is NULL
the list has got
three components: $mu
for the fixed part of the expectation
vector, $b
for the regression coefficients to be associated to
the non precised x2
values, varying part of the expectation
and $gamma
for the variance matrix.
print8mn(condi4joint(rbmn0mn.04, c("1.1", "2.2", "1.2", "2.1"), NULL)); print8mn(condi4joint(rbmn0mn.04, c("1.1", "2.2", "1.2", "2.1"), "C", 0)); print(condi4joint(rbmn0mn.04, c("1.1", "2.2", "1.2", "2.1"), "C", NULL));
print8mn(condi4joint(rbmn0mn.04, c("1.1", "2.2", "1.2", "2.1"), NULL)); print8mn(condi4joint(rbmn0mn.04, c("1.1", "2.2", "1.2", "2.1"), "C", 0)); print(condi4joint(rbmn0mn.04, c("1.1", "2.2", "1.2", "2.1"), "C", NULL));
returns the correlation matrix from the variance preserving possible variable names
cor4var(ma)
cor4var(ma)
ma |
The variance matrix. |
Zero variances are detected and accepted (all associated correlation
coefficients are forced to be zero.
The correlation matrix
cor4var(rbmn0mn.04$gamma);
cor4var(rbmn0mn.04$gamma);
A crossed /nbn/ is a /nbn/ obtained when replacing each node of the
first /nbn/ by the second /nbn/ and vice-versa.
Let nn1/nn2
and na1/na2
be the node and arc numbers of the two
nbn
s, the node number of the crossed nbn
is
nn1*nn2
and its arc number is nn1*na2+nn2*na1
.
The
regression coefficients attributed to the crossed nbn
are the
products of the weights (we1/we2
) and the regression
coefficients of the initial nbn
.
crossed4nbn1nbn(nbn1, nbn2, we1=rep(1, length(nbn1)), we2=rep(1, length(nbn2)), nona=as.vector(outer(names(nbn1), names(nbn2), paste, sep="_")))
crossed4nbn1nbn(nbn1, nbn2, we1=rep(1, length(nbn1)), we2=rep(1, length(nbn2)), nona=as.vector(outer(names(nbn1), names(nbn2), paste, sep="_")))
nbn1 |
The first generating /nbn/. |
nbn2 |
The second generating /nbn/. |
we1 |
The weight to apply to the nodes of the first generating /nbn/. |
we2 |
The weight to apply to the nodes of the second generating /nbn/. |
nona |
The node names to give to the crossed /nbn/, the nodes
of the |
The mu
coefficient is the sum of the two corresponding
mu
s of the generating nbn
.
The sigma
coefficient is the product of the two corresponding sigma
s of
the generating nbn
.
The regression coefficient are directed
inherited from the nbn
which is duplicated with this arc.
The resulting crossed nbn
object.
print8nbn(crossed4nbn1nbn(rbmn0nbn.01, rbmn0nbn.04));
print8nbn(crossed4nbn1nbn(rbmn0nbn.01, rbmn0nbn.04));
From the n
observed values of a vector of size p
(Y),
their expectations (EY) and the variance matrix (VY) supposed
identical for all vectors, returns the deviance, i.e.
-2*log(p(Y))
.
dev4mn(Y, EY, VY)
dev4mn(Y, EY, VY)
Y |
Matrix |
EY |
Expectation of |
VY |
Matrix of the variance of each row of |
When EY
is a vector with length ncol(Y)
this supposes
that all observations have the same expectation.
A scalar
dev4mn(matrix(runif(3), 1), t(rbmn0mn.01$mu), rbmn0mn.01$gamma);
dev4mn(matrix(runif(3), 1), t(rbmn0mn.01$mu), rbmn0mn.01$gamma);
Returns a positive scalar value measuring, in some way, the
difference existing within two /nbn/s sharing the same structure.
diff8nbn(nbn1, nbn2, type=1, scalar=TRUE)
diff8nbn(nbn1, nbn2, type=1, scalar=TRUE)
nbn1 |
First |
nbn2 |
Second |
type |
When 1, the score includes the difference between the sigmas. When -1, sigmas are not taken into account. |
scalar |
When |
For type==1
it is the canonical euclidian difference between
all parameters, including the sigma
. The score to use to
measure the differences between two successive estimations is not
well established (see the code).
Either a scalar or a named vector (according to scalar
).
diff8nbn(rbmn0nbn.01, rbmn0nbn.01); diff8nbn(rbmn0nbn.01, rbmn0nbn.01, scalar=FALSE);
diff8nbn(rbmn0nbn.01, rbmn0nbn.01); diff8nbn(rbmn0nbn.01, rbmn0nbn.01, scalar=FALSE);
Estimations of the parameters of a /nbn/ is done when there are some
equality constraints onto the regression coefficients.
Constant
terms (mu
) and conditional standard deviations (sigma
)
are supposed independent (that is not constrained with
equalities).
Equality constraints are given by sarc
, a list
of matrices with two columns, indicating each the series of arcs
having the same regression coefficient.
estimate8constrainednbn(nbn, sarc, data, imp=0, nite=10, eps=10^-5)
estimate8constrainednbn(nbn, sarc, data, imp=0, nite=10, eps=10^-5)
nbn |
|
sarc |
List of Matrices with two columns indicating the tails
(1rst column) and the heads (2d column) of the arcs having a common
parameter. It is checked that these arcs are indeed included in
|
data |
Data frame to be used for the estimation. It must
comprise all necessary nodes (not only those involved in |
imp |
When |
nite |
Maximum number of iterations. |
eps |
relative difference in successive scores needed to stop the iterations. |
Not linked regression coefficients doesn't require to be included in
sarc
, the function do it by itself.
The score to use to
measure the differences between two successive estimations is not
well established (see the code).
The resulting /nbn/ object with the estimated parameters.
data(boco); print8nbn(rbmn0nbn.05); print8nbn(estimate8nbn(rbmn0nbn.05, boco)); print8nbn(estimate8constrainednbn(rbmn0nbn.05, rbmn0crarc.05, boco));
data(boco); print8nbn(rbmn0nbn.05); print8nbn(estimate8nbn(rbmn0nbn.05, boco)); print8nbn(estimate8constrainednbn(rbmn0nbn.05, rbmn0crarc.05, boco));
From a /nbn/ to describe the DAG, and a data.frame containing the necessary observations, returns the /nbn/ with all its parameters newly estimated.
estimate8nbn(nbn, data)
estimate8nbn(nbn, data)
nbn |
The initial /nbn/. |
data |
The data frame comprising all /nbn/ nodes. |
No constraints are put on the parameters.
The resulting /nbn/ with the estimated parameters.
data(boco); print8nbn(rbmn0nbn.05); print8nbn(estimate8nbn(rbmn0nbn.05, boco));
data(boco); print8nbn(rbmn0nbn.05); print8nbn(estimate8nbn(rbmn0nbn.05, boco));
from a /gema/ object defining a normal Bayesian network, computes the expectation and variance matrix.
gema2mn(gema)
gema2mn(gema)
gema |
Initial |
a list with the following components: mu
and gamma
.
print8mn(gema2mn(rbmn0gema.04));
print8mn(gema2mn(rbmn0gema.04));
from a /gema/ object defining a normal Bayesian network, computes more standard /nbn/ where each node is defined from its parents.
gema2nbn(gema)
gema2nbn(gema)
gema |
Initial |
using general formulae rather a sequential algorithm as done in the
original gema2nbn
implementation.
the corresponding /nbn/.
print8nbn(gema2nbn(rbmn0gema.02));
print8nbn(gema2nbn(rbmn0gema.02));
[randomly] generates a /chain/ /nbn/.
generate8chain(rnn=c(3, 7), proo=0.5, rcor=c(-1, 1), rmu=c(0, 0), rsig=c(0, 1), nona=r.form3names(max(rnn)))
generate8chain(rnn=c(3, 7), proo=0.5, rcor=c(-1, 1), rmu=c(0, 0), rsig=c(0, 1), nona=r.form3names(max(rnn)))
rnn |
Range of the number of nodes. |
proo |
Probabilit[y |
rcor |
Range of the correlations between neighbour nodes. |
rmu |
Range of the expectations. |
rsig |
Range of the standard deviations. |
nona |
Proposed names for the maximum number of nodes, only the necessary first ones will be used. |
Proposed ranges can be a unique value, implying no randomness in the
value.
Roots are placed according to proo
probabilities,
then collider are placed in between with uniform probability on the
possibles nodes.
A /chain/ coding list is returned.
set.seed(1234); print8chain(generate8chain()); print8chain(generate8chain()); print8chain(generate8chain(rnn=10, rcor=0.5)); print8chain(generate8chain(rnn=10, rcor=0.5));
set.seed(1234); print8chain(generate8chain()); print8chain(generate8chain()); print8chain(generate8chain(rnn=10, rcor=0.5)); print8chain(generate8chain(rnn=10, rcor=0.5));
To obtain systematic results, you have to call set.seed
before
hands.
generate8nbn(rnn=c(3, 7), ppar=0.5, rreg=c(-1, 1), rmu=c(0, 0), rsig=c(0, 1), nona=r.form3names(max(rnn)))
generate8nbn(rnn=c(3, 7), ppar=0.5, rreg=c(-1, 1), rmu=c(0, 0), rsig=c(0, 1), nona=r.form3names(max(rnn)))
rnn |
Range of the number of nodes. |
ppar |
Probabilities (not a range) of the parent occurrence for each ancestor of every node. Can be a vector, cycled as necessary. |
rreg |
Range of regression coefficients. |
rmu |
Range of the conditional expectations. |
rsig |
Range of the conditional standard deviations. |
nona |
Proposed names for the maximum number of nodes, only the necessary first ones will be used. |
Node numbers are uniformly drawn. Parent numbers are independently
drawn from all ancestors with the probability associated to the
considered node. Regression coefficient are uniformly drawn.
Conditional expectations and standard deviations are uniformly
drawn.
All range arguments can be given one value instead of two,
to precise the unique value to use.
a /nbn/ object, with nodes in topological order.
set.seed(1234) print8nbn(generate8nbn()); print8nbn(generate8nbn());
set.seed(1234) print8nbn(generate8nbn()); print8nbn(generate8nbn());
From a chain
returns the reduced chain
comprising only
inputs (that is root nodes) and outputs (that is colliders and ends
which are not roots)
inout4chain(chain)
inout4chain(chain)
chain |
The chain object to consider. |
The resulting chain
print8chain(inout4chain(rbmn0chain.02));
print8chain(inout4chain(rbmn0chain.02));
returns TRUE
[the order] or FALSE
[NULL] according that
nbn
is a chain of not [according to order
].
is8nbn8chain(nbn, order=FALSE)
is8nbn8chain(nbn, order=FALSE)
nbn |
The nbn object to consider. |
order |
When |
A logical(1)
when order
si TRUE
if not the
resulting chain order versus NULL.
is8nbn8chain(rbmn0nbn.01); is8nbn8chain(rbmn0nbn.04);
is8nbn8chain(rbmn0nbn.01); is8nbn8chain(rbmn0nbn.04);
From a chain
object returns a list with two components:
$mu
and $sigma
vectors of marginal expectations and
standard deviations.
marginal4chain(chain)
marginal4chain(chain)
chain |
the |
a list with the two components $mu
and $sigma
.
marginal4chain(rbmn0chain.02);
marginal4chain(rbmn0chain.02);
proposes generating matrices of a Bayesian network from a /mn/ object defining a multinormal distribution by expectation and variance, under the assumption that the nodes are in topological order.
mn2gema(mn)
mn2gema(mn)
mn |
Initial |
a list with the /gema/ components $mu
and $li
.
print8gema(mn2gema(rbmn0mn.04));
print8gema(mn2gema(rbmn0mn.04));
returns the expectation and variance of the multinormal normal distribution defined through a marginal subcomponent and a conditional distribution.
mn4joint1condi(lmar, lcon)
mn4joint1condi(lmar, lcon)
lmar |
list defining the distribution of the marginal part with
elements |
lcon |
list defining the distribution of the conditional part (see the Details section). |
The conditional distribution is defined with a list with elements
a
for the constant part of the expectation; b
for the
regression coefficient part of the expectation; and S
for the
residual variance matrix.
A list with elements:
mu |
The expectation vector. |
gamma |
The joint variance matrix. |
that is a /mn/ object.
lcon <- list(a=c(D=2, E=4), b=matrix(1:6, 2, dimnames=list(LETTERS[4:5], LETTERS[1:3])), S=matrix(c(1, 1, 1, 2), 2)); print8mn(mn4joint1condi(rbmn0mn.01, lcon));
lcon <- list(a=c(D=2, E=4), b=matrix(1:6, 2, dimnames=list(LETTERS[4:5], LETTERS[1:3])), S=matrix(c(1, 1, 1, 2), 2)); print8mn(mn4joint1condi(rbmn0mn.01, lcon));
returns the number of different Bayesian networks having n
labelled or not nodes. Non labelled nodes means that nodes are
exchangeable: A -> B
is identical to A <- B
.
nb8bn(n, label=FALSE)
nb8bn(n, label=FALSE)
n |
number of nodes. Must be less or equal to 18. |
label |
Indicates if the nodes must be considered as labelled or not. |
When not labelled nodes, the results were proposed by Sloane in 'the on line encyclopedy of integer sequences' (http://oeis.org/A003087). For labelled nodes, just the application of the recursive formula of Robinson.
Number of Bayesian networks
nb8bn(5) nb8bn(5, TRUE);
nb8bn(5) nb8bn(5, TRUE);
returns a bn.fit
object from a Gaussian nbn
object of
/rbmn/ package.
nbn2bnfit(nbn, onlydag=FALSE)
nbn2bnfit(nbn, onlydag=FALSE)
nbn |
The object to be transformed. |
onlydag |
Indicates if only the DAG must be computed. In that case a /bn/ object of /bnlearn/ |
The resulting bn.fit
(or bn
) object.
returns the chain obtained from nbn
which is supposed to a
chain. If it is not a chain, an error is issued.
nbn2chain(nbn)
nbn2chain(nbn)
nbn |
The /nbn/ object to consider. |
It is advised to use is8nbn8chain
before calling this
function.
The resulting chain
print8chain(nbn2chain(rbmn0nbn.02));
print8chain(nbn2chain(rbmn0nbn.02));
from a /nbn/ object defining a normal Bayesian network, computes the
vector mu
and the matrix li
such that if the vector
E
is a vector of i.i.d. centred and standardized normal, then
mu + li
*
E
has the same distribution as the input /nbn/.
nbn2gema(nbn)
nbn2gema(nbn)
nbn |
|
a list with the two following components: mu
and li
.
identical(nbn2gema(rbmn0nbn.02), rbmn0gema.02);
identical(nbn2gema(rbmn0nbn.02), rbmn0gema.02);
Computes the joint distribution of a /nbn/ with three possible
algorithms according to algo
.
nbn2mn(nbn, algo=3)
nbn2mn(nbn, algo=3)
nbn |
The |
algo |
either |
To be explained if it works
the resulting /mn/ object
print8mn(nbn2mn(rbmn0nbn.05));
print8mn(nbn2mn(rbmn0nbn.05));
returns the proposed /nbn/ with a new topological order without
modifying the joint distribution of all variables.
This allows to
directly find regression formulae within the Gaussian Bayesian
networks.
nbn2nbn(nbn, norder)
nbn2nbn(nbn, norder)
nbn |
The /nbn/ to transform. |
norder |
The topological order to follow. It can be indicated by names or numbers. When not all nodes are included, the resulting /nbn/ is restricted to these nodes after marginalization. |
BE aware that for the moment, no check is made about the topological order and if it is not, the result is FALSE!
The resulting /nbn/.
print8mn(nbn2mn(rbmn0nbn.01, algo=1)); print8mn(nbn2mn(rbmn0nbn.01, algo=2)); print8mn(nbn2mn(rbmn0nbn.01, algo=3)); print8mn(nbn2mn(nbn2nbn(rbmn0nbn.02, c(1, 2, 4, 5, 3)))); print8mn(nbn2mn(nbn2nbn(rbmn0nbn.02, c(4, 1, 2, 3, 5))));
print8mn(nbn2mn(rbmn0nbn.01, algo=1)); print8mn(nbn2mn(rbmn0nbn.01, algo=2)); print8mn(nbn2mn(rbmn0nbn.01, algo=3)); print8mn(nbn2mn(nbn2nbn(rbmn0nbn.02, c(1, 2, 4, 5, 3)))); print8mn(nbn2mn(nbn2nbn(rbmn0nbn.02, c(4, 1, 2, 3, 5))));
from a /nbn/ object defining a normal Bayesian network, returns a
list comprising (i) mm
the vector of the mean of the different
nodes when the parents are nought, (ii) ss
the vector of the
conditional standard deviations and (iii) rr
the matrix of the
regression coefficients of the direct parents (rr[i,j]
contains the regression coefficient of the node j
for its
parents i
or zero when i
is not a parent of j
.
nbn2rr(nbn)
nbn2rr(nbn)
nbn |
|
the resulting list with the three components: mm
, ss
and rr
.
nbn2rr(rbmn0nbn.01);
nbn2rr(rbmn0nbn.01);
returns a /nbn/ object with the same structure as nbn
but all
$mu
are put to zero, all $sigma
to one as well as
$regcof
.
nbn4nbn(nbn)
nbn4nbn(nbn)
nbn |
The |
These coefficient values allows the easy study of the /nbn/ structure.
The resulting nbn
.
print8nbn(nbn4nbn(rbmn0nbn.04));
print8nbn(nbn4nbn(rbmn0nbn.04));
reverse of rmatrix4nbn
but the standard deviations must be
included.
nbn4rmatrix(rmatrix)
nbn4rmatrix(rmatrix)
rmatrix |
The regression coefficient matrix with the standard deviations in the diagonal. |
mu
s are put to nought
A /nbn/ object
print8nbn(nbn4rmatrix(rmatrix4nbn(rbmn0nbn.02)));
print8nbn(nbn4rmatrix(rmatrix4nbn(rbmn0nbn.02)));
returns a nbn
with a given expectation and variance through an
transformation leaving the correlation unchanged.
normalize8nbn(nbn, mu=0, sigma=1)
normalize8nbn(nbn, mu=0, sigma=1)
nbn |
The |
mu |
Imposed expectations. When |
sigma |
The same as |
The transformed nbn
.
print8nbn(normalize8nbn(rbmn0nbn.01));
print8nbn(normalize8nbn(rbmn0nbn.01));
From a chain
object returns one of the possible topological
orders, through a permutation when is.null(ord)
. If not
ord
must be a proposed order to be checked given as a
permutation if is.numeric(ord)
or a vector of ordered names if
is.character(ord)
.
order4chain(chain, ord=NULL)
order4chain(chain, ord=NULL)
chain |
the |
ord |
Indicates what must be done. |
For the moment the ord
option is bad and an error message is
returned when used.
a permutation vector of the nodes of the /nbn/ or a named character with the nodes not having their parents before them; when it is of length zero this means that the check was successful.
order4chain(rbmn0chain.02); order4chain(rbmn0chain.02, order4chain(rbmn0chain.02));
order4chain(rbmn0chain.02); order4chain(rbmn0chain.02, order4chain(rbmn0chain.02));
returns one of the orders of the nodes such as the parents of any
node are less ranked than it when is.null(ord)
. If not check
that the proposed order is either a right permutation
(is.numeric(ord)
) or a vector of node names providing a
topological order (is.character(ord)
).
order4gema(gema, ord=NULL)
order4gema(gema, ord=NULL)
gema |
|
ord |
|
When !is.null(ord)
the order must be an order, if not an error
is issued.
a permutation vector of the nodes of the /gema/ or a named list with the nodes not having their parents before them. That is a topological order.
names(rbmn0gema.04$mu)[order4gema(rbmn0gema.04)];
names(rbmn0gema.04$mu)[order4gema(rbmn0gema.04)];
returns one of the orders of the nodes such as the parents of any
node are less ranked than it when is.null(ord)
. If not check
that the proposed order is either a right permutation
(is.numeric(ord)
) or a vector of node names providing a
topological order (is.character(ord)
).
order4nbn(nbn, ord=NULL)
order4nbn(nbn, ord=NULL)
nbn |
|
ord |
|
When !is.null(ord)
the order must be an order, if not an error
is issued.
a permutation vector of the nodes of the /nbn/ or a named list with the nodes not having their parents before them.
names(rbmn0nbn.04)[order4nbn(rbmn0nbn.04)];
names(rbmn0nbn.04)[order4nbn(rbmn0nbn.04)];
prints a /chain/ object.
print8chain(chain, digits=3)
print8chain(chain, digits=3)
chain |
The |
digits |
when not null, the number of digits for rounding the numerical values. |
See nbn2chain
code for some details about the definition of a
/chain/.
nothing but something is printed
print8chain(rbmn0chain.01); print8chain(rbmn0chain.02); print8chain(rbmn0chain.03);
print8chain(rbmn0chain.01); print8chain(rbmn0chain.02); print8chain(rbmn0chain.03);
prints a /gema/ object completely or a part of it according to
what
specification.
print8gema(gema, what="ml", ordering=NULL, digits=3, printed=TRUE)
print8gema(gema, what="ml", ordering=NULL, digits=3, printed=TRUE)
gema |
|
what |
a |
ordering |
Nodes are given following the indices of "ordering"
if |
digits |
when not null, the number of digits for rounding. |
printed |
|
The gema
is printed or a matrix having nn x ?
is
returned binding which elements are precised in the argument
what
.
print8gema(rbmn0gema.01); print8gema(rbmn0gema.02, "m"); print8gema(rbmn0gema.03, "l", digit=1); print8gema(rbmn0gema.04, printed=FALSE);
print8gema(rbmn0gema.01); print8gema(rbmn0gema.02, "m"); print8gema(rbmn0gema.03, "l", digit=1); print8gema(rbmn0gema.04, printed=FALSE);
prints a /mn/ object completely or a part of it.
print8mn(mn, what="msC", ordering=NULL, digits=3, printed=TRUE)
print8mn(mn, what="msC", ordering=NULL, digits=3, printed=TRUE)
mn |
|
what |
a |
ordering |
Nodes are given following the indices of "ordering"
if |
digits |
when not null, the number of digits for rounding the parameter values. |
printed |
|
The mn
is printed or a matrix having nn x ?
is returned
binding which elements precised in the argument what
.
print8mn(rbmn0mn.01);
print8mn(rbmn0mn.01);
prints a /nbn/ object.
print8nbn(nbn, what="pr", digits=3, ordering=NULL, chk=TRUE)
print8nbn(nbn, what="pr", digits=3, ordering=NULL, chk=TRUE)
nbn |
|
what |
a |
digits |
when not null, the number of digits for rounding. |
ordering |
Nodes are given following the indices of "ordering"
if |
chk |
Check of the consistency of 'nbn' be performed before printing. |
Nothing but but nbn
is printed.
print8nbn(rbmn0nbn.01); print8nbn(rbmn0nbn.03, "pm", order=1:2)
print8nbn(rbmn0nbn.01); print8nbn(rbmn0nbn.03, "pm", order=1:2)
Small examples of adjacency matrices, /nbn/, /chain/, /gema/ and /mn/ objects.
rbmn0chain.01 rbmn0chain.02 rbmn0chain.03 rbmn0nbn.01 rbmn0nbn.02 rbmn0nbn.03 rbmn0nbn.04 rbmn0adja.01 rbmn0adja.02 rbmn0adja.03 rbmn0adja.04 rbmn0mn.01 rbmn0mn.02 rbmn0mn.03 rbmn0mn.04 rbmn0gema.01 rbmn0gema.02 rbmn0gema.03 rbmn0gema.04
rbmn0chain.01 rbmn0chain.02 rbmn0chain.03 rbmn0nbn.01 rbmn0nbn.02 rbmn0nbn.03 rbmn0nbn.04 rbmn0adja.01 rbmn0adja.02 rbmn0adja.03 rbmn0adja.04 rbmn0mn.01 rbmn0mn.02 rbmn0mn.03 rbmn0mn.04 rbmn0gema.01 rbmn0gema.02 rbmn0gema.03 rbmn0gema.04
rbmn0chain.#
objects are chain /nbn/ objects
rbmn0nbn.#
objects are general /nbn/ objects
rbmn0adja.#
objects are adjacency matrices
rbmn0mn.#
objects are /mn/ distributions
rbmn0gema.#
objects are /gema/ generating matrices
Every last numbers (#) refer to the same Gaussian Bayesian networks.
Jean-Baptiste Denis
returns the chain obtained after reversing its node order
reverse8chain(chain)
reverse8chain(chain)
chain |
The chain object to consider. |
The resulting chain
print8chain(rbmn0chain.02); print8chain(reverse8chain(rbmn0chain.02));
print8chain(rbmn0chain.02); print8chain(reverse8chain(rbmn0chain.02));
Eliminates from the adjacency matrix (adja
)all nodes
not breaking the existing links.
Important: the node order in
adja
must be topological.
rm8nd4adja(adja, nodes)
rm8nd4adja(adja, nodes)
adja |
The relation matrix to be consider (same format as those
provided by the function |
nodes |
Numeric or character vector providing the node numbers to use for the generation of the subset. |
When a node is removed, all its parents become parent of its children.
The reduced adjacency matrix.
rm8nd4adja(rbmn0adja.04, "1.1");
rm8nd4adja(rbmn0adja.04, "1.1");
returns a /nbn/ object deduced from an original /nbn/ by integrating on a given subset of nodes.
rm8nd4nbn(nbn, nodes)
rm8nd4nbn(nbn, nodes)
nbn |
The |
nodes |
|
The transformation is made through the associated joint distributions
for the probabilities and with the help of the function
rm8nd4adja
for the relationships.
The resulting nbn
.
rm8nd4nbn(rbmn0nbn.04, "1.1");
rm8nd4nbn(rbmn0nbn.04, "1.1");
returns a dimnamed matrix indicating with rho
an arc from row
to column nodes (0 everywhere else) where rho
is the
regression coefficient. Also conditional standard deviations can be
introduced as diagonal elements but mu
coefficient are lost...
It is advisable to normalize the /nbn/ first.
rmatrix4nbn(nbn, stdev=TRUE)
rmatrix4nbn(nbn, stdev=TRUE)
nbn |
The initial |
stdev |
Indicates if the standard deviations must placed in the diagonal positions. |
A dimnamed matrix
rmatrix4nbn(rbmn0nbn.02); (rmatrix4nbn(rbmn0nbn.02, FALSE)>0)*1;
rmatrix4nbn(rbmn0nbn.02); (rmatrix4nbn(rbmn0nbn.02, FALSE)>0)*1;
returns a matrix of simulated values with the variable in columns and the simulations in rows.
simulate8gema(gema, nbs)
simulate8gema(gema, nbs)
gema |
The |
nbs |
number of simulations to return. |
Just the application of the standard formula to a white noise.
Variables names are taken from those of gema$mu
, when these
does not exist, standard ones are provided.
A matrix of size : nbs x length(gema$mu)
simulate8gema(rbmn0gema.01, 10);
simulate8gema(rbmn0gema.01, 10);
returns a matrix of simulated values with the variable in columns and the simulations in rows.
simulate8gmn(loi, cova, nbs, tol=1e-7)
simulate8gmn(loi, cova, nbs, tol=1e-7)
loi |
list defining the distribution of the initial vector with
|
cova |
Values to give to the covariables. Must be a matrix with
|
nbs |
number of simulations to return. |
tol |
tolerance value to be transmitted to |
Just a call to the function simulate8mn
, adding the terms to
the expectation due to the regression...
A matrix of size : nbs x length(loi$mu)
loi <- list(mu=c(D=2, E=4), rho=matrix(1:6, 2, dimnames=list(LETTERS[4:5], LETTERS[1:3])), gamma=matrix(c(1, 1, 1, 2), 2)); cova <- matrix(runif(36), 12, dimnames=list(NULL, LETTERS[1:3])); print(simulate8gmn(loi, cova, 12));
loi <- list(mu=c(D=2, E=4), rho=matrix(1:6, 2, dimnames=list(LETTERS[4:5], LETTERS[1:3])), gamma=matrix(c(1, 1, 1, 2), 2)); cova <- matrix(runif(36), 12, dimnames=list(NULL, LETTERS[1:3])); print(simulate8gmn(loi, cova, 12));
returns a matrix of simulated values with the variable in columns and the simulations in rows.
simulate8mn(mn, nbs, tol=1e-7)
simulate8mn(mn, nbs, tol=1e-7)
mn |
list defining the distribution of the initial vector with
|
nbs |
number of simulations to return. |
tol |
tolerance value to be transmitted to |
Just a call to the basic function mvrnorm
. Names of the
variables are taken from those of mn$mu
, when these does not
exist, standard ones are provided.
A matrix/data frame of size : nbs x length(mn$mu)
print(simulate8mn(rbmn0mn.01, 12));
print(simulate8mn(rbmn0mn.01, 12));
returns a matrix of simulated values with the variable in columns and the simulations in rows.
simulate8nbn(nbn, nbs)
simulate8nbn(nbn, nbs)
nbn |
The |
nbs |
number of simulations to return. |
Just the sequential simulations of the nodes
A matrix of size : nbs x length(nbn)
simulate8nbn(rbmn0nbn.01, 10);
simulate8nbn(rbmn0nbn.01, 10);
From a chain
object returns a named character precising the
role of each node: "r" for root, "c" for collider, "t" for
transmitter and "l" for leaf.
state4chain(chain)
state4chain(chain)
chain |
the |
a character of the states named with node names.
state4chain(rbmn0chain.01); state4chain(rbmn0chain.03);
state4chain(rbmn0chain.01); state4chain(rbmn0chain.03);
returns a character(1)
describing the dag of the nbn under the
string form.
string7dag4nbn(nbn, sep=";")
string7dag4nbn(nbn, sep=";")
nbn |
The nbn. |
sep |
Separation sign between parents after the conditioning
sign ( |
A character(1)
.
string7dag4nbn(rbmn0nbn.01); string7dag4nbn(rbmn0nbn.04, sep=", ");
string7dag4nbn(rbmn0nbn.01); string7dag4nbn(rbmn0nbn.04, sep=", ");
returns the precision matrix from the variance preserving possible variable names
var2pre(ma)
var2pre(ma)
ma |
The variance matrix. |
Non full rank matrices are accepted, a generalized inverse is returned and a warning is issued.
The precision matrix
var2pre(rbmn0mn.04$gamma);
var2pre(rbmn0mn.04$gamma);