Week2_Challenge_RahulG

challenge_2
instructions
Describing the Basic Structure of a Network
Author

Rahul Gundeti

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.

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?

Code
#loading libraries
library(tidyverse)
-- Attaching core tidyverse packages ------------------------ tidyverse 2.0.0 --
v dplyr     1.1.2     v readr     2.1.4
v forcats   1.0.0     v stringr   1.5.0
v ggplot2   3.4.2     v tibble    3.2.1
v lubridate 1.9.2     v tidyr     1.3.0
v purrr     1.0.1     
-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()
i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Code
library(dplyr)
library(igraph)

Attaching package: 'igraph'

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

    %--%, union

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
Code
library(readr)
Code
#Loading dataset
got_family <- read_csv("_data/got/got_family.csv")
Rows: 21 Columns: 3
-- Column specification --------------------------------------------------------
Delimiter: ","
chr (3): Family, Region, Color

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_family)
# A tibble: 6 x 3
  Family       Region       Color      
  <chr>        <chr>        <chr>      
1 Crownlands   Crownlands   red        
2 Targaryen    Crownlands   red3       
3 Dorne        Dorne        navajowhite
4 Martell      Dorne        orange     
5 Iron Islands Iron Islands grey95     
6 Greyjoy      Iron Islands lightgrey  
Code
ls(got_family)
[1] "Color"  "Family" "Region"
Code
#Network size can be determined by vcount and ecount
#Network features
netw <- graph_from_data_frame(got_family, directed = TRUE)
vcount(netw)
[1] 22
Code
ecount(netw)
[1] 21
Code
is_bipartite(netw)
[1] FALSE
Code
is_weighted(netw) 
[1] FALSE
Code
is_directed(netw) 
[1] TRUE

There are 22 edges and 21 vertices

Code
# Network attributes
igraph::vertex_attr_names(netw)
[1] "name"
Code
igraph::edge_attr_names(netw)
[1] "Color"
Code
#Dyad census
igraph::dyad.census(netw)
$mut
[1] 0

$asym
[1] 11

$null
[1] 220

reciprocal dyads-0

asymetrics dyads-11

null dyads-220

Code
#Triad Census
igraph::triad.census(netw)
 [1] 1321  218    0    0    1    0    0    0    0    0    0    0    0    0    0
[16]    0
Code
#Global and Transitivity clustering
transitivity(netw)
[1] 0
Code
transitivity(netw, type = 'global')
[1] 0
Code
transitivity(netw, type = 'average')
[1] 0
Code
# Pathlength and component structure
average.path.length(netw,directed = T)
[1] 1
Code
igraph::components(netw)$no
[1] 11
Code
igraph::components(netw)$csize
 [1] 2 2 2 2 2 2 3 2 2 1 2