简体   繁体   English

r igraph lapply:在更大的网络中计算自我网络的密度、约束或其他度量

[英]r igraph lapply: Compute density, constraint or other measures for ego networks in a larger network

I have a network data set with thousands of nodes that form a complete network with isolates, different subgraphs, and several distinct components.我有一个包含数千个节点的网络数据集,这些节点形成了一个具有隔离、不同子图和几个不同组件的完整网络。 I want to calculate structural network measures like density and constraint for the ego networks of each node (ie, direct neighbors but also ties to indirect neighbors at distance 2).我想计算每个节点的自我网络的结构网络度量,如密度和约束(即,直接邻居,但也与距离为 2 的间接邻居有联系)。

I found a solution that works for density: Igraph: get ego network density from large network我找到了一个适用于密度的解决方案: Igraph: get ego network density from large network

library(igraph) 


g<- graph.formula(1-+2,1-+3,2-+4,2-+5,3-+6,5-+7,7-+8,8-+9,9+-7, 9-+10,
                  6-+9,1-+5,3-+9,10-+11,11-+12,11-+5,12-+4,4-+10,10-+4,11-+10)

# Density for ego graphs

ego_1_graph <- make_ego_graph(
  g,
  order = 1,
  nodes = V(g),
  mode = c("all"),
  mindist = 0
)

V(g)$ego_1a_dens = lapply(ego_1_graph, graph.density) ## this works
V(g)$ego_1a_dens 

V(g)$ego_1b_dens = lapply(ego_1_graph, edge_density(loops= FALSE)  ) ## this needs a graph specified
## Error in is_igraph(graph) : argument "graph" is missing, with no default

V(g)$ego_1b_dens = lapply(ego_1_graph, function(v) edge_density(ego_1_graph[v], loops= FALSE) ) ## I cannot access the graph saved in the ego_1_graph list
## Error in ego_1_graph[v] : invalid subscript type 'list' 

The linked solution uses graph.density instead of edge.density.链接的解决方案使用 graph.density 而不是 edge.density。 I have not found igraph documentation for the former function.我还没有找到前一个函数的 igraph 文档。 The latter allows for options like loop=FALSE, but there, one has to explicitly specify the graph: graph.density(g,loop=FALSE).后者允许使用 loop=FALSE 之类的选项,但在那里,必须明确指定图形:graph.density(g,loop=FALSE)。 Since I would like to calculate other structural network measures for the ego networks, (eg, constraint) using options, I really would like to work with the documented functions.由于我想使用选项计算自我网络的其他结构网络度量(例如,约束),我真的很想使用文档化的函数。

I struggle to access the constructed ego graph for each network node using the lapply function.我很难使用 lapply 函数访问每个网络节点的构建的自我图。 I hope you can help!我希望你能帮忙! Thank you!谢谢!

As an aside: I have a large network, so computational efficiency is relevant.顺便说一句:我有一个大型网络,因此计算效率是相关的。 I know that apply functions are already vectorized.我知道应用函数已经矢量化了。 From reading StackOverflow I could gather, that igraph's make_ego_graph function could be a potential bottleneck.从阅读 StackOverflow 我可以了解到,igraph 的 make_ego_graph 函数可能是一个潜在的瓶颈。 I found solutions for constructing direct neighbor ego networks via data frames FAST way to sum up neighbors' attributes in a large graph in R but how would I do this with ego networks of distance 2 or more?我找到了通过数据帧快速方法构建直接邻居自我网络的解决方案, 以在 R 中的大图中总结邻居的属性,但是我将如何使用距离为 2 或更多的自我网络来做到这一点?

Try lapply like this像这样尝试lapply

V(g)$ego_1b_dens <- lapply(ego_1_graph, edge_density, loops = FALSE)

and you will see你会看到

> V(g)$ego_1b_dens
[[1]]
[1] 0.3333333

[[2]]
[1] 0.3333333

[[3]]
[1] 0.3333333

[[4]]
[1] 0.3333333

[[5]]
[1] 0.25

[[6]]
[1] 0.5

[[7]]
[1] 0.3333333

[[8]]
[1] 0.5

[[9]]
[1] 0.2333333

[[10]]
[1] 0.4166667

[[11]]
[1] 0.3333333

[[12]]
[1] 0.3333333

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM