Learn R Programming

abn (version 3.1.1)

fitAbn: Fit an additive Bayesian network model

Description

Fits an additive Bayesian network to observed data and is equivalent to Bayesian or information-theoretic multi-dimensional regression modeling. Two numerical options are available in the Bayesian settings, standard Laplace approximation or else an integrated nested Laplace approximation provided via a call to the R INLA library (see r-inla.org - this is not hosted on CRAN).

Usage

fitAbn(object = NULL,
       dag = NULL,
       data.df = NULL,
       data.dists = NULL,
       method = NULL,
       group.var = NULL,
       adj.vars = NULL,
       cor.vars = NULL,
       centre = TRUE,
       compute.fixed = FALSE,
       control = NULL,
       verbose = FALSE,
       debugging = FALSE,
       ...)

fitAbn.bayes( dag = NULL, data.df = NULL, data.dists = NULL, group.var = NULL, cor.vars = NULL, centre = TRUE, compute.fixed = FALSE, control = fit.control(method = "bayes"), mylist = NULL, grouped.vars = NULL, group.ids = NULL, force.method = NULL, verbose = FALSE, debugging = FALSE )

fitAbn.mle( dag = NULL, data.df = NULL, data.dists = NULL, group.var = NULL, grouped.vars = NULL, group.ids = NULL, adj.vars = NULL, cor.vars = NULL, centre = TRUE, control = fit.control(method = "mle"), verbose = FALSE, debugging = FALSE )

regressionLoop( i = NULL, dag = NULL, data.df = NULL, data.df.multi = NULL, data.dists = NULL, group.var = NULL, grouped.vars = NULL, group.ids = NULL, control = NULL, nvars = NULL, nobs = NULL, dag.multi = NULL, verbose = NULL )

Value

An object of class abnFit. A named list. One entry for each of the variables in data.df (excluding the grouping variable, if present) which contains an estimate of the log marginal likelihood for that individual node. An entry "mlik" which is the total log marginal likelihood for the full ABN model. A vector of error.codes - non-zero if a numerical error or warning occurred, and a vector error.code.desc giving a text description of the error. A list modes, which contains all the mode estimates for each parameter at each node. A vector called Hessian accuracy, which is the estimated local error in the log marginal likelihood for each node. If compute.fixed=TRUE then a list entry called marginals which contains a named entry for every parameter in the ABN and each entry in this list is a two-column matrix where the first column is the value of the marginal parameter, say x, and the second column is the respective density value, pdf(x). Also, a list called marginal.quantiles is produced, giving the quantiles for each marginal posterior distribution.

list

Arguments

object

an object of class abnLearned produced by mostProbable, searchHeuristic or searchHillClimber.

dag

a matrix or a formula statement (see details) defining the network structure, a directed acyclic graph (DAG), see details for format. Note that column names and row names must be set up.

data.df

a data frame containing the data used for learning the network, binary variables must be declared as factors, and no missing values all allowed in any variable.

data.dists

a named list giving the distribution for each node in the network, see details.

method

if NULL, takes method of object, otherwise "bayes" or "mle" for the method to be used, see details.

group.var

only applicable for mixed models and gives the column name in data.df of the grouping variable (which must be a factor denoting group membership).

adj.vars

a character vector giving the column names in data.df for which the network score has to be adjusted for, see details.

cor.vars

a character vector giving the column names in data.df for which a mixed model should be used (method = 'bayes' only).

centre

should the observations in each Gaussian node first be standardised to mean zero and standard deviation one.

compute.fixed

a logical flag, set to TRUE for computation of marginal posterior distributions, see details.

control

a list of control parameters. See fit.control for the names of the settable control values and their effect.

verbose

if TRUE then provides some additional output, in particular the code used to call INLA, if applicable.

debugging

if TRUE and method = 'mle' this enables to step into the for-loop.

...

additional arguments passed for optimization.

mylist

result returned from check.valid.data.

grouped.vars

result returned from check.valid.groups. Column indexes of all variables which are affected from grouping effect.

group.ids

result returned from check.valid.groups. Vector of group allocation for each observation (row) in 'data.df'.

force.method

"notset", "INLA" or "C". This is specified in buildScoreCache(control=list(max.mode.error=...)).

i

