Tuesday, January 3, 2012

R code: calculate centrality


There are many ways to define and calculate centrality of a node in a network.
One way to define and calculate the centrality in a network of symptoms or items of (psycho)pathology, is the sum of all correlations of a node (symptom or item), with all other nodes (symptoms or items) in the netwerk. This can be the sum of all correlations, but sometimes correlations in a network are both positive and negative. So, the sum of all absolute correlations can be used as a measure of centrality as well.

At the same time, the average centrality of the nodes in a network can be calculated. It can be used to compare the connectedness of networks. This measure can be scaled (standardized), to represent the average correlation of every node with every other node in the network.




R-Code


These centrality meausures can be calculated from a correlation or covariance matrix. The following code takes one of these matrices as input, and returns a list, containing:


1) vector of node centralities (from original correlations)
2) average centality (form original correlations)
3) vector of scaled node centralities (from original correlations, values from -1 to 1)
4) scaled average centrality (form original correlations, takes values from -1 to 1)
5) vector of node centralities (from absolute correlations)
6) average centrality
(from absolute correlations)
7) scaled node centralities
(from absolute correlations, values from -1 to 1
8) scaled average centrality (from absolute correlations, takes values from -1 to 1)

The code assumes the matrix is either symmetric, or lower triangular.


From correlation matrix:

centrality <- function(cormat) {
cent.or <- apply(cormat, 1, sum)-1
cent.ab <- apply(abs(cormat), 1, sum)-1
sc.cent.or <- cent.or / (nrow(cormat)-1)
sc.cent.ab <- cent.ab / (nrow(cormat)-1)
avcent.or <- mean(cent.or)
avcent.ab <- mean(cent.ab)
sc.avcent.or <- mean(sc.cent.or)
sc.avcent.ab <- mean(sc.cent.ab)
outp <- list(cent.or, avcent.or, sc.cent.or, sc.avcent.or, cent.ab, avcent.ab, sc.cent.ab, sc.avcent.ab)
names(outp) <- c("node.centrality.(original)", "average.centality.(original)",
"scaled.node.centrality.(original)", "scaled.average.centrality.(original)",
"node.centrality.(absolute)", "average.centrality.(absolute)",
"scaled.node.centrality.(absolute)", "scaled.average.centrality.(absolute)")
print(outp)
}


From covariance matrix

centrality <- function(covmat) {
cormat <- cov2cor(covmat)
cent.or <- apply(cormat, 1, sum)-1
cent.ab <- apply(abs(cormat), 1, sum)-1
sc.cent.or <- cent.or / (nrow(cormat)-1)
sc.cent.ab <- cent.ab / (nrow(cormat)-1)
avcent.or <- mean(cent.or)
avcent.ab <- mean(cent.ab)
sc.avcent.or <- mean(sc.cent.or)
sc.avcent.ab <- mean(sc.cent.ab)
outp <- list(cent.or, avcent.or, sc.cent.or, sc.avcent.or, cent.ab, avcent.ab, sc.cent.ab, sc.avcent.ab)
names(outp) <- c("node.centrality.(original)", "average.centality.(original)",
"scaled.node.centrality.(original)", "scaled.average.centrality.(original)",
"node.centrality.(absolute)", "average.centrality.(absolute)",
"scaled.node.centrality.(absolute)", "scaled.average.centrality.(absolute)")
print(outp)
}






See also


http://cran.r-project.org/web/packages/qgraph/index.html (qgraph package for R)
http://www.psychosystems.org/

No comments:

Post a Comment