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 lists many of these.


The packages listed below use the graph, RBGL and Rgraphviz packages. These packages are on bioconductor. Therefore, typing e.g. install.packages("gRbase") will not work unless graph, RBGL and Rgraphviz are already installed on your system.

However, there exists an easy remedy: Run these two lines

if (!require("BiocManager")) install.packages("BiocManager")
BiocManager::install(c("gRbase", "gRain", "gRim"), force=TRUE)




  • 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.


  • 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.


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:

  • 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. 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 (most likely) also install the development version of gRbase.


  • 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

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 - at least in principle - straight forward although there can non-trivial numerical issues.

  • 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 bugs etc.

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.