Search code examples
rcluster-analysisigraphgraph-theory

Calculate local clustering coefficient of a vertex (node) with R (by hand)


local clustering coefficient by hand

I found an example showing how to calculate LCC by hand (see image).

How can I replicate these steps in R? Focus is on finding the "Actual number of links among Neighbors" (middle step)

I would preferably have to calculate it by hand

*Does the igraph package provide this number?

Example adjacency matrix:

matrix(data = c(0,1,0,1,1,0,0,1,1,1,0,1,1,0,1,0), ncol = 4)

Solution

  • All of this can be done in igraph. It is nice that you gave an example, but since the graph is fully connected, all vertices have LCC=1. I decided to use a somewhat more complicated graph. I will go through the "by hand" part in detail for vertex 1.

    Sample graph

    library(igraph)
    
    set.seed(1234)
    g = erdos.renyi.game(5, 0.7)
    LO = matrix(c(0,2,1,1,1,0,0,-1,1,0), nrow=5, ncol=2)
    plot(g, layout=LO)
    

    Sample Graph

    To start with, yes, igraph has a built-in function transitivity for LCC. For my sample graph, you can get this with

    transitivity(g, type="localundirected")
    [1] 0.6666667 0.0000000 0.3333333 0.3333333 0.6666667
    

    But the main part of your question is the hand computation. The only things that you need from the graph are the first two steps - the Degree Centrality and the Actual Links Among Neighbors.

    Degree Centrality is given by the degree function

    degree(g)
    [1] 3 2 3 3 3
    degree(g, 1)        ## Vertex 1 only
    [1] 3
    

    As you suggested in your question, the only challenging part is the Actual Links Among Neighbors. You can get this by taking the subgraph induced by the neighbors of a point and then checking the number of edges. So for vertex 1 we get

    ecount(induced_subgraph(g, neighbors(g, 1)))
    [1] 2
    

    Here is the full computation for vertex 1

    (DC   = degree(g, 1))
    [1] 3
    >(ALAN = ecount(induced_subgraph(g, neighbors(g, 1))))
    [1] 2
    (MaxPoss = DC*(DC-1)/2)
    [1] 3
    (LCC = ALAN/MaxPoss)
    [1] 0.6666667
    

    which agrees with transitivity given above.