Skip to content

igraph "assortativity" for continuous attributes return NA if all attributes are integer, but it works if you add a constant .001 #486

Closed

Description

@JacobElder

I tried to use igraph's assortativity function. It returns positive values if more similar nodes have similar attributes and negative values otherwise.

Indeed, if I randomly generate attributes for a graph, this is the case:

    gg<-random.graph.game(50,0.3,"gnp",directed=T)
    V(gg)$group<-sample(1:7,50,replace=T)
    assortativity(gg, as.numeric(V(gg)$group))
    [1] 0.002059966
    V(gg)$group
     [1] 1 4 7 2 6 5 4 3 3 5 5 1 4 1 3 3 2 4 1 3 7 7 1 1 2 3 6 2 7 4 2 4 1 6 1 2 2 5 2 1 7 1 1 3 6 1 3 6 7 2

This all looks right and makes sense. However, for my dataset, I have a network for a particular situation where all the attributes are identical. Theoretically, this should be high/perfect homophily/assortativity (i.e., 1). But to the contrary, it returns NaN.

    V(gg)$group<-7
    assortativity(gg, as.numeric(V(gg)$group))
    [1] NaN

However, I have found a solution to this, although I cannot explain why this is the case. It seems to be something about the whole number/integer. If I add .001 as a constant to each number in the graph's attribute, to get 7.001 instead of 7, it returns the expected assortativity coefficient of "1". See below:

    gg<-random.graph.game(50,0.3,"gnp",directed=T)
    V(gg)$group<-7
    assortativity(gg, as.numeric(V(gg)$group)+rnorm(1,0,.001))
    [1] 1

In other words, assortativity cannot compute perfect homophily for integers/whole numbers but it can for float/numeric numbers with just a .001 added on.

Activity

  1. ntamas commented on Oct 28, 2021

    @ntamas
    Member

    This is working as intended; assortativity is a correlation-like quantity that has to be applied on quantitative values. Assortativity is normalized by the variance of the input, so if all the input values are identical, the variance is zero, and dividing a finite value by zero yields NaN.

    If your group indices represent categories and not values on a continuous scale, use assortativity_nominal().

  2. JacobElder commented on Oct 28, 2021

    @JacobElder
    Author

    This is working as intended; assortativity is a correlation-like quantity that has to be applied on quantitative values. Assortativity is normalized by the variance of the input, so if all the input values are identical, the variance is zero, and dividing a finite value by zero yields NaN.

    If your group indices represent categories and not values on a continuous scale, use assortativity_nominal().

    Hi Tamas-- Thanks for the reply. That makes sense but quick question. If the variance of 0 is the problem, then why would add the constant .001 to all integers then allow assortativity to be calculated? The variance is still 0, but now they are numerics (7.001) rather than integers (7), but they are all still the same so still a variance of 0.

    I had considred using assortativity_nominal() but the attributes are Likert scale attributes from 1 to 7, so I don't think treating them as categories would necessarily be appropriate as it would ignore than 4 and 5 are more related than 4 and 7.

  3. ntamas commented on Oct 28, 2021

    @ntamas
    Member

    If the variance of 0 is the problem, then why would add the constant .001 to all integers then allow assortativity to be calculated?

    For me it doesn't seem to solve the problem:

    > gg<-random.graph.game(50,0.3,"gnp",directed=T)
    > V(gg)$group<-7
    > assortativity(gg, as.numeric(V(gg)$group)+rnorm(1,0,.001))
    [1] NaN
    > assortativity(gg, as.numeric(V(gg)$group)+rnorm(1,0,.001))
    [1] NaN
    > assortativity(gg, as.numeric(V(gg)$group)+rnorm(1,0,.001))
    [1] NaN
    > assortativity(gg, as.numeric(V(gg)$group)+rnorm(1,0,.001))
    [1] NaN
    

    Does the above code snippet behaves differently on your computer? I am using R 4.1.1 with igraph 1.2.7 on macOS.

    Of course, changing only a single value would make the variance non-zero and you would end up with a finite assortativity metric:

    > assortativity(gg, as.numeric(V(gg)$group) + c(rnorm(1,0,.001), rep_len(0, vcount(gg)-1)))
    [1] -0.01651252
    

    the attributes are Likert scale attributes from 1 to 7, so I don't think treating them as categories would necessarily be appropriate

    You are correct; in this case the "normal" assortativity function is more appropriate.

  4. locked and limited conversation to collaborators on Mar 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions