title: Computation of Network homophily
body: <p>I am quite confusing about the calculation of network homophily in network analysis. Right now I am compute the homophily by the following function, which have been written and also described by the following URL: <a href="http://dappls.umasscreate.net/networks/calculating-network-homophily-part-1/" rel="nofollow noreferrer">http://dappls.umasscreate.net/networks/calculating-network-homophily-part-1/</a>. The goal of this technique is to measure the homophily in a network by their proportion of all edges in a network. My goal is to measure the homophily in a directed network.</p>
  
  <h3>Function</h3>
  
  <pre><code>homophily &lt;- function(graph,vertex.attr,attr.val=NULL,prop=T){
    #Assign names as vertex attributes for edgelist output#
    V(graph)$name&lt;-vertex_attr(graph,vertex.attr)
  
    #Get the basic edgelist#
    ee&lt;-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 &amp; ee[,2]==attr.val)/nrow(ee[ee[,1]==attr.val|ee[,2]==attr.val,]),
             sum(ee[,1]==attr.val &amp; ee[,2]==attr.val))
    }
  }
  </code></pre>
  
  <h3>Sample Data</h3>
  
  <pre><code>set.seed(5165)
  #Random directed graph with 100 nodes and 30% chance of a tie#
  gg&lt;-random.graph.game(100,0.3,"gnp",directed=T)
  
  #Randomly assign the node attribute (group numbers 0:3)#
  V(gg)$group&lt;-sample(1:5,100,replace=T)
  </code></pre>
  
  <h3>Output</h3>
  
  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.
  
  <pre><code>homophily(graph = abc, vertex.attr = "group")
  [1] 0.1971504
  </code></pre>
  
  However I also noticed that the <strong>igraph</strong> package contains as well a homophily method called "<em>assortativity()</em>" <a href="http://igraph.org/r/doc/assortativity.html" rel="nofollow noreferrer">described here</a>. Executing this function receives completely other results which is based on the assortativity coefficient in a range(-1, 1). The assortativity coefficient is positive is similar vertices (based on some external property) tend to connect to each, and negative otherwise.
  
  <pre><code>library(igraph)
  assortativity(abc, V(abc)$group, directed=T)
  [1] -0.02653782
  </code></pre>
  
  <h3>Question</h3>
  
  <p>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?</p>