if("statnet" %in% rownames(installed.packages()) == FALSE) {install.packages("statnet")}
  if("igraph"  %in% rownames(installed.packages()) == FALSE) {install.packages("igraph")}
  if("igraphdata" %in% rownames(installed.packages()) == FALSE) {install.packages("igraphdata")}

library(statnet)
## Loading required package: tergm
## Loading required package: ergm
## Loading required package: network
## 
## 'network' 1.17.1 (2021-06-12), 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
## 
## 'ergm' 4.1.2 (2021-07-26), part of the Statnet Project
## * 'news(package="ergm")' for changes since last version
## * 'citation("ergm")' for citation information
## * 'https://statnet.org' for help, support, and other information
## 'ergm' 4 is a major update that introduces some backwards-incompatible
## changes. Please type 'news(package="ergm")' for a list of major
## changes.
## Loading required package: networkDynamic
## 
## 'networkDynamic' 0.11.0 (2021-06-12), part of the Statnet Project
## * 'news(package="networkDynamic")' for changes since last version
## * 'citation("networkDynamic")' for citation information
## * 'https://statnet.org' for help, support, and other information
## Registered S3 method overwritten by 'tergm':
##   method                   from
##   simulate_formula.network ergm
## 
## 'tergm' 4.0.2 (2021-07-28), part of the Statnet Project
## * 'news(package="tergm")' for changes since last version
## * 'citation("tergm")' for citation information
## * 'https://statnet.org' for help, support, and other information
## 
## Attaching package: 'tergm'
## The following object is masked from 'package:ergm':
## 
##     snctrl
## Loading required package: ergm.count
## 
## 'ergm.count' 4.0.2 (2021-06-18), part of the Statnet Project
## * 'news(package="ergm.count")' for changes since last version
## * 'citation("ergm.count")' for citation information
## * 'https://statnet.org' for help, support, and other information
## Loading required package: sna
## Loading required package: statnet.common
## 
## Attaching package: 'statnet.common'
## The following object is masked from 'package:ergm':
## 
##     snctrl
## The following objects are masked from 'package:base':
## 
##     attr, order
## sna: Tools for Social Network Analysis
## Version 2.6 created on 2020-10-5.
## copyright (c) 2005, Carter T. Butts, University of California-Irvine
##  For citation information, type citation("sna").
##  Type help(package="sna") to get started.
## Loading required package: tsna
## 
## 'statnet' 2019.6 (2019-06-13), part of the Statnet Project
## * 'news(package="statnet")' for changes since last version
## * 'citation("statnet")' for citation information
## * 'https://statnet.org' for help, support, and other information
## unable to reach CRAN
  library(igraph)
## 
## Attaching package: 'igraph'
## The following objects are masked from 'package:sna':
## 
##     betweenness, bonpow, closeness, components, degree, dyad.census,
##     evcent, hierarchy, is.connected, neighborhood, triad.census
## The following objects are masked from 'package:network':
## 
##     %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
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## The following object is masked from 'package:base':
## 
##     union
  library(igraphdata)
# Imports the US Airports Data

 data("USairports", package = "igraphdata")
  network_igraph <- USairports
  rm(USairports)
#Now let's make an edgelist versian

    network_edgelist <- as.data.frame(as_edgelist(network_igraph), stringsAsFactors = FALSE)
  
  #Lets add edge attributes
    network_edgelist$Carrier    <- as.character(E(network_igraph)$Carrier)
    network_edgelist$Departures <- as.character(E(network_igraph)$Departures)
    network_edgelist$Seats      <- as.character(E(network_igraph)$Seats)
    network_edgelist$Passengers <- as.character(E(network_igraph)$Passengers)
    network_edgelist$Aircraft   <- as.character(E(network_igraph)$Aircraft)
    network_edgelist$Distance   <- as.character(E(network_igraph)$Distance)
    network_edgelist$weight <- as.character(E(network_igraph)$Passengers)
#Now let's create a node attribute dataframe
    network_nodes <- data.frame(name = as.character(V(network_igraph)$name), 
                                City = as.character(V(network_igraph)$City),
                                Position = as.character(V(network_igraph)$Position),
                                stringsAsFactors = FALSE)
