You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
igraph "assortativity" for continuous attributes return NA if all attributes are integer, but it works if you add a constant .001 #486
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:
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:
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.
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().
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.
Activity
ntamas commentedon Oct 28, 2021
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().JacobElder commentedon Oct 28, 2021
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.
ntamas commentedon Oct 28, 2021
For me it doesn't seem to solve the problem:
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:
You are correct; in this case the "normal" assortativity function is more appropriate.