From c1cd1f3ff50483210a36b766899b2583dd598899 Mon Sep 17 00:00:00 2001 From: Azza Ahmed Date: Wed, 22 Nov 2017 07:43:05 +0300 Subject: [PATCH] `vertex_attr` for `karate` object of class igraph get.vertex.attribute expects a network object, so used `vertex_attr` for objects of class `igraph` --- sand/inst/code/chapter4.R | 554 +++++++++++++++++++------------------- 1 file changed, 277 insertions(+), 277 deletions(-) diff --git a/sand/inst/code/chapter4.R b/sand/inst/code/chapter4.R index b0e518e..4524cb3 100644 --- a/sand/inst/code/chapter4.R +++ b/sand/inst/code/chapter4.R @@ -1,125 +1,125 @@ -# SAND with R, chapter4.tex - -# CHUNK 1 -library(sand) -data(karate) +# SAND with R, chapter4.tex + +# CHUNK 1 +library(sand) +data(karate) hist(degree(karate), col="lightblue", xlim=c(0,50), - xlab="Vertex Degree", ylab="Frequency", main="") - -# CHUNK 2 + xlab="Vertex Degree", ylab="Frequency", main="") + +# CHUNK 2 hist(graph.strength(karate), col="pink", - xlab="Vertex Strength", ylab="Frequency", main="") - -# CHUNK 3 -library(igraphdata) -data(yeast) - -# CHUNK 4 -ecount(yeast) -# --- -## [1] 11855 -# --- - -# CHUNK 5 -vcount(yeast) -# --- -## [1] 2617 -# --- - -# CHUNK 6 -d.yeast <- degree(yeast) + xlab="Vertex Strength", ylab="Frequency", main="") + +# CHUNK 3 +library(igraphdata) +data(yeast) + +# CHUNK 4 +ecount(yeast) +# --- +## [1] 11855 +# --- + +# CHUNK 5 +vcount(yeast) +# --- +## [1] 2617 +# --- + +# CHUNK 6 +d.yeast <- degree(yeast) hist(d.yeast,col="blue", xlab="Degree", ylab="Frequency", - main="Degree Distribution") - -# CHUNK 7 -dd.yeast <- degree.distribution(yeast) -d <- 1:max(d.yeast)-1 -ind <- (dd.yeast != 0) + main="Degree Distribution") + +# CHUNK 7 +dd.yeast <- degree.distribution(yeast) +d <- 1:max(d.yeast)-1 +ind <- (dd.yeast != 0) plot(d[ind], dd.yeast[ind], log="xy", col="blue", xlab=c("Log-Degree"), ylab=c("Log-Intensity"), - main="Log-Log Degree Distribution") - -# CHUNK 8 -a.nn.deg.yeast <- graph.knn(yeast,V(yeast))$knn + main="Log-Log Degree Distribution") + +# CHUNK 8 +a.nn.deg.yeast <- graph.knn(yeast,V(yeast))$knn plot(d.yeast, a.nn.deg.yeast, log="xy", col="goldenrod", xlab=c("Log Vertex Degree"), - ylab=c("Log Average Neighbor Degree")) - -# CHUNK 9 -A <- get.adjacency(karate, sparse=FALSE) -library(network) -g <- network::as.network.matrix(A) -library(sna) + ylab=c("Log Average Neighbor Degree")) + +# CHUNK 9 +A <- get.adjacency(karate, sparse=FALSE) +library(network) +g <- network::as.network.matrix(A) +library(sna) sna::gplot.target(g, degree(g), main="Degree", circ.lab = FALSE, circ.col="skyblue", usearrows = FALSE, vertex.col=c("blue", rep("red", 32), "yellow"), - edge.col="darkgray") - -# CHUNK 10 -l <- layout.kamada.kawai(aidsblog) + edge.col="darkgray") + +# CHUNK 10 +l <- layout.kamada.kawai(aidsblog) plot(aidsblog, layout=l, main="Hubs", vertex.label="", - vertex.size=10 * sqrt(hub.score(aidsblog)$vector)) + vertex.size=10 * sqrt(hub.score(aidsblog)$vector)) plot(aidsblog, layout=l, main="Authorities", vertex.label="", vertex.size=10 * - sqrt(authority.score(aidsblog)$vector)) - -# CHUNK 11 -eb <- edge.betweenness(karate) -E(karate)[order(eb, decreasing=T)[1:3]] -# --- + sqrt(authority.score(aidsblog)$vector)) + +# CHUNK 11 +eb <- edge.betweenness(karate) +E(karate)[order(eb, decreasing=T)[1:3]] +# --- ## Edge sequence: ## ## [53] John A -- Actor 20 ## [14] Actor 20 -- Mr Hi -## [16] Actor 32 -- Mr Hi -# --- - -# CHUNK 12 -table(sapply(cliques(karate), length)) -# --- +## [16] Actor 32 -- Mr Hi +# --- + +# CHUNK 12 +table(sapply(cliques(karate), length)) +# --- ## ## 1 2 3 4 5 -## 34 78 45 11 2 -# --- - -# CHUNK 13 -cliques(karate)[sapply(cliques(karate), length) == 5] -# --- +## 34 78 45 11 2 +# --- + +# CHUNK 13 +cliques(karate)[sapply(cliques(karate), length) == 5] +# --- ## [[1]] ## [1] 1 2 3 4 8 ## ## [[2]] -## [1] 1 2 3 4 14 -# --- - -# CHUNK 14 -table(sapply(maximal.cliques(karate), length)) -# --- +## [1] 1 2 3 4 14 +# --- + +# CHUNK 14 +table(sapply(maximal.cliques(karate), length)) +# --- ## ## 2 3 4 5 -## 11 21 2 2 -# --- - -# CHUNK 15 -clique.number(yeast) -# --- -## [1] 23 -# --- - -# CHUNK 16 -cores <- graph.coreness(karate) +## 11 21 2 2 +# --- + +# CHUNK 15 +clique.number(yeast) +# --- +## [1] 23 +# --- + +# CHUNK 16 +cores <- graph.coreness(karate) sna::gplot.target(g, cores, circ.lab = FALSE, circ.col="skyblue", usearrows = FALSE, - vertex.col=cores, edge.col="darkgray") -detach("package:network") -detach("package:sna") - -# CHUNK 17 -aidsblog <- simplify(aidsblog) -dyad.census(aidsblog) -# --- + vertex.col=cores, edge.col="darkgray") +detach("package:network") +detach("package:sna") + +# CHUNK 17 +aidsblog <- simplify(aidsblog) +dyad.census(aidsblog) +# --- ## $mut ## [1] 3 ## @@ -127,141 +127,141 @@ dyad.census(aidsblog) ## [1] 177 ## ## $null -## [1] 10405 -# --- - -# CHUNK 18 +## [1] 10405 +# --- + +# CHUNK 18 ego.instr <- induced.subgraph(karate, - neighborhood(karate, 1, 1)[[1]]) + neighborhood(karate, 1, 1)[[1]]) ego.admin <- induced.subgraph(karate, - neighborhood(karate, 1, 34)[[1]]) -graph.density(karate) -# --- -## [1] 0.1390374 -# --- -graph.density(ego.instr) -# --- -## [1] 0.25 -# --- -graph.density(ego.admin) -# --- -## [1] 0.2091503 -# --- - -# CHUNK 19 -transitivity(karate) -# --- -## [1] 0.2556818 -# --- - -# CHUNK 20 -transitivity(karate, "local", vids=c(1,34)) -# --- -## [1] 0.1500000 0.1102941 -# --- - -# CHUNK 21 -reciprocity(aidsblog, mode="default") -# --- -## [1] 0.03278689 -# --- -reciprocity(aidsblog, mode="ratio") -# --- -## [1] 0.01666667 -# --- - -# CHUNK 22 -is.connected(yeast) -# --- -## [1] FALSE -# --- - -# CHUNK 23 -comps <- decompose.graph(yeast) -table(sapply(comps, vcount)) -# --- + neighborhood(karate, 1, 34)[[1]]) +graph.density(karate) +# --- +## [1] 0.1390374 +# --- +graph.density(ego.instr) +# --- +## [1] 0.25 +# --- +graph.density(ego.admin) +# --- +## [1] 0.2091503 +# --- + +# CHUNK 19 +transitivity(karate) +# --- +## [1] 0.2556818 +# --- + +# CHUNK 20 +transitivity(karate, "local", vids=c(1,34)) +# --- +## [1] 0.1500000 0.1102941 +# --- + +# CHUNK 21 +reciprocity(aidsblog, mode="default") +# --- +## [1] 0.03278689 +# --- +reciprocity(aidsblog, mode="ratio") +# --- +## [1] 0.01666667 +# --- + +# CHUNK 22 +is.connected(yeast) +# --- +## [1] FALSE +# --- + +# CHUNK 23 +comps <- decompose.graph(yeast) +table(sapply(comps, vcount)) +# --- ## ## 2 3 4 5 6 7 2375 -## 63 13 5 6 1 3 1 -# --- - -# CHUNK 24 -yeast.gc <- decompose.graph(yeast)[[1]] - -# CHUNK 25 -average.path.length(yeast.gc) -# --- -## [1] 5.09597 -# --- - -# CHUNK 26 -diameter(yeast.gc) -# --- -## [1] 15 -# --- - -# CHUNK 27 -transitivity(yeast.gc) -# --- -## [1] 0.4686663 -# --- - -# CHUNK 28 -vertex.connectivity(yeast.gc) -# --- -## [1] 1 -# --- -edge.connectivity(yeast.gc) -# --- -## [1] 1 -# --- - -# CHUNK 29 -yeast.cut.vertices <- articulation.points(yeast.gc) -length(yeast.cut.vertices) -# --- -## [1] 350 -# --- - -# CHUNK 30 -is.connected(aidsblog, mode=c("weak")) -# --- -## [1] TRUE -# --- - -# CHUNK 31 -is.connected(aidsblog, mode=c("strong")) -# --- -## [1] FALSE -# --- - -# CHUNK 32 -aidsblog.scc <- clusters(aidsblog, mode=c("strong")) -table(aidsblog.scc$csize) -# --- +## 63 13 5 6 1 3 1 +# --- + +# CHUNK 24 +yeast.gc <- decompose.graph(yeast)[[1]] + +# CHUNK 25 +average.path.length(yeast.gc) +# --- +## [1] 5.09597 +# --- + +# CHUNK 26 +diameter(yeast.gc) +# --- +## [1] 15 +# --- + +# CHUNK 27 +transitivity(yeast.gc) +# --- +## [1] 0.4686663 +# --- + +# CHUNK 28 +vertex.connectivity(yeast.gc) +# --- +## [1] 1 +# --- +edge.connectivity(yeast.gc) +# --- +## [1] 1 +# --- + +# CHUNK 29 +yeast.cut.vertices <- articulation.points(yeast.gc) +length(yeast.cut.vertices) +# --- +## [1] 350 +# --- + +# CHUNK 30 +is.connected(aidsblog, mode=c("weak")) +# --- +## [1] TRUE +# --- + +# CHUNK 31 +is.connected(aidsblog, mode=c("strong")) +# --- +## [1] FALSE +# --- + +# CHUNK 32 +aidsblog.scc <- clusters(aidsblog, mode=c("strong")) +table(aidsblog.scc$csize) +# --- ## ## 1 4 -## 142 1 -# --- - -# CHUNK 33 -kc <- fastgreedy.community(karate) - -# CHUNK 34 -length(kc) -# --- -## [1] 3 -# --- -sizes(kc) -# --- +## 142 1 +# --- + +# CHUNK 33 +kc <- fastgreedy.community(karate) + +# CHUNK 34 +length(kc) +# --- +## [1] 3 +# --- +sizes(kc) +# --- ## Community sizes ## 1 2 3 -## 18 11 5 -# --- - -# CHUNK 35 -membership(kc) -# --- +## 18 11 5 +# --- + +# CHUNK 35 +membership(kc) +# --- ## Mr Hi Actor 2 Actor 3 Actor 4 Actor 5 Actor 6 ## 2 2 2 2 3 3 ## Actor 7 Actor 8 Actor 9 Actor 10 Actor 11 Actor 12 @@ -273,52 +273,52 @@ membership(kc) ## Actor 25 Actor 26 Actor 27 Actor 28 Actor 29 Actor 30 ## 1 1 1 1 1 1 ## Actor 31 Actor 32 Actor 33 John A -## 1 1 1 1 -# --- - -# CHUNK 36 -plot(kc,karate) - -# CHUNK 37 -library(ape) -dendPlot(kc, mode="phylo") - -# CHUNK 38 -k.lap <- graph.laplacian(karate) -eig.anal <- eigen(k.lap) - -# CHUNK 39 +## 1 1 1 1 +# --- + +# CHUNK 36 +plot(kc,karate) + +# CHUNK 37 +library(ape) +dendPlot(kc, mode="phylo") + +# CHUNK 38 +k.lap <- graph.laplacian(karate) +eig.anal <- eigen(k.lap) + +# CHUNK 39 plot(eig.anal$values, col="blue", - ylab="Eigenvalues of Graph Laplacian") - -# CHUNK 40 -f.vec <- eig.anal$vectors[, 33] - -# CHUNK 41 -faction <- get.vertex.attribute(karate, "Faction") -f.colors <- as.character(length(faction)) -f.colors[faction == 1] <- "red" -f.colors[faction == 2] <- "cyan" + ylab="Eigenvalues of Graph Laplacian") + +# CHUNK 40 +f.vec <- eig.anal$vectors[, 33] + +# CHUNK 41 +faction <- vertex_attr(karate, "Faction") +f.colors <- as.character(length(faction)) +f.colors[faction == 1] <- "red" +f.colors[faction == 2] <- "cyan" plot(f.vec, pch=16, xlab="Actor Number", - ylab="Fiedler Vector Entry", col=f.colors) -abline(0, 0, lwd=2, col="lightgray") - -# CHUNK 42 -func.class <- get.vertex.attribute(yeast.gc, "Class") -table(func.class) -# --- + ylab="Fiedler Vector Entry", col=f.colors) +abline(0, 0, lwd=2, col="lightgray") + +# CHUNK 42 +func.class <- get.vertex.attribute(yeast.gc, "Class") +table(func.class) +# --- ## func.class ## A B C D E F G M O P R T U -## 51 98 122 238 95 171 96 278 171 248 45 240 483 -# --- - -# CHUNK 43 -yc <- fastgreedy.community(yeast.gc) -c.m <- membership(yc) - -# CHUNK 44 -table(c.m, func.class, useNA=c("no")) -# --- +## 51 98 122 238 95 171 96 278 171 248 45 240 483 +# --- + +# CHUNK 43 +yc <- fastgreedy.community(yeast.gc) +c.m <- membership(yc) + +# CHUNK 44 +table(c.m, func.class, useNA=c("no")) +# --- ## func.class ## c.m A B C D E F G M O P R T U ## 1 0 0 0 1 3 7 0 6 3 110 2 35 14 @@ -351,19 +351,19 @@ table(c.m, func.class, useNA=c("no")) ## 28 0 0 0 0 0 0 0 0 0 6 0 0 0 ## 29 0 0 0 1 0 0 0 1 0 0 3 0 0 ## 30 0 0 0 0 0 0 0 0 0 2 0 0 2 -## 31 0 0 0 0 0 0 0 3 0 0 0 0 0 -# --- - -# CHUNK 45 +## 31 0 0 0 0 0 0 0 3 0 0 0 0 0 +# --- + +# CHUNK 45 assortativity.nominal(yeast, (V(yeast)$Class=="P")+1, - directed=FALSE) -# --- -## [1] 0.4965229 -# --- - -# CHUNK 46 -assortativity.degree(yeast) -# --- -## [1] 0.4610798 -# --- - + directed=FALSE) +# --- +## [1] 0.4965229 +# --- + +# CHUNK 46 +assortativity.degree(yeast) +# --- +## [1] 0.4610798 +# --- +