Formulas#
A model is a list of terms, each contributing one or more network statistics.
ergmx takes it in two forms.
A string in R syntax, so models can be pasted from R:
import ergmx
from ergmx import datasets
mesa = datasets.load("faux.mesa.high")
ergmx.summary_stats(mesa, "edges + nodematch('Grade') + gwesp(0.5, fixed=TRUE)")
{'edges': 203.0,
'nodematch.Grade': 163.0,
'gwesp.fixed.0.5': 141.92580555386385}
summary_stats is R’s summary(net ~ formula): the statistics of a network.
The string follows R’s conventions: TRUE/FALSE (or True/False),
c(2, 3) and 2:3 for vectors, and a left-hand side such as
"mesa ~ edges + triangle" is ignored. Nothing in it is evaluated: arguments
must be literals.
Terms combined with +, which a program can build:
from ergmx import edges, gwesp, nodematch
formula = edges() + nodematch("Grade") + gwesp(0.5, fixed=True)
ergmx.summary_stats(mesa, formula)
{'edges': 203.0,
'nodematch.Grade': 163.0,
'gwesp.fixed.0.5': 141.92580555386385}
Both forms can be mixed: edges() + "triangle".
Operators#
Two operators wrap other terms, as in ergm. offset(term) fixes a term’s
coefficient instead of estimating it (see Fixed coefficients),
and F(~terms, ~filter) evaluates terms on the ties that pass a filter, such
as the ties within a group:
ergmx.summary_stats(mesa, "F(~edges + triangle, ~nodematch('Grade')) + offset(edges)")
{'F(nodematch("Grade"))~edges': 163.0,
'F(nodematch("Grade"))~triangle': 52.0,
'offset(edges)': 203.0}
In Python, F(edges() + triangle(), nodematch("Grade")) and offset(edges()).
Statistic names#
Names follow ergm, so tables can be compared with R’s output line by line. Terms with several statistics name each of them:
ergmx.summary_stats(mesa, "kstar(2:3) + nodefactor('Race') + nodematch('Sex', diff=TRUE)")
{'kstar2': 659.0,
'kstar3': 1010.0,
'nodefactor.Race.Hisp': 178.0,
'nodefactor.Race.NatAm': 156.0,
'nodefactor.Race.Other': 1.0,
'nodefactor.Race.White': 45.0,
'nodematch.Sex.F': 82.0,
'nodematch.Sex.M': 50.0}
Errors#
Unknown terms, bad arguments and terms that don’t apply to the network fail before any fitting, with a message that says why:
try:
ergmx.summary_stats(mesa, "edges + mutual") # mutual needs a directed network
except ValueError as error:
print(error)
mutual() is only implemented for directed networks
The term reference lists every term, its statistic and the networks it applies to.