Hi Danny,

You can extract the components using a package like igraph, and then run the closeness_w-function. I would advice against it though. The values in a smaller component will be on average closer than those in a large component simply because there are fewer paths. I would rather suggest setting the gconly parameter to FALSE instead. See the post above.

Best,

Tore

How can I calculate the “intra-component” closeness centrality (of networks with two components) in R?

R said me: “[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0” if i want to calculate the closeness centrality of a network with two components.

Thank you for the help!

]]>Hi Divya,

I am not familiar with median computation in graph theory. Closeness is the inverse sum of distances to all other nodes in a network.

Good luck!

Tore

I am new to the area of large networks. I have been reading on centrality measures. And came across your article. I was wondering if the closeness centrality measure is similar to median computation in graph theory.

Median problem is a very important facility location model. Have you come across any paper on median computation in large networks? If you have, please suggest some references.

Thank you

regards

Divya

Hi Vivek,

The “normalizations” do not have a max value for weighted networks as there is no max value. See comment #27 on this page on why I don’t think normalizations are appropriate: https://toreopsahl.com/tnet/weighted-networks/node-centrality/

There tends to be a positive correlation between degree and closeness (i.e., a high degree node tends to be closer to all the other nodes) in real-world networks.

Best,

Tore

Thanks for this reference, Sean. There is a whole host of centrality metrics, and this site does not attempt to be a complete source. This post is simply highlighting that it’s possible to calculate closeness centrality on disconnected networks.

Do you get similar scores for the sample network above in UCINET?

Tore

]]>I just ran across this, so I apologize for coming a bit late to the part, but Borgatti (2006) — “Identifying sets of key players in a social network” — uses average reciprocal distance as an alternative closeness measure. It has also been implemented in UCINET for some time, possibly dating back to 2006 or earlier, but I don’t know for sure.

]]>The things you said, it was something that i almost knew .I have changed the code and i found the result i wanted. I just wondered if there was a way to create this by simple changing one variable in the closeness_W function.

Thank you so much for replying

Giannhs90

Hi,

This would require transposing the distance matrix. You can calculate the distance matrix using the distance_w-function (eg dmat <- distance_w(net)), transpose it (tdmat <- t(dmat)), and then supply this matrix as the precomputed distance matrix to the closeness_w-function.

Hope this helps,

Tore

I have the same problem with Leila

” In the case of directed networks, how can I use the option type “in” or type “out” for the closeness indicators. While this option works for the degree it does not work for the closeness.

Do you have an idea how to solve this problem ?”

Your function calculates the “outcoming” paths of a node. What can i do if i am interested in closeness centrality as an “incoming” measure? ]]>

# To speed thing up, you might want to enable JIT compiling library(compiler) enableJIT(3) # Load tnet library(tnet) # Load sample network from blog post net <- cbind( i=c(1,1,2,2,2,3,3,3,4,4,4,5,5,6,6,7,9,10,10,11), j=c(2,3,1,3,5,1,2,4,3,6,7,2,6,4,5,4,10,9,11,10), w=c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)) # New function closeness_w2 <- function (net, directed = NULL, gconly = TRUE, alpha = 1) { if (is.null(attributes(net)$tnet)) net <- as.tnet(net, type = "weighted one-mode tnet") if (attributes(net)$tnet != "weighted one-mode tnet") stop("Network not loaded properly") net[, "w"] <- net[, "w"]^alpha if (is.null(directed)) { tmp <- symmetrise_w(net, method = "MAX") directed <- (nrow(tmp) != nrow(net) | sum(tmp[, "w"]) != sum(net[, "w"])) } # From distance_w-function g <- tnet_igraph(net, type = "weighted one-mode tnet", directed = directed) if (gconly) { stop("This code is only tested on gconly=FALSE") } else { gc <- as.integer(V(g)) } # Closeness scores out <- sapply(gc, function(a) { row <- as.numeric(igraph::shortest.paths(g, v=a, mode = "out", weights = igraph::get.edge.attribute(g,"tnetw"))) return(sum(1/row[row!=0])) }) out <- cbind(node = gc, closeness = out, n.closeness = out/(length(out) - 1)) return(out) } # JIT compiled closeness_w2c <- cmpfun(closeness_w2) # Scores with old function closeness_w(net, gconly=FALSE) # Scores with new function (regular and compiled) closeness_w2(net, gconly=FALSE) closeness_w2c(net, gconly=FALSE) # Disable JIT compiling enableJIT(0)

