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.

network_sizes <- data.frame(
  Network = c("Trust", "Communication", "Advice"),
  Size = c(vcount(trust_graph), vcount(comm_graph_undirected), vcount(advice_graph))
)
kable(network_sizes, caption = "Network Sizes")
Network Sizes
Network Size
Trust 156
Communication 156
Advice 156

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_undirected, loops = FALSE),
    edge_density(advice_graph, loops = FALSE)
  )
)

kable(network_density, caption = "Network Densities", digits = 4)
Network Densities
Network Density
Trust 0.1529
Communication 0.5657
Advice 0.1345

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_undirected, directed = F),
    diameter(advice_graph, directed = T)
  )
)

kable(network_diameter, caption = "Network Diameters")
Network Diameters
Network Diameter
Trust 12
Communication 4
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_undirected, 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.135
Communication 2.333
Advice 2.095

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.000
Q1.25% 0.019 0.032
Median 0.077 0.090
Mean 0.152 0.133
Q3.75% 0.174 0.161
Max 1.000 1.000
Out-degree Centrality Distribution Across Networks
Trust Advice
Min 0.039 0.032
Q1.25% 0.097 0.082
Median 0.145 0.116
Mean 0.152 0.133
Q3.75% 0.194 0.174
Max 0.381 0.419
Communication Network Degree Centrality Distribution (Undirected)
Communication
Min 0.116
Q1.25% 0.431
Median 0.590
Mean 0.566
Q3.75% 0.684
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.000 0.000
Median 0.001 0.002 0.001
Mean 0.007 0.006 0.007
Q3.75% 0.006 0.007 0.005
Max 0.105 0.131 0.208

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.000 0.133
Q1.25% 0.016 0.026 0.488
Median 0.091 0.112 0.659
Mean 0.172 0.167 0.625
Q3.75% 0.220 0.204 0.752
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.512 0.508 0.531
Q1.25% 0.542 0.531 0.637
Median 0.560 0.550 0.709
Mean 0.588 0.567 0.710
Q3.75% 0.594 0.583 0.760
Max 1.000 1.000 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_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 04/2025