CRAN Version Downloads Total

bgms: Bayesian Analysis of Graphical Models

The R package bgms provides tools for Bayesian analysis of graphical models describing networks of variables. The package uses Markov chain Monte Carlo methods combined with a pseudolikelihood approach to estimate the posterior distribution of model parameters. Gibbs variable selection (George and McCulloch 1993) is used to model the underlying network structure of the graphical model. By imposing a discrete spike and slab prior on the pairwise interactions, it is possible to shrink the interactions to exactly zero. The Gibbs sampler embeds a Metropolis approach for mixtures of mutually singular distributions (Gottardo and Raftery 2008) to account for the discontinuity at zero. The goal is to provide these tools for Markov Random Field (MRF) models for a wide range of variable types in the bgms package, and it currently provides them for analyzing networks of binary and/or ordinal variables (Marsman and Haslbeck 2023).

Why use Markov Random Fields?

Multivariate analysis using graphical models has received much attention in the recent psychological and psychometric literature (Robinaugh et al. 2020; Marsman and Rhemtulla 2022; Contreras et al. 2019). Most of these graphical models are Markov Random Field (MRF) models, whose graph structure reflects the conditional associations between variables (Kindermann and Snell 1980). In these models, a missing edge between two variables in the network implies that these variables are independent, given the remaining variables (Lauritzen 2004). In other words, the remaining variables of the network fully account for the potential association between the unconnected variables.

Why use a Bayesian approach to analyze the MRF?

Testing the structure of the MRF requires us to determine the plausibility of the opposing hypotheses of conditional dependence and conditional independence. That is, how plausible is it that the observed data come from a network with a structure that includes the edge between two variables compared to a network structure that excludes that edge? Frequentist approaches are limited in this regard because they can only reject the conditional independence hypothesis, not support it (Wagenmakers et al. 2018; Wagenmakers 2007). This leads to the problem that if an edge is excluded, we do not know whether this is because the edge is absent in the population or because we lack the power to reject the null hypothesis of independence. To avoid this problem, we will use a Bayesian approach using Bayes factors (Kass and Raftery 1995)). The inclusion Bayes factor (Huth et al. 2023; Sekulovski et al. 2023) allows us to quantify how much the data support both conditional dependence -evidence of edge presence- or conditional independence -evidence of edge absence. It also allows us to conclude that there is limited support for either hypothesis (Dienes 2014)-an absence of evidence.


The current developmental version can be installed with

if (!requireNamespace("remotes")) { 


Contreras, A., I. Nieto, C. Valiente, R. Espinosa, and C. Vazquez. 2019. “The Study of Psychopathology from the Network Analysis Perspective: A Systematic Review.” Psychotherapy and Psychosomatics 88 (2): 71–83.

Dienes, Z. 2014. “Using Bayes to Get the Most Out of Non-Significant Results.” Frontiers in Psychology 5 (781): 1–17.

George, E. I., and R. E. McCulloch. 1993. “Variable Selection via Gibbs Sampling.” Journal of the American Statistical Association 88 (423): 881–89.

Gottardo, R., and A. E. Raftery. 2008. “Markov Chain Monte Carlo with Mixtures of Mutually Singular Distributions.” Journal of Computational and Graphical Statistics 17 (4): 949–75.

Huth, K., J. de Ron, A. E. Goudriaan, K. Luigjes, R. Mohammadi, R. J. van Holst, E.-J. Wagenmakers, and M. Marsman. 2023. “Bayesian Analysis of Cross-Sectional Networks: A Tutorial in R and JASP.” Advances in Methods and Practices in Psychological Science 6 (4): 1–18.

Kass, R. E., and A. E. Raftery. 1995. “Bayes Factors.” Journal of the American Statistical Association 90 (430): 773–95.

Kindermann, R., and J. L. Snell. 1980. Markov Random Fields and Their Applications. Vol. 1. Contemporary Mathematics. Providence: American Mathematical Society.

Lauritzen, S. L.. 2004. Graphical Models. Oxford: Oxford University Press.

Marsman, M., and J. M. B. Haslbeck. 2023. “Bayesian Analysis of the Ordinal Markov Random Field.” PsyArXiv.

Marsman, M., and M. Rhemtulla. 2022. “Guest Editors’ Introduction to the Special Issue ‘Network Psychometrics in Action’: Methodological Innovations Inspired by Empirical Problems.” Psychometrika 87 (1): 1–11.

Robinaugh, D. J., R. H. A. Hoekstra, E. R. Toner, and D. Borsboom. 2020. “The Network Approach to Psychopathology: A Review of the Literature 2008–2018 and an Agenda for Future Research.” Psychological Medicine 50: 353–66.

Sekulovski, N., S. Keetelaar, K. Huth, E.-J. Wagenmakers, R. van Bork, D. van den Bergh, and M. Marsman. 2023. “Testing Conditional Independence in Psychometric Networks: An Analysis of Three Bayesian Methods.” PsyArXiv.

Wagenmakers, E.-J. 2007. “A Practical Solution to the Pervasive Problems of p Values.” Psychonomic Bulletin & Review 14: 779–804.

Wagenmakers, E.-J., M. Marsman, T. Jamil, A. Ly, J. Verhagen, J. Love, R. Selker, et al. 2018. “Bayesian Inference for Psychology. Part I: Theoretical Advantages and Practical Ramifications.” Psychonomic Bulleting & Review 25 (1): 58–76.