Types
Module
ERGMRank — Module
ERGMRank.jl - ERGMs for Rank-Order Relational DataExponential-family random graph models for networks whose edge values are ranks: each ego rank-orders all alters (Krivitsky & Butts 2017).
The sample space is the set of complete orderings of the alters by each ego, with the discrete-uniform CompleteOrderReference over orderings. Statistics follow the R ergm.rank package: higher rank values indicate higher standing (y[i,j] > y[i,k] means ego i ranks j over k).
Port of the R ergm.rank package from the StatNet collection.
Core types
ERGMRank.RankNetwork — Type
RankNetworkA network of complete rankings: each ego assigns every alter a distinct rank in 1:(n-1), with greater values indicating higher standing (get_rank(rnet, i, j) > get_rank(rnet, i, k) means ego i ranks j over k), following the R ergm.rank convention.
Fields
n::Int: Number of actorsranks::Matrix{Int}:ranks[i, j]is the rank egoiassigns alterj; the diagonal is 0
RankNetwork(ranks::Matrix{Int}; validate=true) validates the invariant (an ArgumentError naming the ego at fault otherwise); as_rank_network is the checked conversion from any integer matrix or from a directed NetworkCore.Network with a rank edge attribute; RankNetwork(n) builds the index-order ranking. A RankNetwork does not wrap a Network: it holds complete orderings, not dyads, so there is no missing-dyad mask (see as_rank_network for what an unobserved rank means here).
Example
using ERGMRank
m = [0 3 2 1;
3 0 1 2;
1 3 0 2;
2 1 3 0]
rnet = RankNetwork(m)
rnet.n # 4
get_rank(rnet, 1, 2) # 3 — ego 1 ranks actor 2 highest
is_valid_ranking(rnet) # true
rnet # RankNetwork(4 actors)ERGMRank.CompleteOrderReference — Type
CompleteOrderReferenceThe discrete-uniform reference measure over the complete orderings of the alters by each ego — the reference measure of ergm.rank. Under this reference every valid rank configuration has equal baseline weight, so it cancels out of all likelihood ratios; it is the sample-space constraint (each ego's ranks form a permutation) that carries the structure, maintained by the AlterSwap move. It is the only reference measure this package offers, and every RankERGMModel carries one.
Example
using ERGMRank
ref = CompleteOrderReference()
m = [0 3 2 1;
3 0 1 2;
1 3 0 2;
2 1 3 0]
fit = fit_ergm_rank(as_rank_network(m), [RankDeference()]; method=:mple)
fit.model.reference == ref # true: the reference of every rank fitERGMRank.RankERGMModel — Type
RankERGMModelRank-order ERGM specification: terms plus the observed RankNetwork under the CompleteOrderReference. Built by fit_ergm_rank (a copy of the network, the terms as a Vector{AbstractERGMTerm}) and carried on the RankERGMResult as fit.model, so simulate_rank_ergm(fit) and gof(fit) know what was fitted to what.
Fields
terms::Vector{AbstractERGMTerm}: the model's rank termsnetwork::RankNetwork: the observed rankingreference::CompleteOrderReference: the reference measure
Example
using ERGMRank
m = [0 3 2 1;
3 0 1 2;
1 3 0 2;
2 1 3 0]
fit = fit_ergm_rank(as_rank_network(m), [RankDeference()]; method=:mple)
fit.model isa RankERGMModel # true
name.(fit.model.terms) # ["rank.deference"]
fit.model.network.n # 4
string(fit.model) # "RankERGMModel(1 term, 4 actors, CompleteOrder)"ERGMRank.RankERGMResult — Type
RankERGMResultResults from fitting a rank-order ERGM with fit_ergm_rank.
method records which estimator produced it:
:mple— the swap-based maximum pseudo-likelihood;loglikis then the maximized swap pseudo-log-likelihood.:mcmle— the MCMC maximum likelihood estimate (the estimator of Rergm.rank);loglikis then the path-sampling (bridge) estimate of the log-likelihood,NaNwhen the fit was run withbridge_rungs=0.mcmc_convergencecarries the convergence report of the final MCMC sample (anERGM.MCMLEConvergence: iterations, step length, per-statistic t-ratios, Hotelling p-value, effective sample size;nothingfor:mple),terminationthe verdict of the stopping rule behindconverged(a NamedTuple(rule, p_value, precision, confidence, n_samples), asERGM.ERGMResultcarries;nothingfor:mple),mcmc_samplesthe statistics sampled at the returned coefficients, andbridge_rungsthe number of path-sampling segments behindloglik.
se_type records how std_errors/vcov were ACTUALLY obtained:
:hessian— the inverse negative Hessian of the swap pseudo-likelihood. The swap comparisons overlap, so these are anticonservative (2–3.9× too small on Newcomb's fraternity).inference_withheldistruewhen they are the default of amethod=:mplefit: the z values and p-values ofcoeftable/showare thenNaNandconfintrefuses, because no test or interval should be built on them; it isfalsewhense=:hessianwas passed explicitly (the written opt-in to the naive Wald table).:bootstrap— the parametric bootstrap offit_ergm_rank(...; se=:bootstrap)(simulate rank networks at θ̂ with the AlterSwap sampler, refit, empirical covariance).boot_replicatesthen holds then_boot × pmatrix of refitted coefficients — a replicate on which the swap MPLE did not exist (a statistic at the boundary of its attainable range in the SIMULATED ranking) is aNaNrow, excluded from the covariance and counted byapproximations.:fisher(:mcmleonly) — the inverse Fisher information estimated from the final MCMC sample,V = Σ̂⁻¹, plus the Monte-Carlo componentV·Σ_mc·Vof the estimating equation (Hunter & Handcock 2006, §3.3; the same covariance asERGM.mcmle's).vcov_fisherholds the Fisher part alone, sosqrt.(diag(vcov .- vcov_fisher))is the Monte-Carlo standard error andshowprints R's "MCMC %" column.
n_kept is the number of swap comparisons the finite coefficients were estimated on: every comparison (nobs), or, after a boundary statistic was dropped, the comparisons the dropped statistics do not change — the sample size bic uses, as R's logLik nobs attribute does.
separated_terms names the coefficients of a separated swap-MPLE (a combination of statistics no single swap of the observed ranking lowers, so the pseudo-likelihood has no finite maximum; decided by NetworkCore's exact verdict on the swap design). It is empty otherwise. When it is not empty the fit has converged == false, its z values and p-values are NaN, confint returns NaN and se=:bootstrap is refused, as the ecosystem's separation policy prescribes.
se_type is what NetworkCore.se_method(fit) reports, and what show reads before deciding whether the anticonservatism caveat is still true of this fit.
The full StatsAPI surface (coef, stderror, vcov, confint, loglikelihood, nobs, dof, aic, bic, coeftable) and the shared result-metadata protocol (fit_metadata, approximations, estimand, objective, is_exact, se_method, missing_method) are defined on it. The REPL prints the R-style block (show(io, MIME"text/plain"(), fit): estimator, log-likelihood, convergence, the coefficient table, the caveats); print/string give the one-line form.
Example
using ERGMRank
m = [0 3 2 1;
3 0 1 2;
1 3 0 2;
2 1 3 0]
fit = fit_ergm_rank(as_rank_network(m), [RankDeference()]; method=:mple)
fit isa RankERGMResult # true
fit.method # :mple
fit.converged # true
length(coef(fit)) == length(stderror(fit)) == 1 # true
string(fit) # "RankERGMResult(swap-MPLE, 1 term, converged)"Rank access and conversion
as_rank_network has two methods: from an integer rank matrix, and — the Network→RankNetwork adapter of the ecosystem conversion contract — from a directed NetworkCore.Network with a rank edge attribute (attr=:rank, missing=:error only, report=true for a NetworkCore.ConversionReport). NetworkCore.supports_missing(as_rank_network) is true and NetworkCore.missing_policies(as_rank_network) == (:error,).
ERGMRank.get_rank — Function
get_rank(rnet::RankNetwork, i, j) -> IntThe rank ego i assigns alter j (greater = higher standing); 0 for i == j.
Example
using ERGMRank
m = [0 3 2 1;
3 0 1 2;
1 3 0 2;
2 1 3 0]
rnet = as_rank_network(m)
get_rank(rnet, 1, 2) # 3: ego 1 ranks actor 2 highest
get_rank(rnet, 1, 4) # 1: ...and actor 4 lowest
get_rank(rnet, 1, 2) > get_rank(rnet, 1, 3) # true: 1 ranks 2 over 3
get_rank(rnet, 2, 2) # 0: no self-rankERGMRank.set_rank! — Function
set_rank!(rnet::RankNetwork, i, j, rank)Set the rank ego i assigns alter j. This can transiently break the per-ego permutation invariant; prefer swap_ranks!, which preserves it. Validate afterwards with is_valid_ranking. A self-rank (i == j) and a rank outside 1:(n-1) are refused with an ArgumentError.
Example
using ERGMRank
rnet = RankNetwork(4) # ego 1's row: 0 1 2 3
set_rank!(rnet, 1, 2, 3) # duplicates rank 3 in row 1...
is_valid_ranking(rnet) # false
set_rank!(rnet, 1, 4, 1) # ...so move the old 3 to the freed 1
is_valid_ranking(rnet) # true
rank_matrix(rnet)[1, :] # [0, 3, 2, 1]ERGMRank.swap_ranks! — Function
swap_ranks!(rnet::RankNetwork, ego, j, k)Swap the ranks ego assigns alters j and k (the AlterSwap move). This is the elementary move of the rank sample space: it always preserves the complete-ordering invariant. It is the proposal of simulate_rank_ergm and the perturbation behind every swap-MPLE comparison; swap_change gives a term's change under it without mutating anything. ego ∈ {j, k} is refused with an ArgumentError.
Example
using ERGMRank
rnet = RankNetwork(4) # ego 1's row: 0 1 2 3
swap_ranks!(rnet, 1, 2, 4) # ego 1 exchanges its ranks of 2 and 4
rank_matrix(rnet)[1, :] # [0, 3, 2, 1]
is_valid_ranking(rnet) # true — always
swap_ranks!(rnet, 1, 2, 4) # swapping back restores the ranking
rnet == RankNetwork(4) || rank_matrix(rnet) == rank_matrix(RankNetwork(4)) # trueERGMRank.is_valid_ranking — Function
is_valid_ranking(rnet::RankNetwork) -> BoolCheck that every ego's ranks form a permutation of 1:(n-1) — the structural invariant of complete rank-order data. Only set_rank! can break it (the constructors validate, swap_ranks! preserves it); fit_ergm_rank and simulate_rank_ergm refuse a network on which this is false.
Example
using ERGMRank
rnet = RankNetwork(4)
is_valid_ranking(rnet) # true
set_rank!(rnet, 1, 2, 3) # ego 1 now ranks actors 2 and 4 both 3
is_valid_ranking(rnet) # false
set_rank!(rnet, 1, 2, 1) # restored
is_valid_ranking(rnet) # trueERGMRank.as_rank_network — Function
as_rank_network(mat::AbstractMatrix) -> RankNetworkBuild a RankNetwork from a matrix of rank values (row i holds ego i's ranks of the alters, greater = higher standing). The matrix must hold complete orderings: integer entries, a zero diagonal, and each row's off-diagonal a permutation of 1:(n-1).
A matrix containing missing (or nothing) is refused with an ArgumentError: a RankNetwork cannot represent an unobserved rank, and there is no missing= policy to select because a rank has no face value to condition on — unlike a binary tie, an absent rank cannot be read as "0" (see the NetworkCore.jl missing-data guide for how the rest of the ecosystem treats unobserved dyads). Drop the actors whose rankings were not observed, or supply a complete ordering.
Example
using ERGMRank
m = [0 3 2 1;
3 0 1 2;
1 3 0 2;
2 1 3 0]
rnet = as_rank_network(m)
get_rank(rnet, 1, 2) # 3
rank_matrix(rnet) == m # trueas_rank_network(net::Network; attr=:rank, missing=:error, report=false)
-> RankNetwork (or (RankNetwork, ConversionReport) with report=true)Build a RankNetwork from a directed NetworkCore.Network whose edge attribute attr holds the rank ego i assigns alter j on the arc i → j (greater = higher standing) — the Julia counterpart of R's as.matrix(nw, attrname = "rank") on ergm.rank's newcomb networks. The conversion honours the ecosystem conversion contract (NetworkCore.jl docs/src/guide/conversion_invariants.md): what a RankNetwork can hold is preserved, what it cannot is rejected or reported.
- Preserved: the actor set (vertex
iis ego/alteri) and the ranks. - Rejected (an
ArgumentErrorthat says why):- an undirected network — a ranking is ego-specific, so
i's rank ofjandj's rank ofimust be two different arcs; - a two-mode network — every ego must rank every alter;
- a masked (unobserved) dyad —
require_observed(net, missing; context="as_rank_network");missing_policies(as_rank_network) == (:error,): there is no:facepolicy, because aRankNetworkholds complete orderings and a masked dyad has no rank to read at face value (unlike a binary tie, an absent rank cannot be read as "0"). Drop the actors whose rankings were not observed, orclear_missing_dyads!once every dyad really has been observed; - a dyad
i → jwith no arc or noattrvalue, or a non-integer value — the message names the ego, the alter and the attribute; - an ego whose ranks are not a permutation of
1:(n-1)(theRankNetworkinvariant) — the message names the ego and the attribute.
- an undirected network — a ranking is ego-specific, so
- Dropped and reported (
report=truereturns(rnet, ConversionReport)with onerecord_drop!entry each): every other edge attribute, every vertex attribute and every network attribute — aRankNetworkcarries none of them; and self-loop arcs, which have no rank (the diagonal is 0).is_lossless(report)istrueexactly whenattrwas the network's only attribute and it had no loops.
supports_missing(as_rank_network) is true: the routine has considered missingness, and its answer is to refuse.
Example
using ERGMRank, NetworkCore
net = network(3) # directed
ranks = [0 2 1;
1 0 2;
2 1 0]
for i in 1:3, j in 1:3
i == j && continue
add_edge!(net, i, j)
set_edge_attribute!(net, :rank, i, j, ranks[i, j])
end
rnet = as_rank_network(net) # attr=:rank
rank_matrix(rnet) == ranks # true
set_vertex_attribute!(net, :sex, ["m", "f", "m"])
rnet, rep = as_rank_network(net; report=true)
dropped_fields(rep) # [:sex]: a RankNetwork has no vertex attributes
is_lossless(rep) # false
set_missing_dyad!(net, 1, 2) # ego 1's rank of 2 is unobserved
try
as_rank_network(net)
catch err
err isa ArgumentError # true: no face value to condition on
endERGMRank.rank_matrix — Function
rank_matrix(rnet::RankNetwork) -> Matrix{Int}The matrix of rank values (a copy): row i is ego i's ranks of the alters (greater = higher standing), the diagonal 0. The inverse of as_rank_network on a matrix.
Example
using ERGMRank
m = [0 3 2 1;
3 0 1 2;
1 3 0 2;
2 1 3 0]
rnet = as_rank_network(m)
rank_matrix(rnet) == m # true
rank_matrix(rnet) === rnet.ranks # false: a copy, safe to editDisplay
RankNetwork, RankERGMModel and RankERGMResult follow NetworkCore.jl's display convention: two-argument show (used by print, string and inside containers such as the Vector{RankNetwork} of simulated draws) is a one-line form — RankNetwork(4 actors), RankERGMModel(1 term, 4 actors, CompleteOrder), RankERGMResult(MCMC MLE, 2 terms, converged) — and the MIME"text/plain" method the REPL calls prints the block (the rank matrix for up to 12 actors; the reference, network and term names for a model; the R-style coefficient table, convergence verdict and caveats for a fit). display(x) prints the block from a script.