Created
April 17, 2012 01:18
-
-
Save gweissman/2402741 to your computer and use it in GitHub Desktop.
Calculate mixing matrix in igraph by vertex characteristic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# calculate the mixing matrix of in igraph graph object 'mygraph', by some vertex attribute 'attrib' | |
# can change the default use.density=FALSE to return a matrix with raw number of edges rather than density | |
mixmat <- function(mygraph, attrib, use.density=TRUE) { | |
require(igraph) | |
# get unique list of characteristics of the attribute | |
attlist <- sort(unique(get.vertex.attribute(mygraph,attrib))) | |
numatts <- length(attlist) | |
# build an empty mixing matrix by attribute | |
mm <- matrix(nrow=numatts, | |
ncol=numatts, | |
dimnames=list(attlist,attlist)) | |
# calculate edge density for each matrix entry by pairing type | |
# lends itself to parallel if available | |
el <- get.edgelist(mygraph,names=FALSE) | |
for (i in 1:numatts) { | |
for (j in 1:numatts) { | |
mm[i,j] <- length(which(apply(el,1,function(x) { | |
get.vertex.attribute(mygraph, attrib, x[1] ) == attlist[i] && | |
get.vertex.attribute(mygraph, attrib, x[2] ) == attlist[j] } ))) | |
} | |
} | |
# convert to proportional mixing matrix if desired (ie by edge density) | |
if (use.density) mm/ecount(mygraph) else mm | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment