R Igraph Error: "Weight vector must be positive, Invalid value"

Viewed 673

I've built several graphs in iGraph. In each graph, nodes represent words, and edge weights represent the number of times Word A was given as a response (in a word association task) to Word B. In each graph, I've normalised the edge weights so that they vary between 0 and 1 using the following code:

E(G)$weight <- E(G)$weight / max(E(G)$weight)

These values are appropriate when analysing node/network strength, but when calculating functions pertaining to betweenness (e.g. calling the betweenness function, or using betweenness-based community detection, they need to be changed into distances - i.e. inverted:

G2 = G
E(G2)$weight = 1 - E(G2)$weight

The problem is that this results in vectors which contain several 0's (i.e. for those which had a strength of 1 before being inverted. This results (at least, I think that this is the cause) in error messages such as:

Error in cluster_edge_betweenness(G2.JHJ.strong, weights = E(G2.JHJ.strong)$weight,  : 
  At community.c:455 : weights must be strictly positive, Invalid value

What can be done about this?

Thanks,

Peter

1 Answers

If you want to play it safe, you can try sum instead of max to normalize the weights, e.g.,

E(G)$weight <- E(G)$weight / sum((E(G)$weight)

or

E(G)$weight <- 2**((E(G)$weight - min(E(G)$weight)) / diff(range(E(G)$weight)))
Related