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",
  "here",
    "rstudioapi"
  
)

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 143
Communication 143
Advice 143

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.1301
Communication 0.4691
Advice 0.0972

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 14
Communication 4
Advice 6

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 4.411
Communication 2.311
Advice 2.404

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.007
Q1.25% 0.025 0.056
Median 0.063 0.099
Mean 0.129 0.097
Q3.75% 0.155 0.130
Max 1.000 0.225
Out-degree Centrality Distribution Across Networks
Trust Advice
Min 0.028 0.000
Q1.25% 0.085 0.028
Median 0.134 0.063
Mean 0.129 0.097
Q3.75% 0.169 0.113
Max 0.261 0.908
Communication Network Degree Centrality Distribution (Undirected)
Communication
Min 0.134
Q1.25% 0.331
Median 0.451
Mean 0.469
Q3.75% 0.613
Max 1.000

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.000 0.001 0.000
Median 0.002 0.003 0.001
Mean 0.010 0.009 0.007
Q3.75% 0.008 0.012 0.004
Max 0.297 0.136 0.223

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.009 0.152
Q1.25% 0.015 0.175 0.382
Median 0.063 0.357 0.534
Mean 0.152 0.377 0.537
Q3.75% 0.202 0.552 0.700
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.507 0.382 0.536
Q1.25% 0.529 0.514 0.599
Median 0.548 0.528 0.645
Mean 0.566 0.535 0.663
Q3.75% 0.566 0.548 0.721
Max 1.000 0.916 1.000

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