From f9841a270c6267f9dfe299cee32b0419b9845918 Mon Sep 17 00:00:00 2001 From: Azza Ahmed Date: Thu, 16 Nov 2017 17:40:23 +0300 Subject: [PATCH] Fixing some ouptut results --- sand/inst/code/chapter2.R | 508 +++++++++++++++++++------------------- 1 file changed, 254 insertions(+), 254 deletions(-) diff --git a/sand/inst/code/chapter2.R b/sand/inst/code/chapter2.R index 6ac4e47..c252d11 100644 --- a/sand/inst/code/chapter2.R +++ b/sand/inst/code/chapter2.R @@ -1,37 +1,37 @@ -# SAND with R, chapter2.tex - -# CHUNK 1 -library(igraph) +# SAND with R, chapter2.tex + +# CHUNK 1 +library(igraph) g <- graph.formula(1-2, 1-3, 2-3, 2-4, 3-5, 4-5, 4-6, - 4-7, 5-6, 6-7) - -# CHUNK 2 -V(g) -# --- + 4-7, 5-6, 6-7) + +# CHUNK 2 +V(g) +# --- ## Vertex sequence: -## [1] "1" "2" "3" "4" "5" "6" "7" -# --- - -# CHUNK 3 -E(g) -# --- +## [1] "1" "2" "3" "4" "5" "6" "7" +# --- + +# CHUNK 3 +E(g) +# --- ## Edge sequence: ## -## [1] 2 -- 1 -## [2] 3 -- 1 -## [3] 3 -- 2 -## [4] 4 -- 2 -## [5] 5 -- 3 -## [6] 5 -- 4 -## [7] 6 -- 4 -## [8] 7 -- 4 -## [9] 6 -- 5 -## [10] 7 -- 6 -# --- - -# CHUNK 4 -str(g) -# --- +## [1] 1 -- 2 +## [2] 1 -- 3 +## [3] 2 -- 3 +## [4] 2 -- 4 +## [5] 3 -- 5 +## [6] 4 -- 5 +## [7] 4 -- 6 +## [8] 4 -- 7 +## [9] 5 -- 6 +## [10] 6 -- 7 +# --- + +# CHUNK 4 +str(g) +# --- ## IGRAPH UN-- 7 10 -- ## + attr: name (v/c) ## + edges (vertex names): @@ -41,43 +41,43 @@ str(g) ## 4 -- 2, 5, 6, 7 ## 5 -- 3, 4, 6 ## 6 -- 4, 5, 7 -## 7 -- 4, 6 -# --- - -# CHUNK 5 -plot(g) - -# CHUNK 6 -dg <- graph.formula(1-+2, 1-+3, 2++3) -plot(dg) - -# CHUNK 7 -dg <- graph.formula(Sam-+Mary, Sam-+Tom, Mary++Tom) -str(dg) -# --- +## 7 -- 4, 6 +# --- + +# CHUNK 5 +plot(g) + +# CHUNK 6 +dg <- graph.formula(1-+2, 1-+3, 2++3) +plot(dg) + +# CHUNK 7 +dg <- graph.formula(Sam-+Mary, Sam-+Tom, Mary++Tom) +str(dg) +# --- ## IGRAPH DN-- 3 4 -- ## + attr: name (v/c) ## + edges (vertex names): -## [1] Sam ->Mary Sam ->Tom Mary->Tom Tom ->Mary -# --- - -# CHUNK 8 -V(dg)$name <- c("Sam", "Mary", "Tom") - -# CHUNK 9 -E(dg) -# --- +## [1] Sam ->Mary Sam ->Tom Mary->Tom Tom ->Mary +# --- + +# CHUNK 8 +V(dg)$name <- c("Sam", "Mary", "Tom") + +# CHUNK 9 +E(dg) +# --- ## Edge sequence: ## ## [1] Sam -> Mary ## [2] Sam -> Tom ## [3] Mary -> Tom -## [4] Tom -> Mary -# --- - -# CHUNK 10 -get.adjacency(g) -# --- +## [4] Tom -> Mary +# --- + +# CHUNK 10 +get.adjacency(g) +# --- ## 7 x 7 sparse Matrix of class "dgCMatrix" ## 1 2 3 4 5 6 7 ## 1 . 1 1 . . . . @@ -86,95 +86,95 @@ get.adjacency(g) ## 4 . 1 . . 1 1 1 ## 5 . . 1 1 . 1 . ## 6 . . . 1 1 . 1 -## 7 . . . 1 . 1 . -# --- - -# CHUNK 11 -h <- induced.subgraph(g, 1:5) -str(h) -# --- +## 7 . . . 1 . 1 . +# --- + +# CHUNK 11 +h <- induced.subgraph(g, 1:5) +str(h) +# --- ## IGRAPH UN-- 5 6 -- ## + attr: name (v/c) ## + edges (vertex names): -## [1] 1--2 1--3 2--3 2--4 3--5 4--5 -# --- - -# CHUNK 12 -h <- g - vertices(c(6,7)) - -# CHUNK 13 -h <- h + vertices(c(6,7)) -g <- h + edges(c(4,6),c(4,7),c(5,6),c(6,7)) - -# CHUNK 14 -h1 <- h -h2 <- graph.formula(4-6, 4-7, 5-6, 6-7) -g <- graph.union(h1,h2) - -# CHUNK 15 -V(dg)$name -# --- -## [1] "Sam" "Mary" "Tom" -# --- - -# CHUNK 16 -V(dg)$gender <- c("M","F","M") - -# CHUNK 17 -V(g)$color <- "red" - -# CHUNK 18 -is.weighted(g) -# --- -## [1] FALSE -# --- -wg <- g -E(wg)$weight <- runif(ecount(wg)) -is.weighted(wg) -# --- -## [1] TRUE -# --- - -# CHUNK 19 -g$name <- "Toy Graph" - -# CHUNK 20 -library(sand) +## [1] 1--2 1--3 2--3 2--4 3--5 4--5 +# --- + +# CHUNK 12 +h <- g - vertices(c(6,7)) + +# CHUNK 13 +h <- h + vertices(c(6,7)) +g <- h + edges(c(4,6),c(4,7),c(5,6),c(6,7)) + +# CHUNK 14 +h1 <- h +h2 <- graph.formula(4-6, 4-7, 5-6, 6-7) +g <- graph.union(h1,h2) + +# CHUNK 15 +V(dg)$name +# --- +## [1] "Sam" "Mary" "Tom" +# --- + +# CHUNK 16 +V(dg)$gender <- c("M","F","M") + +# CHUNK 17 +V(g)$color <- "red" + +# CHUNK 18 +is.weighted(g) +# --- +## [1] FALSE +# --- +wg <- g +E(wg)$weight <- runif(ecount(wg)) +is.weighted(wg) +# --- +## [1] TRUE +# --- + +# CHUNK 19 +g$name <- "Toy Graph" + +# CHUNK 20 +library(sand) g.lazega <- graph.data.frame(elist.lazega, directed="FALSE", - vertices=v.attr.lazega) -g.lazega$name <- "Lazega Lawyers" - -# CHUNK 21 -vcount(g.lazega) -# --- -## [1] 36 -# --- - -# CHUNK 22 -ecount(g.lazega) -# --- -## [1] 115 -# --- - -# CHUNK 23 -list.vertex.attributes(g.lazega) -# --- + vertices=v.attr.lazega) +g.lazega$name <- "Lazega Lawyers" + +# CHUNK 21 +vcount(g.lazega) +# --- +## [1] 36 +# --- + +# CHUNK 22 +ecount(g.lazega) +# --- +## [1] 115 +# --- + +# CHUNK 23 +list.vertex.attributes(g.lazega) +# --- ## [1] "name" "Seniority" "Status" "Gender" ## [5] "Office" "Years" "Age" "Practice" -## [9] "School" -# --- - -# CHUNK 24 -is.simple(g) -# --- -## [1] TRUE -# --- - -# CHUNK 25 -mg <- g + edge(2,3) -str(mg) -# --- +## [9] "School" +# --- + +# CHUNK 24 +is.simple(g) +# --- +## [1] TRUE +# --- + +# CHUNK 25 +mg <- g + edge(2,3) +str(mg) +# --- ## IGRAPH UN-- 7 11 -- Toy Graph ## + attr: name (g/c), name (v/c), color (v/c) ## + edges (vertex names): @@ -184,24 +184,24 @@ str(mg) ## 4 -- 2, 5, 6, 7 ## 5 -- 3, 4, 6 ## 6 -- 4, 5, 7 -## 7 -- 4, 6 -# --- -is.simple(mg) -# --- -## [1] FALSE -# --- - -# CHUNK 26 -E(mg)$weight <- 1 -wg2 <- simplify(mg) -is.simple(wg2) -# --- -## [1] TRUE -# --- - -# CHUNK 27 -str(wg2) -# --- +## 7 -- 4, 6 +# --- +is.simple(mg) +# --- +## [1] FALSE +# --- + +# CHUNK 26 +E(mg)$weight <- 1 +wg2 <- simplify(mg) +is.simple(wg2) +# --- +## [1] TRUE +# --- + +# CHUNK 27 +str(wg2) +# --- ## IGRAPH UNW- 7 10 -- Toy Graph ## + attr: name (g/c), name (v/c), color (v/c), ## weight (e/n) @@ -212,49 +212,49 @@ str(wg2) ## 4 -- 2, 5, 6, 7 ## 5 -- 3, 4, 6 ## 6 -- 4, 5, 7 -## 7 -- 4, 6 -# --- - -# CHUNK 28 -E(wg2)$weight -# --- -## [1] 1 1 2 1 1 1 1 1 1 1 -# --- - -# CHUNK 29 -neighbors(g,5) -# --- -## [1] 3 4 6 -# --- - -# CHUNK 30 -degree(g) -# --- +## 7 -- 4, 6 +# --- + +# CHUNK 28 +E(wg2)$weight +# --- +## [1] 1 1 2 1 1 1 1 1 1 1 +# --- + +# CHUNK 29 +neighbors(g,5) +# --- +## [1] 3 4 6 +# --- + +# CHUNK 30 +degree(g) +# --- ## 1 2 3 4 5 6 7 -## 2 3 3 4 3 3 2 -# --- - -# CHUNK 31 -degree(dg, mode="in") -# --- +## 2 3 3 4 3 3 2 +# --- + +# CHUNK 31 +degree(dg, mode="in") +# --- ## Sam Mary Tom -## 0 2 2 -# --- -degree(dg, mode="out") -# --- +## 0 2 2 +# --- +degree(dg, mode="out") +# --- ## Sam Mary Tom -## 2 1 1 -# --- - -# CHUNK 32 -is.connected(g) -# --- -## [1] TRUE -# --- - -# CHUNK 33 -clusters(g) -# --- +## 2 1 1 +# --- + +# CHUNK 32 +is.connected(g) +# --- +## [1] TRUE +# --- + +# CHUNK 33 +clusters(g) +# --- ## $membership ## [1] 1 1 1 1 1 1 1 ## @@ -262,49 +262,49 @@ clusters(g) ## [1] 7 ## ## $no -## [1] 1 -# --- - -# CHUNK 34 -is.connected(dg, mode="weak") -# --- -## [1] TRUE -# --- -is.connected(dg, mode="strong") -# --- -## [1] FALSE -# --- - -# CHUNK 35 -diameter(g, weights=NA) -# --- -## [1] 3 -# --- - -# CHUNK 36 -g.full <- graph.full(7) -g.ring <- graph.ring(7) -g.tree <- graph.tree(7, children=2, mode="undirected") -g.star <- graph.star(7, mode="undirected") -par(mfrow=c(2, 2)) -plot(g.full) -plot(g.ring) -plot(g.tree) -plot(g.star) - -# CHUNK 37 -is.dag(dg) -# --- -## [1] FALSE -# --- - -# CHUNK 38 +## [1] 1 +# --- + +# CHUNK 34 +is.connected(dg, mode="weak") +# --- +## [1] TRUE +# --- +is.connected(dg, mode="strong") +# --- +## [1] FALSE +# --- + +# CHUNK 35 +diameter(g, weights=NA) +# --- +## [1] 3 +# --- + +# CHUNK 36 +g.full <- graph.full(7) +g.ring <- graph.ring(7) +g.tree <- graph.tree(7, children=2, mode="undirected") +g.star <- graph.star(7, mode="undirected") +par(mfrow=c(2, 2)) +plot(g.full) +plot(g.ring) +plot(g.tree) +plot(g.star) + +# CHUNK 37 +is.dag(dg) +# --- +## [1] FALSE +# --- + +# CHUNK 38 g.bip <- graph.formula(actor1:actor2:actor3, movie1:movie2, actor1:actor2 - movie1, - actor2:actor3 - movie2) -V(g.bip)$type <- grepl("^movie", V(g.bip)$name) -str(g.bip, v=T) -# --- + actor2:actor3 - movie2) +V(g.bip)$type <- grepl("^movie", V(g.bip)$name) +str(g.bip, v=T) +# --- ## IGRAPH UN-B 5 4 -- ## + attr: name (v/c), type (v/l) ## + vertex attributes: @@ -316,23 +316,23 @@ str(g.bip, v=T) ## [5] movie2 TRUE ## + edges (vertex names): ## [1] actor1--movie1 actor2--movie1 actor2--movie2 -## [4] actor3--movie2 -# --- - -# CHUNK 39 -proj <- bipartite.projection(g.bip) -str(proj[[1]]) -# --- +## [4] actor3--movie2 +# --- + +# CHUNK 39 +proj <- bipartite.projection(g.bip) +str(proj[[1]]) +# --- ## IGRAPH UNW- 3 2 -- ## + attr: name (v/c), weight (e/n) ## + edges (vertex names): -## [1] actor1--actor2 actor2--actor3 -# --- -str(proj[[2]]) -# --- +## [1] actor1--actor2 actor2--actor3 +# --- +str(proj[[2]]) +# --- ## IGRAPH UNW- 2 1 -- ## + attr: name (v/c), weight (e/n) ## + edges (vertex names): -## [1] movie1--movie2 -# --- - +## [1] movie1--movie2 +# --- +