#Lets create an statnet network object from our edgelist
  network_statnet <- network(as.matrix(network_edgelist[1:2]), matrix.type = 'edgelist', directed = TRUE)
  
  network_statnet%e%'Carrier'    <- network_edgelist$Carrier    
  network_statnet%e%'Departures' <- network_edgelist$Departures 
  network_statnet%e%'Seats'      <- network_edgelist$Seats      
  network_statnet%e%'Passangers' <- network_edgelist$Passengers 
  network_statnet%e%'Aircraft'   <- network_edgelist$Aircraft   
  network_statnet%e%'Distance'   <- network_edgelist$Distance
  network_statnet%e%'weight' <- network_edgelist$Passengers 
  
  
  network_statnet%v%'City'       <- as.character(network_nodes$City)
  network_statnet%v%'Distance'   <- as.character(network_nodes$Distance)
#Let's add a helpful message that will tell  users what this script has done.
  cat(
'#################################################################################################
"US Airport.R" has imported a a network of flights between US airports from the igraphdata
package.

This is a large, weighted, directed network. Nodes are airports and edges are flights. Edges are
weighted by total number of passangers. For more information use "?USairports".

The import script has created four objects that represent the network:
     -network_edgelist      (a dataframe of an edgelist and edge attributes)
     -network_nodes         (a dataframe of node attributes)
     -network_igraph        (an igraph object)
     -network_statnet       (a network object compatable with statnet packages like sna & ergm)


Each object name starts, quite generically, with "network_" and ends with the type of object it 
is. Note that the names are generic so that they are  compatable with other scripts you will use
in this course. Feel free to rename the objects for your purposes. 
################################################################################################')
## #################################################################################################
## "US Airport.R" has imported a a network of flights between US airports from the igraphdata
## package.
## 
## This is a large, weighted, directed network. Nodes are airports and edges are flights. Edges are
## weighted by total number of passangers. For more information use "?USairports".
## 
## The import script has created four objects that represent the network:
##      -network_edgelist      (a dataframe of an edgelist and edge attributes)
##      -network_nodes         (a dataframe of node attributes)
##      -network_igraph        (an igraph object)
##      -network_statnet       (a network object compatable with statnet packages like sna & ergm)
## 
## 
## Each object name starts, quite generically, with "network_" and ends with the type of object it 
## is. Note that the names are generic so that they are  compatable with other scripts you will use
## in this course. Feel free to rename the objects for your purposes. 
## ################################################################################################
save(network_edgelist, network_igraph, network_nodes, network_statnet, file="USAirports.rda")

#Describing Network Attrributes

The network has 755 nodes and 8228 edges.

ls()
## [1] "network_edgelist" "network_igraph"   "network_nodes"    "network_statnet"
vcount(network_igraph)
## [1] 755
ecount(network_igraph)
## [1] 23473
print(network_statnet)
##  Network attributes:
##   vertices = 755 
##   directed = TRUE 
##   hyper = FALSE 
##   loops = FALSE 
##   multiple = FALSE 
##   bipartite = FALSE 
##   total edges= 8228 
##     missing edges= 0 
##     non-missing edges= 8228 
## 
##  Vertex attribute names: 
##     City Distance vertex.names 
## 
##  Edge attribute names not shown
print(network_igraph)
## IGRAPH bf6202d DN-- 755 23473 -- US airports
## + attr: name (g/c), name (v/c), City (v/c), Position (v/c), Carrier
## | (e/c), Departures (e/n), Seats (e/n), Passengers (e/n), Aircraft
## | (e/n), Distance (e/n)
## + edges from bf6202d (vertex names):
##  [1] BGR->JFK BGR->JFK BOS->EWR ANC->JFK JFK->ANC LAS->LAX MIA->JFK EWR->ANC
##  [9] BJC->MIA MIA->BJC TEB->ANC JFK->LAX LAX->JFK LAX->SFO AEX->LAS BFI->SBA
## [17] ELM->PIT GEG->SUN ICT->PBI LAS->LAX LAS->PBI LAS->SFO LAX->LAS PBI->AEX
## [25] PBI->ICT PIT->VCT SFO->LAX VCT->DWH IAD->JFK ABE->CLT ABE->HPN AGS->CLT
## [33] AGS->CLT AVL->CLT AVL->CLT AVP->CLT AVP->PHL BDL->CLT BHM->CLT BHM->CLT
## [41] BNA->CLT BNA->CLT BNA->DCA BNA->PHL BTR->CLT BUF->CLT BUF->DCA BUF->PHL
## + ... omitted several edges

Description of several critical network features

The network is not bipartite. It is a directed and unweighted network.

is_bipartite(network_igraph)
## [1] FALSE
is_directed(network_igraph)
## [1] TRUE
is_weighted(network_igraph)
## [1] FALSE
print(network_statnet)
##  Network attributes:
##   vertices = 755 
##   directed = TRUE 
##   hyper = FALSE 
##   loops = FALSE 
##   multiple = FALSE 
##   bipartite = FALSE 
##   total edges= 8228 
##     missing edges= 0 
##     non-missing edges= 8228 
## 
##  Vertex attribute names: 
##     City Distance vertex.names 
## 
##  Edge attribute names not shown

List network attributes: igraph

igraph::vertex_attr_names(network_igraph)
## [1] "name"     "City"     "Position"
igraph::edge_attr_names(network_igraph)
## [1] "Carrier"    "Departures" "Seats"      "Passengers" "Aircraft"  
## [6] "Distance"
network::list.vertex.attributes(network_statnet)
## [1] "City"         "Distance"     "na"           "vertex.names"
network::list.edge.attributes(network_statnet)
## [1] "Aircraft"   "Carrier"    "Departures" "Distance"   "na"        
## [6] "Passangers" "Seats"      "weight"

#Summarize network attributes

summary(E(network_igraph)$ "Aircraft")
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    10.0   612.0   629.0   571.6   655.0   873.0
summary(network_statnet %e% "Passengers")
## Length  Class   Mode 
##      0   NULL   NULL

#Dyad Census

There are 10449 (mututal), 2574 (asymmetic) and 271612 (null) dyads in the network.

igraph::dyad.census(network_igraph)
## $mut
## [1] 10449
## 
## $asym
## [1] 2574
## 
## $null
## [1] 271612
sna::dyad.census(network_statnet)
##       Mut Asym   Null
## [1,] 3605 1018 280012

#Triad Census

igraph::triad_census(network_igraph)
##  [1] 68169544   665870  2427052     1445     1289     2465    15322    19171
##  [9]       91       39   114868      202      376      558     6422    18671
sna::triad.census(network_statnet, mode="graph")
##             0       1      2     3
## [1,] 68527865 2755420 137878 22222

#Transitivity or Global Clustering

The transitivity of the network data is around .33.

transitivity (network_igraph)
## [1] 0.3384609
gtrans(network_statnet)
## [1] 0.3266617

#Local Transitivity and Clustering

The local clustering (local transitivity) coefficient is .64. Interestingly, the global clustering coefficient (.33) is smaller than the local clustering coefficient.

transitivity(network_igraph, type="average")
## [1] 0.6452844

#Average path length

The average path length of the network is 3.45.

average.path.length(network_igraph, directed=F)
## [1] 3.447169

#Component Structure

There are 6 components in this network data. One component has 745 member in it, meaning that most of the airports have flights between them. The rest of components has a few members in it ranging from 1 to 3. It suggests that those airports in smaller size components are probably not for commercial airlines, but for other purposes such as airforce base etc.

There seems to be 166 isolates in the data.

There is one isolate airport that we could identity: DET (Coleman A. Young International Airport).

names(igraph::components(network_igraph))
## [1] "membership" "csize"      "no"
igraph::components(network_igraph)$no
## [1] 6
igraph::components(network_igraph)$csize # 2 members or 3 members in the network 
## [1] 745   2   2   3   2   1
isolates(network_statnet) # why are there isolates?
## [1] 166
as.vector(network_statnet %v% "vertex.names")[c(isolates(network_statnet))]
## [1] "DET"