Professor Aven




1 R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

This R markdown script will help you analyze and visualize the social networks (Trust, Advice, and Communication) for you MBA class.

In the class networks (communication, trust, and advice) what are your centrality measures and how do they compare to those of your classmates (i.e., where are you in the distribution?). Please refer both from both tables and network graph.

#Install the following packages prior to running the code
library(igraph)
library(dplyr)
library(readxl)

library(qgraph)
library(tidyverse)
library(knitr)
library(DT)
library(readr)
library(visNetwork)

# Loading and inspecting data
# Set your own working directory to where you saved the class files. You will have to uncomment out the next line.
#path<-"//Users/brandyaven/Library/CloudStorage/GoogleDrive-aven@andrew.cmu.edu/My Drive/Teaching/1_MON_Macro Core/0_MASTERFILES/Head TA Share/2023 PTOH Class Data/"
path<-"/Users/chaoyiz/Desktop/CMU/MBA/MBA_Networks/2024"
setwd(path)
network_data<- load("2024Jul31-2024_M6_PTOH_All-Graphs.RData")
# File upload code

2 Take a look at our class level network data.

2.1 Network Size

Network Size refers to the number of nodes in the network.

cat("The network size of trust graph is ", vcount(trust_graph), "\n")
## The network size of trust graph is  105
cat("The network size of commnuication graph is ",vcount(comm_graph_undirected), "\n")
## The network size of commnuication graph is  105
cat("The network size of advice graph is ",vcount(advice_graph), "\n")
## The network size of advice graph is  105

2.2 Density

Density refers to proportion of possible ties that actually realized in the network. It is calculated by dividing the number of observed connections in the network by the total number of possible connections. A high edge density indicates that many connections exist among nodes in the network, while a low edge density indicates that there are relatively few connections. In networks with high edge density, information and resources are more likely to flow freely among nodes, On the other hand, networks with low edge density may be more fragmented or concentrated.

# Network/Class-level Network Measures====

#Density
cat("The density of trust graph is ", edge_density(trust_graph, loops = FALSE), "\n")
## The density of trust graph is  0.1903846
cat("The density of commnuication graph is ",edge_density(comm_graph_undirected, loops = FALSE), "\n")
## The density of commnuication graph is  0.442674
cat("The density of advice graph is ",edge_density(advice_graph, loops = FALSE), "\n")
## The density of advice graph is  0.1567766

2.3 Diameter

Diameter refers to the maximum shortest path length between any two nodes in the network. It is a measure of the longest distance between any two nodes in the network.

#Diameter
cat("The diameter of trust graph is ", diameter(trust_graph, directed = T), "\n")
## The diameter of trust graph is  10
cat("The diameter of commnuication graph is ",diameter(comm_graph_undirected, directed = F), "\n")
## The diameter of commnuication graph is  3
cat("The diameter of advice graph is ",diameter(advice_graph, directed = T), "\n")
## The diameter of advice graph is  5

2.4 Average Path Length

Average path length is calculated as the average of all shortest path lengths between any two nodes in the network. Both diameter and average path length provides information on the overall efficiency and connectivity of the network.A small average path length indicates that nodes in the network are closely connected and can easily communicate and share information with one another.

#APL Average Path Length

cat("The Average Path Length of trust graph is ", mean_distance(trust_graph, directed = TRUE, unconnected = TRUE), "\n")
## The Average Path Length of trust graph is  4.072267
cat("The Average Path Length of commnuication graph is ",mean_distance(comm_graph_undirected, directed = FALSE, unconnected = TRUE), "\n")
## The Average Path Length of commnuication graph is  1.795421
cat("The Average Path Length of advice graph is ",mean_distance(advice_graph, directed = TRUE, unconnected = TRUE), "\n")
## The Average Path Length of advice graph is  2.077016

3 Network Centrality Statistics for the Class

3.1 Degree centrality

Degree centrality measures the number of ties an node has in a UNDIRECTED network . In-degree centrality measures the number of incoming ties an node has in a DIRECTED network.Out-degree centrality measures the number of outgoing ties an individual has in a DIRECTED network.

[1] "Trust Graph Indegree Centrality Distribution"
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
0.009615 0.144231 0.173077 0.188370 0.240385 0.413461 
[1] "Trust Graph Outdegree Centrality Distribution"
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
0.00000 0.04808 0.11538 0.18837 0.19231 1.00000 
[1] "Advice Graph Indegree Centrality Distribution"
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
0.009615 0.115385 0.144231 0.155861 0.192308 0.548077 
[1] "Advice Graph Outdegree Centrality Distribution"
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
0.00000 0.04808 0.07692 0.15586 0.17308 1.00000 
[1] "Communication Graph Degree Centrality Distribution"
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
0.01923 0.33654 0.43269 0.44267 0.54808 0.86538 

3.2 Betweenness centrality

Betweenness centrality measures the extent to which an individual lies on the shortest path between other pairs of individuals in the network. Individuals with high betweenness centrality are more likely to have access to diverse sources of information. Individuals with low betweenness centrality are less central to the flow of information and may be more isolated.

