Week 3 Challenge Instructions

challenge_3
instructions
Degree and Density of a Network
Author

Akhilesh Kumar

Published

May 6, 2023

Code
library(igraph)

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

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

    union
Code
library(network)
Warning: package 'network' was built under R version 4.2.3

'network' 1.18.1 (2023-01-24), part of the Statnet Project
* 'news(package="network")' for changes since last version
* 'citation("network")' for citation information
* 'https://statnet.org' for help, support, and other information

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

    %c%, %s%, add.edges, add.vertices, delete.edges, delete.vertices,
    get.edge.attribute, get.edges, get.vertex.attribute, is.bipartite,
    is.directed, list.edge.attributes, list.vertex.attributes,
    set.edge.attribute, set.vertex.attribute

Challenge Overview

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

Describe the Network Data

Code
got_marriages <- read.csv("_data/got/got_marriages.csv")
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
got_marriages.ig <-graph_from_data_frame(got_marriages, directed = TRUE)

Network Size, features and attributes

The “got_marriages” network consists of 20 nodes, which represent characters, and 255 edges, which represent the marriage relation between the characters. The edges are not weighted. The network is not bipartite, meaning that all nodes are of the same type, in this case, characters. Additionally, the network is directed, indicating that the marriage are mutual between the characters.

The analysis also shows that each node has a “name” attribute, which corresponds to the name of the character it represents. The names of all the characters are listed in the output. Additionally, each edge has “Type”, “Notes”,“Generation” attributes, which corresponds to the relationship between the characters connected by the edge.

This analysis provides useful insights into the relationships between the characters in the GoT Marriage Dataset. By studying the Edge Attribute Type, it is possible to identify the relationship status of the chcaracters.

The output shows that:

  • The network got_like_dislike has 20 nodes and 255 edges.
  • The network is not bipartite.
  • The network is directed.
  • The network doesn’t have weighted edges.
  • The only vertex attribute in the network is name, which gives the names of all the nodes.
  • The names of all the nodes in the network are shown in the output.
  • The edge attributes in the network “Type”, “Notes”,“Generation”
Code
# Number of vertices in got_marriages network:

vcount(got_marriages.ig)
[1] 20
Code
# Number of edges in got_marriages network:

ecount(got_marriages.ig)
[1] 255
Code
# Bipartite or single mode network verification in got_marriages network:

is_bipartite(got_marriages.ig)
[1] FALSE
Code
# Directed or undirected edges verification in got_marriages network

is_directed(got_marriages.ig)
[1] TRUE
Code
# Weighted or unweighted edges verification in got_marriages network

is_weighted(got_marriages.ig)
[1] FALSE
Code
# Is Connected verification in got_marriages network

is_connected(got_marriages.ig)
[1] TRUE
Code
# Vertex attributes for got_marriages network

vertex_attr_names(got_marriages.ig)
[1] "name"
Code
# Vertex attribute, Nodes in got_marriages network:

V(got_marriages.ig)$name
 [1] "Targaryen"   "Baratheon"   "Martell"     "Lannister"   "Tyrell"     
 [6] "Reach"       "North"       "Riverlands"  "Westerlands" "Stark"      
[11] "Vale"        "Arryn"       "Tully"       "Frey"        "Crownlands" 
[16] "Stormlands"  "Essos"       "Septa"       "Dorne"       "Beyond Wall"
Code
# Edge attributes for got_marriages network:

edge_attr_names(got_marriages.ig)
[1] "Type"       "Notes"      "Generation"
Code
E(got_marriages.ig)$Type
  [1] "Married" "Engaged" "Engaged" "Married" "Affair"  "Affair"  "Affair" 
  [8] "Affair"  "Affair"  "Married" "Married" "Married" "Married" "Affair" 
 [15] "Affair"  "Affair"  "Affair"  "Affair"  "Affair"  "Affair"  "Married"
 [22] "Affair"  "Married" "Married" "Married" "Married" "Married" "Married"
 [29] "Married" "Married" "Married" "Married" "Married" "Married" "Married"
 [36] "Married" "Affair"  "Married" "Married" "Affair"  "Married" "Married"
 [43] "Married" "Married" "Married" "Married" "Affair"  "Affair"  "Married"
 [50] "Married" "Married" "Married" "Married" "Married" "Married" "Married"
 [57] "Married" "Married" "Married" "Married" "Married" "Married" "Married"
 [64] "Married" "Married" "Married" "Affair"  "Married" "Affair"  "Married"
 [71] "Married" "Married" "Married" "Married" "Married" "Married" "Affair" 
 [78] "Married" "Married" "Married" "Married" "Married" "Married" "Married"
 [85] "Married" "Married" "Married" "Married" "Married" "Married" "Married"
 [92] "Married" "Married" "Married" "Affair"  "Married" "Married" "Married"
 [99] "Married" "Married" "Married" "Married" "Married" "Married" "Married"