number of child-node (mostly corresponds to child node index e.g. in dag).

data.df.multi

extended data.df for one-hot-encoded multinomial variables.

nvars

number of variables in data.dists.

nobs

number of observations in data.df.

dag.multi

extended dag for one-hot-encoded multinomial variables.

Functions

  • fitAbn.bayes(): Internal function called by fitAbn.

  • fitAbn.mle(): Internal function called by fitAbn.

  • regressionLoop(): Internal function called by fitAbn.mle().

Author

Fraser Iain Lewis and Gilles Kratzer.

Details

If method="Bayes":

The procedure fitAbn fits an additive Bayesian network model to data where each node (variable - a column in data.df) can be either: presence/absence (Bernoulli); continuous (Gaussian); or an unbounded count (Poisson). Multinomial distributions are only supported with method = "mle" (see below). The model comprises of a set of conditionally independent generalized linear regressions with or without random effects. Internal code is used by default for numerical estimation in nodes without random effects, and INLA is the default for nodes with random effects. This default behavior can be overridden using control=list(max.mode.error=...). The default is max.mode.error=10, which means that the modes estimated from INLA output must be within 10\ Otherwise, the internal code is used rather than INLA. To force the use of INLA on all nodes, use max.mode.error=100, which then ignores this check, to force the use of internal code then use max.mode.error=0. For the numerical reliability and perform of abn see https://r-bayesian-networks.org/. Generally speaking, INLA can be swift and accurate, but in several cases, it can perform very poorly and so some care is required (which is why there is an internal check on the modes). Binary variables must be declared as factors with two levels, and the argument data.dists must be a list with named arguments, one for each of the variables in data.df (except a grouping variable - if present!), where each entry is either "poisson","binomial", "multinomial" or "gaussian", see examples below. The "poisson" and "binomial" distributions use log and logit link functions, respectively. Note that "binomial" here actually means only binary, one Bernoulli trial per row in data.df.

If the data are grouped into correlated blocks - wherein a standard regression context a mixed model might be used - then a network comprising of one or more nodes where a generalized linear mixed model is used (but limited to only a single random effect). This is achieved by specifying parameters group.var and cor.vars. Where the former defines the group membership variable, which should be a factor indicating which observations belong to the same grouping. The parameter cor.vars is a character vector that contains the names of the nodes for which a mixed model should be used. This is not yet implemented with method = 'mle'. For example, in some problems, it may be appropriate for all variables (except group.var) in data.df to be parametrized as a mixed model while in others it may only be a single variable for which grouping adjustment is required (as the remainder of variables are covariates measured at group level).

In the network structure definition, dag, each row represents a node in the network, and the columns in each row define the parents for that particular node, see the example below for the specific format. The dag can be provided using a formula statement (similar to GLM). A typical formula is ~ node1|parent1:parent2 + node2:node3|parent3. The formula statement have to start with ~. In this example, node1 has two parents (parent1 and parent2). node2 and node3 have the same parent3. The parents names must match those given in data.df. : is the separator between either children or parents, | separates children (left side) and parents (right side), + separates terms, . replaces all the variables in data.df.

If compute.fixed=TRUE then the marginal posterior distributions for all parameters are computed. Note the current algorithm used to determine the evaluation grid is rather crude and may need to be manually refined using variate.vec (one parameter at a time) for publication-quality density estimates. Note that a manual grid can only be used with internal code and not INLA (which uses its own grid). The end points are defined as where the value of the marginal density drops below a given threshold pdf.min. When estimating the log marginal likelihood in models with random effects (using internal code rather than INLA), an attempt is made to minimize the error by comparing the estimates given between a 3pt and 5pt rule when estimating the Hessian in the Laplace approximation. The modes used in each case are identical. The first derivatives are computed using gsl's adaptive finite difference function, and this is embedding inside the standard 3pt and 5pt rules for the second derivatives. In all cases, a central difference approximation is tried first with a forward difference being a fall back (as the precision parameters are strictly positive). The error is minimized through choosing an optimal step size using gsl's Nelder-Mead optimization, and if this fails, (e.g., is larger than max.hessian.error) then the Brent-Dekker root bracketing method is used as a fallback. If the error cannot be reduced to below max.hessian.error, then the step size, which gave the lowest error during the searches (across potentially many different initial bracket choices), is used for the final Hessian evaluations in the Laplace approximation.

