tidygraph包对于计算网络统计数据来说真的很棒。但是,我有一个具有时间维度的网络,我想计算每个网络的网络统计数据(例如中心性)。(例如,分别计算每个日期的中心性。)
我想有一种方法可以通过purrr和map来做到这一点,但我还在纠结于确切的语法。感谢您在这里提供的任何帮助。重复下面的例子。
library(tidygraph)
library('purrr')
library(dplyr)
library(tidyr)
# example network data with temporal dimension
edges <- tibble(
from = c(1, 2, 2, 3, 4, 1, 2, 3, 4, 4, 4, 2),
to = c(2, 3, 4, 2, 1, 2, 3, 4, 3, 2, 1, 3),
date = c(rep(1,4), rep(2,4), rep(3,4))
)
nodes <- tibble(id = 1:4)
# calculate centrality over all time periods of network
graph <-
tbl_graph(
nodes = nodes,
edges = edges,
directed = FALSE
)
graph_out <-
graph %>%
mutate(cent_alpha = centrality_alpha()))
# calculate centrality for each time period of the network?
edges_list <-
split(edges, edges$date)
# this doesn't work for me
graph_list <-
lmap(edges_list,
~ tbl_graph(nodes = nodes, edges = .x, directed = FALSE))
## Yikes... no idea
graph_out <-发布于 2021-07-06 11:45:06
我能想到的最快的方法是使用split函数创建一个列表,列表中的每个元素都是给定日期的图形结构。然后,您可以使用map为每个日期创建一个tidygraph对象,最后为每个日期创建一个中心性度量:
edges %>%
split(.$date) %>%
map(~tbl_graph(edges = ., nodes = nodes, directed = FALSE)) %>%
map(~igraph::alpha_centrality(.))
# $`1`
# [1] 0 -1 -1 0
# $`2`
# [1] -1 -1 -1 -1
# $`3`
# [1] 0 -1 -1 -1如果您希望保存整个过程中的每个步骤,则可以创建一个嵌套表:
df <-
edges %>%
group_by(date) %>%
nest() %>%
rename(edges = data) %>%
mutate(
graph = map(edges, ~tbl_graph(edges = ., nodes = nodes, directed = FALSE)),
cent_alpha = map(graph, ~igraph::alpha_centrality(.))
)
df
## A tibble: 3 x 5
## Groups: date [3]
# date edges graph cent_alpha
# <dbl> <list> <list> <list>
#1 1 <tibble [4 × 2]> <tbl_grph> <dbl [4]>
#2 2 <tibble [4 × 2]> <tbl_grph> <dbl [4]>
#3 3 <tibble [4 × 2]> <tbl_grph> <dbl [4]>
df$cent_alpha
# [[1]]
# [1] 0 -1 -1 0
# [[2]]
# [1] -1 -1 -1 -1
# [[3]]
# [1] 0 -1 -1 -1最后一种方法的好处是,您可以在行中存储有关每个日期图形的任何类型的数据,甚至是曲线图:
library(ggraph)
plot_fun <- function(gr){
gr %>%
ggraph(layout = "kk") +
geom_edge_link() +
geom_node_point(size = 6, colour = 'steelblue') +
geom_node_text(aes(label = id), colour = 'white', vjust = 0.4) +
theme_void()
}
df <-
df %>%
mutate(plot = map(graph, ~plot_fun(.)))
cowplot::plot_grid(plotlist = df$plot, labels = df$date, vjust = 5)

如果只给我们tidygraph对象,而不是边/节点数据帧,我们可以很容易地创建这些数据帧,如下所示:
edges <-
graph %>%
activate(edges) %>%
data.frame()
nodes <-
graph %>%
activate(nodes) %>%
data.frame()发布于 2021-07-06 10:45:00
您可以使用map作为-
library(purrr)
library(tidygraph)
result <- map(edges_list, ~tbl_graph(nodes = nodes,edges = .x,directed = FALSE))或者使用lapply -
result <- lapply(edges_list, function(x)
tbl_graph(nodes = nodes, edges = x, directed = FALSE))https://stackoverflow.com/questions/68263125
复制相似问题