-
Notifications
You must be signed in to change notification settings - Fork 1
/
[Network Summary Statistics] Period-focused analysis.R
63 lines (48 loc) · 1.94 KB
/
[Network Summary Statistics] Period-focused analysis.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Load necessary libraries
library(readxl)
library(igraph)
library(dplyr)
library(ggplot2)
library(gridExtra)
# Initialize a list to store summary statistics for each year
summary_stats <- list()
# Loop through each data frame in the list
for (file_name in names(data_list)) {
# Load the data for the current file
data <- data_list[[file_name]]
# Extract the year from the file name
year <- sub("EU_", "", file_name)
# Create a graph from the data frame
g <- graph_from_data_frame(d = data, directed = TRUE)
# Set edge weights
E(g)$weight <- data$total_claims
# Ensure all weights are positive
if (any(E(g)$weight <= 0)) {
E(g)$weight <- E(g)$weight + abs(min(E(g)$weight)) + 1
}
# Calculate summary statistics
avg_weighted_degree <- mean(strength(g, mode = "all", weights = E(g)$weight))
avg_betweenness <- mean(betweenness(g, weights = E(g)$weight))
# Calculate average path length and diameter for the largest connected component
components <- clusters(g)
largest_component <- which.max(components$csize)
subgraph <- induced_subgraph(g, which(components$membership == largest_component))
avg_path_length <- mean_distance(subgraph, directed = TRUE, weights = E(subgraph)$weight)
diameter <- diameter(subgraph, directed = TRUE, weights = E(subgraph)$weight)
# Store the summary statistics in a data frame
summary_stats[[year]] <- data.frame(
Year = year,
Avg_Weighted_Degree = avg_weighted_degree,
Avg_Betweenness = avg_betweenness,
Avg_Path_Length = avg_path_length,
Diameter = diameter
)
}
# Combine all summary statistics into a single data frame
summary_stats_df <- bind_rows(summary_stats)
# Print the summary statistics
print(summary_stats_df)
# Create a ggplot table
p <- tableGrob(summary_stats_df)
# Save the table as an image
ggsave("Network_Summary_Statistics.png", plot = p, width = 12, height = 8, dpi = 300)