0

I have a graph generated by following code:

library(igraph)

dat <- data.frame(
  V0 = c(0L, 100L, 200L, 0L, 0L, 0L, 0L),
  V2 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L),
  V3 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L),
  V4 = c(120L, 0L, 0L, 0L, 0L, 0L, 0L),
  V6 = c(0L, 0L, 0L, 30L, 0L, 0L, 0L),
  V10 = c(180L, 0L, 0L, 90L, 0L, 0L, 0L),
  V12 = c(0L, 0L, 0L, 0L, 30L, 270L, 0L))
rownames(dat) <- c("V0","V2","V3","V4","V6","V10","V12")
dat <- data.matrix(dat)

g2 <- graph_from_adjacency_matrix(dat, weighted=TRUE)
plot(g2, vertex.size = 20, edge.label = E(g2)$weight)

The graph should look like this: view on this link

My expected output is to calculate the Weighted In-Degree, Weighted Out-Degree. Thank you a lot

Szabolcs
  • 24,728
  • 9
  • 85
  • 174
  • 1
    According to your comments on the answer, you are not sure what you want to calculate, but it is not the weighted degree. I voted to close this question so that people wouldn't spend time writing answers different from what you want. Please edit the post and explain what you want to calculate in precise mathematical terms. – Szabolcs Jul 14 '22 at 09:33

1 Answers1

2

You're looking for igraph::strength with mode = "in" and mode = "out".

strength(g2, mode = "in")
# V0  V2  V3  V4  V6 V10 V12 
#300   0   0 120  30 270 300 

strength(g2, mode = "out")
# V0  V2  V3  V4  V6 V10 V12 
#300 100 200 120  30 270   0 

See also the manual.

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • no, what I meant is different. For example Node V4, the weighted in-degree is 600 (as degree of V0), the weighted out-degree is 600-180(V0 - >V10: the only edge that have no direct and indirect connection with V4). I don't know if I use the correct term or not but my wish is to calculate like that. Thank you for answering my question – Luong Nguyen Thanh Jul 14 '22 at 01:57
  • Why is the weighted in-degree = 600 for V4? V4 has 120 units going in (in-degree weight) and 90 + 30 = 120 units going out (out-degree weight). – Maurits Evers Jul 14 '22 at 01:59
  • hi, sorry if my question is not clear because I am just a novice for network analysis. What I am going to do is to summarise all upstream edges and downstream edge of the node, not just its parents and children – Luong Nguyen Thanh Jul 14 '22 at 06:30
  • No worries but can you walk me through explicitly how you're getting a value of 600 for V4? Show me the calculation. – Maurits Evers Jul 14 '22 at 06:32
  • PS. As you've already guessed: whatever it is you're after, these are not the weighted in & out-degrees. – Maurits Evers Jul 14 '22 at 06:34
  • Here is the request from my supervisor as said in his email: "Yes, hmm. We need to take into account all upstream and downstream relations. What if for vertex 4 you calculate total outdegree as (520) / (600+520). You see, I only exclude 180 that is not up or downstream to it.... hmm..." I guess he miscalculated the total weighted in-degree. It should be 420 instead of 520. Because I also don't know how he came to this number so I cannot asnwer your question – Luong Nguyen Thanh Jul 14 '22 at 06:50
  • I suggest pointing your supervisor to this post on SO. You may have misunderstood what he asked; because what you describe are *not* the weighted in and out-degrees. – Maurits Evers Jul 19 '22 at 11:09