问题
I am trying to find out if node attributes and edge attributes are used during graph network clustering (i.e. community detection algorithms) in R. I could not find an answer, so I decided to write some code for this problem and compare the differences. I first created a file of node attributes and edge attributes - then I created a graph network. On this graph network, I performed the clustering/community detection algorithm.
In Method 1, I use the full information from the edges and nodes for the clustering/community detection.
In Method 2, I ONLY use the relationships between the nodes for the clustering/community detection.
Just by comparing the results from Method 1 and Method 2, there does not seem to be a difference - it seems that community detection algorithms (graph network clustering) does not take into account node and edge attributes. However, this might not always be the case (i.e. maybe just for the example I created, the results are the same).
Does anyone know if all the node attributes and edge attributes are used during community detection algorithms?
Also, I have only 8 unique nodes (John, Jack, Jason, Jim, Julian, Jeff, Jake, Joseph) - yet the resulting graph is displaying 10 nodes. Does anyone know why this is happening?
I have attached my code below.
#load libraries
library(igraph)
library(visNetwork)
#create node attributes file
Personal_Information <- data.frame(
"name" = c("John", "Jack", "Jason", "Jim", "Julian", "Jeff", "Jake", "Joseph"),
"age" = c("41","33","24","66","21","66","29", "50"),
"salary" = c("50000","20000","18000","66000","77000","0","55000","40000"),
"debt" = c("10000","5000","4000","0","20000","5000","0","1000")
)
Personal_Information$age = as.numeric(Personal_Information$age)
Personal_Information$salary = as.numeric(Personal_Information$salary)
Personal_Information$debt = as.numeric(Personal_Information$debt)
#create edge attributes file
Relationship_Information <-data.frame(
"name_a" = c("John","John","John","Jack","Jack","Jack","Jason","Jason","Jeff","Jim","Jim","Julian","Jake","Joseph","Joseph"),
"name_b" = c("Jack", "Jason", "Joseph", "John", "Julian","Jim","Jim", "Joseph", "Jack", "Julian", "John", "Joseph", "John", "Jim", "John"),
"how_much_they_owe_each_other" = c("10000","20000","60000","10000","40000","8000","0","50000","6000","2000","10000","10000","50000","12000","0"),
"how_much_they_paid_each_other" = c("5000","40000","120000","20000","20000","8000","0","20000","12000","0","0","0","50000","0","0")
)
Relationship_Information$how_much_they_owe_each_other = as.numeric(Relationship_Information$how_much_they_owe_each_other)
Relationship_Information$how_much_they_paid_each_other = as.numeric(Relationship_Information$how_much_they_paid_each_other)
Method 1
# Method 1: community detection WITH node attributes and edge attributes
#create graph
g <- graph_from_data_frame(Relationship_Information, directed=FALSE, vertices=Personal_Information)
g1 <- simplify(graph)
#plot graph
plot(g1)
#run graph clustering (also called community dectection) on the graph
fc <- fastgreedy.community(g1)
V(g1)$community <- fc$membership
nodes <- data.frame(id = V(g1)$name, title = V(g1)$name, group = V(g1)$community)
nodes <- nodes[order(nodes$id, decreasing = F),]
edges <- get.data.frame(g1, what="edges")[1:2]
visNetwork(nodes, edges) %>%
visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE)
Method 2
# Method 2: community detection WITHOUT node attributes and edge attributes
#create graph
g <- graph_from_data_frame(Relationship_Information[,c(1:2)], directed=FALSE)
g2 <- simplify(graph)
plot(g2)
#run graph clustering (also called community dectection) on the graph
fc <- fastgreedy.community(g2)
V(g2)$community <- fc$membership
nodes <- data.frame(id = V(g2)$name, title = V(g2)$name, group = V(g2)$community)
nodes <- nodes[order(nodes$id, decreasing = F),]
edges <- get.data.frame(g2, what="edges")[1:2]
visNetwork(nodes, edges) %>%
visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE)
Thanks
来源:https://stackoverflow.com/questions/64864298/r-are-node-attributes-and-edge-attributes-used-during-network-graph-cluster