Hope this helps!

Tore

Hi Jinie,

Did you specify measure=”alpha” as well as setting the alpha parameter different from 1?

The measure-parameter decides which columns are outputted.

Best,

Tore

I got kind of confused about reading the closeness outputs.

I used different alpha values for comparing outputs.

Which one is the weighted closeness score for each alpha- closeness or n.closeness ?

Cause depending on the alpha value, these two scores keep changing.

Thank you!

Jinie

]]>Hi Tania,

The main closeness algorithm in the paper is indeed different, but the work-around elaborated on here is mentioned in footnote 1.

Hope this helps,

Tore

Thanks,

Tania ]]>

I have just come to understand my mystake by analyzing the distance function. Again, Thank you for your excellent research !

Leila ]]>

I send you my databank on your personal address. You may have received it as “spam”. It doesn’t matter. I have just one question. In the case of directed networks, how can I use the option type “in” or type “out” for the closeness indicators. While this option works for the degree it does not work for the closeness.

Do you have an idea how to solve this problem ?

Again, thank you very much for your help and your research !

Leila

Hi Stefan,

Glad you found it useful. It is mentioned in my Node centrality in weighted networks: Generalizing degree and shortest paths-paper.

Thanks,

Tore

As my networks sometimes consist of multiple components I used the proposed normalized closeness measure. Would you have a scientific journal article reference where I can refer to?

Thanks,

Stefan

Hi Janet,

Thank you for discovering this bug. There seems to have been a recent update that broke it. I have updated the code, and will publish a new version of tnet. In the meantime, send me an email, and I can send you the code.

Tore

]]>It seems that the algorithm is doing the inverse(sum(distance)) instead of the sum(inverse(distance))

Could it be that the function was changed at some point? Thanks!

> net

> closeness_w(net, alpha = 1,gconly=FALSE)

And the result is:

node closeness n.closeness

[1,] 1 0.08333333 0.008333333

[2,] 2 0.10000000 0.010000000

[3,] 3 0.11111111 0.011111111

[4,] 4 0.11111111 0.011111111

[5,] 5 0.09090909 0.009090909

[6,] 6 0.09090909 0.009090909

[7,] 7 0.07142857 0.007142857

[8,] 8 Inf Inf

[9,] 9 0.33333333 0.033333333

[10,] 10 0.50000000 0.050000000

[11,] 11 0.33333333 0.033333333

Thanks for your reply last time! I solved the problem by building an index before inputting the network.

However, I have an important question to ask you. I rerun my code and got different closeness measure as I got before. Actually, I don’t even get the right result for your example on this webpage! The code is:

net closeness_w

function (net, directed = NULL, gconly = TRUE, precomp.dist = NULL,

alpha = 1)

{

if (is.null(attributes(net)$tnet))

net <- as.tnet(net, type = "weighted one-mode tnet")

if (attributes(net)$tnet != "weighted one-mode tnet")

stop("Network not loaded properly")

net[, "w"] <- net[, "w"]^alpha

if (is.null(precomp.dist)) {

if (is.null(directed)) {

tmp <- symmetrise_w(net, method = "MAX")

directed <- (nrow(tmp) != nrow(net) | sum(tmp[, "w"]) !=

sum(net[, "w"]))

}

precomp.dist <- distance_w(net = net, directed = directed,

gconly = gconly)

}

precomp.dist[is.infinite(precomp.dist)] <- NA

out <- cbind(node = attributes(precomp.dist)$nodes, closeness = rowSums(precomp.dist,

na.rm = TRUE), n.closeness = NaN)

out[, "closeness"] <- 1/out[, "closeness"]

out[, "n.closeness"] <- out[, "closeness"]/(nrow(out) – 1)

return(out)

}

What do you think could have caused the problem? I’m looking forward to your answer! Thanks a lot!

Janet

]]>Hi Janet,

Glad you are using tnet. It is possible to use a five-digit identifier; however, this will create much larger output objects. You might want to run the compress_ids-function first on the data. If this doesn’t help, please email me the code and data that you are using, and I will have a look.

Best,

Tore

Thank you for the program. I’m just wondering if it’s possible to input the data with each node as a 5 digit identifier, instead a number starting from 1.

I have a dataset where the nodes are identified by firm permno, which is a 5 digit number. And every time I run it, R crashes.

Thanks a lot!

Janet

]]>