I need to compute a giant component of a network I made in R, but the code that was given on this site (http://lists.gnu.org/archive/html/igraph-help/2009-08/msg00064.html) results in a strange output. I need the giant component to calculate the closeness (according to a teacher at the university)
giant.component <- function(graph) {
cl <- clusters(graph)
induced.subgraph(graph, which(cl$membership == which.max(cl$csize)-1)-1)}
G <- giant.component(as.igraph(Q))
result:
IGRAPH U-W- 0 0 --
+ attr: frame.color (v/c), label.color (v/c), label (v/c), shape
(v/c), color (v/c), size (v/n), size2 (v/n), weight (e/n),
arrow.mode (e/n), curved (e/n), color (e/c), label (e/c), lty
(e/n), width (e/n)
Code adaption suggested by Csardi results in the following code and result:
model5 <- matrix(c(0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0), 5, 5, byrow = TRUE)
C <- qgraph(model5, labels = c("X1", "X2", "X3", "X4", "X5"), vsize = 20, esize = 10, asize = 10, layout = "circle")
giant.component <- function(graph) {
cl <- clusters(graph)
induced.subgraph(graph, which(cl$membership == which.max(cl$csize)))}
G <- giant.component(as.igraph(C))
G <- qgraph(C)
IGRAPH D-W- 5 7 --
+ attr: frame.color (v/c), label.color (v/c), label (v/c), shape
(v/c), color (v/c), size (v/n), size2 (v/n), weight (e/n),
arrow.mode (e/n), curved (e/n), color (e/c), lty (e/n), width (e/n)
Older (pre 0.6) versions of igraph index vertices (and everything else) from zero, while newer versions use 1-based indexing. So you need to update the code to something like:
...
induced.subgraph(graph, which(cl$membership == which.max(cl$csize)))}
...
(Untested, as you did not provide an example data set.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With