The distance_w-function considers the directionality of ties. If the network is not fully connected, you might get NaNs. Please email with your data and code, and I will look into it.

Best,

Tore

first of all I’d like to also thank you for your very helpful and interesting approaches on SNA.

If have one question and I’d very grateful if you could help me in this case.

I applied the distance_w function on two different network as described above. Unfortunately, in one case the result was NaN but I was not able to find out why. Both networks are significantly different in terms of their size. The network with the result NaN has very strong betweeness nodes. Could that be a reason for the result? Or is there a way how to interpret NaN as a result in this specific case?

Best regard and thanks a lot in advance,

Alex

A couple of points:

1) closeness can only be computed for the giant component; when you specify gconly=FALSE, an alternative approach is taken (see https://toreopsahl.com/2010/03/20/closeness-centrality-in-networks-with-disconnected-components/)

2) to inspect the code behind the functions, simply type them without the () at the end

3) the equations in that paper normalized the coefficients; the functions in tnet do not do this

Hope this helps!

Tore

So just to make sure I understand, for a weighted and directed network, closeness_w(net) would calculate out-closeness, but if I wanted to find in-closeness, I could do:

trans_distance_mat <- t(distance_w(el_1995_final, gconly=FALSE))

w_incloseness_1995 <- data.frame(closeness_w(el_1995_final, gconly=FALSE, precomp.dist = trans_distance_mat))

Additionally, I think I am still a bit confused as to how closeness_w uses distance_w in its calculations. I tried calculating closeness_w by hand for the example network given here (https://toreopsahl.com/tnet/weighted-networks/shortest-paths/) but am not getting the same numbers that are computed in R. Would you mind sharing how exactly closeness_w is calculated?

I calculated closeness by hand by using the equations in page 30 of this paper: http://www.cepii.fr/PDF_PUB/wp/2013/wp2013-24.pdf.

Thanks so much again,

Hillary

Have a look at this page: https://toreopsahl.com/tnet/weighted-networks/node-centrality/

Specifically, you can compute the standard closeness measure (ie inverted sum of distances to all other nodes) by running closeness_w-function on the network. If you want the reverse closeness measure (ie inverted sum of distances from all other nodes), then you can precompute the distance matrix using distance_w, transpose the matrix, and use this matrix in the closeness_w-function (precomp.dist-parameter).

Hope this helps,

Tore

Thank you so much for these posts! I’ve found them very helpful as I am doing research for my senior thesis. I was hoping to calculate in-closeness and out-closeness centrality and was wondering if there was a way, either in tnet or some other package, that you were aware of for calculating these measures? I’ve been reading a paper by De Benedictis et al., called “Network Analysis of World Trade using the BACI-CEPII Dataset” and they were able to implement it in the tnet library. I’d appreciate any ideas you may have.

Thanks,

Hellary

Glad the post is helpful.

The distance is the sum of inverse tie weights, so an indirect path consisting of two ties with weights of 10 would be preferred over a direct path with a tie weight of less than 5 as 1/10 + 1/10 < 1/4.

Best,

Tore

Thanks

Mehrzad

Glad you are finding the information useful.

I am not entirely sure what is happening in your case. Please drop me a note with your network and code. Then I can better help you.

The 0.5 weight should not be an issue.

Best,

Tore

First of all, many thanks for tnet and all the information in your blog.

Sorry, I am having a problem when trying to get distances in a weighted directed network. I am inputting data that contains 304 nodes and 710 edges. 300 nodes are connected to the giant component and only 4 are not. I was expecting to get a matrix of 300*300 (or 304*304) but I am only getting 66*66 (when using “gconly=TRUE”). I tried to write the “distance_w” function with “gconly=FALSE” but I get a message saying that R has encountered a fatal error and it needs to shut down. Any clue of what I might be doing wrong? (My weights take the values of 1, 2, 2.5 and 3 – So not sure if the ‘.5’ is creating an issue)

Many thanks!

Gabriela

Path lengths are infinite among nodes in disconnected component (i.e., no path exists between them). The average of a vector with an infinite number is infinite. If you’re looking to compute closeness, see https://toreopsahl.com/2010/03/20/closeness-centrality-in-networks-with-disconnected-components/

Best,

Tore

I am using tnet package for my weighted network analysis. I am little bit confuse for average path length of disconnected graphs. After breakdown my network divided into 10 part, where i am considering the sub graph have more than 5 nodes, most of the algorithm is to calculate the mean path length by considering the number of nodes from giant component only. But in a power grid network , each sub graph is a part of the network even if there is no link for a certain period of time, so i wish to consider all the nodes present in disconnected sub graph, and wants to calculated average path length based on total nodes, am i right or wrong? ]]>

Please send me an email with your data and code, and I will have a look at what is going on.

Tore

]]>Unfortunately I’m still a bit confused.

