【问题标题】:degree, closeness, betweenness for each connected network每个连接网络的度数、紧密度、介数
【发布时间】:2019-02-23 04:21:20
【问题描述】:

我有一个带有几个断开连接的组件的 igraph。例如:

library(igraph)
g <- simplify(
  graph.compose(
    graph.ring(10), 
    graph.star(5, mode = "undirected")
  )
) + edge("7", "8")

我想计算这 3 个连接网络中的每一个的紧密度、介数和度数。到目前为止,我已经设法分离了网络,但不知道如何进一步进行。 这是我写的代码:

x <- clusters(g)
dg <- decompose.graph(g) # returns a list of three graphs
plot(dg[[156]], vertex.label=NA)

x1 <- data.frame(x$membership)
x1 <- cbind(Row.Names = rownames(x1), x1)
x1<- data.frame(x1)
rownames(x1) <- NULL
names(x1) <- c("email","membership")

x2 <- data.frame(x$csize)
x2 <- cbind(Row.Names = rownames(x2), x2)
x2 <- data.frame(x2)
rownames(x2) <- NULL
names(x2) <- c("membership","number_of_connections")
x2$membership <- as.numeric(x2$membership)

network <- merge(x1, x2, by = "membership")

这是我的输出:

我正在尝试计算 3 个不同的列:每封电子邮件的度数、接近度、介数。

【问题讨论】:

    标签: r igraph


    【解决方案1】:

    你可以计算所有这些东西。

    B = rep(0, vcount(g))
    for(i in 1:3) { B[x$membership==i] = betweenness(dg[[i]]) }
    C = rep(0, vcount(g))
    for(i in 1:3) { C[x$membership==i] = closeness(dg[[i]]) }
    
    Nodes = data.frame(membership = x$membership, 
            email = 1:10,
            connections = x$csize[x$membership],
            degree = degree(g),
            closeness = C,
            betweenness = B)
    Nodes
       membership email connections degree  closeness betweenness
    1           1     1           7      5 0.14285714         6.0
    2           1     2           7      5 0.14285714         1.5
    3           1     3           7      3 0.11111111         0.5
    4           1     4           7      3 0.11111111         0.5
    5           1     5           7      3 0.11111111         0.5
    6           1     6           7      1 0.08333333         0.0
    7           2     7           2      1 1.00000000         0.0
    8           2     8           2      1 1.00000000         0.0
    9           3     9           1      0        NaN         0.0
    10          1    10           7      4 0.11111111         1.0
    

    【讨论】:

    • 谢谢@G5W。上述方法适用于小数据,但我的数据集很大。计算需要很长时间。
    【解决方案2】:

    如果 dg 是您的网络列表,那么您可以使用 lapply 计算每个网络的中介中心性。

    dg_bet<-lapply(dg,betweenness)
    

    然后从 purrr 包中 map_df 将列表转换为数据框。

    dg_bet <- map_df(dg_bet, ~as.data.frame(.x), .id="id")
    

    这给了你

           id  .x
       1   1 6.0
       2   1 1.5
       3   1 0.5
       4   1 0.5
       5   1 0.5
       6   1 0.0
       7   1 1.0
       8   2 0.0
       9   2 0.0
       10  3 0.0
    

    您可以应用这种方法来计算接近度和度数,然后使用 cbind 将它们连接到您的主数据框。

    【讨论】:

    • 谢谢。但是如何为 dg_bet 中的每一行添加另一列“电子邮件”?请注意,我在这里给出的示例是一个玩具示例,在我的实际数据中,“电子邮件”列不是数字序列,而是一些真实的电子邮件 ID
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 2016-02-15
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多