R:有向图与无向图(参数被忽略,潜在的警告消息) [英] R: Directed Graphs vs Undirected Graphs (arguments being ignored, potential warning message)
问题描述
我正在使用R编程语言,而我使用的是"igraph"库.
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)
这似乎很好.
但是:当我使用相同的数据时,制作一个有向图,然后运行社区检测,我会收到以下警告消息(但代码仍在运行):
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)
图书馆(igraph)
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)
由于两个代码段均产生不同的视觉输出-我不确定计算机是否会忽略此语句 graph<-graph.data.frame(my_data,directed = T)
.
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?
谢谢
推荐答案
否,不会被忽略.结果图形是有向的,您可以从警告消息中看到,也可以打印 graph
变量:
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 --
其中 DN
表示有向图和命名图.最终,您可以在绘制时从箭头看到它.您可以使用 is.directed
函数对其进行检查.
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.
通过边缘间性算法计算的社区结构是不同的,因为它对边缘的方向很敏感.如果使用 directed = FALSE
参数运行它,则有向图将获得相同的社区:
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)
警告消息不是在抱怨社区检测算法,而是在给定的社区结构下计算图的模块化的方法. edge_betweenness
和其他一些社区检测方法因此给出了层次聚类.为了获得单个隶属度向量,igraph计算聚类的每个级别的模块性,并选择具有最高模块性得分的级别.您可以在 cluster $ merges
中找到分层集群,并在 cluster $ modularity
中找到每个级别的模块化.如您所见,在发行后此处为代码警告消息,对于有向图,计算将以相同的方式继续进行,我想只是忽略了方向.同样,这个问题最近也得到解决,有向图的模块化计算很快就会在igraph .
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.
这篇关于R:有向图与无向图(参数被忽略,潜在的警告消息)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!