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 (i.e., direct neighbors but also ties to indirect neighbors at distance 2).
I found a solution that works for density: 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. I have not found igraph documentation for the former function. The latter allows for options like loop=FALSE, but there, one has to explicitly specify the graph: graph.density(g,loop=FALSE). Since I would like to calculate other structural network measures for the ego networks, (e.g., 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. 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. 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?