Whole Network Statistics

R
POL 491
Network Analysis

Whole Network Statistics

Lets use the Spanish High School Network you used earlier this week:

library(igraph)

Attaching package: 'igraph'
The following objects are masked from 'package:stats':

    decompose, spectrum
The following object is masked from 'package:base':

    union
edges <- read.csv(here::here("resources/network_data/spanish_hs_edges.csv"))
nodes <- read.csv(here::here("resources/network_data/spanish_hs_nodes.csv"))
net <- graph_from_data_frame(edges, vertices = nodes, directed = T)
net <- delete_edges(net, E(net)[E(net)$weight <= 0])

Density

Density can be calculated with edge_density() There is an option to turn on loops (edges that start and stop at the same node)

edge_density(net)
[1] 0.09688645

Average Degree

Degree can be calculated with degree() and then average it up with mean()

# note that this has gmode, instead of mode. why?
deg <- igraph::degree(net)
mean(deg)
[1] 20.15238

Component ratio:

Component ratio is not in the SNA package, but the formula is pretty simple:

\[ \textnormal{Component Ratio} = \frac{c-1}{n-1} \]

So we need to calculate the number of components (c), and the number of nodes (n).

comps <- igraph::components(net, mode = "strong")$no
n <- vcount(net)

(comps - 1) / (n - 1)
[1] 0.2788462

Connectedness

There is no native function to calculate connectedness but we have all the tools:

dist <- distances(net, mode = "out") ## Get the distances matrix
reachability <- is.finite(dist) ## Where are there finite paths?
diag(reachability) <- NA ## Ignore the diag
sum(reachability, na.rm = T) / (vcount(net) * (vcount(net) - 1))
[1] 0.7168498

Compactness

Again, no native function but we can get it relatively quickly in a similar manner:

dist <- distances(net, mode = "out") ## Get the distances matrix
diag(dist) <- NA ## Ignore the diag
sum(1 / dist, na.rm = T) / (vcount(net) * (vcount(net) - 1))
[1] 0.2914342

Reciprocity

We calculate reciprocity with the reciprocity() function.

The default is to calculate arc-reciprocity, but we can switch it to the other (less good) measure by setting mode="ratio"

reciprocity(net)
[1] 0.389414
edge_density(net)
[1] 0.09688645

Transitivity:

There is a transitivity() function, which works well with undirected networks. For directed networks we have to make use of the sna library and the gtrans() function.

transitivity(net) ## Treats as undirected
[1] 0.4361186
mat_net <- as_adjacency_matrix(net, sparse = F) # Convert to adj mat
sna::gtrans(mat_net) ## Call SNA function
[1] 0.4617656

Centralization

Finally, we have centralization. You can use a lot of different measures of centrality to measure centralization and so there are a suite of functions:

  • center_degree() - Uses degree centrality
  • center_eigen() - Uses eigen centrality
  • center_betw() - Uses betweenness

They also all output an object with multiple things to get the centralization score use $centralization

centr_betw(net)$centralization
[1] 0.09744318
centr_degree(net)$centralization
[1] 0.144878
centr_eigen(net)$centralization
[1] 0.7185371