class: center, middle, inverse, title-slide # MULTIVARIJATNE STATISTIČKE METODE ## Predavanje 9: Network analiza ### dr.sc. Luka Šikić ### Fakultet hrvatskih studija |
Github MV
--- <style type="text/css"> @media print { .has-continuation { display: block !important; } } remark-slide-content { font-size: 22px; padding: 20px 80px 20px 80px; } .remark-code, .remark-inline-code { background: #f0f0f0; } .remark-code { font-size: 16px; } .mid. remark-code { /*Change made here*/ font-size: 60% !important; } .tiny .remark-code { /*Change made here*/ font-size: 40% !important; } /* custom.css */ .left-code { color: #777; width: 38%; height: 92%; float: left; } .right-plot { width: 60%; float: right; padding-left: 1%; } .plot-callout { height: 225px; width: 450px; bottom: 5%; right: 5%; position: absolute; padding: 0px; z-index: 100; } .plot-callout img { width: 100%; border: 4px solid #23373B; } </style> # Pregled predavanja <br> <br> <br> 1. [Općenito o network analizi](#opc) 2. [Network podatci](#netdta) 3. [Network objekt](#netob) 4. [Praktični primjer (network analiza)](#stat) --- class: inverse, center, middle name: opc # OPĆENITO O NETWORK ANALIZI <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> (Osnova za korištenje) --- # Koncept <br> <br> <br> - modeliranje kompleksnosti i međupovezanosti <br> - centralna metodologija za (digitalne) društvene i humanističke znanosti <br> - upotreba u analizi komunikacijskih kanala, modeliranju epidemije, razumijevanju društvenih interakcija <br> - vizualna analiza - *uvodno* <br> - statistička analiza - *napredno* --- # Software podrška <br> <br> <br> - [statnet](http://statnet.org/) <br> - [igraph](https://igraph.org/) <br> - [tidygraph](https://github.com/thomasp85/tidygraph) <br> - [gggraph](https://www.data-imaginist.com/2017/ggraph-introduction-layouts/) --- # Najbitniji elementi mreže <br> <br> - nodes (*čvorovi*) i vertices (*poveznice*) ili edges i links <br> <br> - atributi (*attributes*) <br> <br> - adjecancy matrice vs. data.frame <br> <br> - mreže mogu biti usmjerene (directed) i neusmjerene (unidirected) <br> --- # Čvorovi (*nodes*) <br> <br> <img src="../Foto/net8.png" width="500px" style="display: block; margin: auto;" /> --- # Stvori demo network objekt (*nodes*) <br> <br> ```r # node-list objekt ima jednu varijablu (kolonu) i sadržava ID oznaku za pripadajući edge node_list <- tibble(id = 1:5) node_list ``` ``` ## # A tibble: 5 x 1 ## id ## <int> ## 1 1 ## 2 2 ## 3 3 ## 4 4 ## 5 5 ``` --- # Poveznice (*edges*) <br> <br> <img src="../Foto/net9.png" width="500px" style="display: block; margin: auto;" /> --- # Stvori demo network objekt (*edges*) <br> ```r # edge-list ima minimalno dvije kolone: kolona izvora čvorova (nodes) i kolona smjernica čvorova # edge-list objekt može sadržavati i dodatne atribute (varijable) edge_list <- tibble(from = c(1, 2, 2, 3, 4), to = c(2, 3, 4, 2, 1)) edge_list ``` ``` ## # A tibble: 5 x 2 ## from to ## <dbl> <dbl> ## 1 1 2 ## 2 2 3 ## 3 2 4 ## 4 3 2 ## 5 4 1 ``` --- # Karakteristike (*attributes*) <br> <br> <img src="../Foto/net10.png" width="500px" style="display: block; margin: auto;" /> --- # Stvori demo network objekt (*attributes*) <br> ```r node_list_attr <- node_list %>% mutate(Spol = c("F","F","M","F","M")) node_list_attr ``` ``` ## # A tibble: 5 x 2 ## id Spol ## <int> <chr> ## 1 1 F ## 2 2 F ## 3 3 M ## 4 4 F ## 5 5 M ``` --- # Reprezentacija mreže <br> ###### Standardni način: adjecancy matrica <br> ```r #> 1 2 3 4 5 #> 1 0 1 0 0 0 #> 2 0 0 1 1 0 #> 3 0 1 0 0 0 #> 4 1 0 0 0 0 #> 5 1 0 0 0 0 ``` --- # *Adjecency* matrica <br> <img src="../Foto/net5.png" width="650px" style="display: block; margin: auto;" /> --- # *Adjecency* matrica <br> <img src="../Foto/net6.png" width="650px" style="display: block; margin: auto;" /> --- # Reprezentacija mreže <br> ###### "Moderni" način: crna kutija (*black box*) <br> ```r g = graph_from_data_frame(edge_list, directed = FALSE, vertices = node_list) g ``` ``` ## IGRAPH b7f6160 UN-- 5 5 -- ## + attr: name (v/c) ## + edges from b7f6160 (vertex names): ## [1] 1--2 2--3 2--4 2--3 1--4 ``` .footnote[[*]Postoji mnoštvo različitih načina za "black box" reprezentaciju. Upoznati ćemo nekoliko načina!] --- class: inverse, center, middle name: netdta # NETWORK PODATCI <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> (Napravi mrežu iz podataka!) --- # Preuzmi podatke <br> ```r url <- "https://github.com/jessesadler/intro-to-r/blob/master/data/correspondence-data-1585.csv" epistole <- read_html(url) %>% html_table(fill = TRUE) %>% as.data.frame() %>% select(-1) %>% row_to_names(1) glimpse(epistole) ``` ``` ## Rows: 114 ## Columns: 4 ## $ writer <chr> "Meulen, Andries van der", "Meulen, Andries van der", "Meu~ ## $ source <chr> "Antwerp", "Antwerp", "Antwerp", "Antwerp", "Antwerp", "An~ ## $ destination <chr> "Delft", "Haarlem", "Haarlem", "Delft", "Haarlem", "Delft"~ ## $ date <chr> "1585-01-03", "1585-01-09", "1585-01-11", "1585-01-12", "1~ ``` --- # Pregledaj podatke <br> ```r head(epistole,15) ``` ``` ## writer source destination date ## 2 Meulen, Andries van der Antwerp Delft 1585-01-03 ## 3 Meulen, Andries van der Antwerp Haarlem 1585-01-09 ## 4 Meulen, Andries van der Antwerp Haarlem 1585-01-11 ## 5 Meulen, Andries van der Antwerp Delft 1585-01-12 ## 6 Meulen, Andries van der Antwerp Haarlem 1585-01-12 ## 7 Meulen, Andries van der Antwerp Delft 1585-01-17 ## 8 Meulen, Andries van der Antwerp Delft 1585-01-22 ## 9 Meulen, Andries van der Antwerp Delft 1585-01-23 ## 10 Della Faille, Marten Antwerp Haarlem 1585-01-24 ## 11 Meulen, Andries van der Antwerp Delft 1585-01-28 ## 12 Meulen, Andries van der Antwerp Delft 1585-01-30 ## 13 Meulen, Andries van der Antwerp Delft 1585-02-05 ## 14 Meulen, Andries van der Antwerp Delft 1585-02-07 ## 15 Della Faille, Jacques Haarlem Delft 1585-02-10 ## 16 Meulen, Andries van der Antwerp Delft 1585-02-11 ``` --- # Stvori node listu <br> ```r sources <- epistole %>% distinct(source) %>% rename(label = source) sources ``` ``` ## label ## 1 Antwerp ## 2 Haarlem ## 3 Dordrecht ## 4 Venice ## 5 Lisse ## 6 Het Vlie ## 7 Hamburg ## 8 Emden ## 9 Amsterdam ``` --- # Stvori node listu <br> ```r destinations <- epistole %>% distinct(destination) %>% rename(label = destination) destinations ``` ``` ## label ## 1 Delft ## 2 Haarlem ## 3 The Hague ## 4 Middelburg ## 5 Bremen ``` --- # Stvori node listu <br> ```r # poveži u jedan objekt nodes <- full_join(sources, destinations, by = "label") nodes ``` ``` ## label ## 1 Antwerp ## 2 Haarlem ## 3 Dordrecht ## 4 Venice ## 5 Lisse ## 6 Het Vlie ## 7 Hamburg ## 8 Emden ## 9 Amsterdam ## 10 Delft ## 11 The Hague ## 12 Middelburg ## 13 Bremen ``` --- # Stvori node listu ```r # dodaj ID nodes <- nodes %>% rowid_to_column("id") nodes ``` ``` ## id label ## 1 1 Antwerp ## 2 2 Haarlem ## 3 3 Dordrecht ## 4 4 Venice ## 5 5 Lisse ## 6 6 Het Vlie ## 7 7 Hamburg ## 8 8 Emden ## 9 9 Amsterdam ## 10 10 Delft ## 11 11 The Hague ## 12 12 Middelburg ## 13 13 Bremen ``` --- # Napravi edge listu ```r per_route <- epistole %>% group_by(source, destination) %>% summarise(weight = n()) %>% ungroup() head(per_route,13) ``` ``` ## # A tibble: 13 x 3 ## source destination weight ## <chr> <chr> <int> ## 1 Amsterdam Bremen 1 ## 2 Antwerp Delft 68 ## 3 Antwerp Haarlem 5 ## 4 Antwerp Middelburg 1 ## 5 Antwerp The Hague 2 ## 6 Dordrecht Haarlem 1 ## 7 Emden Bremen 1 ## 8 Haarlem Bremen 2 ## 9 Haarlem Delft 26 ## 10 Haarlem Middelburg 1 ## 11 Haarlem The Hague 1 ## 12 Hamburg Bremen 1 ## 13 Het Vlie Bremen 1 ``` --- # Napravi edge listu ```r edges <- per_route %>% left_join(nodes, by = c("source" = "label")) %>% rename(from = id) edges ``` ``` ## # A tibble: 15 x 4 ## source destination weight from ## <chr> <chr> <int> <int> ## 1 Amsterdam Bremen 1 9 ## 2 Antwerp Delft 68 1 ## 3 Antwerp Haarlem 5 1 ## 4 Antwerp Middelburg 1 1 ## 5 Antwerp The Hague 2 1 ## 6 Dordrecht Haarlem 1 3 ## 7 Emden Bremen 1 8 ## 8 Haarlem Bremen 2 2 ## 9 Haarlem Delft 26 2 ## 10 Haarlem Middelburg 1 2 ## 11 Haarlem The Hague 1 2 ## 12 Hamburg Bremen 1 7 ## 13 Het Vlie Bremen 1 6 ## 14 Lisse Delft 1 5 ## 15 Venice Haarlem 2 4 ``` --- # Napravi edge listu ```r edges <- edges %>% left_join(nodes, by = c("destination" = "label")) %>% rename(to = id) edges ``` ``` ## # A tibble: 15 x 5 ## source destination weight from to ## <chr> <chr> <int> <int> <int> ## 1 Amsterdam Bremen 1 9 13 ## 2 Antwerp Delft 68 1 10 ## 3 Antwerp Haarlem 5 1 2 ## 4 Antwerp Middelburg 1 1 12 ## 5 Antwerp The Hague 2 1 11 ## 6 Dordrecht Haarlem 1 3 2 ## 7 Emden Bremen 1 8 13 ## 8 Haarlem Bremen 2 2 13 ## 9 Haarlem Delft 26 2 10 ## 10 Haarlem Middelburg 1 2 12 ## 11 Haarlem The Hague 1 2 11 ## 12 Hamburg Bremen 1 7 13 ## 13 Het Vlie Bremen 1 6 13 ## 14 Lisse Delft 1 5 10 ## 15 Venice Haarlem 2 4 2 ``` --- class: inverse, center, middle name: netob # NETWORK OBJEKT <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> (Osnova za analizu) --- # Napravi mrežni objekt I .tiny[ ```r library(network) routes_network <- network(edges, vertex.attr = nodes, matrix.type = "edgelist", ignore.eval = FALSE) class(routes_network) ``` ``` ## [1] "network" ``` ```r routes_network ``` ``` ## Network attributes: ## vertices = 13 ## directed = TRUE ## hyper = FALSE ## loops = FALSE ## multiple = FALSE ## bipartite = FALSE ## total edges= 15 ## missing edges= 0 ## non-missing edges= 15 ## ## Vertex attribute names: ## id label vertex.names ## ## Edge attribute names: ## from to weight ``` ] --- # Vizualiziraj mrežu I ```r plot(routes_network, vertex.cex = 3) ``` <img src="10_NET_files/figure-html/unnamed-chunk-22-1.png" style="display: block; margin: auto;" /> --- # Vizualiziraj mrežu II ```r plot(routes_network, vertex.cex = 3, mode = "circle") ``` <img src="10_NET_files/figure-html/unnamed-chunk-23-1.png" style="display: block; margin: auto;" /> --- # Napravi mrežni objekt II ```r detach(package:network) # ukloni paket rm(routes_network) # Ukloni iz radnog prostora library(igraph) # uredi (names mora biti na prvom mjestu) nodes %>% rename(names=label) %>% select(names,id) -> nod # Napravi mrežni objekt routes_igraph <- graph_from_data_frame(d = edges, vertices = nod, directed = TRUE) routes_igraph # Pogledaj objekt ``` ``` ## IGRAPH b9439a0 DNW- 13 15 -- ## + attr: name (v/c), id (v/n), weight (e/n), from (e/n), to (e/n) ## + edges from b9439a0 (vertex names): ## [1] Amsterdam->Bremen Antwerp ->Delft Antwerp ->Haarlem ## [4] Antwerp ->Middelburg Antwerp ->The Hague Dordrecht->Haarlem ## [7] Emden ->Bremen Haarlem ->Bremen Haarlem ->Delft ## [10] Haarlem ->Middelburg Haarlem ->The Hague Hamburg ->Bremen ## [13] Het Vlie ->Bremen Lisse ->Delft Venice ->Haarlem ``` --- # Vizualiziraj mrežu I ```r plot(routes_igraph, edge.arrow.size = 0.2) ``` <img src="10_NET_files/figure-html/unnamed-chunk-25-1.png" style="display: block; margin: auto;" /> --- # Vizualiziraj mrežu II ```r plot(routes_igraph, layout = layout_with_graphopt, edge.arrow.size = 0.2) ``` <img src="10_NET_files/figure-html/unnamed-chunk-26-1.png" style="display: block; margin: auto;" /> --- # Napravi mrežni objekt III ```r library(tidygraph) library(ggraph) # 1. način routes_tidy <- tbl_graph(nodes = nodes, edges = edges, directed = TRUE) # 2.način routes_igraph_tidy <- as_tbl_graph(routes_igraph) # Pregledaj objekte class(routes_tidy) ``` ``` ## [1] "tbl_graph" "igraph" ``` ```r class(routes_igraph_tidy) ``` ``` ## [1] "tbl_graph" "igraph" ``` ```r class(routes_igraph) ``` ``` ## [1] "igraph" ``` --- # Pregledaj objekt <br> <br> .tiny[ ```r routes_tidy ``` ``` ## # A tbl_graph: 13 nodes and 15 edges ## # ## # A directed acyclic simple graph with 1 component ## # ## # Node Data: 13 x 2 (active) ## id label ## <int> <chr> ## 1 1 Antwerp ## 2 2 Haarlem ## 3 3 Dordrecht ## 4 4 Venice ## 5 5 Lisse ## 6 6 Het Vlie ## # ... with 7 more rows ## # ## # Edge Data: 15 x 5 ## from to source destination weight ## <int> <int> <chr> <chr> <int> ## 1 9 13 Amsterdam Bremen 1 ## 2 1 10 Antwerp Delft 68 ## 3 1 2 Antwerp Haarlem 5 ## # ... with 12 more rows ``` ] --- # Manipulacija sa objektom (tidyverse) <br> <br> .tiny[ ```r routes_tidy %>% activate(edges) %>% arrange(desc(weight)) ``` ``` ## # A tbl_graph: 13 nodes and 15 edges ## # ## # A directed acyclic simple graph with 1 component ## # ## # Edge Data: 15 x 5 (active) ## from to source destination weight ## <int> <int> <chr> <chr> <int> ## 1 1 10 Antwerp Delft 68 ## 2 2 10 Haarlem Delft 26 ## 3 1 2 Antwerp Haarlem 5 ## 4 1 11 Antwerp The Hague 2 ## 5 2 13 Haarlem Bremen 2 ## 6 4 2 Venice Haarlem 2 ## # ... with 9 more rows ## # ## # Node Data: 13 x 2 ## id label ## <int> <chr> ## 1 1 Antwerp ## 2 2 Haarlem ## 3 3 Dordrecht ## # ... with 10 more rows ``` ] --- # Vizualiziraj objekt I ```r # Osnovna vizualizacija ggraph(routes_tidy) + geom_edge_link() + geom_node_point() + theme_graph() ``` <img src="10_NET_files/figure-html/unnamed-chunk-30-1.png" style="display: block; margin: auto;" /> --- # Vizualiziraj objekt II ```r # Poboljšana vizualizacija ggraph(routes_tidy, layout = "graphopt") + geom_node_point() + geom_edge_link(aes(width = weight), alpha = 0.8) + scale_edge_width(range = c(0.2, 2)) + geom_node_text(aes(label = label), repel = TRUE) + labs(edge_width = "Letters") + theme_graph() ``` <img src="10_NET_files/figure-html/unnamed-chunk-31-1.png" style="display: block; margin: auto;" /> --- class: inverse, center, middle # INTERAKTIVNE VIZUALIZACIJE <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> --- # Interaktivne vizualizacije I ```r options(htmltools.preserve.raw = FALSE) library(visNetwork) visNetwork(nodes, edges) ```
--- # Interaktivne vizualizacije II ```r options(htmltools.preserve.raw = FALSE) edges <- mutate(edges, width = weight/5 + 1) visNetwork(nodes, edges) %>% visIgraphLayout(layout = "layout_with_fr") %>% visEdges(arrows = "middle") ```
--- # Interaktivne vizualizacije III ```r options(htmltools.preserve.raw = FALSE) library(networkD3) nodes_d3 <- mutate(nodes, id = id - 1) edges_d3 <- mutate(edges, from = from - 1, to = to - 1) forceNetwork(Links = edges_d3, Nodes = nodes_d3, Source = "from", Target = "to", NodeID = "label", Group = "id", Value = "weight", opacity = 1, fontSize = 16, zoom = TRUE) ```
--- # Interaktivne vizualizacije IV ```r options(htmltools.preserve.raw = FALSE) sankeyNetwork(Links = edges_d3, Nodes = nodes_d3, Source = "from", Target = "to", NodeID = "label", Value = "weight", fontSize = 16, unit = "Letter(s)") ```
--- class: inverse, center, middle name: stat # PRAKTIČNI PRIMJER (NETWORK ANALIZA) <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> (Primjer terorističke mreže) --- # Snaga slabih veza <br> <br> <img src="../Foto/net2.png" width="500px" style="display: block; margin: auto;" /> .footnote[[*] Rad M.S. Granovetter-a na [poveznici](http://snap.stanford.edu/class/cs224w-readings/granovetter73weakties.pdf).] --- # Slabe veze <br> <br> <img src="../Foto/net3.png" width="500px" style="display: block; margin: auto;" /> --- # Snažne veze <br> <br> <img src="../Foto/net4.png" width="500px" style="display: block; margin: auto;" /> --- # Teroristički napad u Madridu 2004 <br> <img src="../Foto/madrid.jpg" width="500px" style="display: block; margin: auto;" /> .footnote[ [*] Vidi rad od Jose A. Rodriguez na [poveznici](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.98.4408&rep=rep1&type=pdf)! ] --- # Podatci <br> ```r # Učitaj podatke terrorists <- read_csv("../Podatci/names.csv") # Pogledaj podatke head(terrorists,5) ``` ``` ## # A tibble: 5 x 1 ## name ## <chr> ## 1 Jamal Zougam ## 2 Mohamed Bekkali ## 3 Mohamed Chaoui ## 4 Vinay Kholy ## 5 Suresh Kumar ``` --- # Podatci <br> ```r # Učitaj podatke ties <- read_csv("../Podatci/edges.csv") # Pogledaj podatke head(ties,5) ``` ``` ## # A tibble: 5 x 3 ## x y weight ## <dbl> <dbl> <dbl> ## 1 1 2 1 ## 2 1 3 3 ## 3 1 4 1 ## 4 1 5 1 ## 5 1 6 1 ``` --- # Podatci ```r # Prilagodi podatke terrorists = mutate(terrorists, id = 1:nrow(terrorists)) %>% select(id, everything()) # Napravi mrežu g = graph_from_data_frame(ties, directed = FALSE, vertices = tibble(1:nrow(terrorists))) g # Pregled mreže ``` ``` ## IGRAPH 75ff9a7 UNW- 70 243 -- ## + attr: name (v/c), weight (e/n) ## + edges from 75ff9a7 (vertex names): ## [1] 1--2 1--3 1--4 1--5 1--6 1--7 1--8 1--9 1--11 1--12 1--13 1--14 ## [13] 1--15 1--16 1--18 1--19 1--24 1--25 1--28 1--30 1--31 1--33 1--40 1--41 ## [25] 1--44 1--50 1--57 1--58 1--61 2--3 3--4 3--5 3--7 3--8 3--9 3--11 ## [37] 3--12 3--13 3--14 3--15 3--16 3--18 3--19 3--24 3--25 3--28 3--30 3--31 ## [49] 3--33 3--40 3--41 3--44 3--57 3--58 3--61 4--5 4--19 4--24 4--28 4--30 ## [61] 4--31 4--33 4--41 5--19 5--24 5--28 5--30 5--31 5--33 5--41 6--19 6--20 ## [73] 6--39 6--40 6--44 6--54 7--8 7--10 7--11 7--12 7--13 7--14 7--15 7--16 ## [85] 7--18 7--30 7--40 7--41 7--44 7--50 7--53 7--56 7--57 7--58 7--59 7--61 ## + ... omitted several edges ``` --- # Podatci <br> <br> ```r # Pregledaj čvorove V(g) ``` ``` ## + 70/70 vertices, named, from 75ff9a7: ## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ## [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 ## [51] 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 ``` ```r vcount(g) # Broj čvorova ``` ``` ## [1] 70 ``` --- # Podatci ```r # Pregledaj poveznice E(g) ``` ``` ## + 243/243 edges from 75ff9a7 (vertex names): ## [1] 1 --2 1 --3 1 --4 1 --5 1 --6 1 --7 1 --8 1 --9 1 --11 1 --12 ## [11] 1 --13 1 --14 1 --15 1 --16 1 --18 1 --19 1 --24 1 --25 1 --28 1 --30 ## [21] 1 --31 1 --33 1 --40 1 --41 1 --44 1 --50 1 --57 1 --58 1 --61 2 --3 ## [31] 3 --4 3 --5 3 --7 3 --8 3 --9 3 --11 3 --12 3 --13 3 --14 3 --15 ## [41] 3 --16 3 --18 3 --19 3 --24 3 --25 3 --28 3 --30 3 --31 3 --33 3 --40 ## [51] 3 --41 3 --44 3 --57 3 --58 3 --61 4 --5 4 --19 4 --24 4 --28 4 --30 ## [61] 4 --31 4 --33 4 --41 5 --19 5 --24 5 --28 5 --30 5 --31 5 --33 5 --41 ## [71] 6 --19 6 --20 6 --39 6 --40 6 --44 6 --54 7 --8 7 --10 7 --11 7 --12 ## [81] 7 --13 7 --14 7 --15 7 --16 7 --18 7 --30 7 --40 7 --41 7 --44 7 --50 ## [91] 7 --53 7 --56 7 --57 7 --58 7 --59 7 --61 8 --9 8 --10 8 --25 9 --25 ## + ... omitted several edges ``` ```r ecount(g) # Broj poveznica ``` ``` ## [1] 243 ``` --- # Podatci ```r # Doddaj naziv g$name <- "Madrid network" g$name ``` ``` ## [1] "Madrid network" ``` ```r g ``` ``` ## IGRAPH 75ff9a7 UNW- 70 243 -- Madrid network ## + attr: name (g/c), name (v/c), weight (e/n) ## + edges from 75ff9a7 (vertex names): ## [1] 1--2 1--3 1--4 1--5 1--6 1--7 1--8 1--9 1--11 1--12 1--13 1--14 ## [13] 1--15 1--16 1--18 1--19 1--24 1--25 1--28 1--30 1--31 1--33 1--40 1--41 ## [25] 1--44 1--50 1--57 1--58 1--61 2--3 3--4 3--5 3--7 3--8 3--9 3--11 ## [37] 3--12 3--13 3--14 3--15 3--16 3--18 3--19 3--24 3--25 3--28 3--30 3--31 ## [49] 3--33 3--40 3--41 3--44 3--57 3--58 3--61 4--5 4--19 4--24 4--28 4--30 ## [61] 4--31 4--33 4--41 5--19 5--24 5--28 5--30 5--31 5--33 5--41 6--19 6--20 ## [73] 6--39 6--40 6--44 6--54 7--8 7--10 7--11 7--12 7--13 7--14 7--15 7--16 ## [85] 7--18 7--30 7--40 7--41 7--44 7--50 7--53 7--56 7--57 7--58 7--59 7--61 ## + ... omitted several edges ``` --- # Podatci <br> <br> ```r E(g)$weight # Pogledaj težinu čvorova ``` ``` ## [1] 1 3 1 1 1 4 1 1 4 1 1 2 2 2 2 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 1 1 3 1 1 2 1 ## [38] 1 1 1 1 2 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ## [75] 1 1 1 1 3 1 1 1 2 2 3 1 1 1 1 1 1 1 2 2 1 1 1 2 1 1 1 1 1 2 1 2 1 1 1 1 1 ## [112] 1 1 1 2 1 1 1 1 1 1 1 2 2 1 1 3 2 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ## [149] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ## [186] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ## [223] 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ``` --- # Vizualiziraj mrežu ```r ggraph(g, layout = "with_kk") + geom_edge_link(aes(alpha = weight)) + geom_node_point() ``` <img src="10_NET_files/figure-html/unnamed-chunk-48-1.png" style="display: block; margin: auto;" /> --- # Mjere centraliteta <br> <br> - Koji su najvažniji čvorovi u mreži? <br> - npr. najbitnije web stranice o odrđenoj temi,utjecajni akademski članci ili knjige, tajna društva (masoni) <br> - stupanj, težina, međusobnost --- # Izračunaj stupanj čvorova <br> <br> ```r degree(g) ``` ``` ## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ## 29 2 27 10 10 7 22 6 4 2 18 10 10 10 11 11 0 16 15 6 8 5 3 16 8 0 ## 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 ## 4 11 0 12 14 3 12 5 5 5 5 4 2 11 17 1 1 10 1 0 0 0 1 4 1 1 ## 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 ## 1 2 4 6 8 10 2 2 13 1 11 6 6 6 6 2 2 2 ``` --- # Izračunaj snagu čvorova <br> <br> ```r strength(g) ``` ``` ## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ## 43 2 34 10 10 7 35 7 4 3 27 10 12 14 19 17 0 21 15 6 8 5 3 16 8 0 ## 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 ## 4 11 0 12 14 3 12 5 5 5 5 4 2 12 19 1 1 10 1 0 0 0 1 4 1 1 ## 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 ## 1 2 5 7 9 11 2 2 14 1 11 6 6 6 6 2 2 2 ``` --- # Međusobnost (betweenness) <img src="../Foto/net1.png" width="500px" style="display: block; margin: auto;" /> --- # Vizualizacija međusobnosti ```r dist_weight = 1 / E(g)$weight g$betweenness <- edge_betweenness(g, weights = dist_weight) ggraph(g, layout = "with_kk") + geom_edge_link(aes(alpha = g$betweenness)) + geom_node_point() ``` <img src="10_NET_files/figure-html/unnamed-chunk-52-1.png" style="display: block; margin: auto;" /> --- # Najpovezaniji teroristi I ```r terrorists <- terrorists %>% mutate(degree = degree(g), strength = strength(g)) arrange(terrorists, desc(degree)) ``` ``` ## # A tibble: 70 x 4 ## id name degree strength ## <int> <chr> <dbl> <dbl> ## 1 1 Jamal Zougam 29 43 ## 2 3 Mohamed Chaoui 27 34 ## 3 7 Imad Eddin Barakat 22 35 ## 4 11 Amer Azizi 18 27 ## 5 41 Said Berrak 17 19 ## 6 18 Galeb Kalaje 16 21 ## 7 24 Naima Oulad Akcha 16 16 ## 8 19 Abderrahim Zbakh 15 15 ## 9 31 Jamal Ahmidan 14 14 ## 10 61 Mohamed El Egipcio 13 14 ## # ... with 60 more rows ``` --- # Najpovezaniji teroristi II ```r arrange(terrorists, desc(strength)) ``` ``` ## # A tibble: 70 x 4 ## id name degree strength ## <int> <chr> <dbl> <dbl> ## 1 1 Jamal Zougam 29 43 ## 2 7 Imad Eddin Barakat 22 35 ## 3 3 Mohamed Chaoui 27 34 ## 4 11 Amer Azizi 18 27 ## 5 18 Galeb Kalaje 16 21 ## 6 15 Mohamed Belfatmi 11 19 ## 7 41 Said Berrak 17 19 ## 8 16 Said Bahaji 11 17 ## 9 24 Naima Oulad Akcha 16 16 ## 10 19 Abderrahim Zbakh 15 15 ## # ... with 60 more rows ``` --- # Izolirani teroristi ```r filter(terrorists,degree == 0) ``` ``` ## # A tibble: 6 x 4 ## id name degree strength ## <int> <chr> <dbl> <dbl> ## 1 17 "Al\xed Amrous" 0 0 ## 2 26 "Abdelhalak Bentasser" 0 0 ## 3 29 "Faisal Alluch" 0 0 ## 4 46 "Mohamad Bard Ddin Akkab" 0 0 ## 5 47 "Abu Zubaidah" 0 0 ## 6 48 "Sanel Sjekirika" 0 0 ``` --- # Slični parovi terorista ```r ties <- ties %>% rename(from = x, to = y) # prilagodi nazive ggplot(ties, aes(x = from, y = to, color = as.factor(weight))) + geom_point() + labs(color = "weigth") ``` <img src="10_NET_files/figure-html/unnamed-chunk-56-1.png" style="display: block; margin: auto;" /> --- # Slični parovi terorista ```r # Dodaj recipročne veze ties2 = mutate(ties, temp = to, to = from, from = temp) %>% select(-temp) ties2 = rbind(ties, ties2) ggplot(ties2, aes(x = from, y = to, color = as.factor(weight))) + geom_point() + labs(color = "weigth") ``` <img src="10_NET_files/figure-html/unnamed-chunk-57-1.png" style="display: block; margin: auto;" /> --- # Slični parovi terorista ```r ##Pripremi podatke## # Ukloni izolirane teroriste isolated = filter(terrorists, degree == 0)$id g = delete_vertices(g, isolated) # Napravi adjecany matricu A = as_adjacency_matrix(g, attr = "weight", sparse = FALSE) # Sličnost kao Pearsonova korelacija S = cor(A) # Ukloni sličnost sa samim sobom S = S + diag(-1, nrow(A)) # Deskriptiva summary(c(S)) ``` ``` ## Min. 1st Qu. Median Mean 3rd Qu. Max. ## -0.27574 -0.07729 -0.03336 0.07074 0.17037 1.00000 ``` --- # Slični parovi terorista ```r # Napravi adjecancy matricu h <- graph_from_adjacency_matrix(S, mode = "undirected", weighted = TRUE) # Pretvori u data.frame(tibble) sim = as.tibble(as_data_frame(h, what = "edges")) %>% mutate(from = as.integer(from), to = as.integer(to)) %>% arrange(desc(weight)) # join sa podatkovnim skupom terrorists sim_joint <- sim %>% left_join(terrorists, c("from" = "id")) %>% left_join(terrorists, c("to" = "id")) %>% filter(degree.x >= 10, degree.y >= 10) %>% select(-strength.x, -strength.y) ``` --- # Najsličniji parovi <br> ```r head(sim_joint) ``` ``` ## # A tibble: 6 x 7 ## from to weight name.x degree.x name.y degree.y ## <int> <int> <dbl> <chr> <dbl> <chr> <dbl> ## 1 13 14 0.906 Mohamed Atta 10 Ramzi Binalshibh 10 ## 2 4 5 0.881 Vinay Kholy 10 Suresh Kumar 10 ## 3 12 58 0.855 Abu Musad Alsakaoui 10 Shakur 10 ## 4 12 13 0.847 Abu Musad Alsakaoui 10 Mohamed Atta 10 ## 5 12 15 0.836 Abu Musad Alsakaoui 10 Mohamed Belfatmi 11 ## 6 4 28 0.831 Vinay Kholy 10 Basel Ghayoun 11 ``` --- # Najmanje slični parovi <br> ```r tail(sim_joint) ``` ``` ## # A tibble: 6 x 7 ## from to weight name.x degree.x name.y degree.y ## <int> <int> <dbl> <chr> <dbl> <chr> <dbl> ## 1 16 63 -0.188 Said Bahaji 11 Semaan Gaby Eid 11 ## 2 1 63 -0.194 Jamal Zougam 29 Semaan Gaby Eid 11 ## 3 12 63 -0.196 Abu Musad Alsakaoui 10 Semaan Gaby Eid 11 ## 4 40 63 -0.199 Abdeluahid Berrak 11 Semaan Gaby Eid 11 ## 5 3 63 -0.219 Mohamed Chaoui 27 Semaan Gaby Eid 11 ## 6 7 63 -0.226 Imad Eddin Barakat 22 Semaan Gaby Eid 11 ``` --- # Vizualiziraj sličnost ```r h2 <- graph_from_data_frame(filter(sim_joint, weight >= 0.60), directed = FALSE, vertices = terrorists) h2 <- delete_vertices(h2, which(degree(h2) == 0)) ggraph(h2) + geom_edge_link(aes(alpha = weight)) + geom_node_point() + theme_graph() ``` <img src="10_NET_files/figure-html/unnamed-chunk-62-1.png" style="display: block; margin: auto;" /> --- # Vizualiziraj sličnost ```r # Dodaj snagu čvorova ggraph(h2) + geom_edge_link(aes(alpha = weight)) + geom_node_point(aes(size = strength)) + theme_graph() ``` <img src="10_NET_files/figure-html/unnamed-chunk-63-1.png" style="display: block; margin: auto;" /> --- # Vizualiziraj sličnost ```r # Dodaj nazive čvorova ggraph(h2) + geom_edge_link(aes(alpha = weight)) + geom_node_point() + geom_node_text(aes(label = name), repel=T) + theme_graph() ``` <img src="10_NET_files/figure-html/unnamed-chunk-64-1.png" style="display: block; margin: auto;" /> --- # Vizualiziraj sličnost ```r V(h2)$betw = betweenness(h2) # dodaj međusobnost u mrežu ggraph(h2) + geom_edge_link(aes(alpha = weight)) + geom_node_point(aes(size = betw)) + theme_graph() ``` <img src="10_NET_files/figure-html/unnamed-chunk-65-1.png" style="display: block; margin: auto;" /> --- class: inverse, center, middle # Hvala na pažnji <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> (Zadnje predavanje!)