Search code examples
rconstraintsigraphsubgraph

igraph r constraint for ego vertices in ego networks


I like to calculate the constraint for vertices in their ego network (e.g., distance 1). I want to automate this using an apply function.

Up to now, I can calculate the constraint for all vertices of ego networks. I have, however, not managed to pick ego's constraint because the position of the ego vertex in ego networks varies (vertices are ordered and depending on the vertices that are part of the ego network, ego's position varies).

Here is a small sample network that hopefully clarifies the task.

Thank you!


library(igraph)



g <- graph.formula( Andre----Beverly:Diane:Fernando:Carol,
                   Beverly--Andre:Diane:Garth:Ed,
                   Carol----Andre:Diane:Fernando,
                   Diane----Andre:Carol:Fernando:Garth:Ed:Beverly,
                   Ed-------Beverly:Diane:Garth,
                   Fernando-Carol:Andre:Diane:Garth:Heather,
                   Garth----Ed:Beverly:Diane:Fernando:Heather,
                   Heather--Fernando:Garth:Ike,
                   Ike------Heather:Jane,
                   Jane--Ike )
g <- simplify(g)
coords <- c(5,5,119,256,119,256,120,340,478,
           622,116,330,231,116,5,330,451,231,231,231)
coords <- matrix(coords, nc=2)
V(g)$label <- V(g)$name
g$layout <- coords
plot(g)


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

# ideally, I would want to calculate Ego's constraint in their ego network and store this value in a data frame with one line for each vertice.


dat <- data.frame(
 Node_ID = names(V(g)),
 ego_1_cons = lapply(ego_1_graph, constraint) %>% unlist()
 )

# constraint saves the constraint for each vertex in each ego network

# I need to access the individual ego networks (10 = one for each vertex)
# I need to pick the constraint only for the ego vertex

# Problem: vertices are ordered 1-10. 
# Ego networks are constructed from all vertices with connections to the ego vertex or from connections among such vertices.
# Therefore, the ego vertex is not always the first vertex in the ego network. 
#, E.g.:

# Vertex name: Heather (eighth vertex in the complete network)
cons8 <- round(constraint(ego_1_graph[[8]]),4)
cons8 # Heather's constraint is at position 3 of the list because of her connections to Fernando (fourth vertex in the complete network) and Garth (sixth vertex in the complete network)
cons8[[3]]


# How can I select the constraint value for the ego vertex? 
# How could I automate this for all ego networks in an apply function?```

Solution

  • Try sapply like below

    dat <- transform(
      data.frame(
        Node_ID = names(V(g))
      ),
      ego_1_cons = sapply(
        seq_along(Node_ID),
        function(k) constraint(ego_1_graph[[k]])[Node_ID[k]]
      )
    )
    

    which gives

    > dat
              Node_ID ego_1_cons
    Andre       Andre  0.7044271
    Beverly   Beverly  0.7044271
    Diane       Diane  0.4984568
    Fernando Fernando  0.5541667
    Carol       Carol  0.9259259
    Garth       Garth  0.5541667
    Ed             Ed  0.9259259
    Heather   Heather  0.6111111
    Ike           Ike  0.5000000
    Jane         Jane  1.0000000