challenge_3
Degree and Density of a Network
Author

Ben Ramsey

Published

March 6, 2023

Challenge Overview

Describe the many measures of degree, as well as density, of a network and compare

Degree

Code
climate <- read.csv("_data/climate0205-allop.csv", sep =  ";") %>% subset(select = -c(X))
climate.ig <- graph_from_adjacency_matrix(as.matrix(climate), weighted = TRUE, mode = "directed", add.rownames = TRUE)
climate.net <- as.network(as.matrix(climate), directed = TRUE, weighted = TRUE)
ggnet2(climate.net, label = colnames(climate))

If you have not done it before, evaluate the structure of the network (number of edges and vertices, dyad and triad census, etc.).

Code
print(climate.net)
 Network attributes:
  vertices = 34 
  directed = TRUE 
  hyper = FALSE 
  loops = FALSE 
  multiple = FALSE 
  bipartite = FALSE 
  total edges= 291 
    missing edges= 0 
    non-missing edges= 291 

 Vertex attribute names: 
    vertex.names 

No edge attributes
Code
dyad.census(climate.net)
     Mut Asym Null
[1,]  65  161  335
Code
triad.census(climate.net, mode = "graph")
        0    1   2   3
[1,] 3023 2016 787 158

This is the Swiss climate policy network, which has 34 vertices and 291 edges. 65 dyads are mutual and 161 are asymmetric. There are 158 triads with 3 edges, and 787 triads with 2 edges.

Compute the many measures of degree of the network of your choice, most preferably directed. Create a data frame called nodes where each row corresponds to a node and each column to an attribute of the node, namely the id, name or label, and different measures of degree: total, out-degree, and in-degree. What is the average degree of the network?

Code
climate.nodes = data.frame(name = climate.net %v% "vertex.names", degree = degree(climate.net), indegree = degree(climate.net, cmode = "indegree"), outdegree = degree(climate.net, cmode = "outdegree"))

mean(climate.nodes$degree)
[1] 17.11765

The average degree is 17.12.

Compute the distributions of those measures (or histograms if your network is small). What does this tell us about the structure of the network?

Code
hist(climate.nodes$degree)

Code
hist(climate.nodes$indegree)

Code
hist(climate.nodes$outdegree)

Code
summary(climate.nodes)
     name               degree         indegree        outdegree     
 Length:34          Min.   : 2.00   Min.   : 0.000   Min.   : 0.000  
 Class :character   1st Qu.:10.00   1st Qu.: 5.250   1st Qu.: 3.000  
 Mode  :character   Median :16.50   Median : 7.500   Median : 8.000  
                    Mean   :17.12   Mean   : 8.559   Mean   : 8.559  
                    3rd Qu.:21.00   3rd Qu.:11.000   3rd Qu.:11.000  
                    Max.   :40.00   Max.   :22.000   Max.   :29.000  

Most of the vertices in the network have a low degree.

Density

Compute the density of the network. Is this a global or local measure? Does it have a relationship with average degree?

Code
network.density(climate.net)
[1] 0.2593583

The density is .259, is a global measure and it does have a relationship with average degree, because it is a measure of the existing edges out of all the possible edges.

Random Network

Create a random (Erdos-Renyi) network with the same number of nodes and edges than the network of your choice. On igraph, the necessary commands are random.graph.game(n, p.or.m, type = c("gnp", "gnm"), directed = FALSE, loops = FALSE) (deprecated), sample_gnp(n, p, directed = FALSE, loops = FALSE) or sample_gnm(n, m, directed = FALSE, loops = FALSE). The p.or.m argument is to specify the probability of an edge \(p\) or the number of edges \(m\).

Compare the densities, dyad and triad censuses, and degree distributions, with the measures of the network of your choice. Does the comparison us something about the network of your choice?

Code
set.seed(1)
random.ig <- sample_gnm(34, 291)
dyad_census(random.ig)
Warning in dyad_census(random.ig): At motifs.c:857 :Dyad census called on
undirected graph
$mut
[1] 291

$asym
[1] 0

$null
[1] 270
Code
triad_census(random.ig)
Warning in triad_census(random.ig): At motifs.c:1055 :Triad census called on an
undirected graph
 [1]  686    0 2124    0    0    0    0    0    0    0 2334    0    0    0    0
[16]  840
Code
edge_density(random.ig)
[1] 0.5187166
Code
random.nodes <- data.frame(degree = igraph::degree(random.ig))
hist(random.nodes$degree)

The Swiss climate policy network has a long right tail when it comes to total degree and it is less dense and there are more complete triads compared to the randomly generated graph.