class: center, middle, inverse, title-slide # Intro to Network Analysis with R ## R-Ladies St. Louis ### Maria C. Ramos ### 2020/11/11 --- class: center, middle # Intro & Overview --- # Network Analysis -- + Map and understand complex structures & systems -- + Relational methods -- + Connections, structures & positions -- .center[ ##<br><br>_**Entities**_ and their _**relations**_ ] --- class: inverse, center, middle # Some Applications --- COVID-19  Goodreau SM, Pollock ED, Birnbaum JK, Hamilton DT, Morris M, on behalf of the Statnet Development Team. 2020. Can’t I please just visit one friend?: Visualizing social distancing networks in the era of COVID-19. http://statnet.org/COVID-JustOneFriend/] --- Political Polarization  <br>Moody, J., & Mucha, P. J. (2013). Portrait of political party polarization. Network Science, 1(1), 119-121. --- Connectome<br><br>  <br><br>Wu, G. R., Stramaglia, S., Chen, H., Liao, W., & Marinazzo, D. (2013). Mapping the voxel-wise effective connectome in resting state fMRI. PloS one, 8(9), e73670. --- Food Web<br><br>  <br><br>Ahn, Y. Y., Ahnert, S. E., Bagrow, J. P., & Barabasi, A. L. (2011). Flavor network and the principles of food pairing. Sci Rep, 1, 196. --- class: center, top # Today's Workshop .center[ <img src="Intro_Nets_slides_files/figure-html/unnamed-chunk-4-1.png" width="504" /> ] --- class: center, middle # Network Elements --- # Basic Network Elements - Vertices (nodes) .center[ <img src="Intro_Nets_slides_files/figure-html/unnamed-chunk-6-1.png" width="504" /> ] --- # Basic Network Elements - Edges (ties) .center[ <img src="Intro_Nets_slides_files/figure-html/unnamed-chunk-7-1.png" width="504" /> ] --- # Basic Network Elements ## Direction .pull-left[ - Directed <img src="Intro_Nets_slides_files/figure-html/unnamed-chunk-8-1.png" width="504" /> ] .pull-right[ - Undirected <img src="Intro_Nets_slides_files/figure-html/unnamed-chunk-9-1.png" width="504" /> ] --- # Basic Network Elements ## Weights .pull-left[ - Unweighted <img src="Intro_Nets_slides_files/figure-html/unnamed-chunk-10-1.png" width="504" /> ] .pull-right[ - Weighted <img src="Intro_Nets_slides_files/figure-html/unnamed-chunk-11-1.png" width="504" /> ] --- class: inverse, center, middle # Let's take a look at our network --- class: inverse, center, top # Your turn! .left[ + Explore the karate network + How many nodes does the network have? + Is the network directed or indirected? + What vertex and edge attributes does the network have? ]
02
:
00
--- ```r data(karate) karate ``` ``` *## IGRAPH 4b458a1 UNW- 34 78 -- Zachary's karate club network *## + attr: name (g/c), Citation (g/c), Author (g/c), Faction (v/n), name *## | (v/c), label (v/c), color (v/n), weight (e/n) ## + edges from 4b458a1 (vertex names): ## [1] Mr Hi --Actor 2 Mr Hi --Actor 3 Mr Hi --Actor 4 Mr Hi --Actor 5 ## [5] Mr Hi --Actor 6 Mr Hi --Actor 7 Mr Hi --Actor 8 Mr Hi --Actor 9 ## [9] Mr Hi --Actor 11 Mr Hi --Actor 12 Mr Hi --Actor 13 Mr Hi --Actor 14 ## [13] Mr Hi --Actor 18 Mr Hi --Actor 20 Mr Hi --Actor 22 Mr Hi --Actor 32 ## [17] Actor 2--Actor 3 Actor 2--Actor 4 Actor 2--Actor 8 Actor 2--Actor 14 ## [21] Actor 2--Actor 18 Actor 2--Actor 20 Actor 2--Actor 22 Actor 2--Actor 31 ## [25] Actor 3--Actor 4 Actor 3--Actor 8 Actor 3--Actor 9 Actor 3--Actor 10 ## + ... omitted several edges ``` --- class: center, middle # Closer inspection --- class: inverse, center, top # Your turn! .left[ Replace every [xxx] in the code to answer each question. + How many factions does the karate network have? + How many nodes belong to each faction? ]
02
:
00
--- class: center, middle # Network Visualization --- # Basics -- + Via algorithms -- + Reduce the crossing of edges -- + Keep the edge length as constant as possible --- # Plotting Parameters **Nodes** vertex.[parameter] (e.g., vertex.color =) <table> <thead> <tr> <th style="text-align:left;"> node_parameter </th> <th style="text-align:left;"> purpose_options </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> color </td> <td style="text-align:left;"> node color </td> </tr> <tr> <td style="text-align:left;"> shape </td> <td style="text-align:left;"> circle, square, rectangle, sphere, pie, raster, etc. </td> </tr> <tr> <td style="text-align:left;"> size </td> <td style="text-align:left;"> default = 15 </td> </tr> <tr> <td style="text-align:left;"> label </td> <td style="text-align:left;"> vector with labels </td> </tr> <tr> <td style="text-align:left;"> label family </td> <td style="text-align:left;"> font family of the label: Helvetica, Times </td> </tr> <tr> <td style="text-align:left;"> label font </td> <td style="text-align:left;"> 1= plain, 2= bold, 3= italic, 4 = bold italic </td> </tr> <tr> <td style="text-align:left;"> label cex </td> <td style="text-align:left;"> font size </td> </tr> <tr> <td style="text-align:left;"> label distance </td> <td style="text-align:left;"> distance between node and label </td> </tr> <tr> <td style="text-align:left;"> </td> <td style="text-align:left;"> </td> </tr> </tbody> </table> --- # Plotting Parameters **Edges** edge.[parameter] (e.g., edge.color =) <table> <thead> <tr> <th style="text-align:left;"> edge_parameter </th> <th style="text-align:left;"> purpose_options </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> color </td> <td style="text-align:left;"> edge color </td> </tr> <tr> <td style="text-align:left;"> lty </td> <td style="text-align:left;"> type of line: 0='blank', 1= 'solid', 2= 'dashed', 3= 'dotted', 4 = 'dotdash', 5= 'longdash', 6= 'twodash' </td> </tr> <tr> <td style="text-align:left;"> width </td> <td style="text-align:left;"> default = 1 </td> </tr> <tr> <td style="text-align:left;"> label </td> <td style="text-align:left;"> vector with labels </td> </tr> <tr> <td style="text-align:left;"> label family </td> <td style="text-align:left;"> font family of the label: Helvetica, Times </td> </tr> <tr> <td style="text-align:left;"> label font </td> <td style="text-align:left;"> 1= plain, 2= bold, 3= italic, 4 = bold italic </td> </tr> <tr> <td style="text-align:left;"> label cex </td> <td style="text-align:left;"> font size </td> </tr> <tr> <td style="text-align:left;"> curvature </td> <td style="text-align:left;"> edge curvature - range [0,1] </td> </tr> <tr> <td style="text-align:left;"> arrow size </td> <td style="text-align:left;"> default = 1 </td> </tr> </tbody> </table> --- # Two Options .pull-left[Option 1: Setting visual properties outside the plot function + 1. Set vertex/edge attributes + 2. Plot the net ```r # vertex properties *V(UKfaculty)$color <- "dodgerblue" # vertex label properties V(UKfaculty)$label.color <- "white" V(UKfaculty)$label.cex <- 0.5 # edge properties E(UKfaculty)$color <- "gray" *plot(UKfaculty) ``` ] .pull-right[Option 2 Setting visual properties inside the plot function + Use the function arguments ```r *plot(UKfaculty, # === vertex properties * vertex.color = "dodgerblue", vertex.frame.color = "dodgerblue", vertex.size = 9, # === vertex label properties vertex.label.cex = 0.5, vertex.label.color = "white", vertex.label.family = "Helvetica", # === edge properties edge.color = "gray", edge.width = 0.5, edge.arrow.size = 0.2) ``` ] --- #Which option to choose? .pull-left[Option 1: Setting visual properties outside the plot function + 1. Set vertex/edge attributes + 2. Plot the net <br><br>_**Use for assigning local visual properties to different subsets**_ ] .pull-right[Option 2 Setting visual properties inside the plot function + Use the function arguments <br><br><br>_**Use for assigning global visual properties**_ ] -- .center[<br><br><br>_**Most often combine the two**_] --- class: inverse, center, middle # Let's try these! --- class: inverse, center, top # Your turn! .left[ Create your own plot of the karate data. Try changing different parameters. + Use the parameter cheatsheet and color cheatsheet Reach goal: color different subsets of vertices and edges in a different way using the selection tools we learned in the previous section. ]
10
:
00
--- class: center, middle # Descriptive Network Statistics --- # Descriptive Network Statistics -- ## Density Proportion of actual edges out of possible edges. -- ## Reciprocity Two types -- + Number of reciprocal edges over the total number of edges -- + Number of reciprocal edges over the number of dyads with only one, unreciprocated edge -- .center[**Indication of connectivity**] --- # Descriptive Network Statistics ## Dyad Census -- Dyads are pairs of vertices. In directed graphs they can be: -- + Null: no edge between the pair. -- + Asymmetric: one directed edge. -- + Mutual: two directed edges. -- .center[**Indication of hierarchical structure**] --- # Descriptive Network Statistics ## Transitivity -- Also known as clustering coefficient -- Fraction of transitive triplets -- .pull-left[ **Transitive triplet** <img src="Intro_Nets_slides_files/figure-html/unnamed-chunk-20-1.png" width="504" /> ] .pull-right[ **Intransitive triplet** <img src="Intro_Nets_slides_files/figure-html/unnamed-chunk-21-1.png" width="504" /> ] --- # Descriptive Network Statistics ## Reachability - Average path length .center[ <img src="Intro_Nets_slides_files/figure-html/unnamed-chunk-22-1.png" width="504" /> ] --- # Descriptive Network Statistics ### Diameter Distance (number of steps) between farthest vertices <img src="Intro_Nets_slides_files/figure-html/unnamed-chunk-23-1.png" width="504" /> --- # Descriptive Network Statistics ### Connected components .pull-left[ **Strongly connected** <img src="Intro_Nets_slides_files/figure-html/unnamed-chunk-24-1.png" width="504" /> ] .pull-right[ **Weakly connected** <img src="Intro_Nets_slides_files/figure-html/unnamed-chunk-25-1.png" width="504" /> ] --- # Descriptive Network Statistics ### Articulation points Nodes that if removed would break the network into more components. <img src="Intro_Nets_slides_files/figure-html/unnamed-chunk-26-1.png" width="504" /> --- class: inverse, center, middle # Let's try these! --- class: center, top # Your turn! .left[ Obtain the following descriptive statistics for the karate data: ] <table> <thead> <tr> <th style="text-align:left;"> measures </th> <th style="text-align:left;"> functions </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> density </td> <td style="text-align:left;"> graph.density(net) </td> </tr> <tr> <td style="text-align:left;"> reciprocity </td> <td style="text-align:left;"> reciprocity(net) </td> </tr> <tr> <td style="text-align:left;"> dyad census </td> <td style="text-align:left;"> dyad_census(net) </td> </tr> <tr> <td style="text-align:left;"> transitivity </td> <td style="text-align:left;"> transiticity(net) </td> </tr> <tr> <td style="text-align:left;"> average path length </td> <td style="text-align:left;"> average.path.length(net) </td> </tr> <tr> <td style="text-align:left;"> diameter </td> <td style="text-align:left;"> diameter(net) </td> </tr> <tr> <td style="text-align:left;"> articulation points </td> <td style="text-align:left;"> articulation.points(net) </td> </tr> </tbody> </table> .center[What can you say about this network?]
05
:
00
--- class: center, middle # Centrality --- # Centrality ### Degree Centrality .pull-left[ **Indegree** <img src="Intro_Nets_slides_files/figure-html/unnamed-chunk-29-1.png" width="504" /> ] .pull-right[ **Outdegree** <img src="Intro_Nets_slides_files/figure-html/unnamed-chunk-30-1.png" width="504" /> ] --- # Centrality ### Closeness Centrality -- + Based on the distance to all other nodes. -- + Inverse of the node’s average geodesic distance to others in the network. -- ### Betweenness Centrality -- + Based on its brokerage position. -- + Number of shortest that pass through the node. -- .center[<br><br>**Tease out local versus global patterns**] --- # Centrality .center[ <img src="Intro_Nets_slides_files/figure-html/unnamed-chunk-31-1.png" width="504" /> ] --- class: inverse, center, middle # Let's try these! --- class: inverse, center, top # Your turn! .left[ Calculate degree, closeness, and betweenness centrality for the karate network. What are the most central nodes according to different measures? ]
05
:
00
--- class: center, middle # Community Detection --- # Community Detection -- .center[<br> **Goal: find cohesive groups**] -- + Different ways of doing this -- + Modularity: more intra-community edges than one would expect at random -- + Algorithms try to maximize modularity -- + Most of these procedures are done with undirected & unweighted data -- <br><br>.center[**Always check substantively**] --- class: inverse, center, middle # Let's try these! --- class: inverse, center, top # Your turn! .left[ Conduct a community detection procedure for the karate data? What are the communities you find? Plot your results ]
05
:
00
--- class: center, middle # Where to go from here? --- # Key Questions -- ## Networks as independent variables -- Non-network outcome: -- - Are people whose friends suffer from some disease more likely to have the disease themselves? -- - Do central actors have access to more resources? -- ## Networks as dependent variables -- Network outcome -- - How structurally cohesive is a given network? -- - What processes generate certain network structures? --- # Mechanism of Action -- ## Connectionist -- Networks as pipes -- Outcome results from what flows in the network: -- - Spread of a virus -- - Sharing of information -- ## Positional -- Outcome results from the positions & roles that nodes occupy: -- - Popularity effects -- - Network embeddedness -- --- # More Resources + Kolaczyk, E. D., & Csárdi, G. (2014). Statistical analysis of network data with R (Vol. 65): Springer. + Luke, D. (2015). A User’s Guide to Network Analysis in R. Cham: Springer International Publishing : Imprint: Springer. + [Duke Network Analysis Center Tutorials](https://https://https://dnac.ssri.duke.edu/) + [Katya Ognyanova Tutorials](https://https://kateto.net/) --- class: center, middle # Thank you! .center[Slides created via the R package [**xaringan**](https://github.com/yihui/xaringan).] .center[<br><br><br>[mariacramos.com](https://https://mariacramos.com/) <br><br>.large[@mariacramosf] ]