首页 文章

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

提问于
浏览
5

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

person  Event
Amy     football_game
Sam     picnic
Bob     art_show

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

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

net <- graph.edgelist(myobject)

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

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

  • 如何使用 bipartite.mapping 功能?

  • 如何将igraph对象输入tnet?

  • 如果所有其他方法都失败了,我该如何输入带有重复边的数据框到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"

1 回答

  • 18

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

    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'字母告诉您这是一个二分图 . 您可以通过以下方式创建此网络的单一投影:

    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'是边数 .

相关问题