Getting Started

Create a count network with an explicit edge-value attribute, choose its admissible support and reference measure, and fit a baseline before adding dependence terms. Inspect both the estimator caveats and the finite-support diagnostics when interpreting a fit.

Before you begin

Counts must be nonnegative integers on a loop-free, one-mode network; continuous weights are outside this model. Missing dyads are refused. The default estimator follows R's ergm.count (method=:auto): the count MPLE, which is then the exact MLE, for a dyad-independent formula, and Monte-Carlo maximum likelihood for a dyad-dependent one; method=:mple asks for the count MPLE explicitly. Unbounded reference measures use adaptive finite support, so inspect support diagnostics even for a dyad-independent model.

Installation

Use Julia 1.12 or newer and the shared workspace installation guide. These development packages are not yet registered; the guide prepares the required sibling checkouts and a Julia environment for the examples.

Run the blocks below in order in that environment. They build on variables from earlier steps; stochastic examples use seeded random number generators where shown.

Basic Workflow

The typical ERGMCount.jl workflow consists of four steps:

  1. Create or load a count-valued network – Prepare network data with integer edge weights
  2. Choose a reference measure – Select the baseline distribution for edge values
  3. Define count terms – Specify which network statistics to include
  4. Fit the model – Estimate coefficients via MPLE

Step 1: Create a Count-Valued Network

Count-valued networks use the Network type from NetworkCore.jl with the counts stored as the :weight edge attribute. That name is fixed: it is what R's ergm.count selects per call with response="w". If your counts live under another attribute, pass fit_ergm_count(net, terms; weight=:w) (the fit runs on a copy carrying them under :weight); a network that has edges but no :weight at all is refused rather than fitted as a 0/1 network, and a non-integer weight (2.5, "3") is refused naming the dyad.

using NetworkCore
using ERGMCount

# Create a directed network with 10 nodes
net = network(10; directed=true)

# Add edges with integer weights
add_edge!(net, 1, 2)
set_edge_attribute!(net, :weight, 1, 2, 3)   # 3 interactions from 1 to 2

add_edge!(net, 2, 1)
set_edge_attribute!(net, :weight, 2, 1, 1)   # 1 interaction from 2 to 1

add_edge!(net, 1, 3)
set_edge_attribute!(net, :weight, 1, 3, 5)   # 5 interactions from 1 to 3

add_edge!(net, 3, 2)
set_edge_attribute!(net, :weight, 3, 2, 2)   # 2 interactions from 3 to 2

Creating from a Matrix

For larger networks, build from a weight matrix:

# Weight matrix: w[i,j] = number of interactions from i to j
W = [0 3 5 0;
     1 0 0 2;
     0 2 0 4;
     3 0 1 0]

n = size(W, 1)
net = network(n; directed=true)

for i in 1:n, j in 1:n
    if W[i, j] > 0
        add_edge!(net, i, j)
        set_edge_attribute!(net, :weight, i, j, W[i, j])
    end
end

Inspecting Network Properties

println("Nodes: ", nv(net))          # Number of vertices
println("Edges: ", ne(net))          # Number of non-zero edges
println("Directed: ", is_directed(net))

# Access edge weights
weights = get_edge_attribute(net, :weight)
println("Total weight: ", sum(values(weights)))

Step 2: Choose a Reference Measure

The reference measure determines the baseline distribution for edge values. This is the key distinction from binary ERGMs.

# Poisson reference: factorial downweighting on nonnegative integers
ref = PoissonReference(1.0)

# Geometric reference: constant reference weight on nonnegative integers
ref = GeometricReference()

# Binomial: for bounded counts (0 to n)
ref = BinomialReference(10)

# Discrete Uniform: equal probability on {0, 1, ..., max}
ref = DiscUnifReference(10)

Choosing the Right Reference

ReferenceSupport and baseline weight
PoissonReference(1.0)Nonnegative integers, weight 1/y!
GeometricReference()Nonnegative integers, constant weight; the fitted model must be normalizable
BinomialReference(n)Counts from 0 to n, with binomial multiplicity weights
DiscUnifReference(b)Counts from 0 to b, with constant weight
DiscUnif2Reference(a, b)Counts from a to b, with constant weight

Choose support from what could have been observed, then assess the reference measure together with the model terms. A uniform reference is still a modeling assumption; observed variance alone does not select a reference.

See Reference Measures for a detailed comparison.

Step 3: Define Count Terms

