Week 3 Challenge Instructions

challenge_3
instructions
Degree and Density of a Network
Author

Mani Kanta Gogula

Published

March 6, 2023

Code
library(igraph)
Warning: package 'igraph' was built under R version 4.1.3

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.1.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
Code
library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.1.3
-- Attaching packages --------------------------------------- tidyverse 1.3.2 --
v ggplot2 3.4.0      v purrr   0.3.5 
v tibble  3.1.8      v dplyr   1.0.10
v tidyr   1.2.1      v stringr 1.5.0 
v readr   2.1.3      v forcats 0.5.2 
Warning: package 'ggplot2' was built under R version 4.1.3
Warning: package 'tibble' was built under R version 4.1.3
Warning: package 'tidyr' was built under R version 4.1.3
Warning: package 'readr' was built under R version 4.1.3
Warning: package 'purrr' was built under R version 4.1.3
Warning: package 'dplyr' was built under R version 4.1.3
Warning: package 'stringr' was built under R version 4.1.3
Warning: package 'forcats' was built under R version 4.1.3
-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x dplyr::as_data_frame() masks tibble::as_data_frame(), igraph::as_data_frame()
x purrr::compose()       masks igraph::compose()
x tidyr::crossing()      masks igraph::crossing()
x dplyr::filter()        masks stats::filter()
x dplyr::lag()           masks stats::lag()
x purrr::simplify()      masks igraph::simplify()
Code
library(readr)
got_marriages <- read_csv("_data/got/got_marriages.csv")
Rows: 255 Columns: 5
-- Column specification --------------------------------------------------------
Delimiter: ","
chr (5): From, To, Type, Notes, Generation

i Use `spec()` to retrieve the full column specification for this data.
i Specify the column types or set `show_col_types = FALSE` to quiet this message.
Code
head(got_marriages)
# A tibble: 6 x 5
  From      To      Type    Notes  Generation
  <chr>     <chr>   <chr>   <chr>  <chr>     
1 Targaryen Stark   Married R+L=J  Current   
2 Baratheon Martell Engaged died   Current   
3 Baratheon Stark   Engaged broken Current   
4 Martell   Essos   Married <NA>   Current   
5 Martell   Reach   Affair  <NA>   Current   
6 Martell   Essos   Affair  <NA>   Current   
Code
got_marriages.ig <-graph_from_data_frame(got_marriages, directed = TRUE)
Code
# number of edges
ecount(got_marriages.ig)
[1] 255
Code
# number of vertices
vcount(got_marriages.ig)
[1] 20
Code
# vertex and edge attributes
vertex_attr_names(got_marriages.ig)
[1] "name"
Code
edge_attr_names(got_marriages.ig)
[1] "Type"       "Notes"      "Generation"
Code
# network features
is_directed(got_marriages.ig)
[1] TRUE
Code
is_bipartite(got_marriages.ig)
[1] FALSE
Code
is_weighted(got_marriages.ig)
[1] FALSE
Code
# dyad census
igraph::dyad.census(got_marriages.ig)
$mut
[1] 3

$asym
[1] 57

$null
[1] 130
Code
# triad census
triad_census(got_marriages.ig)
 [1] 408 227 217 110  44  47   9  18  50   1   0   3   5   1   0   0

This network has 255 edges and 20 vertices. The vertex attribute is ‘name’ and edge attributes are “Type”, “Notes”, “Generation”.

Code
# number of components
igraph::components(got_marriages.ig)$no
[1] 1
Code
# size of components
igraph::components(got_marriages.ig)$csize 
[1] 20

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

Code
# network density
graph.density(got_marriages.ig)
[1] 0.6710526
Code
# density without loops
graph.density(got_marriages.ig, loops=TRUE)
[1] 0.6375
Code
# average network degree
igraph::degree(got_marriages.ig)
  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
nodes_ig<-data.frame(name=V(got_marriages.ig)$name, degree=igraph::degree(got_marriages.ig,loops=FALSE))
nodes_ig<-nodes_ig %>%
    mutate(indegree=igraph::degree(got_marriages.ig, mode="in", loops=FALSE),
           outdegree=igraph::degree(got_marriages.ig, mode="out", loops=FALSE))
head(nodes_ig)
               name degree indegree outdegree
Targaryen Targaryen     47        1        46
Baratheon Baratheon     17        2        15
Martell     Martell     12        3         9
Lannister Lannister     20        2        18
Tyrell       Tyrell     12        4         8
Reach         Reach     25       21         4
Code
erdos_renyi.ig <- sample_gnm(103, 200, directed = TRUE, loops = FALSE)

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

$asym
[1] 194

$null
[1] 5056
Code
# triad census of random network
igraph::triad.census(erdos_renyi.ig)
 [1] 157678  18168    285    183    166    351     10      5      2      1
[11]      1      0      1      0      0      0
Code
nodes_rand<-data.frame(degree=igraph::degree(erdos_renyi.ig))
nodes_rand<-nodes_rand %>%
    mutate(indegree=igraph::degree(erdos_renyi.ig, mode="in", loops=FALSE),
           outdegree=igraph::degree(erdos_renyi.ig, mode="out", loops=FALSE))
head(nodes_rand)
  degree indegree outdegree
1      4        3         1
2      5        1         4
3      8        5         3
4      4        2         2
5      5        2         3
6      4        3         1
Code
summary(nodes_rand)
     degree         indegree       outdegree    
 Min.   :0.000   Min.   :0.000   Min.   :0.000  
 1st Qu.:3.000   1st Qu.:1.000   1st Qu.:1.000  
 Median :4.000   Median :2.000   Median :2.000  
 Mean   :3.883   Mean   :1.942   Mean   :1.942  
 3rd Qu.:5.000   3rd Qu.:3.000   3rd Qu.:3.000  
 Max.   :9.000   Max.   :6.000   Max.   :6.000