1

I am trying out tidygraph on networks coming from different experimental treatments and am mostly interested to get out graph-wide metrics and potentially also node-level metrics. I can't seem to be able to get my head around the way tidygraph works.

I am using R v3.5 and tidygraph v1.1.

My data are structured in that way:

dat <- data.frame(Treatment = rep(c("A","B"),each = 2),
                  from = c("sp1","sp2","sp1","sp2"),
                  to = c("sp2","sp3","sp3","sp3"),
                  weight = runif(4))

If I want to get per treatment a graph-wide metric like the diameter I would be tempted to do:

dat %>%
 as_tbl_graph() %>%
 activate(edges) %>%
 group_by(Treatment) %>%
 mutate(Diameter = graph_diameter(weights = weight))

But I am unsure about the result as the diameters are then given for each edges while I would be expecting one measure per treatment (per graph).

Similarly if I want to derive some metrics like the connectivity of each node for each treatment this appears to be not so straightforward since the treatment variables is dropped from the nodes table. I have been trying various hacks like pasting the treatment IDs to the from and to columns before calling as_tbl_graph() along these lines:

dat %>%
 mutate(from = paste(from, Treatment, sep = "_"),
       to = paste(to, Treatment, sep = "_")) %>%
 as_tbl_graph() %>%
 mutate(Treatment = substr(name, 5, 5), name = substr(name, 1, 3)) %>%
 group_by(Treatment) %>%
 mutate(Centrality = centrality_betweenness())

But I got errors that the resulting vectors were of the wrong size (6 instead of 3 or 1).

Is there a way with tidygraph to derive group-level graph-wide and node-level metrics?

Lionel
  • 73
  • 2
  • 6
  • I would be very much interested in the answer as well. How to calculate graph-level statistics in a tidy way? – strohne Mar 07 '19 at 20:05

1 Answers1

1

I think that this is an interesting problem. I have tried several times and still haven't got the best solution for it. But, I suspect that this problem needs morph() function in order to separate graph properly. Again, I haven't explore that much. But, here I give the simple solution for the diameter problem using morph() function. Hope it helps you.

dat %>% 
 as_tbl_graph() %>%
 activate(edges) %>% 
 morph(to_split,split_by = "edges")%>% 
 filter(Treatment=="A") %>% 
 mutate(Diameter = graph_diameter(weights = weight)) %>% 
unmorph() %>% activate(edges) %>%  
morph(to_split,split_by = "edges") %>%
filter(Treatment=="B") %>%
mutate(Diameter = graph_diameter(weights = weight)) %>% 
unmorph()