| Title: | Bayesian Multi-Layer ERGMs for Weighted and Signed Weighted Networks |
|---|---|
| Description: | Bayesian hierarchical multi-layer dissolution exponential random graph models for weighted and signed weighted networks. A weighted network is decomposed into nested binary layers by thresholding its edge weights, and each transition between consecutive layers is modelled conditionally on the observed lower layers: on the layers alone with bergm_w(), or jointly with the edge signs with bergm_ws(). Layer-specific parameters are partially pooled through a Normal-inverse-Wishart hierarchical prior and estimated with an adaptive approximate exchange algorithm: joint or layer-wise updates, delayed acceptance with a Gaussian approximation of the likelihood, the hierarchical mean integrated out of the exchange step, and a proposal covariance initialised from the Fisher information, learned during the burn-in and pooled across chains. The package also provides a model builder that checks that every statistic is computed on its own layer, prior and posterior predictive checks with goodness-of-fit statistics not used in the model, convergence diagnostics and a check of the length of the auxiliary chains. |
| Authors: | Alberto Caimo [aut, cre] (ORCID: <https://orcid.org/0000-0001-8956-7166>), Isabella Gollini [aut] (ORCID: <https://orcid.org/0000-0002-7738-5688>) |
| Maintainer: | Alberto Caimo <[email protected]> |
| License: | GPL-3 |
| Version: | 0.1.0 |
| Built: | 2026-11-26 12:13:28 UTC |
| Source: | https://github.com/cran/Bergm.ws |
A weighted network is decomposed into nested binary layers
by thresholding its edge
weights. Each transition is modelled by an exponential
random graph model whose statistics are computed on layer ,
conditionally on the observed lower layers: bergm_w fits the
model to the layers alone. When the sign of every edge of
is also recorded, bergm_ws models the signs jointly with the
layers:
where is the observed preceding layer. The model is
therefore conditional on the observed lower layers. The layer-specific
parameters are partially pooled by a Normal-inverse-Wishart prior,
,
, , and the
posterior is sampled with an adaptive approximate exchange algorithm
(bergm_w, bergm_ws): delayed acceptance with a Gaussian approximation of
the likelihood, so that most proposals that would be rejected are discarded
without simulating a network; the hierarchical mean integrated out of the
exchange step; and a proposal covariance initialised from the Fisher
information, learned during the burn-in, pooled across chains and frozen for
the retained draws.
The typical workflow is:
ws_data: build the layers from a weight matrix, a sign
matrix and the thresholds (ws_simulate creates a toy
example; toy_ws is one).
ws_model: write the model as an ergm.multi
formula on the layers x_1, ..., x_K and the signs
z, or once as a template with x_k. The formula is checked:
every statistic must use exactly one layer and every layer the same
statistics; nearly collinear statistics are flagged.
ws_prior, ws_predictive with
type = "prior": prior and prior predictive check.
bergm_w or bergm_ws: adaptive exchange sampler, then
summary.bergm_w for estimates and convergence diagnostics,
as.mcmc.list() for the draws.
ws_predictive: posterior predictive check, including
statistics not used in the model; ws_aux_check: are the
auxiliary chains long enough?
Alberto Caimo, Paolo Dalena, Isabella Gollini
Caimo, A. and Friel, N. (2011). Bayesian inference for exponential random graph models. *Social Networks*, 33, 41–55.
Caimo, A. and Gollini, I. (2020). A multi-layer exponential random graph modelling approach for weighted networks. *Computational Statistics & Data Analysis*, 142, 106825.
Caimo, A., Dalena, P, and Gollini, I. (2026). Multi-layer dissolution exponential-family models for weighted signed networks. [arxiv.org/abs/2511.03420](https://arxiv.org/abs/2511.03420)
Murray, I., Ghahramani, Z. and MacKay, D. (2006). MCMC for doubly-intractable distributions. Proceedings of UAI.
Christen, J. A. and Fox, C. (2005). Markov chain Monte Carlo using an approximation. Journal of Computational and Graphical Statistics, 14, 795–810.
Haario, H., Saksman, E. and Tamminen, J. (2001). An adaptive Metropolis algorithm. Bernoulli, 7, 223–242.
Krivitsky, P. N., Koehly, L. M. and Marcum, C. S. (2020). Exponential-family random graph models for multi-layer networks. Psychometrika, 85, 630–659.
van Dyk, D. A. and Park, T. (2008). Partially collapsed Gibbs samplers: theory and methods. Journal of the American Statistical Association, 103, 790–796.
vignette("Bergm.ws", package = "Bergm.ws")
bergm_w() fits the model to a weighted network without signs, with
statistics computed on each layer ; bergm_ws() fits it to a
signed weighted network, with the signs modelled jointly with the layers and
statistics computed on the positive and negative edges of each layer.
Both sample the posterior of the layer-specific parameters and of
the hierarchical parameters with an adaptive approximate
exchange algorithm. Each exchange step simulates an auxiliary network from the
joint model on the observed supports, starting at the observed network, so
that the intractable normalising constant cancels; and
are then drawn from their Normal and inverse-Wishart full conditionals.
Compared with the basic algorithm, proposals are screened by delayed acceptance, the hierarchical mean is integrated out of the exchange step, and the proposal covariance is learned from the chains during the burn-in, pooled across chains and then frozen. None of these changes the posterior.
bergm_w(data, formula = NULL, scale = NULL, labels = NULL, prior = NULL, update = c("joint", "layer"), n_iter = 10000, burnin = floor(n_iter / 4), thin = 1, n_chains = 4, cores = 1, aux_iters = NULL, init = "mple", prop_cov = "information", adapt_cov = TRUE, pool_adaptation = TRUE, adapt_every = NULL, adapt_start = NULL, adapt_scaling = 0.1, target_accept = 0.234, delayed = TRUE, collapse_mu = TRUE, n_info = 200, n_newton = 2, dispersion = 4, seed = NULL, verbose = TRUE) bergm_ws(data, formula = NULL, scale = NULL, labels = NULL, prior = NULL, update = c("joint", "layer"), n_iter = 10000, burnin = floor(n_iter / 4), thin = 1, n_chains = 4, cores = 1, aux_iters = NULL, init = "mple", prop_cov = "information", adapt_cov = TRUE, pool_adaptation = TRUE, adapt_every = NULL, adapt_start = NULL, adapt_scaling = 0.1, target_accept = 0.234, delayed = TRUE, collapse_mu = TRUE, n_info = 200, n_newton = 2, dispersion = 4, seed = NULL, verbose = TRUE)bergm_w(data, formula = NULL, scale = NULL, labels = NULL, prior = NULL, update = c("joint", "layer"), n_iter = 10000, burnin = floor(n_iter / 4), thin = 1, n_chains = 4, cores = 1, aux_iters = NULL, init = "mple", prop_cov = "information", adapt_cov = TRUE, pool_adaptation = TRUE, adapt_every = NULL, adapt_start = NULL, adapt_scaling = 0.1, target_accept = 0.234, delayed = TRUE, collapse_mu = TRUE, n_info = 200, n_newton = 2, dispersion = 4, seed = NULL, verbose = TRUE) bergm_ws(data, formula = NULL, scale = NULL, labels = NULL, prior = NULL, update = c("joint", "layer"), n_iter = 10000, burnin = floor(n_iter / 4), thin = 1, n_chains = 4, cores = 1, aux_iters = NULL, init = "mple", prop_cov = "information", adapt_cov = TRUE, pool_adaptation = TRUE, adapt_every = NULL, adapt_start = NULL, adapt_scaling = 0.1, target_accept = 0.234, delayed = TRUE, collapse_mu = TRUE, n_info = 200, n_newton = 2, dispersion = 4, seed = NULL, verbose = TRUE)
data |
a |
formula, scale, labels
|
the ergm.multi formula on the layers
|
prior |
a |
update |
|
n_iter, burnin, thin
|
iterations per chain, burn-in and thinning. |
n_chains |
number of chains. The first starts at the initial value,
the others at draws from |
cores |
chains run in parallel with |
aux_iters |
MCMC steps for each auxiliary network. The default is ten
proposals per dyad-layer pair that can change (at least 2000); check it
with |
init |
|
prop_cov |
initial proposal covariance |
adapt_cov |
if |
pool_adaptation |
with several chains and |
adapt_every, adapt_start
|
adaptation interval and first adaptation
iteration; by default |
adapt_scaling |
the proposal scale is multiplied by
|
target_accept |
target acceptance rate. |
delayed |
delayed acceptance (Christen and Fox, 2005). Each proposal is
first screened with a Gaussian approximation of the log-likelihood,
|
collapse_mu |
if |
n_info |
networks simulated to estimate the Fisher information. |
n_newton |
moment-matching Newton steps from the penalised MPLE
towards the posterior mode before the chains start:
|
dispersion |
variance inflation of the starting points of chains 2, 3, ... |
seed |
random seed (each chain gets its own seed). |
verbose |
print progress. |
The initial proposal scale is for a block of
parameters: the usual random-walk scale, reduced by because the
exchange step adds noise to the acceptance ratio with about the same variance
as the exact log-likelihood ratio.
Statistics enter in the rescaled form of ws_model; use
coef(fit, scale = "ergm") for coefficients on the ergm scale.
An object of class "bergm_w" (and "bergm_ws" for signed
networks): the model, the prior, the chains (draws
of , and the lower triangle of ,
acceptance indicators, final proposal scales and covariances), the settings and
the initialisation (starting value, initial proposal covariance and a fit
check of the starting value).
summary.bergm_w,
ws_predictive, ws_aux_check
data(toy_ws) y <- ws_data(toy_ws$weights, toy_ws$thresholds, toy_ws$signs, toy_ws$attributes) x <- ws_data(toy_ws$weights, toy_ws$thresholds) f <- ~ L(~edges, ~(z & x_k)) + L(~edges, ~(!z & x_k)) + gwespL(0.5, fixed = TRUE, L.base = ~(z & x_k), Ls.path = list(~(z & x_k), ~(z & x_k))) fit_ws <- bergm_ws(y, f, scale = "gwesp", n_iter = 300, burnin = 100, n_chains = 2, seed = 1) summary(fit_ws) fit_w <- bergm_w(x, ~ L(~edges, ~x_k) + L(~triangle, ~x_k), n_iter = 300, burnin = 100, n_chains = 2, seed = 1) summary(fit_w)data(toy_ws) y <- ws_data(toy_ws$weights, toy_ws$thresholds, toy_ws$signs, toy_ws$attributes) x <- ws_data(toy_ws$weights, toy_ws$thresholds) f <- ~ L(~edges, ~(z & x_k)) + L(~edges, ~(!z & x_k)) + gwespL(0.5, fixed = TRUE, L.base = ~(z & x_k), Ls.path = list(~(z & x_k), ~(z & x_k))) fit_ws <- bergm_ws(y, f, scale = "gwesp", n_iter = 300, burnin = 100, n_chains = 2, seed = 1) summary(fit_ws) fit_w <- bergm_w(x, ~ L(~edges, ~x_k) + L(~triangle, ~x_k), n_iter = 300, burnin = 100, n_chains = 2, seed = 1) summary(fit_w)
Posterior means, standard deviations and quantiles of the hierarchical means
, the layer-specific parameters and the between-layer
standard deviations , with the potential scale
reduction factor and the effective sample size of every
parameter, the acceptance rate of each chain after burn-in and the largest
posterior correlation between parameters of different layers.
## S3 method for class 'bergm_w' summary(object, probs = c(0.025, 0.5, 0.975), ...) ## S3 method for class 'summary.bergm_w' print(x, digits = 3, ...) ## S3 method for class 'bergm_w' print(x, ...) ## S3 method for class 'bergm_w' coef(object, what = c("phi", "mu"), scale = c("model", "ergm"), ...) ## S3 method for class 'bergm_w' as.mcmc.list(x, what = c("phi", "mu", "sd"), ...)## S3 method for class 'bergm_w' summary(object, probs = c(0.025, 0.5, 0.975), ...) ## S3 method for class 'summary.bergm_w' print(x, digits = 3, ...) ## S3 method for class 'bergm_w' print(x, ...) ## S3 method for class 'bergm_w' coef(object, what = c("phi", "mu"), scale = c("model", "ergm"), ...) ## S3 method for class 'bergm_w' as.mcmc.list(x, what = c("phi", "mu", "sd"), ...)
object, x
|
an |
probs |
posterior quantiles to report. |
digits |
digits for printing. |
what |
|
scale |
|
... |
ignored. |
uses coda::gelman.diag without discarding further draws
and needs at least two chains; values above 1.05, or effective sample sizes
below 100, call for longer chains. A large correlation between parameters of
different layers suggests update = "joint".
summary returns an object of class "summary.bergm_w";
coef a matrix (or a vector for what = "mu");
as.mcmc.list a coda mcmc.list.
A signed weighted network on 40 nodes simulated with
ws_simulate(n = 40, seed = 33). It is entirely synthetic: three
modules of nodes with strong positive edges inside them, a minority faction
whose edges with the others tend to be negative and weak, and signs that are
noisier for weak edges. As in co-expression networks built from interaction
confidence scores, the share of positive edges grows with the weight (77%,
87%, 94% and 97% in the four layers).
data(toy_ws)data(toy_ws)
A list with components
weights40 x 40 symmetric matrix of edge weights in (0, 1).
signs40 x 40 symmetric matrix of signs ().
thresholdsthe four thresholds defining layers
to .
attributesdata frame with the node attributes module
(three modules) and pathway (a binary annotation that does not
affect the edges).
data(toy_ws) dat <- ws_data(toy_ws$weights, toy_ws$thresholds, toy_ws$signs, toy_ws$attributes) dat ws_data(toy_ws$weights, toy_ws$thresholds, attributes = toy_ws$attributes)data(toy_ws) dat <- ws_data(toy_ws$weights, toy_ws$thresholds, toy_ws$signs, toy_ws$attributes) dat ws_data(toy_ws$weights, toy_ws$thresholds, attributes = toy_ws$attributes)
The exchange algorithm needs auxiliary networks drawn from the model. They are
obtained by running aux_iters MCMC steps from the observed network, so
they are only approximately exact. At the posterior mean, this function
compares the statistics of n such networks with those of n
networks from chains factor times longer.
ws_aux_check(fit, n = 100, factor = 20, seed = NULL) ## S3 method for class 'ws_aux_check' print(x, ...)ws_aux_check(fit, n = 100, factor = 20, seed = NULL) ## S3 method for class 'ws_aux_check' print(x, ...)
fit |
an |
n |
networks per chain length. |
factor |
length of the long chains relative to |
seed |
random seed. |
x |
an |
... |
ignored. |
A data frame with, for each statistic, the means of the short and long
chains, the long-chain standard deviation, the standardised difference of the
means and its z-score. A statistic is flagged when the difference exceeds a
quarter of a standard deviation and its z-score exceeds the Bonferroni
threshold for statistics, so that a single
large z-score among many statistics is not mistaken for a problem; flagged
statistics suggest increasing aux_iters. With fewer than about 100
networks the Monte Carlo error of the means is large.
Thresholds a weighted network into nested binary layers
, , optionally records the
sign of every edge of , and builds the multilayer network and the
support constraints used by the model: varies on the dyads of the
observed , and the signs, when given, on the dyads of
. Without signs the data are for bergm_w, with signs
for bergm_ws.
ws_data(weights, thresholds, signs = NULL, attributes = NULL) ## S3 method for class 'ws_data' print(x, ...)ws_data(weights, thresholds, signs = NULL, attributes = NULL) ## S3 method for class 'ws_data' print(x, ...)
weights |
symmetric numeric matrix of edge weights (for example
interaction confidence scores); |
signs |
optional symmetric matrix whose sign gives the sign of each
edge (for example a correlation matrix). Only dyads of |
thresholds |
strictly increasing vector of length |
attributes |
optional data frame of node attributes, one row per node, available to the terms of the model formula. |
x |
an |
... |
ignored. |
An object of class "ws_data": a list with the multilayer network
y (signs, if any, and layers ), the layers x
(), the matrix levels (for each dyad, the highest
layer containing it, or -1), the sign matrix signs, the support
constraint formula and the inputs.
data(toy_ws) y <- ws_data(toy_ws$weights, toy_ws$thresholds, toy_ws$signs, toy_ws$attributes) y x <- ws_data(toy_ws$weights, toy_ws$thresholds) xdata(toy_ws) y <- ws_data(toy_ws$weights, toy_ws$thresholds, toy_ws$signs, toy_ws$attributes) y x <- ws_data(toy_ws$weights, toy_ws$thresholds) x
Takes an ergm.multi formula on the layers of a ws_data
object and checks it: every statistic must refer to exactly one layer, the
statistics must be grouped by layer, and every layer must have the same
statistics, because the hierarchical prior pools the -th statistic of
all layers. Statistics equal to zero in the observed network and statistics
whose change statistics are nearly collinear on the dyads that can change are
reported (their parameters are then separated mostly by the prior).
ws_model(data, formula, scale = NULL, labels = NULL) ## S3 method for class 'ws_model' print(x, ...)ws_model(data, formula, scale = NULL, labels = NULL) ## S3 method for class 'ws_model' print(x, ...)
data |
a |
formula |
a one-sided ergm.multi formula. The layers are called
|
scale |
multipliers of the statistics: |
labels |
optional names of the |
x |
a |
... |
ignored. |
Any ergm or ergm.multi term can be used, applied to a layer with
L() or with the layer-logic terms of ergm.multi. In signed data,
(z & x_k) selects the positive edges of layer and
(!z & x_k) its negative edges; for example
~ L(~edges, ~(z & x_k)) + L(~edges, ~(!z & x_k)) +
gwespL(0.5, fixed = TRUE, L.base = ~(z & x_k),
Ls.path = list(~(z & x_k), ~(z & x_k)))
models the persistence of positive and negative edges and positive triangle
closure in every layer. Without signs the layers are used directly, as in
L(~edges, ~x_k). Objects named in the formula (a decay value, a vector
of attributes) are taken from its environment.
The coefficient of a statistic multiplied by scale is on the scale of
the multiplied statistic; with scale = "gwesp" a GWESP coefficient is
the change in log-odds for an edge with many shared partners, on the same
scale in every layer, which helps the hierarchical prior pool comparable
quantities. coef(fit, scale = "ergm") returns coefficients on the
original ergm scale.
A warning is given when the -th statistics of different layers are
not the same term.
An object of class "ws_model" with the expanded formula (rhs),
the template if one was used, the statistic labels, the observed statistics
(s_obs, and s_obs_scaled after multiplication by scale),
the multipliers scale and the change-statistic correlations
change_cor.
Simulates networks from the prior or from the posterior and compares them with
the observed network, both on the model statistics and on statistics that are
not in the model: for every layer, the numbers of triangles of each sign type
(+++, -++, +--, ---) and the standard deviation of
the positive degrees.
ws_predictive(object, type = c("posterior", "prior"), prior = NULL, nsim = 200, burnin = NULL, gof = TRUE, seed = NULL, verbose = TRUE) ## S3 method for class 'ws_predictive' print(x, digits = 2, ...)ws_predictive(object, type = c("posterior", "prior"), prior = NULL, nsim = 200, burnin = NULL, gof = TRUE, seed = NULL, verbose = TRUE) ## S3 method for class 'ws_predictive' print(x, digits = 2, ...)
object |
an |
type |
|
prior |
prior for the prior check (default: that of the fit, or
|
nsim |
number of simulated networks, one per parameter draw. |
burnin |
MCMC steps per simulated network, started at the observed network; default five times the auxiliary chain length. |
gof |
also compute the statistics not in the model (needs the simulated networks, slower). |
seed |
random seed. |
verbose |
print progress. |
x |
an |
digits |
digits for printing. |
... |
ignored. |
The simulated networks are conditional on the observed lower layers (see
Bergm.ws-package), so each transition is checked given what is
observed below it. The mid-p value is the share of simulated values below
the observed one plus half the share of ties; values near 0 or 1 flag
statistics that the model does not reproduce.
An object of class "ws_predictive" with the simulated (stats,
gof) and observed (obs, gof_obs) statistics and the
parameter draws used.
data(toy_ws) y <- ws_data(toy_ws$weights, toy_ws$thresholds, toy_ws$signs, toy_ws$attributes) f <- ~ L(~edges, ~(z & x_k)) + L(~nodematch("pathway"), ~(z & x_k)) + L(~edges, ~(!z & x_k)) mod <- ws_model(y, f) pp <- ws_predictive(mod, type = "prior", nsim = 50, seed = 1) ppdata(toy_ws) y <- ws_data(toy_ws$weights, toy_ws$thresholds, toy_ws$signs, toy_ws$attributes) f <- ~ L(~edges, ~(z & x_k)) + L(~nodematch("pathway"), ~(z & x_k)) + L(~edges, ~(!z & x_k)) mod <- ws_model(y, f) pp <- ws_predictive(mod, type = "prior", nsim = 50, seed = 1) pp
Normal-inverse-Wishart prior:
independently for
, and
, with .
ws_prior(model, mu0 = 0, Sigma0 = 3, nu0 = NULL, S0 = 1) ## S3 method for class 'ws_prior' print(x, ...)ws_prior(model, mu0 = 0, Sigma0 = 3, nu0 = NULL, S0 = 1) ## S3 method for class 'ws_prior' print(x, ...)
model |
an |
mu0 |
prior mean of |
Sigma0 |
prior covariance of |
nu0 |
inverse-Wishart degrees of freedom; default |
S0 |
inverse-Wishart scale: a scalar (times the identity) or a matrix. |
x |
an |
... |
ignored. |
With few layers, the between-layer covariance is determined
mostly by its prior: check the sensitivity of the conclusions to nu0
and S0.
An object of class "ws_prior".
data(toy_ws) y <- ws_data(toy_ws$weights, toy_ws$thresholds, toy_ws$signs, toy_ws$attributes) m <- ws_model(y, ~ L(~edges, ~(z & x_k)) + L(~edges, ~(!z & x_k))) ws_prior(m)data(toy_ws) y <- ws_data(toy_ws$weights, toy_ws$thresholds, toy_ws$signs, toy_ws$attributes) m <- ws_model(y, ~ L(~edges, ~(z & x_k)) + L(~edges, ~(!z & x_k))) ws_prior(m)
Generates a synthetic weighted network with modular structure, for
examples and tests. Nodes belong to modules placed on a circle; the weight of
a dyad decreases with the distance between its nodes, so that strong edges are
clustered. With signed = TRUE, a minority faction has mostly negative and weak
edges with the other nodes, and signs are flipped more often for weak edges, so that the share
of positive edges increases with the weight.
ws_simulate(n = 40, K = 3, n_modules = 3, density = 0.30, retention = c(0.55, 0.50, 0.50), signed = TRUE, faction_share = 0.18, sign_noise = 0.08, seed = NULL)ws_simulate(n = 40, K = 3, n_modules = 3, density = 0.30, retention = c(0.55, 0.50, 0.50), signed = TRUE, faction_share = 0.18, sign_noise = 0.08, seed = NULL)
n |
number of nodes. |
K |
number of layer transitions (layers |
n_modules |
number of modules. |
density |
share of dyads in the baseline layer |
retention |
share of the edges of |
signed |
if |
faction_share |
probability that a node belongs to the minority faction. |
sign_noise |
probability of flipping the sign of the weakest edges; it decreases linearly to zero for the strongest ones. |
seed |
optional random seed. |
A list with weights (symmetric matrix in (0, 1)), signs
(symmetric matrix of , only if signed), thresholds (length K + 1)
and attributes (data frame with module and pathway),
ready for ws_data.
sim <- ws_simulate(n = 30, seed = 1) y <- ws_data(sim$weights, sim$thresholds, sim$signs, sim$attributes) ysim <- ws_simulate(n = 30, seed = 1) y <- ws_data(sim$weights, sim$thresholds, sim$signs, sim$attributes) y