Count terms are sufficient statistics computed from the valued adjacency matrix:

# Basic terms
terms = [
    SumTerm(),           # Total edge weight: Sigma y_ij
    NonzeroTerm(),       # Number of non-zero dyads: Sigma I(y_ij != 0)
    CountMutualTerm(),   # Weighted mutuality: Sigma min(y_ij, y_ji)
]

Exploring Available Terms

ERGMCount.jl provides terms organized by type:

CategoryTermsDescription
Basic (dyad-independent)SumTerm, NonzeroTerm, GreaterthannTerm, CountAtleastnTerm, SmallerthanTerm, EqualToTerm, InIntervalTermAggregate edge value statistics, R's sum/nonzero/greaterthan/atleast/smallerthan/equalto/ininterval
Covariates (dyad-independent)CountNodeMatchTerm, CountNodeFactorTerm, CountAbsDiffTerm, CountNodeCovTerm, CountNodeOCovTerm, CountNodeICovTerm, CountEdgeCovTermR's valued nodematch/nodefactor/absdiff/nodecov/nodeocov/nodeicov/edgecov (form="sum"/"nonzero")
StructuralCountMutualTerm (forms :min, :nabsdiff, :geometric, :product, :threshold), TransitiveWeightsTerm, CyclicalWeightsTermR's valued mutual, transitiveweights, cyclicalweights
Structural, no R counterpartTransitiveTiesTerm, CyclicalTiesTermTriple-wise minimum sums (not validated against R)
DegreeNodeOSumTerm, NodeISumTerm, NodeSumTermNode strength heterogeneity

Example: Comprehensive Model

terms = [
    # Edge value effects
    SumTerm(),                  # Overall activity level
    NonzeroTerm(),              # Network density
    GreaterthannTerm(3),        # High-frequency ties

    # Structural effects
    CountMutualTerm(),          # Reciprocity in intensity

    # Degree heterogeneity
    NodeOSumTerm(),             # Sender activity variance
    NodeISumTerm(),             # Receiver popularity variance
]

Step 4: Fit the Model

The examples below fit a 12-node seeded network (the 4-node matrix above has too few dyads to say much):

using Random
rng = Xoshiro(1)
net = network(12; directed=true)
for i in 1:12, j in 1:12
    if i != j && rand(rng) < 0.2
        add_edge!(net, i, j)
        set_edge_attribute!(net, :weight, i, j, rand(rng, 1:5))
    end
end
terms = [SumTerm(), NonzeroTerm(), CountMutualTerm()]

result = ergm_count(net, terms; reference=PoissonReference(1.0), method=:mple)

mutual.min makes this formula dyad-dependent, so the default (method=:auto) would fit it by Monte-Carlo maximum likelihood, as R does; method=:mple asks for the fast count MPLE first, to show what it does and does not report. fit_ergm_count is the standardized name of the same function (ergm_count is the R-faithful alias).

Key Parameters

ParameterDescriptionDefault
referenceReference measurePoissonReference()
method:auto (R's rule: :mple for a dyad-independent formula, :mcmle otherwise), :mple (maximum pseudo-likelihood; the exact MLE of a dyad-independent model) or :mcmle (Monte-Carlo maximum likelihood, ergm.count's estimator); a keyword of the estimator not chosen is an ArgumentError:auto
max_valFixed top of the enumerated support for an unbounded reference; nothing chooses it by error controlnothing
support_tolStop doubling the support once the estimates move by at most this many SEs and the omitted tail is this small1e-3
seMPLE only: :hessian (inverse pseudo-Hessian) or :bootstrap (parametric bootstrap; n_boot, rng). The default reports Hessian standard errors and, for a dyad-dependent model fit with method=:mple, withholds z, p and intervalsnothing
maxiterMaximum Newton iterations100
rngRandomness source for the bootstrap and the MCMLERandom.default_rng()
warnfalse silences the fit diagnostics (still recorded)true

Viewing Results

println(result)

Output:

Count ERGM Results
==================
Reference: PoissonReference(1.0)
Method: mple (maximum pseudo-likelihood: an approximation under dyadic dependence; the default method=:auto fits the MCMLE here, as R's ergm.count does)
Support:   0:40  (TRUNCATED — reference is unbounded; chosen by doubling until max|Δθ| ≤ 0.001·SE and the omitted tail ≤ 0.001; achieved 3.57e-10·SE, tail 1.61e-10)
Boundary mass: 6.97e-32 (max over dyads, at the fitted coefficients)
Pseudo-log-likelihood: -115.8861
AIC: 237.77, BIC: 246.42  (pseudo-likelihood; compare only across models on the same network and support)
Converged: true
Std. errors: inverse pseudo-Hessian

Coefficients:
            Estimate  Std.Error  z value  Pr(>|z|)
sum           1.0529     0.1221      NaN       NaN
nonzero      -4.2415     0.4265      NaN       NaN
mutual.min    0.2183     0.1729      NaN       NaN
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Note: z values and p-values are not reported (NaN). This model contains
dyad-dependent terms and was fit by maximum pseudo-likelihood; the
standard errors shown are the naive inverse pseudo-Hessian ones, which
treat dependent dyads as independent and under-cover (95% Wald intervals
covered 0.70-0.91 in simulation), so no test or interval is built on
them. The point estimates are not the MLE that R's ergm.count reports.
For inference refit with method=:mcmle (maximum likelihood) or
se=:bootstrap; se=:hessian requests the naive Wald table explicitly.

The support line says the enumerated count support was chosen by doubling (0:40 here) and how far the estimates moved at the last doubling; the note identifies the pseudo-likelihood fit with a dyad-dependent term (mutual.min). Its inverse pseudo-Hessian standard errors are too small, so no z value, p-value or confidence interval is built on them by default. se=:bootstrap estimates the covariance by simulation and refitting under the fitted model; it does not remove the approximation in the point estimate.

For inference on this model, fit it by maximum likelihood — the estimator R's ergm.count uses, and the default for a dyad-dependent formula:

mle = ergm_count(net, terms; reference=PoissonReference(1.0), rng=Xoshiro(2))
println(mle)
Count ERGM Results
==================
Reference: PoissonReference(1.0)
Method: mcmle (Monte-Carlo maximum likelihood, Gibbs sampler)
Support:   0:40  (TRUNCATED — reference is unbounded; chosen by doubling until max|Δθ| ≤ 0.001·SE and the omitted tail ≤ 0.001; achieved 3.57e-10·SE, tail 1.61e-10)
Boundary mass: 4.23e-31 (max over dyads, at the fitted coefficients)
Log-likelihood: -116.2132  (path sampling; MC s.e. 0.0173)
AIC: 238.43, BIC: 247.07
Converged: true
Termination: confidence rule (R ergm 4), convergence test p-value 6.63e-19 (1024 draws, 1 sweep apart)
Std. errors: inverse Fisher information + Monte-Carlo error

Coefficients:
            Estimate  Std.Error  z value  Pr(>|z|)
sum           1.0499     0.1217   8.6293    <1e-16 ***
nonzero      -4.2388     0.4275  -9.9158    <1e-16 ***
mutual.min    0.2341     0.2517   0.9300    0.3524
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(The path-sampling grid points run on threads, each with its own seeded generator, so the output is the same for any thread count.) The standard error of mutual.min is 0.25 here against the naive 0.17 above: that gap is why the MPLE table withholds z and p.

Accessing Results Programmatically

Prefer the StatsAPI verbs to the fields:

coef(result)             # [1.0529, -4.2415, 0.2183]
stderror(result)         # standard errors
vcov(result)             # covariance matrix
confint(mle)             # normal-theory 95% limits, one row per coefficient
                         # (`confint(result)` is refused: dyad-dependent MPLE)
coeftable(result)        # the table `show` prints
loglikelihood(result)    # pseudo-log-likelihood
nobs(result), dof(result)
aic(result), bic(result) # pseudo-likelihood criteria

result.converged         # true
result.support_control   # :converged — the doubling stopped
result.max_val           # 40

Interpreting Coefficients

Coefficients in count ERGMs relate to the conditional distribution of each edge value given the rest of the network.

CoefficientInterpretation
SumTerm > 0Higher edge values are more likely overall
NonzeroTerm > 0More edges (ties) than expected under reference
CountMutualTerm > 0Tendency for reciprocal intensity matching (mutual.min)
TransitiveWeightsTerm > 0Strong two-paths go with strong closing ties
NodeOSumTerm > 0Activity heterogeneity (some nodes send more)
NodeISumTerm > 0Popularity heterogeneity (some nodes receive more)

Example interpretations:

  • SumTerm = 0.2 with Poisson reference: Edge values are shifted upward; the conditional mean for each dyad increases by a factor of exp(0.2) = 1.22
  • NonzeroTerm = -2.0: A strong penalty on having edges, making sparse networks more likely (analogous to the edges term in binary ERGMs)
  • CountMutualTerm = 0.5: Reciprocal dyads tend to have matched intensities

Complete Example

using NetworkCore
using ERGMCount
using Random
using ERGM: compute, name   # generic term interface shared with ERGM.jl
using Statistics: mean

Random.seed!(42)

# Create a count-valued communication network
n = 15
net = network(n; directed=true)

# Simulate edge weights with some structure
for i in 1:n, j in 1:n
    i == j && continue
    # Construct observed zero counts and positive counts
    rate = rand() < 0.3 ? rand(1:8) : 0
    if rate > 0
        add_edge!(net, i, j)
        set_edge_attribute!(net, :weight, i, j, rate)
    end
end

println("Network: $(nv(net)) nodes, $(ne(net)) edges")

# Define model terms
# (A squared-strength term such as NodeOSumTerm() would estimate a positive
# coefficient on these data, and under a Poisson reference that model is not
# normalisable whatever the data: the fit would warn, and the MCMLE and
# simulation would refuse it)
terms = [
    SumTerm(),              # Total communication volume
    NonzeroTerm(),          # Network density (ties vs non-ties)
    CountMutualTerm(),      # Reciprocity in communication frequency
]

# Fit with Poisson reference. The formula is dyad-dependent, so the default
# is Monte-Carlo maximum likelihood (pass `rng` for reproducible draws)
result = ergm_count(net, terms; reference=PoissonReference(1.0), rng=Xoshiro(1))

# Display results
println(result)

# Check convergence
if result.converged
    println("\nModel converged successfully")
else
    println("\nWarning: Model did not converge")
end

Simulating Count Networks

After fitting a model, simulate new networks from the estimated parameters:

# Simulate 10 networks from the fitted model. burnin/interval are Gibbs
# sweeps (every dyad redrawn once per sweep); left unset they follow the
# dyad-scaled rule shared with ERGM.jl — 20 sweeps of burn-in, 1 between
# draws on any network with more than ten nodes — and the draws use the
# fit's own support
sim_nets = simulate_count_ergm(result; n_sim=10, rng=Xoshiro(3))

# ... or spell the controls out
sim_nets = simulate_count_ergm(result; n_sim=10, burnin=50, interval=5, rng=Xoshiro(3))

# Compare observed vs simulated statistics
for term in terms
    obs = compute(term, net)
    sim_vals = [compute(term, sn) for sn in sim_nets]
    println("$(name(term)): observed=$(round(obs, digits=2)), " *
            "simulated mean=$(round(mean(sim_vals), digits=2))")
end

Comparing Models

# Model 1: Basic effects
terms1 = [SumTerm(), NonzeroTerm()]

# Model 2: Add structural effects
terms2 = [SumTerm(), NonzeroTerm(), CountMutualTerm()]

# The count MPLE for both, so the criteria are on the same (pseudo-likelihood)
# scale: compare only on the same network, reference and support
result1 = ergm_count(net, terms1; reference=PoissonReference(), method=:mple)
result2 = ergm_count(net, terms2; reference=PoissonReference(), method=:mple)

println("Model 1: AIC=", round(aic(result1), digits=2), " BIC=", round(bic(result1), digits=2))
println("Model 2: AIC=", round(aic(result2), digits=2), " BIC=", round(bic(result2), digits=2))

Comparing Reference Measures

# Same terms, different references
terms = [SumTerm(), NonzeroTerm(), CountMutualTerm()]

result_poisson = ergm_count(net, terms; reference=PoissonReference(1.0), method=:mple)
result_geom = ergm_count(net, terms; reference=GeometricReference(), method=:mple)

println("Poisson coefficients: ", round.(coef(result_poisson), digits=3))
println("Geometric coefficients: ", round.(coef(result_geom), digits=3))

Best Practices

  1. Start with SumTerm and NonzeroTerm: These are analogous to intercept terms and control for overall scale and density
  2. Choose reference carefully: Match the reference measure to your data characteristics (see Reference Measures)
  3. Check convergence: an unconverged fit warns and prints "Converged: false"; verify result.converged and result.support_stable
  4. Start simple: Begin with basic terms before adding structural effects
  5. Validate with simulation: Compare simulated network statistics to observed ones
  6. Scale matters: the Poisson rate λ in PoissonReference(λ) is fixed, not estimated; it is confounded with the sum coefficient, so keep it at 1 unless a baseline rate is known

Next Steps