Week 3 Challenge Instructions

challenge_3
instructions
Degree and Density of a Network
Author

Hannah Rosenbaum

Published

March 6, 2023

Challenge Overview

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

Degree

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

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?

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

Density

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

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
got_marriages <- read.csv("C:/Users/18639/Social_Networks_Spring_2023/posts/_data/got/got_marriages.csv")
Code
graph <- graph_from_edgelist(as.matrix(got_marriages[, c("From", "To")]))
Code
# List/display data
head(got_marriages)
       From      To    Type  Notes Generation
1 Targaryen   Stark Married  R+L=J    Current
2 Baratheon Martell Engaged   died    Current
3 Baratheon   Stark Engaged broken    Current
4   Martell   Essos Married           Current
5   Martell   Reach  Affair           Current
6   Martell   Essos  Affair           Current
Code
# Show network size
vcount(graph)
[1] 20
Code
ecount(graph)
[1] 255
Code
# Is the graph bipartite
is_bipartite(graph)
[1] FALSE
Code
# Is teh graph weighted
is_weighted(graph)
[1] FALSE
Code
# Is the graph directed
is_directed(graph)
[1] TRUE
Code
# Listing vertex attributes
vertex_attr_names(graph)
[1] "name"
Code
# Listing edge attributes
edge_attr_names(graph)
character(0)
Code
# Perform a dyad census of the network
dyad.census(graph)
$mut
[1] 3

$asym
[1] 57

$null
[1] 130
Code
# Perform a triad census on the network
triad.census(graph)
 [1] 408 227 217 110  44  47   9  18  50   1   0   3   5   1   0   0
Code
# Compute the transitivity of the netwrok
transitivity(graph)
[1] 0.4411765
Code
# Find the average path length
average.path.length(graph)
[1] 1.86875
Code
# Find the width of the network
diameter(graph)
[1] 4
Code
#Compute the degree of the network
degree(graph)
  Targaryen       Stark   Baratheon     Martell       Essos       Reach 
         85          34          17          12          15          49 
      Septa       Dorne   Lannister        Vale  Riverlands  Crownlands 
          1           4          28          37          39          22 
Westerlands      Tyrell       North Beyond Wall        Frey       Tully 
         31          12          46           1          51           5 
      Arryn  Stormlands 
         10          11 
Code
# Build `nodes`
nodes <- data.frame("Name"=c(), "Total"=c(), "Out"=c(), "In"=c())
families <- unique.data.frame(as.data.frame(got_marriages[, c("To")]))

# Iterate across family names computing degrees
for (name in families) {
  obs <- data.frame("Name"=name, "Total"=degree(graph, v = name, mode = "total"), "Out"=degree(graph, v = name, mode = "out"), "In"=degree(graph, v = name, mode = "in"))

  nodes <- rbind(nodes, obs)
}
nodes
                   Name Total Out In
Stark             Stark    34  27  7
Martell         Martell    12   9  3
Essos             Essos    15   0 15
Reach             Reach    49  16 33
Septa             Septa     1   0  1
Dorne             Dorne     4   0  4
Targaryen     Targaryen    85  65 20
Lannister     Lannister    28  22  6
Vale               Vale    37  11 26
Riverlands   Riverlands    39   5 34
Crownlands   Crownlands    22   5 17
Westerlands Westerlands    31   3 28
Tyrell           Tyrell    12   8  4
North             North    46  13 33
Beyond Wall Beyond Wall     1   0  1
Frey               Frey    51  47  4
Tully             Tully     5   2  3
Stormlands   Stormlands    11   1 10
Arryn             Arryn    10   6  4
Baratheon     Baratheon    17  15  2
Code
# Calculate the average total degree
mean(nodes$Total)
[1] 25.5
Code
# Show histogram of total degree
hist(nodes$Total)

Code
# Show histogram of In degree
hist(nodes$In)

Code
# Show histogram of Out degree
hist(nodes$Out)

Code
# Compute the density of the network
graph.density(graph)
[1] 0.6710526
Code
random_network <- sample_gnp(length(V(graph)), graph.density(graph))
plot(random_network)

Code
# Find density of random network
graph.density(random_network)
[1] 0.6578947
Code
# Performa a dyad census of the random network
dyad.census(random_network)
$mut
[1] 125

$asym
[1] 0

$null
[1] 65
Code
# Perform a triad census on the random network
triad.census(random_network)
Warning in triad.census(random_network): At core/misc/motifs.c:1165 : Triad
census called on an undirected graph.
 [1]  38   0 283   0   0   0   0   0   0   0 490   0   0   0   0 329
Code
#Compute the degree of the random network
degree(random_network)
 [1] 13 14 13 15  9 15 11 13 10  8 12 13 16 11 12 15 13 12 12 13