[1] "Trust Graph Betweenness Centrality Distribution"
     Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
0.0000000 0.0005134 0.0039476 0.0106551 0.0142215 0.0898118 
[1] "Advice Graph Betweenness Centrality Distribution"
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
0.000000 0.001070 0.002979 0.009462 0.009678 0.115521 
[1] "Communication Graph Betweenness Centrality Distribution"
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
0.000000 0.001164 0.003351 0.007312 0.008413 0.059016 

3.3 Eigenvector centrality

Eigenvector centrality measures the extent to which an individual is connected to other well-connected individuals in the network. Individuals with high eigenvector centrality are well-connected to other well-connected individuals in the network, and thus are more likely to have influence over others in the network (i.e., greater power and/or status).

[1] "Trust Graph Eigenvector Centrality Distribution"
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
0.01357 0.31493 0.43008 0.44923 0.57823 1.00000 
[1] "Advice Graph Eigenvector Centrality Distribution"
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
0.01591 0.20917 0.29097 0.31562 0.39935 1.00000 
[1] "Communication Graph Eigenvector Centrality Distribution"
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
0.01454 0.42456 0.54161 0.54445 0.67719 1.00000 

3.4 Closeness centrality

Closeness centrality measures the extent to which an individual is close to other individuals in the network, in terms of the shortest path length.Individuals with high closeness centrality can more easily access information and resources from others in the network.

[1] "Trust Graph Closeness Centrality Distribution"
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.5049  0.5591  0.5876  0.6158  0.6154  1.0000 
[1] "Advice Graph Closeness Centrality Distribution"
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.5123  0.5417  0.5591  0.5831  0.5876  1.0000 
[1] "Communication Graph Closeness Centrality Distribution"
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.4094  0.6012  0.6341  0.6473  0.6842  0.8814 

3.5 Function For Network Measures

#This function makes a data frame of individual statistics/network measures 
graph_ind_stats<- function (class_network) {
  
  #SNA_ids<-V(class_network)
  bet<-betweenness(class_network, v = V(class_network), directed = TRUE, weights = NULL, normalized = T)
  clo<-centr_clo(class_network, mode = c("total"), normalized = TRUE)$res
  eig<-centr_eigen(class_network, directed = TRUE, scale = TRUE, normalized = TRUE)$vector
  
  
  if (is_directed(class_network)){
    indegree<-degree(class_network, v = V(class_network), mode = "in", loops = F, normalized = T)
    outdegree<-degree(class_network, v = V(class_network), mode = "out", loops = F, normalized = T)
    
    results<-as.data.frame(cbind(indegree,outdegree, bet,clo,eig))
    names(results)<-c("indegree","outdegree", "betweenness","closeness","eigenvector")
    results <- rownames_to_column(results, var = "SNA_ids")
  }else {
    degree<-degree(class_network, v = V(class_network),  loops = F, normalized = T)
    
    results<-as.data.frame(cbind(degree, bet,clo,eig))
    names(results)<-c("degree", "betweenness","closeness","eigenvector")
    results <- rownames_to_column(results, var = "SNA_ids")
  }
  return(results)
}



get_net_stats<- function (y, your_id){
  x<-y[,-1]
  class_stats<-sapply(x, function(x) c( "Stand dev" = round(sd(x),2), 
                                        "Mean"= round(mean(x,na.rm=TRUE),2),
                                        "n" = length(x),
                                        "Median" = round(median(x),2),
                                        "Minimum" = round(min(x),2),
                                        "Maximun" = round(max(x),2),
                                        "Upper Quantile" = round(quantile(x,.75),2),
                                        "LowerQuartile" = round(quantile(x,.25),2)
  )
  )
  
  #  print("******Class Statistics******")
  print(class_stats)
  #   print("******YOUR Statistics******")
  y[match(your_id, y$SNA_id),]
  
} 
#Run function to get the statistics
advice_stats<-graph_ind_stats(advice_graph)
trust_stats<-graph_ind_stats(trust_graph)
comm_stats<-graph_ind_stats(comm_graph_undirected)

4 Your Individual Network Measures

In the tables below you may filter by your SNA_ID to find your corresponding values.

4.1 Advice Network Scores

4.2 Trust Network Scores

4.3 Communication Network Scores

5 Advice Network Graph

Where are you in the class advice network? Do you have a large or small size of node? Do you know your centrality score? (Hint: by selecting your node id from the drop down box and click on your own node)
NOTE: You can resize the graphs and relocate individual nodes.


# add node network attributes from network graph stats data from generated above
V(advice_graph)$betweenness <- advice_stats$betweenness
V(advice_graph)$indegree <- advice_stats$indegree
V(advice_graph)$outdegree <- advice_stats$outdegree
V(advice_graph)$eigenvector <- advice_stats$eigenvector
V(advice_graph)$closeness <- advice_stats$closeness
V(advice_graph)$value <- advice_stats$between

advice_graph<-set_graph_attr(advice_graph, "layout", layout_with_fr)

data <- toVisNetworkData(advice_graph)
node_df <- as.data.frame(data$nodes)
node_df <- node_df %>%
  mutate(id = as.numeric(id)) %>%  
  arrange(id)
