问题
I am using the R programming language and the "igraph" library.
Suppose I have some data and I make this into a undirected graph and perform community detection (graph clustering)
# undirected graph
library(igraph)
my_data <- data.frame(
"node_a" = c("Store_A", "Store_A", "Store_A", "Store_B", "Store_B", "Store_C", "Store_C", "Store_C", "Store_C", "Store_C", "Store_B", "Store_C", "customer_4", "customer_9", "customer_1"),
"node_b" = c("customer_1", "customer_2", "customer_3", "customer_3", "customer_4", "customer_2", "customer_5", "customer_6", "customer_7", "Store_B", "customer_9","customer_9", "customer_5", "customer_4", "customer_1")
)
node_size <- data.frame(col = unique(unlist(my_data)))
node_size$size <- c("50","50","50","15","15","15","15","15","15","15","15")
graph <- graph.data.frame(my_data, directed=F)
graph <- simplify(graph)
cluster = cluster_edge_betweenness(graph)
plot(cluster,graph)
This seems to work fine.
However: when I use the same data, make a directed graph and then run community detection, I get the following warning message (but the code still runs):
#directed graph (warning messages)
library(igraph)
my_data <- data.frame(
"node_a" = c("Store_A", "Store_A", "Store_A", "Store_B", "Store_B", "Store_C", "Store_C", "Store_C", "Store_C", "Store_C", "Store_B", "Store_C", "customer_4", "customer_9", "customer_1"),
"node_b" = c("customer_1", "customer_2", "customer_3", "customer_3", "customer_4", "customer_2", "customer_5", "customer_6", "customer_7", "Store_B", "customer_9","customer_9", "customer_5", "customer_4", "customer_1")
)
node_size <- data.frame(col = unique(unlist(my_data)))
node_size$size <- c("50","50","50","15","15","15","15","15","15","15","15")
graph <- graph.data.frame(my_data, directed=T)
graph <- simplify(graph)
cluster = cluster_edge_betweenness(graph)
#warning message produced by R:
Modularity is implemented for undirected graphs only.
Modularity is implemented for undirected graphs only.
Modularity is implemented for undirected graphs only.
Modularity is implemented for undirected graphs only.
Modularity is implemented for undirected graphs only.
Modularity is implemented for undirected graphs only.
Modularity is implemented for undirected graphs only.
Modularity is implemented for undirected graphs only.
Modularity is implemented for undirected graphs only.
Modularity is implemented for undirected graphs only.
Modularity is implemented for undirected graphs only.
plot(cluster,graph)
Since both segments of code produce different visual outputs - I am not sure if this statement graph <- graph.data.frame(my_data, directed=T)
is being ignored by the computer.
Does anyone know if this statement is being ignored?
Thanks
回答1:
No, it's not being ignored. The resulted graph is directed, this you can see from the warning messages, and also if you print the graph
variable:
> graph
IGRAPH ad7270b DN-- 11 15 --
Where DN
means directed and named graph. Finally, you see it when plotting, from the arrowsheads. And you can check it with the is.directed
function.
The community structure calculated by the edge betweenness algorithm is different because it's sensitive to the direction of the edges. If you run it with directed = FALSE
parameter, then you get the same communities for the directed graph:
cluster <- cluster_edge_betweenness(graph, directed = FALSE)
The warning messages complain about not the community detection algorithm, but the method calculating the modularity of the graph with a given community structure. edge_betweenness
and some other community detection methods give a hierarchical clustering as a result. To get a single membership vector, igraph calculates the modularity for each level of the clustering and chooses the level with the highest modularity score. The hierarchical clustering you can find in cluster$merges
and the modularity for each level in cluster$modularity
. As you see here in the code, after issuing the warning message, the calculation continues the same way for directed graphs, I guess just ignoring directions. Also this issue recently has been addressed and soon modularity calculation for directed graphs will be available in igraph.
来源:https://stackoverflow.com/questions/65219685/r-directed-graphs-vs-undirected-graphs-arguments-being-ignored-potential-warn