Bayesian analysis of graphical models with binary and ordinal variables
The bgms package implements Bayesian estimation and model comparison for ordinal Markov random fields (MRFs), graphical models that represent networks of binary and/or ordinal variables (Marsman et al., 2025). The likelihood is approximated with a pseudolikelihood, and Markov chain Monte Carlo (MCMC) methods are used to sample from the corresponding pseudoposterior distribution of the model parameters.
Main functions
The package has two main entry points:
-
bgm()
– estimates a single network in a one-sample design.
-
bgmCompare()
– compares networks between groups in an independent-sample design (see Marsman et al., 2024).
Effect selection
Both functions support effect selection with spike-and-slab priors:
Edges in one-sample designs:
bgm()
models the presence or absence of edges between variables. Posterior inclusion probabilities indicate the plausibility of each edge and can be converted into Bayes factors for conditional independence tests (see Marsman et al., 2025; Sekulovski et al., 2024).Communities/clusters in one-sample designs:
bgm()
can also model community structure. Posterior probabilities for the number of clusters quantify the plausibility of clustering solutions and can be converted into Bayes factors (see Sekulovski et al., 2025).Group differences in independent-sample designs:
bgmCompare()
models differences in edge weights and category thresholds between groups. Posterior inclusion probabilities indicate the plausibility of parameter differences and can be converted into Bayes factors for tests of parameter equivalence (see Marsman et al., 2024).
Learn more
For worked examples and tutorials, see the package vignettes:
You can also access these directly from R with:
browseVignettes("bgms")
Why use Markov Random Fields?
Graphical models or networks have become central in recent psychological and psychometric research (Contreras et al., 2019; Marsman & Rhemtulla, 2022; Robinaugh et al., 2020). Most are Markov random field (MRF) models, where the graph structure reflects partial associations between variables (Kindermann & Snell, 1980).
In an MRF, a missing edge between two variables implies conditional independence given the rest of the network (Lauritzen, 2004). In other words, the remaining variables fully explain away any potential association between the unconnected pair.
Why use a Bayesian approach?
When analyzing an MRF, we often want to compare competing hypotheses:
-
Edge presence vs. edge absence (conditional dependence vs. independence) in one-sample designs.
- Parameter difference vs. parameter equivalence in independent-sample designs.
Frequentist approaches are limited in such comparisons: they can reject a null hypothesis, but they cannot provide evidence for it.
As a result, when an edge or difference is excluded, it remains unclear whether this reflects true absence or simply insufficient power.
Bayesian inference avoids this problem.
Using inclusion Bayes factors (Huth et al., 2023; Sekulovski et al., 2024), we can quantify evidence in both directions:
-
Evidence of edge presence vs. evidence of edge absence, or
- Evidence of parameter difference vs. evidence of parameter equivalence.
This makes it possible not only to detect structure and group differences, but also to conclude when there is an absence of evidence.
Installation
The current developmental version can be installed with
if (!requireNamespace("remotes")) {
install.packages("remotes")
}
remotes::install_github("Bayesian-Graphical-Modelling-Lab/bgms")