node_df$title = paste0("node id:",node_df$id,"</b><br>betweenness_centrality:", node_df$betweenness, "</b><br>eigenvector_centrality:",node_df$eigenvector,"</b><br>indegree_centrality:", node_df$indegree, "</b><br>outdegree_centrality:", node_df$outdegree, "</b><br>closeness_centrality:", node_df$closeness)

vn = visNetwork(node_df, edges = data$edges,height = "1500px", width = "150%") %>%
  visConfigure(enabled = FALSE) %>%
  visPhysics(enabled = FALSE, stabilization = FALSE) %>%
  visIgraphLayout(layout = "layout.fruchterman.reingold") %>%
  visLayout(randomSeed = 123)%>%
  visOptions(
             highlightNearest = list(enabled = TRUE, degree = 2, hover = TRUE),
             nodesIdSelection = list(enabled = TRUE,
                                     style = 'width: 200px; height: 26px;background: #f8f8f8; 
                                             color: darkblue;
                                             border:none;
                                             outline:none;'), 
        
             )
        
vn = visNodes(vn, id = data$nodes$id, 
                  borderWidthSelected = 15,
                  color = list(highlight ="red"))
vn <- visEdges(vn, id = data$edges$id,
               color = 'pink', 
               arrows = 'from')
vn

6 Trust Network Graph

# add node network attributes from network graph stats data fram generated above
V(trust_graph)$betweenness <- trust_stats$betweenness
V(trust_graph)$indegree <- trust_stats$indegree
V(trust_graph)$outdegree <- trust_stats$outdegree
V(trust_graph)$eigenvector <- trust_stats$eigenvector
V(trust_graph)$closeness <- trust_stats$closeness
V(trust_graph)$value <- trust_stats$betweenness

trust_graph<-set_graph_attr(trust_graph, "layout", layout_with_fr)

data <- toVisNetworkData(trust_graph)
node_df <- as.data.frame(data$nodes)
node_df$title = paste0("node id:",node_df$id,"</b><br>betweenness_centrality:", node_df$betweenness, "</b><br>eigenvector_centrality:",node_df$eigenvector,"</b><br>indegree_centrality:", node_df$indegree, "</b><br>outdegree_centrality:", node_df$outdegree, "</b><br>closeness_centrality:", node_df$closeness)

vn = visNetwork(node_df, edges = data$edges,height = "1500px", width = "200%") %>%
  visConfigure(enabled = FALSE) %>%
  visPhysics(enabled = FALSE, stabilization = FALSE) %>%
  visIgraphLayout(layout = "layout.fruchterman.reingold") %>%
  visLayout(randomSeed = 100) %>%
  visOptions(highlightNearest =list(enabled = TRUE, hover = TRUE,degree = 3),
              nodesIdSelection = list(enabled = TRUE,
                                 style = 'width: 200px; height: 26px;
                                 background: #f8f8f8;
                                 color: darkblue;
                                 border:none;
                                 outline:none;'))
vn <- visNodes(vn, id = data$nodes$id, 
                  borderWidthSelected = 15,
                  color = list(highlight ="red"))
vn <- visEdges(vn, id = data$edges$id,
               color = 'pink', 
               arrows = 'from')
vn

7 Communication Network Graph

# add node network attributes from network graph stats data fram generated above
V(comm_graph_undirected)$betweenness <- comm_stats2$betweenness
V(comm_graph_undirected)$degree <- comm_stats2$degree
V(comm_graph_undirected)$eigenvector <- comm_stats2$eigenvector
V(comm_graph_undirected)$closeness <- comm_stats2$closeness
V(comm_graph_undirected)$value <- comm_stats2$betweenness

comm_graph_undirected<-set_graph_attr(comm_graph_undirected, "layout", layout_with_fr)

data <- toVisNetworkData(comm_graph_undirected)
node_df <- as.data.frame(data$nodes)
node_df$title = paste0("node id:",node_df$id,"</b><br>betweenness_centrality:", node_df$betweenness, "</b><br>eigenvector_centrality:",node_df$eigenvector,"</b><br>degree_centrality:",node_df$degree, "</b><br>closeness_centrality:", node_df$closeness)

vn = visNetwork(node_df, edges = data$edges,height = "1500px", width = "200%") %>%
  visConfigure(enabled = FALSE) %>%
  visPhysics(enabled = FALSE, stabilization = FALSE) %>%
  visIgraphLayout(layout = "layout.fruchterman.reingold") %>%
  visLayout(randomSeed = 123) %>%
  visOptions(highlightNearest =list(enabled = TRUE, hover = TRUE,degree = 3),
              nodesIdSelection = list(enabled = TRUE,
                                 style = 'width: 200px; height: 26px;
                                 background: #f8f8f8;
                                 color: darkblue;
                                 border:none;
                                 outline:none;'))
vn = visNodes(vn, id = data$nodes$id, 
                  borderWidthSelected = 6,
                  color = list(highlight ="red"))
vn <- visEdges(vn, id = data$edges$id,
               color = 'pink')

vn






Last updated on the 03/2024