如何使用igraph或tnet在R中创建双向网络
我有一个用于两种模式的网络的边缘列表,类似于此:
I have an edgelist for a two mode network, similar to this:
person Event
Amy football_game
Sam picnic
Bob art_show
我想在R中对此进行分析,但是似乎我尝试的所有操作都失败了.将其转换为单模网络会遇到内存限制,而且我不知道如何在igraph或tnet中将其分析为二分法.
I want to perform an analysis on this in R, but seemingly everything I try fails. Converting it to a one mode network runs into memory limitations, and I can't figure out how to analyze it as bipartite in either igraph or tnet.
在igraph中,对于使用
In igraph, bipartite.projection
gives me all FALSE
, on the igraph object created using
net <- graph.edgelist(myobject)
在tnet上,我无法将igraph网络转换为tnet网络,而当我尝试使用原始数据帧时,由于图中的重复项,它拒绝了.
On tnet, I can't convert the igraph net to a tnet one, and when I try to use the original data frame, it refuses because of duplicates in the graph.
因此,对以下任何问题的回答将受到高度赞赏:
So answers to any of the following would be super appreciated:
- 如何使用
bipartite.mapping
功能? - 如何在inet中输入igraph对象?
- 如果所有其他方法都失败了,如何将具有重复边缘的数据帧输入到tnet中?
- How do I use the
bipartite.mapping
function? - How do I input an igraph object into tnet?
- If all else fails, how I do I input a data frame with duplicate edges into tnet?
很抱歉,如果这些是基本问题,但是文档很少.
Sorry if these are basic questions, but there's very little documentation.
示例:
edgelist <- read.table(text="Person Event
Amy football
Bob picnic
Sam artshow",
header=TRUE)
edgelist <- as.matrix(edgelist)
## Igraph Issues
igraph <- graph.edgelist(edgelist)
typevector <- bipartite.projection(igraph)
# gets all FALSE
edgelist2 <- get.edgelist(igraph)
typevector <- bipartite.projection(edgelist2)
# same thing
## tnet issues
tnet <- as.tnet(edgelist)
# gives error: "There are duplicate events in the edgelist"
tnet <- as.tnet(edgelist2)
clusterMat <- clustering_local_tm(tnet)
# gives error: "max not meaningful for factors"
onemode <- projecting_tm(tnet, method="Newman")
# gives error: "arguments must have same length"
在igraph中,双向网络是具有type
顶点属性的网络.此属性必须是逻辑属性,对于一种节点类型,必须为TRUE
,对于其他节点类型,必须为FALSE
.因此,要从边缘列表创建双向网络,只需创建一个正则图,然后添加type
顶点属性:
In igraph a bipartite network is one that has a type
vertex attribute. This attribute must be logical and must the TRUE
for one of the node types and FALSE
for the others. So to create a bipartite network from your edge list, you simply create a regular graph and then add the type
vertex attribute:
edgelist <- read.table(text="Person Event
Amy football
Bob picnic
Sam artshow",
header=TRUE)
igraph <- graph.data.frame(edgelist)
V(igraph)$type <- V(igraph)$name %in% edgelist[,1]
igraph
# IGRAPH DN-B 6 3 --
# + attr: name (v/c), type (v/x)
"B"字母告诉您这是一个二部图.您可以通过以下方式创建此网络的单方投影:
The 'B' letter tells you that this is a bipartite graph. You can create the unipartite projections of this network via:
bipartite.projection(igraph)
# $proj1
# IGRAPH UN-B 3 0 --
# + attr: name (v/c), type (v/x)
#
# $proj2
# IGRAPH UN-B 3 0 --
# + attr: name (v/c), type (v/x)
这将返回两个图的列表.如果您认为投影可能太大,可以先调用bipartite.projection.size
函数,这将为您提供两个投影中的顶点和边的数量. igraph图形的内存要求为(4m + 2n)* 8 + O(1)字节,其中"n"是顶点数,"m"是边数.
This will return a list of two graphs. If you think that the projection might be too big, you can first call the bipartite.projection.size
function, this will give you the number of vertices and edges in both projections. The memory requirement for an igraph graph is (4m+2n)*8+O(1) bytes, where 'n' is the number of vertices and 'm' is the number of edges.