igraph-help
[Top][All Lists]

Re: [igraph] Assortativity function for R

 From: Tamas Nepusz Subject: Re: [igraph] Assortativity function for R Date: Thu, 26 Aug 2010 16:39:13 +0100 User-agent: Mutt/1.5.20 (2009-06-14)

```Hi Tom,

I've just tried the two assortativity functions you sent, and they give
approximately the same results for me; the maximal difference in 1000
tries was 0.01531. I guess the difference can be attributed to the fact
that R divides by m-1 when calculating the covariances in the
correlation formula, while the assortativity() function divides by m.

--
Tamas

On Wed, Aug 25, 2010 at 07:01:28PM +0100, Tom Hebbron wrote:
> 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-help mailing list