If method="mle":

The procedure fitAbn with the argument method= "mle" fits an additive Bayesian network model to data where each node (variable - a column in data.df) can be either: presence/absence (Bernoulli); continuous (Gaussian); an unbounded count (Poisson); or a discrete variable (Multinomial). The model comprises of a set of conditionally independent generalized linear regressions with or without adjustment. Binary and discrete variables must be declared as factors and the argument data.dists must be a list with named arguments, one for each of the variables in data.df, where each entry is either "poisson","binomial", "multinomial" or "gaussian", see examples below. The "poisson" and "binomial" distributions use log and logit link functions, respectively. Note that "binomial" here actually means only binary, one Bernoulli trial per row in data.df.

If the data are grouped into correlated blocks - wherein a standard regression context a mixed-effect model might be used - then a network comprising of one or more nodes where a generalized linear mixed model is used (but limited to only a single random intercept). This is achieved by specifying parameter group.var (cor.vars as with method = "bayes" is not yet implemented with method = "mle"). The parameter group.var defines the group membership variable, which should be a factor indicating which observations belong to the same grouping. This corresponds to "1|group.var" in the formula notation of e.g. lme4.

In the context of fitAbn adjustment means that irrespective to the adjacency matrix the adjustment variable set (adj.vars) will be add as covariate to every node defined by cor.vars.

In the network structure definition, dag, each row represents a node in the network, and the columns in each row define the parents for that particular node, see the example below for the specific format. The dag can be provided using a formula statement (similar to GLM). A typical formula is ~ node1|parent1:parent2 + node2:node3|parent3. The formula statement have to start with ~. In this example, node1 has two parents (parent1 and parent2). node2 and node3 have the same parent3. The parents names have to exactly match those given in data.df. : is the separator between either children or parents, | separates children (left side) and parents (right side), + separates terms, . replaces all the variables in data.df.

The Information-theoretic based network scores used in fitAbn with argument method="mle" are the maximum likelihood (mlik, called marginal likelihood in this context as it is computed node wise), the Akaike Information Criteria (aic), the Bayesian Information Criteria (bic) and the Minimum distance Length (mdl). The classical definitions of those metrics are given in Kratzer and Furrer (2018).

The numerical routine is based on an iterative scheme to estimate the regression coefficients. The Iterative Reweighed Least Square (IRLS) programmed using Rcpp/RcppArmadrillo. One hard coded feature of fitAbn with argument method="mle" is a conditional use of a bias reduced binomial regression when a classical Generalized Linear Model (GLM) fails to estimate the maximum likelihood of the given model accurately. Additionally, a QR decomposition is performed to check for rank deficiency. If the model is rank deficient and the BR GLM fails to estimate it, then predictors are sequentially removed. This feature aims at better estimating network scores when data sparsity is present.

A special care should be taken when interpreting or even displaying p-values computed with fitAbn. Indeed, the full model is already selected using goodness of fit metrics based on the (same) full dataset.

The control argument is a list with separate arguments for the Bayesian and MLE implementation. See fit.control for details.

References

Kratzer, G., Lewis, F.I., Comin, A., Pittavino, M. and Furrer, R. (2019). "Additive Bayesian Network Modelling with the R Package abn". arXiv preprint arXiv:1911.09006.

Kratzer, G., and Furrer, R., 2018. Information-Theoretic Scoring Rules to Learn Additive Bayesian Network Applied to Epidemiology. Preprint; Arxiv: stat.ML/1808.01126.

Lewis, F. I., and McCormick, B. J. J. (2012). Revealing the complexity of health determinants in resource poor settings. American Journal Of Epidemiology. DOI:10.1093/aje/KWS183.

Further information about abn can be found at: r-bayesian-networks.org.

See Also

buildScoreCache

Other fitAbn: fit.control()

Other Bayes: buildScoreCache(), calc.node.inla.glm(), calc.node.inla.glmm(), getmarginals()

Examples

