I am quite confused about the calculation of network homophily in network analysis. Right now I am computing the homophily using the following function, which has been written and also described by the following URL: http://dappls.umasscreate.net/networks/calculating-network-homophily-part-1/. Well the definition of homophily in social science is "the tendency of individuals to associate and bond with similar others". In network analysis homophily is described as a process where similar nodes on a particular trait are more likely to form ties, which is quite the same as in social science right? My goal is to measure the homophily in a directed network to identify which group of actors are similar to each other.
Function
homophily <- function(graph, vertex.attr, attr.val=NULL,
prop=TRUE){
#Assign names as vertex attributes for edgelist output#
V(graph)$name <- vertex_attr(graph, vertex.attr)
#Get the basic edgelist#
ee <- get.data.frame(graph)
#If not specifying on particular attribute value,
#get percentage (prop=TRUE)#
#or count (prop=FALSE) of all nodes tied with matching attribute#
if(is.null(attr.val)){
ifelse(prop==TRUE, sum(ee[, 1]==ee[, 2])/nrow(ee),
sum(ee[, 1]==ee[, 2]))
#If not null, get proportion (prop=TRUE) or count (prop=FALSE) of#
#edges among nodes with that particular node attribute value#
} else {
ifelse(prop==TRUE, sum(ee[,1]==attr.val &
ee[, 2]==attr.val)/nrow(ee[ee[, 1]==attr.val|ee[ ,2] ==
attr.val,]), sum(ee[,1]==attr.val & ee[,2]==attr.val))
}
}
Sample Data
set.seed(5165)
#Random directed graph with 100 nodes and 30% chance of a tie#
gg <- random.graph.game(100, 0.3, "gnp", directed=TRUE)
#Randomly assign the node attribute (group numbers 0:3)#
V(gg)$group<-sample(1:5,100,replace=T)
Output
By applying the function on sample data I receive the following output, which means that 20% of the ties in the network are between actors in the same group. It is also possible to compute the homophily for a specific group in percentage.
homophily(graph = abc, vertex.attr = "group")
[1] 0.1971504
However I also noticed that the igraph package contains as well a homophily method called assortativity()
described here. Executing this function gives completely different results, with the assortativity coefficient in a range(-1, 1). The assortativity coefficient is positive if similar vertices (based on some external property) tend to connect to each other, and negative otherwise.
library(igraph)
assortativity(abc, V(abc)$group, directed=TRUE)
[1] -0.02653782
Question
So right now I am quite confused, which of these methods is the right one to measure the homophily in a network, because both functions received different results. I also noticed that the igraph method does not support the calculation of particular groups. In my opinion I would rather go with the first one which is self-coded (not sure if there are some mistakes), because the interpretation makes more sense. So my question is, which of the following methods is the right one for measuring the homophily in a network? I mean if I want to know how heterogeneous or homogenous actors in a network communicate, I would rather choose the first technique. Both techniques measure homophily & receive different results, but right know I can not see any difference (advantage) which one will be used for any reason. The goal of this technique is to measure the homophily in a network by their proportion of all edges in a network.