# Set correct working directory for knitting
knitr::opts_knit$set(root.dir = "C:/Users/terry/OneDrive/Documents/working dirctory")

# Load packages
library(readr)
library(igraph)
library(dplyr)
library(ggraph)

drugnet_edges <- read_csv(
  "C:/Users/terry/OneDrive/Documents/working dirctory/DRUGNET_edgelist.csv",
  show_col_types = FALSE
)

# Build directed graph
g <- graph_from_data_frame(drugnet_edges, directed = TRUE)

# Convert to undirected
g_u <- as.undirected(g, mode = "collapse")

# Extract giant component
comp <- components(g_u)
giant_id <- which.max(comp$csize)
keep <- V(g_u)[comp$membership == giant_id]
g_main <- induced_subgraph(g_u, keep)

# Centrality measures
deg <- degree(g_main)
bet <- betweenness(g_main, directed = FALSE, normalized = TRUE)
clo <- closeness(g_main, normalized = TRUE)

# Display top central nodes
sort(deg, decreasing = TRUE)[1:5]
## 50 30 64 38 55 
## 15 11 11 10  7
sort(bet, decreasing = TRUE)[1:5]
##        83        98       130        50       165 
## 0.3668474 0.3556685 0.3267437 0.2750736 0.1944822
# Community detection
comm <- cluster_louvain(g_main)
V(g_main)$cluster <- factor(membership(comm))
V(g_main)$degree <- deg

# Centrality table sorted by betweenness
centrality_tbl <- tibble(
  node = names(bet),
  betweenness = as.numeric(bet),
  closeness = as.numeric(clo)
) %>%
  arrange(desc(betweenness))

centrality_tbl
## # A tibble: 193 × 3
##    node  betweenness closeness
##    <chr>       <dbl>     <dbl>
##  1 83          0.367     0.215
##  2 98          0.356     0.204
##  3 130         0.327     0.194
##  4 50          0.275     0.214
##  5 165         0.194     0.210
##  6 38          0.177     0.176
##  7 8           0.164     0.205
##  8 64          0.160     0.206
##  9 31          0.149     0.157
## 10 220         0.126     0.187
## # ℹ 183 more rows
# Centrality table sorted by closeness
centrality_tbl_closeness <- centrality_tbl %>%
  arrange(desc(closeness))

centrality_tbl_closeness
## # A tibble: 193 × 3
##    node  betweenness closeness
##    <chr>       <dbl>     <dbl>
##  1 83         0.367      0.215
##  2 50         0.275      0.214
##  3 165        0.194      0.210
##  4 64         0.160      0.206
##  5 8          0.164      0.205
##  6 98         0.356      0.204
##  7 55         0.0903     0.194
##  8 68         0.0828     0.194
##  9 130        0.327      0.194
## 10 30         0.120      0.193
## # ℹ 183 more rows
# Identify top 10 nodes
top_bet_nodes <- names(sort(bet, decreasing = TRUE)[1:10])
top_clo_nodes <- names(sort(clo, decreasing = TRUE)[1:10])

V(g_main)$role <- case_when(
  V(g_main)$name %in% top_bet_nodes & V(g_main)$name %in% top_clo_nodes ~ "Both",
  V(g_main)$name %in% top_bet_nodes ~ "Betweenness",
  V(g_main)$name %in% top_clo_nodes ~ "Closeness",
  TRUE ~ "Neither"
)

V(g_main)$role <- factor(V(g_main)$role,
                         levels = c("Neither","Betweenness","Closeness","Both"))

# Final network plot
set.seed(100)

ggraph(g_main, layout = "fr", niter = 4000) +
  geom_edge_link(alpha = 0.10) +
  geom_node_point(aes(size = degree, color = cluster, shape = role), stroke = 1) +
  geom_node_text(
    data = function(x) x %>% dplyr::filter(role != "Neither"),
    aes(label = name),
    repel = TRUE,
    size = 3
  ) +
  scale_shape_manual(values = c(
    "Neither" = 16,
    "Betweenness" = 17,
    "Closeness" = 15,
    "Both" = 8
  )) +
  theme_void()

8.1 Which numbered individual had the highest betweenness centrality? The individual with ID 83 had the highest betweenness centrality.

8.2 Which numbered individual had the highest closeness centrality? The individual with ID 83 also had the highest closeness centrality.

8.3 What cluster (color) did the individual with the highest betweenness belong to? Individual 83 belonged to Cluster 6 (the color assigned to that cluster in the plot).

8.4 What cluster (color) did the individual with the highest closeness belong to? Because individual 83 also had the highest closeness score, they also belonged to Cluster 6.

8.5 Given this drug network, what can be said about the individual with the highest closeness score? The individual with the highest closeness score (83) is structurally positioned near the center of the network. This means they can quickly reach many others through relatively short paths. In practical terms, they may be highly efficient at spreading information, resources, or influence throughout the network. From an enforcement or intervention perspective, targeting this individual could significantly disrupt communication flow within the broader network.