This page describes some of the R packages for graphical modelling that I have been involved with. There are many more packages for grapical modelling, and the CRAN Task View gRaphical Models in R lists many of these.

Packages

gRain

gRim

  • Graphical interaction models (graphical log-linear models for discrete data, Gaussian graphical models for continuous data and Mixed interaction models for mixed data).
  • See Højsgaard, Edwards, Lauritzen (2012) Graphical Modelling with R. Springers UseR! series.

gRbase

  • Efficient graph algorithms, functions for easy creation of graphs, functions for manipulation of highdimensional tables, data relevant to graphical models.
  • Many graph modelling packages depend on gRbase, but gRbase itself provides only limited modelling facilities.
  • The reference to gRbase is: Dethlefsen, C., Højsgaard, S. (2005) A Common Platform for Graphical Models in R: The gRbase Package. Journal of Statistical Software Vol. 14, No. 17.

gRc

Installation

  1. The packages listed above use the graph, RBGL and Rgraphviz packages. These packages are on bioconductor. To enable that these are installed when installing the packates above run

    setRepositories()

    and make sure that BioC software is checked.

  2. Then install the graphical modelling packages from CRAN with:

    install.packages("gRbase", dependencies=TRUE); 
    install.packages("gRain", dependencies=TRUE); 
    install.packages("gRim", dependencies=TRUE)

Books

  • Højsgaard, Edwards, Lauritzen (2012) Graphical Modelling with R. Springers UseR! series. The book contains several illustrations of the use of the gRbase, gRain and gRim packages.

  • Errata list for Graphical Modelling with R.

  • See also Lauritzen (1996) Graphical Models. Oxford University Press

Examples, scripts and notes

  • Specification of conditional probability tables (CPTs) here.
  • Reference card for working with arrays in gRbase here.

Tutorial and short courses

  • Slides from tutorial on graphical models and Bayesian networks (using gRain and gRim) at useR!2015 i Aalborg are available here. The tutorial was organized together with Therese Graversen, University of Copenhagen.

Development versions

Development versions of the packages reside on github. To use these versions, PLEASE install the CRAN versions FIRST (see section on installation) to get dependencies right and then AFTERWARDS install the development versions using:

devtools::install_github("hojsgaard/gRbase")
  • Notice that for this to succeed you will need tools for building R packages from sources on your computer. For windows users this translates to that you will have to install Rtools which can be obtained from https://cran.r-project.org/bin/windows/Rtools/. Just follow the suggestions of the installer.

  • Notice that the packages are interdependent: For example, gRain depends on gRbase. Therefore, to use the development version of e.g. gRain you must also install the development version of gRbase.

FAQ (frequently asked questions)

  • Q: Is it possible to specify likelihood evidence (also called virtual evidence) in gRain?

  • A: Yes, as of version 1.1-2 this has been implemented. The function to use is setEvidence(). A vignette on the topic has also been added.
    Please report unexpected behaviour.

  • Q: I want to build a Bayesian network with 80.000 nodes. Can I do so with gRain?
  • A: Work has been done on supporting large networks. Please report sucesses and failures.

  • Q: Does gRain have support for Bayesian networks for variables that are (multivariate) normal? Or for mixtures of discrete and normal variables?
  • A: No. Implementation for the multivariate normal distribution is straight forward (if you work with the canoncical rather than the moment parameters). Any contribution would be most welcome. For mixed variables, the only algorithm I know of is numerically unstable.
  • Q: Does gRain have support for Bayesian networks for variables that are not discrete (and with a finite state space)?
  • A: Not in full generality. However, using the likelihood evidence facilities, one can work with some types of non-discrete variables.

Reporting unexpected behaviour

When reporting unexpected behaviours, bugs etc. PLEASE supply:

When reporting unexpected behaviours, bugs etc. PLEASE supply:

  • A small reproducible example in terms of a short code fragment.

  • The data. The preferred way of sending the data “mydata” is to copy and paste the result from running dput(mydata).

  • The result of running the sessionInfo() function.