# Computation of Network Homophily / Heterogeneity

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=T){
#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=T)# #or count (prop=F) of all nodes tied with matching attribute# if(is.null(attr.val)){ ifelse(prop==T,sum(ee[,1]==ee[,2])/nrow(ee),sum(ee[,1]==ee[,2])) #If not null, get proportion (prop=T) or count (prop=F) of# #edges among nodes with that particular node attribute value# } else { ifelse(prop==T,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=T) #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")
 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=T)  -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. ## 1 Answer The second one (igraph function) is correct. The first one does not take into account the baseline probability that two people from the same group will be connected by random change. Because your groups are randomly assigned---not due to a process of preferential in-group attachment---you should not expect a proper homophily measurement to show any homophily. To see this, try using just 2 groups instead of 5, e.g.: V(gg)$group<-sample(1:2,100,replace=T)


Note how the homphily then jumps to around 0.5, despite the tie-formation process being the same. With the assortativity measure, it is around zero.

To see this even more strongly, use two groups of unequal sizes. In a network with 90% of group 1 and 10% of group 2, people of group 1 are likely to be connected to each other by sheer chance:

V(gg)$group<-runif(100,0,1)>0.1  Homophily is now 0.8 despite people still connecting randomly. With the assortativity measure, it is around zero. For further reading and an explanation of the igraph measurement, see Chapter 4 from the classic book "Networks, Crowds, and Markets," which can be found at this URL: http://www.cs.cornell.edu/home/kleinber/networks-book/ One more thing to note about assortativity is that it is also more general; it can measure correlation in continuous variables, not just nominal group assignments. For this reason, you should use "assortativity_nominal" to measure homophily for group labels as in your example. (See the igraph help file on the function) This is also why it can be negative: it can show if people tend to avoid each other. Update: This answer does not depend on the underlying topology of the network. For example: set.seed(5165) #Random directed "small world" graph with 100 nodes and 30% chance of a tie# gg<-sample_smallworld(1, 100, 4, 0.3, loops=T) #Randomly assign the node attribute (group numbers 0:5)# V(gg)$group<-sample(1:5,100,replace=T)

homophily(graph = gg, vertex.attr = "group")
 0.225
assortativity_nominal(gg, factor(V(gg)\$group), directed=T)
 0.0251419

• Thank you for your detailed explanation to this topic. I have to question about your statements. In this example a random network is choosen, where groups are randomly assigned, what about small world network for example a network of tweets by twitter? Is there any difference in this, I mean in most of the cases these groups would not be randomly assigned right? Another question is based on the E-I Index, which I tried to compute (based on probability), I thought the Krackhardts and Colesman computation is for measuring the homophily in a network? – Daniel Sep 20 '18 at 5:22
• On Small Worlds: You are conflating the topology of the edges with node-attribute homophily. You could have a "random" (Erdos-Renyi / Poisson graph) network that shows homophily---i.e., in which node attributes are not assigned randomly. If you started with a random small-world network in the code example above, you'd still have no homophily because node attributes are assigned randomly. Whether or not Twitter in particular shows homophily is an empirical question, not a theoretical question. – samplesize1 Sep 20 '18 at 22:53
• On the Krackhardt E/I index and the Coleman homophily index: these are two different indices. However, both of them are similar in that they measure in-group tendences for a particular group. The homophily/assortativity measure you give in your question measures it for the network as a whole, considering all groups therein. – samplesize1 Sep 20 '18 at 22:54
• Added small-world example. – samplesize1 Sep 20 '18 at 22:59