[106] "Affair"  "Married" "Married" "Married" "Married" "Married" "Married"
[113] "Married" "Married" "Married" "Married" "Married" "Married" "Married"
[120] "Married" "Married" "Married" "Married" "Married" "Married" "Married"
[127] "Married" "Married" "Married" "Married" "Married" "Married" "Married"
[134] "Married" "Married" "Married" "Married" "Married" "Married" "Married"
[141] "Married" "Married" "Affair"  "Married" "Married" "Married" "Married"
[148] "Married" "Married" "Married" "Married" "Married" "Married" "Married"
[155] "Married" "Married" "Married" "Married" "Married" "Married" "Married"
[162] "Married" "Married" "Affair"  "Married" "Married" "Married" "Married"
[169] "Married" "Married" "Married" "Married" "Married" "Married" "Married"
[176] "Married" "Married" "Married" "Affair"  "Married" "Married" "Married"
[183] "Married" "Married" "Married" "Married" "Married" "Married" "Married"
[190] "Married" "Married" "Married" "Married" "Married" "Married" "Married"
[197] "Married" "Married" "Married" "Married" "Married" "Married" "Married"
[204] "Affair"  "Affair"  "Married" "Affair"  "Affair"  "Affair"  "Affair" 
[211] "Affair"  "Affair"  "Married" "Married" "Married" "Married" "Affair" 
[218] "Married" "Married" "Married" "Married" "Married" "Married" "Married"
[225] "Married" "Married" "Married" "Married" "Married" "Affair"  "Married"
[232] "Married" "Married" "Married" "Married" "Married" "Married" "Married"
[239] "Married" "Married" "Married" "Married" "Married" "Married" "Married"
[246] "Married" "Married" "Married" "Married" "Married" "Affair"  "Married"
[253] "Married" "Married" "Married"
Code
E(got_marriages.ig)$Notes
  [1] "R+L=J"           "died"            "broken"          ""               
  [5] ""                ""                ""                ""               
  [9] ""                ""                "died"            ""               
 [13] ""                ""                ""                ""               
 [17] ""                ""                ""                "cersei & jamie" 
 [21] ""                "renly, loras"    ""                ""               
 [25] ""                ""                ""                "died"           
 [29] ""                "Lannister"       "Lannister"       ""               
 [33] ""                ""                ""                ""               
 [37] "Lynesse"         "died"            "Cupps"           "Alysane Mormont"
 [41] ""                ""                ""                ""               
 [45] "Mooton"          ""                "Lancel"          "Tysha"          
 [49] "forced"          ""                ""                ""               
 [53] ""                ""                ""                ""               
 [57] "Littlefinger"    "died"            ""                ""               
 [61] ""                ""                ""                ""               
 [65] ""                ""                ""                ""               
 [69] ""                ""                ""                ""               
 [73] ""                "Bolton"          "died"            "died"           
 [77] "various"         "died"            "died"            ""               
 [81] ""                ""                ""                ""               
 [85] ""                ""                ""                ""               
 [89] ""                ""                ""                ""               
 [93] ""                "died, Bolton"    "bolton"          "bolton"         
 [97] "killed, bolton"  ""                ""                ""               
[101] ""                "talhart"         ""                ""               
[105] "Poole"           ""                ""                ""               
[109] ""                ""                ""                ""               
[113] "Nymeros"         ""                ""                ""               
[117] ""                ""                "Tywin"           ""               
[121] ""                ""                ""                ""               
[125] ""                ""                ""                ""               
[129] ""                ""                ""                ""               
[133] ""                ""                "Walder"          "Walder"         
[137] "Walder"          "Walder son"      "Walder"          "Walder"         
[141] "Walder son"      "Walder"          "Walder"          "Walder son"     
[145] "Walder son"      "Walder son"      "Walder son"      ""               
[149] ""                ""                ""                ""               
[153] ""                ""                ""                ""               
[157] ""                ""                ""                ""               
[161] ""                ""                ""                ""               
[165] ""                ""                ""                ""               
[169] ""                ""                ""                ""               
[173] ""                ""                ""                ""               
[177] ""                ""                ""                ""               
[181] ""                ""                ""                ""               
[185] ""                ""                ""                ""               
[189] ""                ""                ""                ""               
[193] ""                ""                ""                ""               
[197] ""                ""                "Blackfyre"       "Blackfyre"      
[201] ""                ""                ""                ""               
[205] ""                ""                ""                ""               
[209] ""                ""                ""                ""               
[213] ""                ""                ""                ""               
[217] ""                ""                ""                ""               
[221] ""                ""                ""                ""               
[225] ""                ""                ""                ""               
[229] ""                ""                ""                ""               
[233] ""                ""                ""                ""               
[237] ""                ""                ""                ""               
[241] ""                ""                ""                ""               
[245] ""                ""                ""                ""               
[249] ""                ""                ""                ""               
[253] ""                ""                ""               
Code
E(got_marriages.ig)$Generation
  [1] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
  [8] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
 [15] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
 [22] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
 [29] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
 [36] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
 [43] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
 [50] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
 [57] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
 [64] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
 [71] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
 [78] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
 [85] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
 [92] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
 [99] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
