Utilities

Layout algorithms, random-graph generators, and the missing-data contract that every SNA.jl measure honours.

Missing Data

Every descriptive measure in SNA.jl takes a missing= keyword. A masked dyad is unobserved, not absent, so by default (missing=:error) a measure refuses to run on a network with masked dyads rather than silently computing a number from the stored face value of a tie nobody observed. Pass missing=:face to opt in to face values explicitly.

using SNA

net = Network(3)
add_edge!(net, 1, 2)
degreecent(net)
set_missing_dyad!(net, 1, 2)
try
    degreecent(net)
catch err
    @assert err isa ArgumentError
end
degreecent(net; missing=:face)  # explicit opt-in to stored values

This is the ecosystem missing-data contract. It is defined in NetworkCore.jl and re-exported here, so require_observed, supports_missing and MISSING_POLICIES are all callable unqualified after using SNA. require_observed is the guard the measures call; supports_missing is the trait a routine opts into.

No SNA measure implements a principled missing-data estimator — there is no listwise deletion, no density-over-observed-dyads, no imputation — so none declares supports_missing. The guard plus a documented :face treatment is the contract here. Layouts and random-graph generators are exempt: a layout draws a picture rather than reporting a statistic, and the generators create fresh unmasked networks.

The full contract is documented in the NetworkCore.jl manual: Ecosystem Contracts.

Result Metadata

netlm and netlogit implement the ecosystem's shared result-metadata protocol, so NetworkCore.fit_metadata(result) reports what the fit actually did. The per-result-type methods are documented on the Measures page; the protocol itself is in the NetworkCore.jl manual under Result Metadata.

Layouts

Vertex-coordinate algorithms for plotting, following R's sna::gplot.layout.*. Each returns an n × 2 coordinate matrix.

SNA.layout_fruchterman_reingold — Function
layout_fruchterman_reingold(net; iterations=100, rng=Random.default_rng()) -> Matrix{Float64}

Force-directed layout (Fruchterman & Reingold 1991). Connected vertices attract, all vertex pairs repel; positions settle over iterations steps with a cooling schedule. Direction is ignored. The random starting positions are drawn from rng.

Example

using SNA, Random
net = network(5; directed=false)
add_edges!(net, [(1, 2), (2, 3), (3, 4), (4, 5)])
layout_fruchterman_reingold(net; rng=Xoshiro(1))
source
SNA.layout_kamada_kawai — Function
layout_kamada_kawai(net; maxiter=500, tol=1e-6) -> Matrix{Float64}

Kamada–Kawai (1989) layout: positions that minimize the Kamada–Kawai energy (the stress)

Σ_{i<j} (‖xᵢ − xⱼ‖ − dᵢⱼ)² / dᵢⱼ²

where dᵢⱼ is the geodesic distance on the network with direction ignored. The energy is minimized by stress majorization (Gansner, Koren & North 2004), which decreases it monotonically, starting from classical multidimensional scaling of the distances; the result is deterministic. Pairs in different components are placed at distance `max finite distance

  • 1. Iteration stops aftermaxiter` steps or when the relative decrease

of the energy falls below tol.

Before 0.2.0 this function returned the classical MDS starting point itself.

Example

using SNA
path = network(4; directed=false)
add_edges!(path, [(1, 2), (2, 3), (3, 4)])
xy = layout_kamada_kawai(path)
size(xy)                             # (4, 2)
source
SNA.layout_circle — Function
layout_circle(net) -> Matrix{Float64}

Place vertices evenly around the unit circle.

Example

using SNA
layout_circle(network(4))            # the four points (±1, 0), (0, ±1)
source
SNA.layout_random — Function
layout_random(net; rng=Random.default_rng()) -> Matrix{Float64}

Place vertices uniformly at random in the square [-1, 1]².

Example

using SNA, Random
layout_random(network(5); rng=Xoshiro(1))
source

Random Graphs

Random-graph generators following R's sna::rgraph and sna::rgnm, and the Erdős–Rényi rgnp. All draw from an rng keyword.

SNA.rgraph — Function
rgraph(n; m=1, tprob=0.5, mode=:digraph, rng=Random.default_rng())

Generate Bernoulli random networks (R sna::rgraph).

Arguments

  • n::Int: Number of vertices
  • m::Int=1: Number of networks to generate
  • tprob::Float64=0.5: Tie probability
  • mode::Symbol=:digraph: :digraph (directed) or :graph (undirected)
  • rng: Random number generator

Returns

A single Network when m == 1, otherwise a Vector{Network}.

Example

using SNA, Random
net = rgraph(10; tprob=0.2, rng=Xoshiro(1))
nets = rgraph(10; m=3, tprob=0.2, mode=:graph, rng=Xoshiro(2))
source
SNA.rgnm — Function
rgnm(n, m; directed=true, rng=Random.default_rng()) -> Network

Generate an Erdős–Rényi G(n, m) random network: exactly m edges placed uniformly at random among the possible dyads. (sna's rgnm(n, nv, m) takes the number of networks first; here a single network is returned.)

Example

using SNA, Random
ne(rgnm(10, 15; directed=false, rng=Xoshiro(1)))    # 15
source
SNA.rgnp — Function
rgnp(n, p; directed=true, rng=Random.default_rng()) -> Network

Generate an Erdős–Rényi G(n, p) random network: each possible edge is present independently with probability p.

Example

using SNA, Random
rgnp(20, 0.1; rng=Xoshiro(1))
source

Renamed in 0.2.0

These names were used during development and were never part of a release; they have no aliases. Use the new names. The full table, with the Graphs.jl functions SNA no longer extends, is in the R concordance and the README.

Old nameUse instead
bonacich_power(net)bonpow(net)
geodesic_distance(net)geodist(net).gdist
reciprocity(net; method=m)grecip(net; measure=m)
structural_equivalence(net; method=m)sedist(net; method=m) (default Hamming, a count)
consensus(clusterings)consensus_clustering(clusterings)
SNA.components(net)component_dist(net).membership
equiv_clust(net; method=:structural)equiv_clust(net; method=:correlation, cluster_method=:average)
equiv_clust(net; method=:regular)equiv_clust(net; equiv_fun=regular_equivalence, cluster_method=:average)
reps= (qaptest, netlm, netlogit)n_sim=
max_iter= (regular_equivalence)maxiter=