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 your 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
package.names <- c(
  "tidyverse",   
  "igraph",
  "qgraph",
  "knitr",
  "DT", 
  "visNetwork", 
  "ggplot2", 
  "gridExtra", 
  "RColorBrewer"
  
)

check.packages <- function(package.names) {
  for (package in package.names) {
    if (!require(package, character.only = TRUE)) {
      install.packages(package)
    }
    library(package, character.only = TRUE)
  }
}

# Load packages
check.packages(package.names)

2 class level network data.

2.1 Network Size

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

# Create a data frame for the network size information
network_sizes <- data.frame(
  Network = c("Trust", "Communication", "Advice"),
  Size = c(vcount(trust_graph), vcount(comm_graph), vcount(advice_graph))
)
kable(network_sizes, caption = "Network Sizes")
Network Sizes
Network Size
Trust 91
Communication 91
Advice 91

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.

# Create a data frame for the network density information
network_density <- data.frame(
  Network = c("Trust", "Communication", "Advice"),
  Density = c(
    edge_density(trust_graph, loops = FALSE),
    edge_density(comm_graph, loops = FALSE),
    edge_density(advice_graph, loops = FALSE)
  )
)

kable(network_density, caption = "Network Densities", digits = 4)
Network Densities
Network Density
Trust 0.3802
Communication 0.5221
Advice 0.1983

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.

# Create a data frame for the network diameter information
network_diameter <- data.frame(
  Network = c("Trust", "Communication", "Advice"),
  Diameter = c(
    diameter(trust_graph, directed = T),
    diameter(comm_graph, directed = F),
    diameter(advice_graph, directed = T)
  )
)

kable(network_diameter, caption = "Network Diameters")
Network Diameters
Network Diameter
Trust 8
Communication 3
Advice 4

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.

# Create a data frame for the average path length information
network_apl <- data.frame(
  Network = c("Trust", "Communication", "Advice"),
  "Average_Path_Length" = c(
    mean_distance(trust_graph, directed = TRUE, unconnected = TRUE),
    mean_distance(comm_graph, directed = FALSE, unconnected = TRUE),
    mean_distance(advice_graph, directed = TRUE, unconnected = TRUE)
  )
)

# Display the average path length information in a simple table
kable(network_apl, caption = "Network Average Path Lengths", digits = 3)
Network Average Path Lengths
Network Average_Path_Length
Trust 2.984
Communication 1.724
Advice 1.961

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.

In-degree Centrality Distribution Across Networks
Trust Advice
Min 0.000 0.011
Q1.25% 0.106 0.156
Median 0.256 0.189
Mean 0.377 0.198
Q3.75% 0.589 0.244
Max 1.000 0.456
Out-degree Centrality Distribution Across Networks
Trust Advice
Min 0.156 0.000
Q1.25% 0.311 0.067
Median 0.367 0.144
Mean 0.377 0.198
Q3.75% 0.433 0.289
Max 0.589 1.000
Communication Network Degree Centrality Distribution (Undirected)
Communication
Min 0.033
Q1.25% 0.417
Median 0.522
Mean 0.522
Q3.75% 0.644
Max 0.911

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.

Betweenness Centrality Distribution Across Networks
Trust Advice Communication
Min 0.000 0.000 0.000
Q1.25% 0.001 0.001 0.001
Median 0.003 0.005 0.003
Mean 0.009 0.010 0.008
Q3.75% 0.010 0.013 0.011
Max 0.097 0.073 0.044

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).

Eigenvector Centrality Distribution Across Networks
Trust Advice Communication
Min 0.000 0.024 0.031
Q1.25% 0.111 0.308 0.492
Median 0.258 0.414 0.621
Mean 0.379 0.433 0.617
Q3.75% 0.569 0.538 0.763
Max 1.000 1.000 1.000

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.

Closeness Centrality Distribution Across Networks
Trust Advice Communication
Min 0.542 0.503 0.479
Q1.25% 0.627 0.554 0.629
Median 0.672 0.577 0.677
Mean 0.721 0.597 0.686
Q3.75% 0.779 0.616 0.738
Max 1.000 1.000 0.918

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$id <- as.numeric(node_df$id)
node_df <- node_df[order(node_df$id), ]
node_df$id <- as.character(node_df$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 = "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)$betweenness <- comm_stats2$betweenness
V(comm_graph)$degree <- comm_stats2$degree
V(comm_graph)$eigenvector <- comm_stats2$eigenvector
V(comm_graph)$closeness <- comm_stats2$closeness
V(comm_graph)$value <- comm_stats2$betweenness

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

data <- toVisNetworkData(comm_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>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 07/2025