Equivalence API Reference
Structural and regular equivalence, positional clustering, blockmodels and the consensus of several partitions.
Equivalence measures
SNA.sedist — Function
sedist(net; method=:hamming, diag=false, ignore_eval=true, attr=:weight,
missing=:error) -> Matrix{Float64}Structural equivalence between every pair of vertices, R sna::sedist.
Vertex i's profile is its row and its column of the adjacency matrix, the ties it sends followed by the ties it receives. As in sna (and Wasserman & Faust, eq. 9.4), the comparison of i and j leaves out the cells that are not about third parties: i's and j's self cells and the ties between i and j (profile positions i, j, n+i, n+j). Two actors with identical ties to and from everyone else are therefore equivalent whether or not they are tied to each other. diag=true keeps every cell.
:hamming(default): the number of differing cells (for valued data, the sum of absolute differences), sna's default;:correlation: the Pearson correlation of the two profiles (a similarity;0where it is undefined, as in sna, including the diagonal entry of a vertex with a constant profile);:euclidean: the Euclidean distance between the profiles;:gamma: Goodman–Kruskal gamma,(agreements − disagreements) / (agreements + disagreements);:exact:1if the profiles differ anywhere, else0.
missing=:error refuses masked dyads (sna would drop them pairwise); missing=:face keeps each masked dyad in the profiles at its stored face value.
Example
using SNA
net = network(4)
add_edges!(net, [(1, 2), (2, 1), (1, 3), (2, 3), (4, 1), (4, 2)])
sedist(net)[1, 2] # 0.0: 1 and 2 are structurally equivalent
sedist(net; method=:correlation)[1, 2] # 1.0SNA.regular_equivalence — Function
regular_equivalence(net; maxiter=100, tol=1e-6, missing=:error) -> Matrix{Float64}Regular equivalence similarity between every pair of vertices, in [0, 1].
Two vertices are regularly equivalent if they have equivalent ties to equivalent others (a recursive definition). The similarity is computed by iterative neighbour matching (CATREGE-style best-match averaging over in- and out-neighbourhoods), starting from all pairs equivalent, so it converges to the maximal regular equivalence: on a network in which every actor both sends and receives ties it can stay at 1 for every pair. It is in the spirit of White & Reitz regular equivalence but is not the Burt/White REGE algorithm, so scores are not directly comparable to UCINET's REGE. sna has no regular-equivalence function.
maxiter bounds the iterations (a warning is issued if tol is not reached). Weighted ties are not supported (ignore_eval=false raises an ArgumentError).
missing=:error refuses masked dyads; missing=:face includes them in the neighbourhoods at their stored face values.
Example
using SNA
# a boss, two managers, four workers
tree = network(7)
add_edges!(tree, [(1, 2), (1, 3), (2, 4), (2, 5), (3, 6), (3, 7)])
sim = regular_equivalence(tree)
sim[2, 3], sim[4, 7], sim[1, 4] # (1.0, 1.0, 0.0)Clustering and blockmodeling
SNA.equiv_clust — Function
equiv_clust(net; k=nothing, equiv_fun=sedist, method=:hamming,
cluster_method=:complete, missing=:error) -> Vector{Int}Cluster the vertices into k positions by equivalence, R sna::equiv.clust followed by cutree(k): agglomerative hierarchical clustering of an equivalence distance, cut at k clusters.
With the defaults this is what cutree(equiv.clust(dat)$cluster, k) returns in R: the sedist Hamming distance, clustered by complete linkage, with ties between equally close pairs broken as R's hclust breaks them (the pair with the lowest vertex first) and clusters numbered by their first vertex.
Arguments
k: the number of clusters. Defaults tomax(2, n ÷ 4);k ≥ ngives one vertex per cluster. (R returns the tree and leaves the cut tocutree; SNA.jl returns the labels of the cut.)equiv_fun: the equivalence function,sedist(sna's default) orregular_equivalence, whose similaritysis clustered as the distance1 − s.method: thesedistmethod (sna'smethod)::hamming(default),:euclidean,:exact, or the similarities:correlationand:gamma, which are negated into dissimilarities as sna'scode.diss=TRUEdoes.cluster_method: the linkage,:complete(sna's default),:average(UPGMA) or:single.missing=:error: masked dyads are refused;missing=:faceuses their stored face values in the equivalence matrix.
Average linkage on the profile correlation is equiv_clust(net; method=:correlation, cluster_method=:average), and clustering on regular equivalence is equiv_clust(net; equiv_fun=regular_equivalence).
Example
using SNA
net = network(6)
add_edges!(net, [(1, 3), (1, 4), (2, 3), (2, 4), (3, 5), (4, 6)])
equiv_clust(net; k=3) # [1, 1, 2, 2, 3, 3]
equiv_clust(net; k=3, method=:correlation, cluster_method=:average)SNA.blockmodel — Function
blockmodel(net; k, equiv_fun=sedist, method=:hamming,
cluster_method=:complete, missing=:error) -> NamedTupleA blockmodel, R sna::blockmodel(dat, equiv.clust(dat), k=k): the k positions found by equiv_clust (whose keywords are passed on) and the density of ties between them.
Returns (membership, block_matrix, n_blocks): the position of each vertex, the n_blocks × n_blocks matrix of block densities and the number of positions. As in sna, a diagonal block's density is taken over the ordered pairs of distinct vertices in it, so a position with a single vertex has an undefined (NaN) diagonal density. (R's block.membership lists the same positions in dendrogram order; membership is in vertex order.)
missing=:error refuses masked dyads; missing=:face lets them enter the clustering and the densities at their stored face values (densities are not renormalized over observed dyads).
Example
using SNA
net = network(4)
add_edges!(net, [(1, 2), (2, 1), (3, 4), (4, 3)])
bm = blockmodel(net; k=2)
bm.block_matrixSNA.consensus_clustering — Function
consensus_clustering(clusterings; threshold=0.5) -> Vector{Int}Combine several partitions of the same vertices into one. Two vertices are linked when they share a cluster in at least a fraction threshold of the partitions, and the consensus clusters are the connected components of that graph (so the result does not depend on the vertex order and is transitive). Clusters are numbered by their lowest vertex.
This is not R sna::consensus, which builds a consensus network from several informants' reports, so the name consensus is not used.
Example
using SNA
consensus_clustering([[1, 1, 2, 2], [1, 1, 2, 3], [1, 2, 2, 2]]) # [1, 1, 2, 2]