3

I have two networks of same vertex based on different criteria. I want to add edge attributes of one of the network based on the connection of the other network. That is, if node A and B are connected in network 2, I want to note down "1" as the attribute in network 1, if not connected, note down "0". I am wondering how can I achieve my goal with R package or other software? Any suggestion is welcomed. Thanks a lot for suggestion!

G5W
  • 36,531
  • 10
  • 47
  • 80
Jingy Niu
  • 31
  • 4
  • Thanks a lot for the guidance. That is very helpful! May I ask a follow up question? since I have edge attribute of g2, can I apply this information in my ERGM model, to check the effect of network alignment in the odds ratio of an edge in the focal model? – Jingy Niu Jan 02 '19 at 09:45
  • I have vote to reopen ; it doesn't have a reproducible example but it seems clear enough and has a good answer that may be useful for others. – user20650 Jan 02 '19 at 13:54

1 Answers1

1

You can do this in R using the igraph package. Since you do not provide any data, I will make up an example.

Example Data

library(igraph)

set.seed(1234)
g1=erdos.renyi.game(10, 0.35)
g2=erdos.renyi.game(10, 0.35)
par(mfrow=c(1,2))
plot(g1)
plot(g2)

Two example graphs

Now we can create the edge attribute that you want. We initialize all values to zero, then loop through each edge in g2. If the same edge occurs in g1, we change the attribute to be 1.

E(g2)$net1 = 0
for(e in E(g2)) {
    if(are_adjacent(g1, ends(g2,e)[1], ends(g2,e)[2])) {
        E(g2)$net1[e] = 1 }
}

E(g2)$net1
[1] 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
E(g2)[which(E(g2)$net1 > 0)]
+ 4/19 edges from 3bdc176:
[1] 3--4 4--5 4--6 5--7

You can see that the attribute net1 says that the shared links are:
3--4 4--5 4--6 5--7
which agrees with the plot.

G5W
  • 36,531
  • 10
  • 47
  • 80
  • I wonder if this could be done using `intersection`: for example, `inter <- intersection(g1, g2) ; E(g2, P=t(get.edgelist(inter)))$net1 <- 1`, although this way does require a dense matrix to be formed – user20650 Jan 01 '19 at 21:15
  • Thanks a lot for the guidance. That is very helpful! May I ask a follow up question? since I have edge attribute of g2, can I apply this information in my ERGM model, to check the effect of network alignment in the odds ratio of an edge in the focal model? – Jingy Niu Jan 02 '19 at 09:44
  • @JingyNiu I know little about ERGMs. I cannot help you with that. – G5W Jan 02 '19 at 13:52
  • Thanks a lot anyway! – Jingy Niu Jan 03 '19 at 09:21