Bstergm fits
Bayesian separable temporal ERGMs to sequences of binary networks, with
bstergm(), and of signed networks, with
bstergm_sign(). This vignette analyses the U.S. Senate data
of Caimo and Gollini (2025): the signs with bstergm_sign()
and the interactions, a sequence of binary networks, with
bstergm().
A signed network \(y_t\) on \(n\) nodes has ties \(y_{ij,t} \in \{-1, 0, +1\}\). It is written as an interaction layer \(x_t\) (\(x_{ij,t} = |y_{ij,t}|\)) and a sign layer \(z_t\) (\(z_{ij,t} = 1\) if \(y_{ij,t} = +1\)), which is only defined for interacting pairs.
As in the separable temporal ERGM of Krivitsky and Handcock (2014), each transition \(y_{t-1} \to y_t\) is split into a formation network \(y^F_t = y_{t-1} \cup y_t\) and a persistence network \(y^P_t = y_{t-1} \cap y_t\), for both layers. The model is \[ p(y_t \mid y_{t-1}) = \prod_{U \in \{F, P\}} p(x^U_t \mid y_{t-1}; \xi^U)\; p(z^U_t \mid x^U_t, y_{t-1}; \zeta^U), \] where every factor is an ERGM. The sign model \(p(z^U_t \mid x^U_t, \cdot\,; \zeta^U)\) lets the sign of every interacting dyad of \(x^U_t\) vary while the interactions stay fixed. Its log-odds of a positive sign are \(\zeta^{U\top} \Delta_{ij} s(z^U_t; x^U_t, y_{t-1})\). Under independent priors the posterior factorises into four independent parts: \(\zeta^F\), \(\zeta^P\), \(\xi^F\) and \(\xi^P\).
senators70_am contains the signed co-sponsorship
networks of the 70 senators who served in the 99th to 102nd Congress
(Neal, 2020). As in the paper, we use the first three Congresses
(1985–1991).
library(Bstergm)
#> Loading required package: ergm
#> Loading required package: network
#>
#> 'network' 1.20.0 (2026-02-06), part of the Statnet Project
#> * 'news(package="network")' for changes since last version
#> * 'citation("network")' for citation information
#> * 'https://statnet.org' for help, support, and other information
#>
#> 'ergm' 4.12.0 (2026-02-17), part of the Statnet Project
#> * 'news(package="ergm")' for changes since last version
#> * 'citation("ergm")' for citation information
#> * 'https://statnet.org' for help, support, and other information
#> 'ergm' 4 is a major update that introduces some backwards-incompatible
#> changes. Please type 'news(package="ergm")' for a list of major
#> changes.
data(senators70_am)
senate <- signed_series(senators70_am[1:3],
vertex.attr = attr(senators70_am$S99, "senators"))
senate
#> Sequence of 3 undirected signed networks on 70 nodes
#> time positive negative active density %positive
#> S99 284 747 1031 0.427 27.5
#> S100 234 808 1042 0.431 22.5
#> S101 363 810 1173 0.486 30.9
#> Vertex attributes: ID, Senator, State, Partysummary() shows the formation and persistence networks
of each transition and the signed triangle census. Balance theory says
that triangles with one or three positive ties (+++,
+--) should be common and those with two (++-)
rare.
summary(senate)
#> Ties by time point:
#> time positive negative active density %positive
#> S99 284 747 1031 0.427 27.5
#> S100 234 808 1042 0.431 22.5
#> S101 363 810 1173 0.486 30.9
#>
#> Formation (F) and persistence (P) networks by transition:
#> transition F.active F.positive P.active P.positive
#> S99 -> S100 1442 380 631 138
#> S100 -> S101 1472 441 743 156
#>
#> Signed triangles by time point:
#> +++ ++- +-- ---
#> S99 644 105 2753 2066
#> S100 384 88 2171 3200
#> S101 1119 114 3521 3193Dark solid lines are positive ties and light dotted lines negative ties; Democrats are dark and Republicans light grey.
The sign process is modelled on the positive ties of each
formation or persistence network. The interacting pairs are fixed and
stored with the network, so negative ties are the interacting pairs that
are not positive. as_signed_network() builds this
representation for a single network. Any ergm term then describes
positive ties, and the signed terms of the package also use the negative
ties:
nw <- as_signed_network(senators70_am$S99,
vertex.attr = attr(senators70_am$S99, "senators"))
summary(nw ~ edges + nodematch("Party", diff = TRUE, levels = "R") +
gwesf_pos(0.6) + gwese_pos(0.6) + gwese_neg(0.6) + triangle_signed())
#> edges nodematch.Party.R gwesf_pos.fixed.0.6 gwese_pos.fixed.0.6
#> 284.0000 36.0000 451.0102 495.2528
#> gwese_neg.fixed.0.6 triangle_signed+++ triangle_signed++- triangle_signed+--
#> 1258.0762 644.0000 105.0000 2753.0000
#> triangle_signed---
#> 2066.0000| paper | term | description |
|---|---|---|
| \(\text{edges}^+\) | edges |
number of positive ties |
| \(\text{homophily}^+(\text{rep})\) | nodematch("Party", diff = TRUE, levels = "R") |
positive ties between Republicans |
| \(\text{gwdegree}^+(0.2)\) | gwdegree(0.2, fixed = TRUE) |
geometrically weighted positive degree |
| \(\text{gwesf}^+(0.6)\) | gwesf_pos(0.6) |
positive ties weighted by shared friends |
| \(\text{gwese}^+(0.6)\) | gwese_pos(0.6) |
positive ties weighted by shared enemies |
| \(\text{gwese}^-(0.6)\) | gwese_neg(0.6) |
negative ties weighted by shared enemies |
These terms are implemented in C. They give the same values as the multilayer terms of ergm.multi used in the first version of the package, which are still supported but considerably slower:
positive <- ~positive
negative <- ~(active & !positive)
z <- network::as.network((senators70_am$S99 > 0) * 1, directed = FALSE)
x <- network::as.network((senators70_am$S99 != 0) * 1, directed = FALSE)
summary(~ gwespL(0.6, fixed = TRUE, L.base = positive, Ls.path = list(positive, positive)) +
gwespL(0.6, fixed = TRUE, L.base = positive, Ls.path = list(negative, negative)) +
gwespL(0.6, fixed = TRUE, L.base = negative, Ls.path = list(negative, negative)),
basis = Layer(positive = z, active = x))
#> L(pth=(positive),bse=positive,inord=FALSE)~gwesp.fixed.0.6
#> 451.0102
#> L(pth=((active&!positive)),bse=positive,inord=FALSE)~gwesp.fixed.0.6
#> 495.2528
#> L(pth=((active&!positive)),bse=(active&!positive),inord=FALSE)~gwesp.fixed.0.6
#> 1258.0762bstergm_sign() samples the posterior of the formation
and persistence parameters of the sign process with the approximate
exchange algorithm. By default it uses adaptive Metropolis-Hastings. The
two components are independent and run in parallel with
cores, as do several chains. A short run looks like
this:
f <- ~ edges + nodematch("Party", diff = TRUE, levels = "R") +
gwdegree(0.2, fixed = TRUE) + gwesf_pos(0.6) + gwese_pos(0.6) + gwese_neg(0.6)
quick <- bstergm_sign(f, data = senate, iters = 300, burn.in = 100, seed = 1, verbose = FALSE)
quick
#> Bayesian separable temporal ERGM for signed networks
#> process: sign | method: AMH | engine: native
#> formula: ~edges + nodematch("Party", diff = TRUE, levels = "R") + gwdegree(0.2, fixed = TRUE) + gwesf_pos(0.6) + gwese_pos(0.6) + gwese_neg(0.6)
#> transitions: 2 | iterations: 300 | burn-in: 100 | thin: 1
#>
#> Formation (F) - sign process: 1 chain, acceptance 0.05
#> Mean SD
#> edges -8.564 0.757
#> nodematch.Party.R 0.550 0.062
#> gwdeg.fixed.0.2 -0.296 0.445
#> gwesf_pos.fixed.0.6 2.251 0.152
#> gwese_pos.fixed.0.6 0.252 0.448
#> gwese_neg.fixed.0.6 -1.478 0.198
#>
#> Persistence (P) - sign process: 1 chain, acceptance 0.08
#> Mean SD
#> edges -3.703 0.101
#> nodematch.Party.R 0.697 0.222
#> gwdeg.fixed.0.2 0.489 0.295
#> gwesf_pos.fixed.0.6 1.315 0.105
#> gwese_pos.fixed.0.6 -0.508 0.102
#> gwese_neg.fixed.0.6 -0.389 0.034Each iteration runs one auxiliary network simulation per transition
and component, started at the observed network. Their length is set by
default to three proposals per free dyad (see
control.bstergm()). Chains start at a pooled maximum
pseudo-likelihood estimate, which also gives the initial proposal
covariance; init = "ads" instead runs adaptive direction
sampling first, the scheme used by Caimo and Gollini. Results are
reproducible with seed, whatever the number of cores.
The analysis below used two chains of 35,000 iterations, with a 10,000 iteration burn-in and the priors of the paper: \(N(-1, 25)\) for the edges parameter and \(N(0, 25)\) for the others. It took 19 minutes on two cores; on the same machine, version 0.1 of the package would have needed about 3 days. The result is shipped with the package:
fit <- bstergm_sign(f, data = senate, iters = 35000, burn.in = 10000, thin = 25,
n_chains = 2, cores = 2, seed = 2025,
prior.mean = c(-1, 0, 0, 0, 0, 0), prior.sigma = 25)summary(fit)
#> Posterior summary (AMH, 35000 iterations, 10000 burn-in, thin 25)
#>
#> Formation (F) - sign process
#> chains: 2 | acceptance: 0.106 0.107
#> Mean SD Naive SE Time-series SE 2.5% 50%
#> edges -10.387 1.739 0.039 0.066 -14.124 -10.276
#> nodematch.Party.R 0.433 0.133 0.003 0.005 0.179 0.428
#> gwdeg.fixed.0.2 0.487 0.745 0.017 0.027 -1.027 0.508
#> gwesf_pos.fixed.0.6 2.612 0.277 0.006 0.009 2.061 2.622
#> gwese_pos.fixed.0.6 0.864 0.861 0.019 0.032 -0.553 0.757
#> gwese_neg.fixed.0.6 -1.522 0.190 0.004 0.006 -1.871 -1.531
#> 97.5% ESS Rhat
#> edges -7.414 702.296 1.000
#> nodematch.Party.R 0.713 812.896 1.001
#> gwdeg.fixed.0.2 1.881 763.788 1.000
#> gwesf_pos.fixed.0.6 3.155 927.422 1.001
#> gwese_pos.fixed.0.6 2.775 731.985 1.001
#> gwese_neg.fixed.0.6 -1.124 1059.959 1.001
#>
#> Persistence (P) - sign process
#> chains: 2 | acceptance: 0.108 0.118
#> Mean SD Naive SE Time-series SE 2.5% 50% 97.5%
#> edges -4.351 0.484 0.011 0.016 -5.398 -4.341 -3.474
#> nodematch.Party.R 0.711 0.240 0.005 0.008 0.238 0.715 1.162
#> gwdeg.fixed.0.2 0.356 0.471 0.011 0.017 -0.543 0.353 1.271
#> gwesf_pos.fixed.0.6 1.514 0.244 0.005 0.007 1.061 1.502 2.022
#> gwese_pos.fixed.0.6 -0.247 0.137 0.003 0.005 -0.500 -0.253 0.039
#> gwese_neg.fixed.0.6 -0.411 0.052 0.001 0.002 -0.516 -0.411 -0.313
#> ESS Rhat
#> edges 938.377 1.004
#> nodematch.Party.R 819.173 1.007
#> gwdeg.fixed.0.2 770.853 1.002
#> gwesf_pos.fixed.0.6 1122.199 1.004
#> gwese_pos.fixed.0.6 841.423 1.000
#> gwese_neg.fixed.0.6 724.448 1.001
#>
#> Total time: 19.4minThe R-hat values are close to 1 and the effective sample sizes are large. The trace, density and autocorrelation plots of each parameter are given by:
Taken at face value, the formation and persistence of positive ties both show transitivity among friends (\(\text{gwesf}^+ > 0\)), while negative ties whose endpoints share many enemies are unlikely to form or persist (\(\text{gwese}^- < 0\)). Both are in line with strong structural balance. Positive ties are also more likely between Republicans. The posterior probability that each effect is positive summarises the evidence for its direction:
sapply(fit$theta, function(a) round(apply(a, 2, function(v) mean(v > 0)), 3))
#> F P
#> edges 0.000 0.000
#> nodematch.Party.R 0.999 0.996
#> gwdeg.fixed.0.2 0.751 0.776
#> gwesf_pos.fixed.0.6 1.000 1.000
#> gwese_pos.fixed.0.6 0.857 0.040
#> gwese_neg.fixed.0.6 0.000 0.000The next two sections show that, for this model, these estimates must be read with care.
bgof() simulates the formation and persistence networks
of each transition from the posterior predictive distribution. It
returns the centred cumulative differences \(\sum_t \{s(z'_t) - s(z_t)\}\) of the
model statistics, which should be centred at zero for a well-fitting
model, together with any other monitored statistics. By default the
simulations are five times longer than those used for fitting, so the
check is not tied to the fitting length:
gf <- bgof(fit, nsim = 50, seed = 1, verbose = FALSE,
monitor = ~ degree(0:12) + degree_neg(0:12) + triangle_signed())
gs <- summary(gf)
gs$F[c(1:6, 33:36), ]
#> observed mean 2.5% 97.5% p-value
#> edges 821.0000 1320.0800 986.5500 1641.3250 0.00
#> nodematch.Party.R 194.0000 281.5000 224.2500 335.4250 0.00
#> gwdeg.fixed.0.2 158.9383 167.0646 152.7649 170.9963 0.20
#> gwesf_pos.fixed.0.6 1403.0557 2354.3795 1716.3009 2970.3685 0.00
#> gwese_pos.fixed.0.6 1490.9141 2307.1650 1786.1258 2784.3681 0.00
#> gwese_neg.fixed.0.6 3781.0617 2274.5028 443.9927 3446.2097 0.00
#> triangle_signed+++ 2505.0000 3416.2000 1790.5250 5877.0250 0.64
#> triangle_signed++- 1346.0000 8253.2600 5995.6500 10301.8750 0.00
#> triangle_signed+-- 11729.0000 11454.4000 9555.9750 14087.5000 0.44
#> triangle_signed--- 11514.0000 3970.1400 277.0000 7974.4000 0.00
plot(gf, type = "model")The simulated formation networks have on average 1320 positive ties
over the two transitions, against 821 observed, and 8253 unbalanced
++- triangles, against 1346. The model statistics are not
centred at zero. The next section shows why.
The approximate exchange algorithm treats the end of each auxiliary
simulation, which starts at the observed network, as an exact draw from
the model. aux_diagnostics() checks this assumption. It
runs much longer simulations from the observed networks at the posterior
mean and records the model statistics. In the plots, the dashed line is
the observed value and the dotted vertical line the length used for
fitting:
dg <- aux_diagnostics(fit, visits = 50, seed = 1)
dg
#> Auxiliary simulations started at the observed networks (50 proposals per free dyad)
#> drift = (mean over the second half - value at the fitting length) / SD over the second half
#> (SD floored at 10% of the SD of the whole trajectory)
#>
#> Component F, transition t1 (fitting used 3 proposals per free dyad)
#> observed at fitting length second half mean drift
#> edges 380.00 384.00 673.24 5.31
#> nodematch.Party.R 65.00 75.00 133.18 5.85
#> gwdeg.fixed.0.2 80.79 78.64 85.50 25.37
#> gwesf_pos.fixed.0.6 631.76 647.07 1212.20 5.73
#> gwese_pos.fixed.0.6 689.33 696.95 1185.83 7.50
#> gwese_neg.fixed.0.6 1917.67 1912.40 42.09 -24.12
#>
#> Component P, transition t1 (fitting used 3 proposals per free dyad)
#> observed at fitting length second half mean drift
#> edges 138.00 153.00 270.27 11.37
#> nodematch.Party.R 22.00 32.00 63.37 7.07
#> gwdeg.fixed.0.2 60.96 63.54 72.99 5.34
#> gwesf_pos.fixed.0.6 180.37 211.67 421.36 10.06
#> gwese_pos.fixed.0.6 188.08 222.91 275.94 5.55
#> gwese_neg.fixed.0.6 624.99 555.63 148.31 -15.11
plot(dg)With three proposals per free dyad the simulated networks stay close
to the observed ones. Longer simulations move away from them: in the
first formation network, positive ties grow from 380 to about 673, and
the negative ties weighted by shared enemies (\(\text{gwese}^-\)) fall from 1918 to about
42. All-negative triangles almost disappear, while unbalanced
++- triangles become far more frequent than observed (see
the goodness-of-fit plots). At these parameter values the model’s
stationary distribution is far from the data, and the auxiliary
simulations used for fitting are far from stationarity. The sampled
distribution is then a local, contrastive-divergence-like approximation
around the observed networks rather than the posterior of the model.
Longer auxiliary simulations (aux.iters, or
control.bstergm(aux.visits =)) move the approximation
towards the posterior of the model, at a cost that grows linearly. For
models like this one, aux_diagnostics() and
bgof() should be checked before the estimates are
interpreted, and models less prone to degeneracy should be
considered.
The interaction layer is a sequence of binary networks and follows an
ordinary separable temporal ERGM. Formation networks contain the
previous interactions, and persistence networks are contained in them.
It is fitted with bstergm() and ordinary ergm terms.
bstergm() accepts a signed series, whose interactions it
uses, or any list of binary adjacency matrices or network
objects, which network_series() converts:
fit_x <- bstergm(~ edges + nodematch("Party", diff = TRUE, levels = "R") +
gwdegree(0.2, fixed = TRUE) + gwesp(0.6, fixed = TRUE),
data = senate, iters = 35000,
burn.in = 10000, thin = 25, n_chains = 2, cores = 2, seed = 2025,
prior.mean = c(-1, 0, 0, 0), prior.sigma = 25)summary(fit_x)
#> Posterior summary (AMH, 35000 iterations, 10000 burn-in, thin 25)
#>
#> Formation (F) - binary network
#> chains: 2 | acceptance: 0.227 0.227
#> Mean SD Naive SE Time-series SE 2.5% 50% 97.5%
#> edges 1.426 4.325 0.097 0.100 -7.291 1.410 9.451
#> nodematch.Party.R -0.124 0.102 0.002 0.003 -0.323 -0.125 0.075
#> gwdeg.fixed.0.2 0.081 5.038 0.113 0.128 -9.476 0.037 10.047
#> gwesp.fixed.0.6 -1.220 2.374 0.053 0.055 -5.614 -1.208 3.586
#> ESS Rhat
#> edges 1873.846 1.000
#> nodematch.Party.R 1589.867 1.002
#> gwdeg.fixed.0.2 1539.744 1.000
#> gwesp.fixed.0.6 1873.980 1.000
#>
#> Persistence (P) - binary network
#> chains: 2 | acceptance: 0.164 0.168
#> Mean SD Naive SE Time-series SE 2.5% 50% 97.5%
#> edges 0.184 0.340 0.008 0.009 -0.530 0.198 0.823
#> nodematch.Party.R -0.394 0.130 0.003 0.004 -0.638 -0.397 -0.129
#> gwdeg.fixed.0.2 -2.218 3.191 0.071 0.088 -8.344 -2.378 4.568
#> gwesp.fixed.0.6 0.270 0.163 0.004 0.004 -0.030 0.264 0.617
#> ESS Rhat
#> edges 1355.801 1.014
#> nodematch.Party.R 1221.122 1.000
#> gwdeg.fixed.0.2 1323.610 1.002
#> gwesp.fixed.0.6 1365.571 1.014
#>
#> Total time: 30.7minSome of these posteriors are as wide as the prior. In these dense
networks the pairs that could start interacting already have many shared
partners, and all senators have high degrees. On the formation sample
space the change statistic of gwesp(0.6) is therefore
almost constant and that of gwdegree(0.2) is zero, so these
parameters are not identified separately from edges
(ergmMPLE() warns about the linear dependence). The draws
of edges and gwesp are perfectly
correlated:
round(cor(as.matrix(as.mcmc.list(fit_x, components = "F"))), 2)
#> edges nodematch.Party.R gwdeg.fixed.0.2 gwesp.fixed.0.6
#> edges 1.00 0.00 -0.01 -1.00
#> nodematch.Party.R 0.00 1.00 0.01 -0.01
#> gwdeg.fixed.0.2 -0.01 0.01 1.00 0.01
#> gwesp.fixed.0.6 -1.00 -0.01 0.01 1.00Their combination, which gives the probability that a new interaction
forms, is well determined, so predictions are not affected, but the
separate parameters should not be interpreted. Dropping
gwdegree(), or using terms whose change statistics vary on
the sample space, avoids the problem.
simulate() returns posterior predictive replicates of
the observed transitions: the formation and persistence networks and the
signed networks they imply at each time point, given the observed
interactions:
sims <- simulate(fit, nsim = 1, seed = 2)
sapply(sims[[1]]$y, signed_triad_census)
#> S100 S101
#> +++ 1271 1700
#> ++- 2157 3042
#> +-- 1821 2435
#> --- 594 770predict() forecasts future networks. A
bstergm() fit alone forecasts binary networks:
summary(predict(fit_x, horizon = 1, nsim = 20, seed = 3, verbose = FALSE))
#> Forecast T+1:
#> start mean 2.5% 97.5%
#> ties 1173 1149.8 1129.0 1183.2
#> triangles 7947 6129.0 5707.5 6690.5With a bstergm_sign() fit of the signs as well, it
forecasts signed networks. At each step it simulates the formation and
persistence of interactions, then the signs of the new and persisting
interactions:
fc <- predict(fit, fit_x, horizon = 1, nsim = 20, seed = 3, verbose = FALSE)
summary(fc)
#> Forecast T+1:
#> start mean 2.5% 97.5%
#> positive 363 476.8 363.2 639.2
#> negative 810 673.0 511.0 800.5
#> ties 1173 1149.8 1129.0 1183.2
#> +++ 1119 769.0 370.9 1487.8
#> ++- 114 1737.0 1276.3 2357.9
#> +-- 3521 2300.3 1791.0 2660.4
#> --- 3193 1322.8 538.7 2144.2Here the forecasts inherit the drift of the sign model described above.
formula can
be a list with one formula per transition. Lagged dyadic covariates of
the previous network are available as edgecov("lag.pos"),
edgecov("lag.neg") and
edgecov("lag.active").method = "ads" (or aea_ads()) runs a
population of interacting chains; init = "ads" uses it only
to find starting values for adaptive Metropolis-Hastings.prior.mean, prior.sigma and init
accept component-specific values as list(F = ..., P = ...).
A previous fit can be passed as init to continue its
chains.as_signed_network() can be used directly with
ergm::ergm() and the constraint
~active_signs.aea_amh(),
aea_ads() and simulate_stats() accept the
arguments of the first version of the package (then called B2Lstergm),
including multilayer formulas with the constraint term
L(~edges, active). That term is detected and dropped
automatically, and the formula is translated into the equivalent native
terms when possible.Caimo, A. and Friel, N. (2011). Bayesian inference for exponential random graph models. Social Networks, 33(1), 41–55.
Caimo, A. and Gollini, I. (2025). Separable models for dynamic signed networks. arXiv:2505.07669.
Haario, H., Saksman, E. and Tamminen, J. (2001). An adaptive Metropolis algorithm. Bernoulli, 7(2), 223–242.
Krivitsky, P. N. and Handcock, M. S. (2014). A separable model for dynamic networks. Journal of the Royal Statistical Society B, 76(1), 29–46.
Neal, Z. P. (2020). A sign of the times? Weak and strong polarization in the U.S. Congress, 1973–2016. Social Networks, 60, 103–112.