Here’s my problem. Nodes in my graph represent animal names, produced in category/semantic fluency test. 2 most strongly connected nodes are “cat” and “dog”. I cannot think of any other shortest path between these two nodes than the direct one, as this is the path with highest weight in graph. Node “cat” was numericaly labeled as 1 and node “dog” as 2. When i lookup shorthest path between 1 and 2 in dmat matrix the value is 2.74 and this doesn’t make any sense to me.

I’m definitely missing something, would be glad for any further guidance.

]]>1) The “normalization” of ties weights is a constant applied to all weights. You can undo this normalization with the mean time weight. Have a look at the function by typing distance_w without the (). You can then copy this code and comment out the normalization line.

2) The functions rely on the normalized distances. But it’s key to remember that the same paths are identified as the shortest ones with and without normalization.

Hope this helps!

Tore

thanks for this post, I’m just getting started with SNA and tnet package.

1. Is there a way to calculate shortest distances like in example 1. (without normalisation) with tnet package?

2. Does measures like closeness use normalised shortest distances or just shortest distances?

Thank you!

]]>Glad you are able to use tnet. To answer your questions in order:

1) The distance matrix is only from the main / giant component of the network. If you would like it for all nodes, you must specify gconly=FALSE in the distance_w-function.

2) The node ids of the rows and columns in the distance matrix when gconly=TRUE is saved as an attribute of the output from the distance_w-function. For example, if the output object is called dmat, the node list can be found by typing attributes(dmat)$nodes.

Good luck!

Tore

I’ve recently come about 2 uncertaineties. I’ve analyzed a graph consisting of 157 nodes. When i compute a matrix of weighted distances (shorthest paths) between nodes my matrix is 141×141 not 157×157 (as I understand it, this is mainly because of existing separated dyads (pairs of 2 nodes connected only to each other and disconnected from all other nodes in graph). There are 7 of these separated pairs in my graph. Could this be a reason for recieving 141×141 matrix and not the 157×157 matrix?

Secondly. If i’m looking at the specific value of distance in matrix lets say row = 28 and collumn = 31, is this value a distance between nodes (28-31) that were originaly inputed in the analyzed dataframe? In other words, is row number 28 representing node number 28 in original data?

Thanks for any advice,

With regards

Drahomír

The diameter of a graph is simply the longest path in the network, and the probability is found by studying the non-diagonal values in the distance matrix. The code below shows how to compute both (including a plot of the probability of randomly picking a path with a particular length or less).

Good luck!

Tore

# Load tnet library(tnet) # Load network net <- cbind( i=c(1,1,2,2,2,2,3,3,4,5,5,6), j=c(2,3,1,3,4,5,1,2,2,2,6,5), w=c(4,2,4,1,4,2,2,1,4,2,1,1)) # Compute distances dmat <- distance_w(net) # Diameter max(dmat, na.rm=TRUE) # Vectorize distance matrix and take out diagonal NAs dvec <- as.vector(dmat) dvec <- dvec[!is.na(dvec)] # Plot histogram / distribution hist(dvec) # Cumulative distribution dCumuDist <- hist(dvec) plot(dCumuDist$mids, cumsum(dCumuDist$density), type="l", xlab="distance", ylab="cumulative probability")]]>

First thanks for a lot of useful information provided here. I had 2 questions regarding the average shortest path in weighted graph, particluary if there’s a similar way to compute Diameter of graph and also to display a distribution of shortest paths (in a way like “what is the probability of choosing a path with a certain distance if picking randomly?”)

With regards,

Drahomir

Glad that you find this site useful!

Indeed the assumption that tie weights represent costs instead of strength is often leading to some incorrect findings. The distance_w-function in tnet takes the inverse of the tie weights and then utilize the igraph implementation of Dijkstra’s algorithm. I also scale the weights with the mean tie weight so that one can more easily interpret the values. I am not sure how igraph implements transitivity and modularity, so you might reach out to them directly. All the tnet functions work on the assumption of high values equal stronger connections.

Good luck!

Tore

I am using iGraph for my calculations and I find the weights issue to be a bit confusing, especially after reading this entry and trying to calculate average shortest path length for networks. I assumed that in igraph the weight of an edge is a measurement of tie strength. Therefore, higher values indicate stringer connection. However, is seems that when using Dijkstra’s algorithm in Igraph, the package treats the weights of the edges as distances, looking for paths that minimize the sum of all paths between two nodes. This seems to indicate that the algorithm sees lower weights as stronger ties (ie shorter distances). My solution will be, just for this calculation to convert every edge weight x in my edge weights list to 1/x (as with the Newman reference you provided).

Did the problem I observed is right and do you think that solution a good approach?

Additionally, is this a problem only in this case (and centrality measures) but not an issue in the calculation of transitivity and modularityin igraph (for example, when using random walks).

Best,

Walter