[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[igraph] Assortativity function for R
From: |
Tom Hebbron |
Subject: |
[igraph] Assortativity function for R |
Date: |
Wed, 25 Aug 2010 19:01:28 +0100 |
Hi list,
I wonder if anyone can tell me why the assortativity code found on the
wiki at http://igraph.wikidot.com/r-recipes#toc7 gives different
results to simply passing the degrees of the edges directly to the R
cor function and selecting pearson correlation?
Looking back at Newman's paper
(http://arxiv.org/PS_cache/cond-mat/pdf/0205/0205405v1.pdf), it's
quite clear that the assortativity is equivalent to the Pearson
correlation coefficient of the degree at either ends of an edge. I can
also see that the provided R assortativity function from the igraph
wiki is a sound implementation of equation (4) from the paper.
#from igraph wiki http://igraph.wikidot.com/r-recipes#toc7
assortativity <- function(graph)
{
deg <- degree(graph)
deg.sq <- deg^2
m <- ecount(graph)
num1 <- 0; num2 <- 0; den <- 0
edges <- get.edgelist(graph, names=FALSE)+1
num1 <- sum(deg[edges[,1]] * deg[edges[,2]]) / m
num2 <- (sum(deg[edges[,1]] + deg[edges[,2]]) / (2 * m))^2
den <- sum(deg.sq[edges[,1]] + deg.sq[edges[,2]]) / (2 * m)
return((num1-num2)/(den-num2))
}
assortativity_b <- function(graph)
{
deg <- degree(graph)
edges <- get.edgelist(graph, names=FALSE)+1
return(cor(deg[edges[,1]],deg[edges[,2]], method="pearson"))
}
#test with, e.g.:
rg = erdos.renyi.game(40, p=0.2, directed=TRUE)
assortativity(rg)
assortativity_b(rg)
I get different values (in this case, -0.05837817 and -0.9867207)
Can anyone point out the problem here?
Thanks,
Tom.
- [igraph] Assortativity function for R,
Tom Hebbron <=