如何使用igraph或tnet在R中创建二分网络

Olg*_* Mu 6 r igraph sna

我有一个双模网络的边缘列表,类似于:

person  Event
Amy     football_game
Sam     picnic
Bob     art_show
Run Code Online (Sandbox Code Playgroud)

我想在R中对此进行分析,但看起来我尝试的一切都失败了.将其转换为单模式网络会遇到内存限制,我无法弄清楚如何在igraph或tnet中将其分析为二分.

在igraph中,bipartite.projection给我所有FALSE,在使用的igraph对象上

net <- graph.edgelist(myobject)
Run Code Online (Sandbox Code Playgroud)

在tnet上,我无法将igraph网转换为tnet网,当我尝试使用原始数据框时,它会因图中的重复而拒绝.

因此,以下任何一个的答案将非常感激:

  1. 我该如何使用该bipartite.mapping功能?
  2. 如何将igraph对象输入tnet?
  3. 如果所有其他方法都失败了,我该如何输入带有重复边的数据框到tnet?

很抱歉,如果这些是基本问题,但文档很少.

编辑

例:

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"
Run Code Online (Sandbox Code Playgroud)

Gab*_*rdi 19

在igraph中,二分网络是具有type顶点属性的网络.此属性必须是逻辑的,并且必须是TRUE其中一个节点类型和FALSE其他节点类型.因此,要从边列表创建二分网络,只需创建一个常规图形,然后添加type顶点属性:

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)
Run Code Online (Sandbox Code Playgroud)

'B'字母告诉您这是一个二分图.您可以通过以下方式创建此网络的单独投影:

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)
Run Code Online (Sandbox Code Playgroud)

这将返回两个图表的列表.如果您认为投影可能太大,您可以先调用该bipartite.projection.size函数,这将为您提供两个投影中的顶点和边数.igraph图的内存要求是(4m + 2n)*8 + O(1)字节,其中'n'是顶点数,'m'是边数.