"Social Network Analysis Labs in R" (Stanford tutorials): Confusion over graph object / network class

StackOverflow https://stackoverflow.com/questions/17520799

  •  02-06-2022
  •  | 
  •  

Вопрос

I apologize if this question seems redundant, but I am beginning to play around with R and its SNA tools for a class and have been running a couple of different tutorials/labs to get accustomed. A resource that always gets recommended are the SNA labs over at Stanford, but even just running the introductory lab returns a number of errors that leave me confused. The full R code with annotations is available here:

http://sna.stanford.edu/lab.php?l=1

The first parts are fairly straight-forward and I understand most of what's going on.But once I try adding vertex attributes to the graph (line 236 onwards), I encounter problems with the graph object "krack_full", that we just created. Running this... :

for (i in V(krack_full)) {
    for (j in names(attributes)) {
        krack_full <- set.vertex.attribute(krack_full, 
                                           j, 
                                           index = i, 
                                           attributes[i + 1, j])
    }
}

... returns this:

Error in set.vertex.attribute(krack_full, j, index = i, attributes[i +  : 
  unused argument (index = i)

So I think, fine, use the second method they outlined and just follow through:

attributes = cbind(1:length(attributes[,1]), attributes)
krack_full <- graph.data.frame(d = krack_full_nonzero_edges, 
+ vertices = attributes) 

Which seems to work fine - except that it literally creates an attribute called "(1:length(attributes[, 1])"...

> summary(krack_full)
IGRAPH DN-- 21 232 -- 
attr: name (v/c), 1:length(attributes[, 1]) (v/n), AGE (v/n), TENURE (v/n), LEVEL (v/n), DEPT
  (v/n), advice_tie (e/n), friendship_tie (e/n), reports_to_tie (e/n)

So, everything is acting weird already. And finally, when I try to get the vertex attributes in the next step, I encounter some errors regarding the object's class:

> get.vertex.attribute(krack_full, 'AGE')
Error in get.vertex.attribute(krack_full, "AGE") : 
  get.vertex.attribute requires an argument of class network.
> get.vertex.attribute(krack_full, 'TENURE')
Error in get.vertex.attribute(krack_full, "TENURE") : 
  get.vertex.attribute requires an argument of class network.
> get.vertex.attribute(krack_full, 'LEVEL')
Error in get.vertex.attribute(krack_full, "LEVEL") : 
  get.vertex.attribute requires an argument of class network.
> get.vertex.attribute(krack_full, 'DEPT')
Error in get.vertex.attribute(krack_full, "DEPT") : 
  get.vertex.attribute requires an argument of class network.

... From here on out pretty much nothing works the way I expected. So I suspect the graph object "krack_full" that the data was imported to is somehow not what it's supposed to be...?

Again, I'm sorry if this a complete rookie mistake I'm making, but I would greatly appreciate if you could point me in the right direction. I'd like to get a better grasp of what's going on here.

Thank you very much.

Это было полезно?

Решение

I strongly suspect that the tutorial you are trying to follow was developed for igraph version 0.5.4 or earlier. At that time, vertices and edges in an igraph object were indexed from zero instead of one, and the tutorial seems to account for this, judging from the following comment in the tutorial:

# IMPORTANT NOTE: Unlike in most languages, R objects are numbered
# from 1 instead of 0, so if you want the first element in a
# vector, you would reference it by vector_name[1]. HOWEVER,
# igraph objects are numbered starting from 0. This can lead to 
# lots of confusion, since it's not always obvious at first which 
# objects are native to R and which belong to igraph.

Since igraph 0.6, this is not true anymore; vertices and edges in the R interface of igraph are indexed from 1 just like every other well-behaved R object. You have two options here (besides asking the authors of the tutorial to update it for igraph 0.6):

  1. You can modify the commands in the tutorial to make sure that every vertex and edge index is 1-based; i.e., if they subtracted 1 from the indices somewhere for some reason, just omit the subtraction, and similarly, if they added 1 to the indices somewhere, omit the addition. This would also be a good way to check whether you really understand what you are doing :)

  2. Use the igraph0 package instead of igraph. The igraph0 package is identical to igraph but uses zero-based indexing to ensure that old igraph codes are still functional during the transition period. However, you should keep on using igraph for new analysis projects.

Другие советы

For the function

get.vertex.attribute 

try the new function

vertex_attr 

instead

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top