[106] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
[113] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[120] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[127] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[134] "Past"    "Current" "Current" "Current" "Current" "Current" "Current"
[141] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
[148] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[155] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[162] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[169] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[176] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[183] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[190] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[197] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[204] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[211] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[218] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[225] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[232] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[239] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[246] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[253] "Past"    "Past"    "Past"   

Diad & Triad Analysis

The first analysis calculates the dyad census, which classifies all possible pairs of nodes in the network into three categories: mutual ties, asymmetric ties and null ties. In this case, there were 3 mutual ties and 57 null ties, and 130 asymmetric ties.

The triad census counts the number of each possible combination of three nodes in a network, which are called triads. The function returns a vector with 16 values, each corresponding to the count of a particular triad type. In this case, there are 408 triads of type 003, 227 triads of type 012, 217 triads of type 102, 110 triads of type 021D, 44 triads of type 021Um, 47 triads of 021C type etc.

Finally, the code sums up the total number of triads returned by the triad census, which is 1140 This provides an overall picture of the patterns of relationships between nodes in the network.

Code
# Classify all Dyads in the got_like_dislike network: igraph

igraph::dyad.census(got_marriages.ig)
$mut
[1] 3

$asym
[1] 57

$null
[1] 130
Code
# Classify all Triads in the got_like_dislike Network: igraph

igraph::triad_census(got_marriages.ig)
 [1] 408 227 217 110  44  47   9  18  50   1   0   3   5   1   0   0
Code
# Total number of Triads returned by triad census

sum(igraph::triad_census(got_marriages.ig))
[1] 1140

Degree

The degree distribution of the “got_marriages” network, obtained from the GoT Marriage Dataset, provides insights into the connectivity of characters within the context of marriages. The degree of a node represents the number of connections or marriages it has with other characters in the network.

Network In Degree, Out Degree, Degree are calculated below:

Code
in_degree = igraph::degree(got_marriages.ig, mode = "in")
out_degree = igraph::degree(got_marriages.ig, mode = "out")
degree = igraph::degree(got_marriages.ig)

print(degree)
  Targaryen   Baratheon     Martell   Lannister      Tyrell       Reach 
         85          17          12          28          12          49 
      North  Riverlands Westerlands       Stark        Vale       Arryn 
         46          39          31          34          37          10 
      Tully        Frey  Crownlands  Stormlands       Essos       Septa 
          5          51          22          11          15           1 
      Dorne Beyond Wall 
          4           1 
Code
print(in_degree)
  Targaryen   Baratheon     Martell   Lannister      Tyrell       Reach 
         20           2           3           6           4          33 
      North  Riverlands Westerlands       Stark        Vale       Arryn 
         33          34          28           7          26           4 
      Tully        Frey  Crownlands  Stormlands       Essos       Septa 
          3           4          17          10          15           1 
      Dorne Beyond Wall 
          4           1 
Code
print(out_degree)
  Targaryen   Baratheon     Martell   Lannister      Tyrell       Reach 
         65          15           9          22           8          16 
      North  Riverlands Westerlands       Stark        Vale       Arryn 
         13           5           3          27          11           6 
      Tully        Frey  Crownlands  Stormlands       Essos       Septa 
          2          47           5           1           0           0 
      Dorne Beyond Wall 
          0           0 

Density

Network density measures the level of connectivity within a network. It is the ratio of the actual number of connections to the maximum possible connections. It provides insight into how tightly connected the nodes are within the network structure. Density is a local measure, focusing on internal connections rather than external factors. In simple undirected networks, density is related to average degree, but this relationship may not hold for all network types. Considering the specific characteristics of the network being studied is crucial for understanding the relationship between density and average degree.

Code
graph.density(got_marriages.ig)
[1] 0.6710526

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
erdos_renyi.ig <- sample_gnm(200, 200, directed = TRUE, loops = FALSE)

# density of random network
graph.density(erdos_renyi.ig)
[1] 0.005025126
Code
# dyad census of random network
igraph::dyad.census(erdos_renyi.ig)
$mut
[1] 0

$asym
[1] 200

$null
[1] 19700
Code
# triad census of random network
igraph::triad.census(erdos_renyi.ig)
 [1] 1274220   38761       0     105     104     209       0       0       1
[10]       0       0       0       0       0       0       0