Run this code
if (FALSE) {
## Built-in dataset with a subset of cols
mydat <- ex0.dag.data[, c("b1", "b2", "b3", "g1", "b4", "p2", "p4")]

## setup distribution list for each node
mydists <- list(b1 = "binomial",
                b2 = "binomial",
                b3 = "binomial",
                g1 = "gaussian",
                b4 = "binomial",
                p2 = "poisson",
                p4 = "poisson")

## Null model - all independent variables
mydag_empty <- matrix(0, nrow = 7, ncol = 7)
colnames(mydag_empty) <- rownames(mydag_empty) <- names(mydat)

## Now fit the model to calculate its goodness-of-fit
myres <- fitAbn(dag = mydag_empty,
                data.df = mydat,
                data.dists = mydists)

## Log-marginal likelihood goodness-of-fit for complete DAG
print(myres$mlik)

## fitAbn accepts also the formula statement
myres <- fitAbn(dag = ~ b1 | b2 + b2 | p4:g1 + g1 | p2 + b3 | g1 + b4 | b1 + p4 | g1,
                data.df = mydat,
                data.dists = mydists)
print(myres$mlik) # a much weaker fit than full independence DAG

# Plot the DAG via Rgraphviz
plot(myres)

## Or equivalently using the formula statement, with plotting
## Now repeat but include some dependencies first
mydag <- mydag_empty
mydag["b1", "b2"] <- 1 # b1<-b2 and so on
mydag["b2", "p4"] <- mydag["b2", "g1"] <- mydag["g1", "p2"] <- 1
mydag["b3", "g1"] <- mydag["b4", "b1"] <- mydag["p4", "g1"] <- 1
myres_alt <- fitAbn(dag = mydag,
                    data.df = mydat,
                    data.dists = mydists)
plot(myres_alt)

## -----------------------------------------------------------------------------
## This function contains an MLE implementation accessible through a method
## parameter use built-in simulated data set
## -----------------------------------------------------------------------------
myres_mle <- fitAbn(dag = ~ b1 | b2 + b2 | p4 + g1 + g1 | p2 + b3 | g1 + b4 | b1 + p4 | g1,
                    data.df = mydat,
                    data.dists = mydists,
                    method = "mle")

## Print the output for mle first then for Bayes:
print(myres_mle)
plot(myres_mle)

print(myres)
plot(myres)

## This is a basic plot of some posterior densities. The algorithm used for
## selecting density points is quite straightforward, but it might result
## in a sparse distribution. Therefore, we also recompute the density over
## an evenly spaced grid of 50 points between the two endpoints that had
## a minimum PDF at f = min.pdf.
## Setting max.mode.error = 0 forces the use of the internal C code.
myres_c <- fitAbn(dag = mydag,
                  data.df = mydat,
                  data.dists = mydists,
                  compute.fixed = TRUE,
                  control = list(max.mode.error = 0))

print(names(myres_c$marginals)) # gives all the different parameter names

## Repeat but use INLA for the numerics using max.mode.error = 100
## as using internal code is the default here rather than INLA
myres_inla <- fitAbn(dag = mydag,
                     data.df = mydat,
                     data.dists = mydists,
                     compute.fixed = TRUE,
                     control = list(max.mode.error = 100))

## Plot posterior densities
default_par <- par(no.readonly = TRUE) # save default par settings
par(mfrow = c(2, 2), mai = c(.7, .7, .2, .1))
plot(myres_c$marginals$b1[["b1 | (Intercept)"]], type = "l", xlab = "b1 | (Intercept)")
lines(myres_inla$marginals$b1[["b1 | (Intercept)"]], col = "blue")
plot(myres_c$marginals$b2[["b2 | p4"]], type = "l", xlab = "b2 | p4")
lines(myres_inla$marginals$b2[["b2 | p4"]], col = "blue")
plot(myres_c$marginals$g1[["g1 | precision"]], type = "l", xlab = "g1 | precision")
lines(myres_inla$marginals$g1[["g1 | precision"]], col = "blue")
plot(myres_c$marginals$b4[["b4 | b1"]], type = "l", xlab = "b4 | b1")
lines(myres_inla$marginals$b4[["b4 | b1"]], col = "blue")
par(default_par) # reset par settings

## An elementary mixed model example using built-in data specify DAG,
## only two variables using a subset of variables from ex3.dag.data
## both variables are assumed to need (separate) adjustment for the
## group variable, i.e., a binomial GLMM at each node

mydists <- list(b1 = "binomial",
                b2 = "binomial")

## Compute marginal likelihood - use internal code via max.mode.error=0
## as using INLA is the default here.
## Model where b1 <- b2
myres_c <- fitAbn(dag = ~b1 | b2,
                  data.df = ex3.dag.data[, c(1, 2, 14)],
                  data.dists = mydists,
                  group.var = "group",
                  cor.vars = c("b1", "b2"),
                  control = list(max.mode.error = 0))
print(myres_c) # show all the output

## compare mode for node b1 with glmer(), lme4::glmer is automatically attached.

## Now for marginals - INLA is strongly preferable for estimating marginals for
## nodes with random effects as it is far faster, but may not be reliable
## see https://r-bayesian-networks.org/

## INLA's estimates of the marginals, using high n.grid = 500
## as this makes the plots smoother - see below.
myres_inla <- fitAbn(dag = ~b1 | b2,
                   data.df = ex3.dag.data[, c(1, 2, 14)],
                  data.dists = mydists,
                  group.var = "group",
                  cor.vars = c("b1", "b2"),
                  compute.fixed = TRUE,
                  n.grid = 500,
                  control = list(max.mode.error = 100,
                                 max.hessian.error = 10E-02))

## this is NOT recommended - marginal density estimation using fitAbn in
## mixed models is really just for diagnostic purposes, better to use
## fitAbn.inla() here; but here goes... be patient
myres_c <- fitAbn(dag = ~b1 | b2,
                  data.df = ex3.dag.data[, c(1, 2, 14)],
                  data.dists = mydists,
                  group.var = "group",
                  cor.vars = c("b1", "b2"),
                  compute.fixed = TRUE,
                  control = list(max.mode.error = 0,
                                 max.hessian.error = 10E-02))

## compare marginals between internal and INLA.
default_par <- par(no.readonly = TRUE) # save default par settings
par(mfrow = c(2, 3))
# 5 parameters - two intercepts, one slope, two group level precisions
plot(myres_inla$marginals$b1[[1]], type = "l", col = "blue")
lines(myres_c$marginals$b1[[1]], col = "brown", lwd = 2)
plot(myres_inla$marginals$b1[[2]], type = "l", col = "blue")
lines(myres_c$marginals$b1[[2]], col = "brown", lwd = 2)
# the precision of group-level random effects
plot(myres_inla$marginals$b1[[3]], type = "l", col = "blue", xlim = c(0, 2))
lines(myres_c$marginals$b1[[3]], col = "brown", lwd = 2)
plot(myres_inla$marginals$b2[[1]], type = "l", col = "blue")
lines(myres_c$marginals$b2[[1]], col = "brown", lwd = 2)
plot(myres_inla$marginals$b2[[1]], type = "l", col = "blue")
lines(myres_c$marginals$b2[[1]], col = "brown", lwd = 2)
# the precision of group-level random effects
plot(myres_inla$marginals$b2[[2]], type = "l", col = "blue", xlim = c(0, 2))
lines(myres_c$marginals$b2[[2]], col = "brown", lwd = 2)
par(default_par) # reset par settings

### these are very similar although not exactly identical

## use internal code but only to compute a single parameter over a specified
## grid.
## This can be necessary if the simple auto grid finding functions does
## a poor job.
myres_c <- fitAbn(dag = ~b1 | b2,
                  data.df = ex3.dag.data[, c(1, 2, 14)],
                  data.dists = mydists,
                  group.var = "group",
                  cor.vars = c("b1", "b2"),
                  centre = FALSE,
                  compute.fixed = TRUE,
                  control = list(marginal.node = 1,
                                 marginal.param = 3, # precision term in node 1
                                 variate.vec = seq(0.05, 1.5, len = 25),
                                 max.hessian.error = 10E-02))

default_par <- par(no.readonly = TRUE) # save default par settings
par(mfrow = c(1, 2))
plot(myres_c$marginals$b1[[1]], type = "l", col = "blue") # still fairly sparse
# An easy way is to use spline to fill in the density without recomputing other
# points provided the original grid is not too sparse.
plot(spline(myres_c$marginals$b1[[1]], n = 100), type = "b", col = "brown")
par(default_par) # reset par settings
}

Run the code above in your browser using DataLab