Calculating the mixing matrix and assortativity coefficient with igraph in R

The mixing matrix of a graph gives the density of edges between vertices with different characteristics. The mixing matrix for a given igraph object can be calculated using the following function:

The assortativity coefficient, based on Newman’s paper, can be calculated from the mixing matrix by the following:

Here is an example (be sure to load the functions mentioned above):

Cool.

This entry was posted in Modeling, R, Social Networks and tagged , , , , . Bookmark the permalink.

4 Responses to Calculating the mixing matrix and assortativity coefficient with igraph in R

  1. Vessy says:

    Nice!

    However, I’ve noticed that your code does not distinguish between directed and undirected graphs and does not produce eij =eji (it seems that the program always considers first node in the edge list as a source and the second node as a target).

    This part should fix it (and it also should run bit faster)

    for (i in 1:numatts)
    for (j in 1:numatts)
    {
    iNode <- V(mygraph)[attrib == attlist[i]]
    jNode <- V(mygraph)[attrib == attlist[j]]

    if (is.directed(mygraph))
    mm[i, j] %jNode])
    else
    mm[i, j] <- length(E(mygraph)[iNode%--%jNode])
    }

  2. simone gabbriellini says:

    Vessy,

    nice add, but could you please check the code you posted? looks like it is meaningless in:
    mm[i, j] %jNode])

    did you mean:
    mm[i,j] %jNode])

    best,
    Simone

  3. simone gabbriellini says:

    ok, I see, it’s the posting thing that strips out the code… let’s try this

    mm[i,j] <- length(E(mygraph)[iNode%-&%jNode])

  4. wiven says:

    Is there a standard way (and syntax) to assess the significance of the assortativity coefficient for a given graph?