Goodness of fit#
A model converges to the coefficients that reproduce the statistics in its
formula, but does it reproduce the rest of the network’s structure?
ergmx.gof() simulates networks from the model and compares their
distributions with the observed network’s, like R’s gof():
the degree distribution (in- and out-degrees if directed),
the edgewise shared partners: for each tie, the number of partners its two vertices share (outgoing two-paths if directed),
the minimum geodesic distances between pairs of vertices,
and the model statistics themselves.
import ergmx
from ergmx import datasets
mesa = datasets.load("faux.mesa.high")
fit = ergmx.ergm(
mesa, "edges + nodematch('Grade') + nodematch('Race') + gwesp(0.5, fixed=TRUE)", seed=1
)
result = fit.gof(seed=1)
result["espartners"]
Goodness-of-fit for edgewise shared partners
obs min mean max MC p-value
0 83 67 87.29 110 0.68
1 70 33 60.12 93 0.58
2 36 3 32.20 76 0.80
3 13 0 13.32 54 0.86
4 0 0 4.29 29 0.52
5 1 0 1.26 16 0.96
6 0 0 0.38 5 1.00
7 0 0 0.09 4 1.00
8 0 0 0.02 1 1.00
For each value, obs is the observed count, min, mean and max those of
the simulated networks, and the Monte Carlo p-value is ergm’s: twice the
share of simulated networks at least as far out as the observed one, on the
smaller side. Small p-values point at what the model misses.
result.plot();
The model reproduces the shared partners well, but not everything: the
observed network has more pairs of students 8 or more steps apart than its
simulations, so it is more elongated, made of longer chains of friendships,
than the model’s networks. Print result["distance"] for the numbers.
Options#
nsim sets the number of simulated networks (100 by default) and stats
which distributions to compute, among "degree", "idegree", "odegree",
"b1degree", "b2degree" (bipartite), "espartners", "dspartners",
"distance" and "model". The simulated networks are spaced
by the MCMC interval the fit ended with; interval= and burnin= change it.
gof also works without a fit, from a formula and coefficients:
ergmx.gof(mesa, "edges + nodematch('Grade')", [-6.0, 2.0])
By level, and network by network#
by="level" computes the distributions within each value of a vertex
attribute, and the ties between two values, as for the levels of a
multilevel network. For models of several networks,
ergmx.gofN() checks each network’s statistics against its simulations,
with Pearson residuals, as ergm.multi’s gofN(): see
Goodness of fit network by network.