Learn R Programming

⚠️There's a newer version (0.7.8) of this package.Take me there.

dbnR

An implementation of Gaussian dynamic Bayesian networks (GDBN) structure learning and inference based on Marco Scutari’s package bnlearn (http://www.bnlearn.com/). The structure learning algorithm implemented is a variation on Ghada Trabelsi’s dynamic max-min hill climbing (https://tel.archives-ouvertes.fr/tel-00996061/document). The inference is performed either via the particle filtering offered by bnlearn or by doing exact inference over the multivariate Gaussian equivalent of a net implemented in this package. A visualization tool is implemented for GDBNs and bnlearn’s BNs via the visNetwork package (https://github.com/datastorm-open/visNetwork).

Current development

As of today (17/03/2020), the main functionality of the package is running and working. In order of importance, the primary objectives are:

  • To add examples of the main functionality of the package and fix R CMD checks in order to upload it to CRAN
  • To add an automatically generated shiny interface of the net
  • To run testing of some known unexpected behaviours on bn and dbn prediction (e.g. providing all variables as objective variables)
  • To add the possibility of learning nets with only certain previous lags instead of all of them. For example, a dbn with only the time slices for t_0 and t_12, or one with t_0, t_3 and t_4.
  • To provide a visualization tool of some sub-network. Many times, you end up with quite a big BN or DBN and you are interested in only a few of the nodes. We sketched a function to plot the Markov blanket of a node, but it needs further parametrization.

For now, the dbn.fit object as an extension of bnlearn’s bn.fit object will stay the same except for the “mu” and “sigma” attributes added to it. This way, it remains easy to call bnlearn’s methods on the dbn.fit object and I can store the MVN transformation inside the same object. Not an elegant solution, but its simplicity is enough.

Getting Started

Prerequisites

This package requires R ≥ 3.6.1 to work properly. It’s possible to make it work for older versions of R and of each of the packages by fiddling with the DESCRIPTION file, although this is not recommended.

The bnlearn and data.table packages, among others, are required for this package to work. They will be installed automatically when installing this package. They can also be installed manually via CRAN with the command

install.packages(c("bnlearn", "data.table"))

The packages visNetwork, magrittr and grDevices are optional for the visualization tool. They will only be required if you want to use it.

Installing

As of today, the easiest way of installing dbnR is via the install_github function in the devtools package. The commands you need to run are

library(devtools)
devtools::install_github("dkesada/dbnR")

This will install the required dependencies if they are not available. After this, you will be ready to use the package.

Basic examples

To get the structure of a GDBN from a dataset, you need to use the function learn_dbn_struc

library(dbnR)
data(motor)

size <- 3
dt_train <- motor[200:2500]
dt_val <- motor[2501:3000]
net <- learn_dbn_struc(dt_train, size)

The dt argument has to be either a data.frame or a data.table of numeric columns, in the example we use the sample dataset included in the package. The size argument determines the number of time slices that your net is going to have, that is, the Markovian order of the net. A Markovian order of 2 means that your data in the present is independent of the past given the previous time slice. If your case doesn’t meet this criteria, the size of the net can be increased, to take into account more past time slices in the inference. In our function, Markovian order = size - 1. The function returns a list with the learned structure and the folded dataset with the extended rows.

Once the structure is learnt, it can be plotted and used to learn the parameters

plot_dynamic_network(net)

f_dt_train <- fold_dt(dt_train, size)
fit <- fit_dbn_params(net, f_dt_train, method = "mle")

After learning the net, two different types of inference can be performed: point-wise inference over a dataset and forecasting to some horizon. Point-wise inference uses the folded dt to try and predict the objective variables in each row. Forecasting to some horizon, on the other hand, tries to predict the behaviour in the future M instants given some initial evidence of the variables.

License

This project is licensed under the GPL-3 License, following on bnlearn’s GPL(≥ 2) license.

References

Copy Link

Version

Install

install.packages('dbnR')

Monthly Downloads

788

Version

0.3.3

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

David Quesada

Last Published

March 20th, 2020

Functions in dbnR (0.3.3)

fit_dbn_params

Fits a markovian n DBN model
node_levels

Defines a level for every node in the net
fold_dt

Widens the dataset to take into account the t previous time slices
plot_dynamic_network

Plots a dynamic Bayesian network in a hierarchical way
time_rename

Renames the columns in a data.table so that they end in '_t_0'
exact_inference

Performs exact inference forecasting with the GDBN over a data set
dynamic_ordering

Gets the ordering of a single time slice in a DBN
predict_dt

Performs inference over a test data set with a GBN
expand_time_nodes

Extends the names of the nodes in t_0 to t_(max-1)
exact_prediction_step

Performs exact inference in a time slice of the dbn
add_attr_to_fit

Adds the mu vector and sigma matrix as attributes to the bn.fit or dbn.fit object
forecast_ts

Performs forecasting with the GDBN over a data set
fold_dt_rec

Widens the dataset to take into account the t previous time slices
plot_network

Plots a Bayesian networks in a hierarchical way
motor

Multivariate time series dataset on the temperature of an electric motor
learn_dbn_struc

Learns the structure of a markovian n DBN model from data
merge_nets

Merges and replicates the arcs in the static BN into all the time-slices in the DBN
predict_bn

Performs inference over a fitted GBN
mvn_inference

Performs inference over a multivariate normal distribution
calc_mu_cpp

Calculate the mu vector of means of a Gaussian linear network. This is the C++ backend of the function.
calc_mu

Calculate the mu vector of means of a Gaussian linear network. Front end of a C++ function.
approx_prediction_step

Performs approximate inference in a time slice of the dbn
approximate_inference

Performs approximate inference forecasting with the GDBN over a data set
acc_successions

Returns a vector with the number of consecutive nodes in each level
calc_sigma

Calculate the sigma covariance matrix of a Gaussian linear network. Front end of a C++ function.
calc_sigma_cpp

Calculate the sigma covariance matrix of a Gaussian linear network. This is the C++ backend of the function.
check_time0_formatted

Checks if the vector of names are time formatted to t0
create_blacklist

Creates the blacklist of arcs from a folded data.table