Search code examples
rigraphsnadegreesundirected-graph

"sna" or "igraph" : Why do I get different degree values for undirected graph?


I am doing some basic network analysis using networks from the R package "networkdata". To this end, I use the package "igraph" as well as "sna". However, I realised that the results of descriptive network statistics vary depending on the package I use. Most variation is not too grave but the average degree of my undirected graph halved as soon as I switched from "sna" to "igraph".

library(networkdata)
n_1 <- covert_28

library(igraph)
library(sna)

n_1_adjmat <- as_adjacency_matrix(n_1)
n_1_adjmat2 <- as.matrix(n_1_adjmat)

mean(sna::degree(n_1_adjmat2, cmode = "freeman")) # [1] 23.33333
mean(igraph::degree(n_1, mode = "all")) # [1] 11.66667

This doesn't happen in case of my directed graph. Here, I get the same results regardless of using "sna" or "igraph".

Is there any explanation for this phenomenon? And if so, is there anything I can do in order to prevent this from happening?

Thank you in advance!


Solution

  • This is explained in the documentation for sna::degree.

    indegree of a vertex, v, corresponds to the cardinality of the vertex set N^+(v) = {i in V(G) : (i,v) in E(G)}; outdegree corresponds to the cardinality of the vertex set N^-(v) = {i in V(G) : (v,i) in E(G)}; and total (or “Freeman”) degree corresponds to |N^+(v)| + |N^-(v)|.

    (Note that, for simple graphs,

    indegree=outdegree=total degree/2.)

    A simpler example than yours makes it clear.

    library(igraph)
    library(sna)
    
    g = make_ring(3)
    plot(g)
    

    Simple ring graph with three nodes

    AM = as.matrix(as_adjacency_matrix(g))
    sna::degree(AM)
    [1] 4 4 4
    
    igraph::degree(g)
    [1] 2 2 2
    

    Vertex 1 has links to both vertices 2 and 3. These count in the in-degree and also count in the out-degree, so
    Freeman = in + out = 2 + 2 = 4
    The "Note" in the documentation states this.