Week 2 Challenge Instructions

challenge_2
instructions
Describing the Basic Structure of a Network
Author

Mani Kanta Gogula

Published

February 22, 2023

Challenge Overview

Describe the basic structure of a network following the steps in tutorial of week 2, this time using a dataset of your choice: for instance, you could use Marriages in Game of Thrones or Like/Dislike from week 1.

#Loading Libraries

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::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()
Code
library(dplyr)
library(igraph)
Warning: package 'igraph' was built under R version 4.1.3

Attaching package: 'igraph'

The following objects are masked from 'package:dplyr':

    as_data_frame, groups, union

The following objects are masked from 'package:purrr':

    compose, simplify

The following object is masked from 'package:tidyr':

    crossing

The following object is masked from 'package:tibble':

    as_data_frame

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

    decompose, spectrum

The following object is masked from 'package:base':

    union

Another more complex option is the newly added dataset of the US input-output table of direct requirements by industry, availabe in the Bureau of Economic Analysis. Input-output tables show the economic transactions between industries of an economy and thus can be understood as a directed adjacency matrix. Data is provided in the form of an XLSX file, so using read_xlsx from package readxl is recommended, including the sheet as an argument (2012 for instance).

Identify and describe content of nodes and links, and identify format of data set (i.e., matrix or edgelist, directed or not, weighted or not), and whether attribute data are present. Be sure to provide information about network size (e.g., information obtained from network description using week 1 network basic tutorial commands.)

Explore the dataset using commands from week 2 tutorial. Comment on the highlighted aspects of network structure such as:

  • Geodesic and Path Distances; Path Length
  • Dyads and Dyad Census
  • Triads and Triad Census
  • Network Transitivity and Clustering
  • Component Structure and Membership

Be sure to both provide the relevant statistics calculated in R, as well as your own interpretation of these statistics.

Describe the Network Data

  1. List and inspect List the objects to make sure the datafiles are working properly.
  2. Network Size What is the size of the network? You may use vcount and ecount.
  3. Network features Are these networks weighted, directed, and bipartite?
  4. Network Attributes Now, using commands from either statnet or igraph, list the vertex and edge attributes.

Dyad and Triad Census

Now try a full dyad census. This gives us the number of dyads where the relationship is:

  • Reciprocal (mutual), or mut
  • Asymmetric (non-mutual), or asym, and
  • Absent, or null

Now use triad.census in order to do a triad census.

Global and Local Transitivity or Clustering

Compute global transitivity using transitivity on igraph or gtrans on statnet and local transitivity of specific nodes of your choice, in addition to the average clustering coefficient. What is the distribution of node degree and how does it compare with the distribution of local transitivity?

Path Length and Component Structure

Can you compute the average path length and the diameter of the network? Can you find the component structure of the network and identify the cluster membership of each node?

#Loading dataset

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
ls(got_marriages)
[1] "From"       "Generation" "Notes"      "To"         "Type"      

Network size can be determined by vcount and ecount

#Network features

Code
network.ig <- graph_from_data_frame(got_marriages, directed = TRUE)
vcount(network.ig)
[1] 20
Code
ecount(network.ig)
[1] 255
Code
is_bipartite(network.ig)
[1] FALSE
Code
is_weighted(network.ig) 
[1] FALSE
Code
is_directed(network.ig) 
[1] TRUE

###There are 20 Vertices and 255 edges

#Network attributes

Code
igraph::vertex_attr_names(network.ig)
[1] "name"
Code
igraph::edge_attr_names(network.ig)
[1] "Type"       "Notes"      "Generation"

#Dyad census

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

$asym
[1] 57

$null
[1] 130

Using the above function we can determine

reciprocal dyads-3 asymetrics dyads-12 null dyads-130

#Triad Census

Code
igraph::triad.census(network.ig)
 [1] 408 227 217 110  44  47   9  18  50   1   0   3   5   1   0   0

#Global and Transitivity clustering

Code
transitivity(network.ig)
[1] 0.4411765
Code
transitivity(network.ig, type = 'global')
[1] 0.4411765
Code
transitivity(network.ig, type = 'average')
[1] 0.5478074

Pathlength and component structure

Code
average.path.length(network.ig,directed = T)
[1] 1.86875
Code
igraph::components(network.ig)$no
[1] 1
Code
igraph::components(network.ig)$csize
[1] 20