# Help seeking on calculating betweenness centrality with valued ties

**URL:** <https://igraph.discourse.group/t/help-seeking-on-calculating-betweenness-centrality-with-valued-ties/280>\
**Category:** Usage\
**Tags:** R\
**Created:** [7 June 2020 03:58 UTC](https://igraph.discourse.group/t/help-seeking-on-calculating-betweenness-centrality-with-valued-ties/280 "2020-06-07T03:58:19Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![lingchuding](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/lingchuding/32/257_2.png) [@lingchuding](https://igraph.discourse.group/u/lingchuding)\
**Post date:** [7 June 2020 03:58 UTC](https://igraph.discourse.group/t/help-seeking-on-calculating-betweenness-centrality-with-valued-ties/280/1 "2020-06-07T03:58:19Z")

</div>

Dear all,

I am trying to calculate the betweenness centrality with valued ties, but I do not figure it out. Here are the dataset and codes.

1. This is an example dataset with only four nodes/individuals. When collecting the network data, I asked the participants to answer the question about friendship tie on a 7-point Likert scale. So, this is a **directed** and **valued** network. Moreover, when inputting the network data, I adopted the edge list format and saved it into a CSV file. The details of the data are as follows:

```auto
Actor Target Friend
1001 1002 5
1001 1003 6
1001 1004 5
1002 1001 6
1002 1003 6
1002 1004 6
1003 1001 4
1003 1002 4
1003 1004 4
1004 1001 6
1004 1002 6
1004 1003 6

```

1. Then I ran the following codes to calculate the betweenness centrality:

```auto
library(igraph)

#Step 1. read the edgelist format dataset into R
Mydata <- read.table("Example.csv", header=TRUE, sep=",")

#Step 2. convert an edgelist matrix with valued edges/ties into a graph
Mygraph <- graph_from_data_frame(Mydata, directed=TRUE)

#Step 3. calculate betweenness centrality but fail to account for the value/weight of the tie
betweenness(Mygraph, directed = T, normalized = T)

```

1. The results came out are as follows:

```auto
1001 1002 1003 1004

0 0 0 0

```

It seems that the package treated the dataset as that the four members mutually nominated each other as friends while ignored the strength of the tie. Therefore, each of them was connected to the others and no one had the opportunity to be a broker.

I have searched archival of the list, but I failed to locate the information that can completely solve my problem. So, I am wondering whether any colleagues here could share with me any information about this. I would be grateful if you can provide me any suggestions or references. Many thanks in advance!

Best,  
Chuding

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [7 June 2020 10:36 UTC](https://igraph.discourse.group/t/help-seeking-on-calculating-betweenness-centrality-with-valued-ties/280/2 "2020-06-07T10:36:01Z")

</div>

The betweenness centrality of a vertex is, roughly speaking, the number of shortest paths that pass through that vertex. In your graph, all shortest paths consist of a single edge. None of the shortest paths pass through any intermediate vertices. Therefore, all betweenness values are zero.

> [@lingchuding](#):
>
> It seems that the package treated the dataset as that the four members mutually nominated each other as friends while ignored the strength of the tie.

The strengths of links are not ignored, but even so all shortest paths consist of a single edge.

> [@lingchuding](#):
>
> I asked the participants to answer the question about friendship tie on a 7-point Likert scale

I am not familiar with the Likert scale, I just wanted to warn you that with betweenness calculations, edge “weights” are taken to mean edge _lengths_. Thus, the larger the number, the weaker the tie. The larger the number, the “further” the nodes it connects are.

---

<div class="post-metadata">

**Author:** ![lingchuding](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/lingchuding/32/257_2.png) [@lingchuding](https://igraph.discourse.group/u/lingchuding)\
**Post date:** [7 June 2020 11:17 UTC](https://igraph.discourse.group/t/help-seeking-on-calculating-betweenness-centrality-with-valued-ties/280/3 "2020-06-07T11:17:44Z")

</div>

Thanks for your prompt reply. And thanks for pointing out my example might be an extreme case that no one is occupying a “broker” position. However, when I try with degree centrality, the results come out are still equal for each vertex, which is not consistent with the reality in the network. Here are the codes:

```auto
library(igraph)

#read the edgelist format dataset into R

Mydata <- read.table("Example.csv", header=TRUE, sep=",")

#convert an edgelist matrix with valued edges/ties into a graph

graph <- graph_from_data_frame(Mydata, directed=TRUE)

#calculate degree centrality but fail to account for the value/weight of the tie

degree(graph)

1001 1002 1003 1004 
   6 6 6 6

```

How do you think about this?

Best,  
Chuding

---

<div class="post-metadata">

**Author:** ![vtraag](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/vtraag/32/38_2.png) [@vtraag](https://igraph.discourse.group/u/vtraag)\
**Post date:** [7 June 2020 11:57 UTC](https://igraph.discourse.group/t/help-seeking-on-calculating-betweenness-centrality-with-valued-ties/280/4 "2020-06-07T11:57:34Z")

</div>

Edge weights have two different interpretations. They can be interpreted as the strength of a tie (e.g. friendship, volume of trade, communication frequency), or as a cost that needs to be paid somehow (e.g. travel time, friction, distance).

Centrality measures based on shortest paths are only meaningful in the second interpretation. Shortest paths refer to paths that have the _lowest_ total weight, and hence _higher_ weights indicate edges that are _less_ likely to be chosen as a shortest path. Centrality measures that use this interpretation are betweennness centrality and closeness centrality.

Centrality measures based on random walks, or eigenvectors, are meaningful in the former interpretation. _Higher_ weights indicate edges that are _more_ likely to be chosen, for example for a random walk, or weigh the importance of a node higher. Centrality measures that are consistent with this interpretation include PageRank and eigenvector centrality.

When using edge weights for calculating centrality, make sure that the interpretation of the weights is in line with the interpretation of weights as used in the centrality measure.

In short, in this context, centrality measures such as PageRank of eigenvector centrality make more sense.

Note that the _degree_ does not account for the weight of edges, you would use the _strength_, i.e. the weighted degree, in this context.

---

<div class="post-metadata">

**Author:** ![lingchuding](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/lingchuding/32/257_2.png) [@lingchuding](https://igraph.discourse.group/u/lingchuding)\
**Post date:** [7 June 2020 12:46 UTC](https://igraph.discourse.group/t/help-seeking-on-calculating-betweenness-centrality-with-valued-ties/280/5 "2020-06-07T12:46:07Z")

</div>

Thanks for your explanation, now I have better understood the interpretation of different centrality measures. And for the degree centrality with valued ties, I tried with you suggestion. Here are the codes and results:

```auto
> strength(graph, mode="in")
1001 1002 1003 1004 
   3 3 3 3 

```

It seems that the package still ignores the weight in each tie. And I think I need to do some manipulation on the object/graph to be proceeded before I call for strength command. But I don’t know how to do it. Could you give me some suggestions?

Best,  
Chuding

---

<div class="post-metadata">

**Author:** ![vtraag](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/vtraag/32/38_2.png) [@vtraag](https://igraph.discourse.group/u/vtraag)\
**Post date:** [7 June 2020 14:20 UTC](https://igraph.discourse.group/t/help-seeking-on-calculating-betweenness-centrality-with-valued-ties/280/6 "2020-06-07T14:20:24Z")

</div>

> [@lingchuding](#):
>
> the package still ignores the weight in each tie

You need to indicate which edge attribute you want to use as the weight, see [igraph R manual pages](https://igraph.org/r/doc/strength.html).

---

<div class="post-metadata">

**Author:** ![lingchuding](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/lingchuding/32/257_2.png) [@lingchuding](https://igraph.discourse.group/u/lingchuding)\
**Post date:** [8 June 2020 01:15 UTC](https://igraph.discourse.group/t/help-seeking-on-calculating-betweenness-centrality-with-valued-ties/280/7 "2020-06-08T01:15:53Z")

</div>

It is still reporting errors:

strength(graph, mode = ‘in’, weights = ‘Friend’)  
Error in strength(graph, mode = “in”, weights = “Friend”) :  
At structural\_properties.c:6014 : Invalid weight vector length, Invalid value

---

<div class="post-metadata">

**Author:** ![vtraag](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/vtraag/32/38_2.png) [@vtraag](https://igraph.discourse.group/u/vtraag)\
**Post date:** [8 June 2020 04:46 UTC](https://igraph.discourse.group/t/help-seeking-on-calculating-betweenness-centrality-with-valued-ties/280/8 "2020-06-08T04:46:55Z")

</div>

The graph doesn’t seem to have the edge attribute “Friend”. You can check the attributes by calling [`edge_attr`](https://igraph.org/r/doc/edge_attr.html).

---

<div class="post-metadata">

**Author:** ![lingchuding](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/lingchuding/32/257_2.png) [@lingchuding](https://igraph.discourse.group/u/lingchuding)\
**Post date:** [8 June 2020 05:36 UTC](https://igraph.discourse.group/t/help-seeking-on-calculating-betweenness-centrality-with-valued-ties/280/9 "2020-06-08T05:36:21Z")

</div>

I checked and it seems that the edge attributes have been included in. Would you please have me take a look at the data in the attachment and the codes as follows:

[Example.csv](https://igraph.discourse.group/uploads/short-url/tjviSSfuYtsjN4xKXb8e7XZKEsf.csv) (177 Bytes)

```auto
library(igraph)

#read the edgelist format dataset into R

Mydata <- read.table("Example.csv", header=TRUE, sep=",")

#convert an edgelist matrix with valued edges/ties into a graph

graph <- graph_from_data_frame(Mydata, directed=TRUE)

#check the edge attributes

edge.attributes(graph)
$Friend
 [1] 5 6 5 6 6 6 4 4 4 6 6 6

#calculate degree centrality but fail to account for the value/weight of the tie

strength(graph, mode="in")

1001 1002 1003 1004 
   3 3 3 3

```

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [8 June 2020 06:38 UTC](https://igraph.discourse.group/t/help-seeking-on-calculating-betweenness-centrality-with-valued-ties/280/10 "2020-06-08T06:38:33Z")

</div>

Please place all code into code blocks when posting. You can use the formatting toolbar, or write Markdown, like this: [How to use this forum?](https://igraph.discourse.group/t/how-to-use-this-forum/35)

---

<div class="post-metadata">

**Author:** ![jboynyc](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/jboynyc/32/240_2.png) [@jboynyc](https://igraph.discourse.group/u/jboynyc)\
**Post date:** [9 June 2020 13:08 UTC](https://igraph.discourse.group/t/help-seeking-on-calculating-betweenness-centrality-with-valued-ties/280/11 "2020-06-09T13:08:36Z")

</div>

Thanks for the clear explanation. When handling graphs where weights indicate strength of tie, would it make sense to define an edge attribute, `cost`, that is the inverse of weight (1/weight), to use when calculating betweenness or closeness centrality?

---

<div class="post-metadata">

**Author:** ![vtraag](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/vtraag/32/38_2.png) [@vtraag](https://igraph.discourse.group/u/vtraag)\
**Post date:** [9 June 2020 13:25 UTC](https://igraph.discourse.group/t/help-seeking-on-calculating-betweenness-centrality-with-valued-ties/280/12 "2020-06-09T13:25:18Z")

</div>

That might be a possibility. A shortest path is then defined as the path that minimizes

\sum\_e \frac{1}{w\_e}

where w\_e is the weight of edge e. This means that it would be better to have two edges of relatively large weight than a single edge of relatively low weight. Whether that is really what you want is up for debate.

Another alternative that I have thought about sometimes is to use -\log w\_e if w\_e \leq 1 for all e (which means that -\log w\_e \geq 0). The shortest path is then the path that minimizes

\sum\_e - \log(w\_e)

which means it maximizes

\sum\_e \log(w\_e) = \log \prod\_e w\_e,

and since \log is concave, it maximizes

\prod\_e w\_e.

If w\_e is interpreted as some probability that edge e functions (or can be travelled in some way), then this is the probability that each edge in the path functions, i.e. that the path as a whole is likely to function.

---

<div class="post-metadata">

**Author:** ![jboynyc](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/jboynyc/32/240_2.png) [@jboynyc](https://igraph.discourse.group/u/jboynyc)\
**Post date:** [9 June 2020 14:31 UTC](https://igraph.discourse.group/t/help-seeking-on-calculating-betweenness-centrality-with-valued-ties/280/13 "2020-06-09T14:31:10Z")

</div>

Thanks! I read up on this some more (in [Opsahl et al.](https://doi.org/10.1016/j.socnet.2010.03.006)) and found that it is customary to use a positive “tuning parameter,” \alpha, when calculating inverse edge weights: \frac{1}{(w\_e)^\alpha}. A common value is for \alpha is 0.5.

---

<div class="post-metadata">

**Author:** ![vtraag](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/vtraag/32/38_2.png) [@vtraag](https://igraph.discourse.group/u/vtraag)\
**Post date:** [10 June 2020 11:42 UTC](https://igraph.discourse.group/t/help-seeking-on-calculating-betweenness-centrality-with-valued-ties/280/14 "2020-06-10T11:42:21Z")

</div>

Sorry, my mistake, apparently you cannot pass just the edge attribute, you must explicitly pass the vector, i.e.

```auto
strength(graph, mode="in", weights=E(graph)$Friend)

```

---

<div class="post-metadata">

**Author:** ![lingchuding](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/lingchuding/32/257_2.png) [@lingchuding](https://igraph.discourse.group/u/lingchuding)\
**Post date:** [10 June 2020 12:31 UTC](https://igraph.discourse.group/t/help-seeking-on-calculating-betweenness-centrality-with-valued-ties/280/15 "2020-06-10T12:31:20Z")

</div>

The results come out are: 13 8 14 13, which is not consistent with those I calculate manually: 16 15 18 15. What is wrong with this code?

---

<div class="post-metadata">

**Author:** ![vtraag](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/vtraag/32/38_2.png) [@vtraag](https://igraph.discourse.group/u/vtraag)\
**Post date:** [10 June 2020 12:47 UTC](https://igraph.discourse.group/t/help-seeking-on-calculating-betweenness-centrality-with-valued-ties/280/16 "2020-06-10T12:47:12Z")

</div>

If I run the code above, I get the result

```auto
> strength(graph, mode="in", weights=E(graph)$Friend)
1001 1002 1003 1004 
  16 15 18 15

```

---

<div class="post-metadata">

**Author:** ![lingchuding](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/lingchuding/32/257_2.png) [@lingchuding](https://igraph.discourse.group/u/lingchuding)\
**Post date:** [11 June 2020 01:34 UTC](https://igraph.discourse.group/t/help-seeking-on-calculating-betweenness-centrality-with-valued-ties/280/17 "2020-06-11T01:34:21Z")

</div>

Sorry for the trouble caused. Yes, it works when a restart R session. Many thanks!

And I have another question: how can I process a dataset with adjacency matrix format? Here is the dataset and codes, with which I failed to get the results:

[Example 6.csv](https://igraph.discourse.group/uploads/short-url/tlnM6zJ9IFkzXJiEPKe24X8BonZ.csv) (54 Bytes)

> library(igraph)

> Mydata ← read.table(“Example 6.csv”, header=TRUE, sep=“,”)

> Mygraph ← graph\_from\_adjacency\_matrix(Mydata, mode = c(“directed”), weighted = TRUE)

> strength(Mygraph, mode=“in”, weights=E(graph)$Friend)
