Assignment7

assignment for political and social network analysis

Zoe Bean
3/23/2022
library(tidyverse)
library(GGally)

source("./Import Scripts/Game of Thrones Interactions.R")

The Network

This is an edgelist dataset. It has 298 vertices and 9131 edges, it is not directed or bipartite, and it is weighted. The vertices are the people, with attributes regarding the time and location of their appearances, as well as the point of view the story is in for these appearances.

The dataset is the Game of Thrones Interactions dataset, with characters as nodes, and instances of characters being mentioned in the same vicinity are the edges.

Here is the network plots that I have managed to create with this data so far:

V(network_igraph)$size<-5
V(network_igraph)$label.cex = 1/2
plot(network_igraph, layout_with_lgl(network_igraph), vertex.label=NA)
#a subset of the data with a lot of weight on the edges- meaning more interactions between the characters
#weight chosen by rerunning until the plot is somewhat readable 
sub_stat<-get.inducedSubgraph(network_statnet,eid=which(network_statnet%e%'weight'>10))
plot(sub_stat)
#create the same igrpah subset
sub_igraph <- subgraph.edges(network_igraph, E(network_igraph)[E(network_igraph)$weight>10], del=F)
sub_igraph <- delete_vertices(sub_igraph, degree(sub_igraph, mode = "in")==0)

#format igrpah subset to be more readable
V(sub_igraph)$size<-5
V(sub_igraph)$label.cex = 1/2
#plot igrpah
plot(sub_igraph, layout=layout_nicely)

Community Clusters

I know that this is a big mess, but I couldn’t figure out how to make a subset of the network_nodes variable such that it has the same nodes as the above subgraphs. Which means that it is very dfficult to actually see and understand the created communites. So this is the code I have for now… I’ll hold off on analysis until I can figure out the nodes thing

#function to summarize the node statistics by community group
nodes.by.gp<-function(network.nodes, groupvar){
  network.nodes%>%
  select(-Name)%>%
  group_by_(groupvar) %>%
  mutate(n=n())%>%
  summarise_all(mean, na.rm=TRUE)%>%
  as.matrix()%>%
  print(digits=2)
}

Fast and Greedy Detection

#Run clustering algorithm: fast_greedy
got.fg<-cluster_fast_greedy(network_igraph)

#to get an idea of who is in what group
igraph::groups(network_igraph)
list()
#blockmodel with community membership
print(blockmodel(network_statnet,got.fg$membership)$block.model, digits=2)
        Block 1 Block 2 Block 3 Block 4
Block 1   0.303   0.090    0.13   0.055
Block 2   0.090   0.500    0.20   0.051
Block 3   0.134   0.203    0.41   0.117
Block 4   0.055   0.051    0.12   0.500
#add community membership as a node attribute
network_nodes$got.fg<-got.fg$membership

#summarize node statistics by community
nodes.by.gp(network_nodes,"got.fg")
     got.fg Appear   pov majorpov major   n
[1,]      1   12.7 0.118    0.094  0.14 127
[2,]      2   14.0 0.153    0.119  0.19  59
[3,]      3   15.0 0.071    0.071  0.13  99
[4,]      4    5.4 0.000    0.000  0.00  13
#plot network with community coloringy
plot(got.fg,network_igraph, layout=layout_with_graphopt, max.sa.movement=10 )

Walktrap Detection

#Run clustering algorithm: walktrap
got.wt<-walktrap.community(network_igraph,steps=55)

#to get an idea of who is in what group
igraph::groups(got.wt)
$`1`
 [1] "Aegon Frey (Jinglebell)" "Aegon Targaryen"        
 [3] "Allar Deem"              "Barra"                  
 [5] "Benjen Stark"            "Beric Dondarrion"       
 [7] "Bronn"                   "Cersei Lannister"       
 [9] "Chiswyck"                "Colemon"                
[11] "Cortnay Penrose"         "Dick Crabb"             
[13] "Dontos Hollard"          "Eddard Karstark"        
[15] "Edmure Tully"            "Emmon Frey"             
[17] "Garlan Tyrell"           "Gerris Drinkwater"      
[19] "Gladden Wylde"           "Gregor Clegane"         
[21] "Guyard Morrigen"         "Harlan Hunter"          
[23] "Hizdahr zo Loraq"        "Hugh"                   
[25] "Illyrio Mopatis"         "Ilyn Payne"             
[27] "Jaqen H'ghar"            "Jeyne Westerling"       
[29] "Kindly Man"              "Lollys Stokeworth"      
[31] "Lommy Greenhands"        "Walder Frey"            
[33] "Lothar Mallery"          "Lothor Brune"           
[35] "Lucas Blackwood"         "Lymond Vikary"          
[37] "Mace Tyrell"             "Meryn Trant"            
[39] "Moqorro"                 "Mycah"                  
[41] "Nan"                     "Nymeria"                
[43] "Osha"                    "Osmund Kettleblack"     
[45] "Pate"                    "Penny"                  
[47] "Polliver"                "Raymun Darry"           
[49] "Raymund Frey"            "Rickard Karstark"       
[51] "Sandor Clegane"          "Satin"                  
[53] "Stiv"                    "Styr"                   
[55] "Tickler"                 "Vardis Egen"            
[57] "Varly"                   "Oppo"                   
[59] "Yurkhaz zo Yunzak"      

$`2`
  [1] "Addam Marbrand"       "Aemon Targaryen"     
  [3] "Alester Florent"      "Alfyn"               
  [5] "Amory Lorch"          "Areo Hotah"          
  [7] "Arthur Dayne"         "Arya Stark"          
  [9] "Asha Greyjoy"         "Balon Greyjoy"       
 [11] "Bowen Marsh"          "Bran Stark"          
 [13] "Brandon Stark"        "Catelyn Tully"       
 [15] "Chayle"               "Coldhands"           
 [17] "Craster"              "Dareon"              
 [19] "Daryn Hornwood"       "Davos Seaworth"      
 [21] "Doran Martell"        "Khal Drogo"          
 [23] "Dywen"                "Edric Storm"         
 [25] "Emmon Cuy"            "Eon Hunter"          
 [27] "Garse Goodbrook"      "Gilly"               
 [29] "Haldon"               "Harwin"              
 [31] "Helman Tallhart"      "Hodor"               
 [33] "Hoster Tully"         "Janos Slynt"         
 [35] "Jared Frey"           "Joffrey Baratheon"   
 [37] "Jojen Reed"           "Jon Arryn"           
 [39] "Jon Connington"       "Jon Snow"            
 [41] "Jory Cassel"          "Lark"                
 [43] "Loras Tyrell"         "Luton"               
 [45] "Luwin"                "Lyanna Stark"        
 [47] "Aemon"                "Cressen"             
 [49] "Mag Mar Tun Doh Weg"  "Mance Rayder"        
 [51] "Merrett Frey"         "Mordane"             
 [53] "Myrcella Baratheon"   "Olenna Redwyne"      
 [55] "Ollo Lophand"         "Orton Merryweather"  
 [57] "Osney Kettleblack"    "Paul"                
 [59] "Podrick Payne"        "Pypar"               
 [61] "Quentyn Martell"      "Qyburn"              
 [63] "Ralf Kenning"         "Ramsay Snow"         
 [65] "Randyll Tarly"        "Rattleshirt"         
 [67] "Rhaegar Frey"         "Rickon Stark"        
 [69] "Robar Royce"          "Rodrik Cassel"       
 [71] "Roose Bolton"         "Rorge"               
 [73] "Ryman Frey"           "Salladhor Saan"      
 [75] "Samwell Tarly"        "Sansa Stark"         
 [77] "Sawane Botley"        "Selyse Florent"      
 [79] "Senelle"              "Shireen Baratheon"   
 [81] "Softfoot"             "Symond Frey"         
 [83] "Theon Greyjoy"        "Thoros of Myr"       
 [85] "Timeon"               "Timett"              
 [87] "Tion Frey"            "Tytos Frey"          
 [89] "Tywin Lannister"      "Vargo Hoat"          
 [91] "Vayon Poole"          "Viserys Targaryen"   
 [93] "Walder Frey (Little)" "Waymar Royce"        
 [95] "Willem Lannister"     "Wyman Manderly"      
 [97] "Ygritte"              "Yohn Royce"          
 [99] "Yoren"                "Dagon Codd"          
[101] "Adrack Humble"        "the Others"          

$`3`
 [1] "Aegon I Targaryen"              
 [2] "Aron Santagar"                  
 [3] "Arys Oakheart"                  
 [4] "Balman Byrch"                   
 [5] "Balon Swann"                    
 [6] "Belwas"                         
 [7] "Ben Plumm"                      
 [8] "Boros Blount"                   
 [9] "Brienne of Tarth"               
[10] "Daario Naharis"                 
[11] "Elia Martell"                   
[12] "Groleo"                         
[13] "Guncer Sunglass"                
[14] "Hubard Rambton"                 
[15] "Irri"                           
[16] "Jhiqui"                         
[17] "Jhogo"                          
[18] "Kevan Lannister"                
[19] "Kraznys mo Nakloz"              
[20] "Lancel Lannister"               
[21] "Mero"                           
[22] "Mirri Maz Duur"                 
[23] "Missandei"                      
[24] "Oznak zo Pahl"                  
[25] "Paxter Redwyne"                 
[26] "Petyr Baelish"                  
[27] "Pycelle"                        
[28] "Qotho"                          
[29] "Quaithe"                        
[30] "Quaro"                          
[31] "Renly Baratheon"                
[32] "Reznak mo Reznak"               
[33] "Rhaegar Targaryen"              
[34] "Rhaego"                         
[35] "Rhaenys Targaryen"              
[36] "Robert Arryn"                   
[37] "Skahaz mo Kandaq"               
[38] "Tommen Baratheon"               
[39] "Tyrion Lannister"               
[40] "Tysha"                          
[41] "Willas Tyrell"                  
[42] "First High Septon (the fat one)"
[43] "Khrazz"                         

$`4`
 [1] "Aeron Greyjoy"        "Antario Jast"        
 [3] "Arnolf Karstark"      "Aurane Waters"       
 [5] "Axell Florent"        "Biter"               
 [7] "Chett"                "Cleos Frey"          
 [9] "Cotter Pyke"          "Cragorn"             
[11] "Dacey Mormont"        "Daenerys Targaryen"  
[13] "Dalla"                "Dirk"                
[15] "Donal Noye"           "Ebben"               
[17] "Eddard Stark"         "Eddison Tollett"     
[19] "Gared"                "Garth Greyfeather"   
[21] "Grenn"                "Hake"                
[23] "Hal"                  "Hosteen Frey"        
[25] "Hot Pie"              "Hullen"              
[27] "Jacelyn Bywater"      "Jack Bulwer"         
[29] "Jafer Flowers"        "Jaremy Rykker"       
[31] "Jeyne Poole"          "Jon Umber (Greatjon)"
[33] "Kyra"                 "Mandon Moore"        
[35] "Othor"                "Prendahl na Ghezn"   
[37] "Pyg"                  "Qhorin Halfhand"     
[39] "Reek"                 "Rickard Stark"       
[41] "Robb Stark"           "Roland Crakehall"    
[43] "Rupert Brax"          "Ryles"               
[45] "Sallor the Bald"      "Shae"                
[47] "Shagwell"             "Stafford Lannister"  
[49] "Stannis Baratheon"    "Stevron Frey"        
[51] "Tom of Sevenstreams"  "Tormund"             
[53] "Utt"                  "Varamyr"             
[55] "Varys"                "Weese"               
[57] "Yellow Dick"          "Talbert Serry"       
[59] "Alliser Thorne"      

$`5`
 [1] "Aerys II Targaryen" "Alebelly"           "Arianne Martell"   
 [4] "Barristan Selmy"    "Benfred Tallhart"   "Brynden Tully"     
 [7] "Donella Manderly"   "Euron Greyjoy"      "Falyse Stokeworth" 
[10] "Gendry"             "Iggo"               "Jaime Lannister"   
[13] "Jeor Mormont"       "Jorah Mormont"      "Lyman Darry"       
[16] "Lysa Tully"         "Margaery Tyrell"    "Meera Reed"        
[19] "Melisandre"         "Mikken"             "Oberyn Martell"    
[22] "Rafford"            "Robert Baratheon"   "Robett Glover"     
[25] "Shagga"             "Syrio Forel"        "Taena of Myr"      
[28] "Thistle"            "Torrhen Karstark"   "Val"               
[31] "Victarion Greyjoy"  "Weeper"             "Will"              
[34] "Wylis Manderly"     "Kerwin"            
#blockmodel with community membership
print(blockmodel(network_statnet,got.wt$membership)$block.model, digits=2)
        Block 1 Block 2 Block 3 Block 4 Block 5
Block 1    0.18    0.18    0.19    0.13    0.20
Block 2    0.18    0.27    0.20    0.22    0.26
Block 3    0.19    0.20    0.39    0.12    0.24
Block 4    0.13    0.22    0.12    0.19    0.19
Block 5    0.20    0.26    0.24    0.19    0.24
#add community membership as a node attribute
network_nodes$got.wt<-got.wt$membership

#summarize node statistics by community
nodes.by.gp(network_nodes,"got.wt")
     got.wt Appear   pov majorpov major got.fg   n
[1,]      1     10 0.034    0.017 0.051    2.5  59
[2,]      2     16 0.137    0.118 0.176    1.7 102
[3,]      3     14 0.093    0.093 0.186    2.3  43
[4,]      4     12 0.085    0.068 0.119    1.7  59
[5,]      5     15 0.171    0.143 0.171    2.0  35
#plot network with community coloring
plot(got.wt,network_igraph, layout=layout_with_graphopt, max.sa.movement=10 )

Leading Label Propagation Detection

#Run clustering algorithm: llp
got.lab<-label.propagation.community(network_igraph)

#to get an idea of who is in what group
igraph::groups(got.lab)
$`1`
  [1] "Addam Marbrand"                 
  [2] "Aegon Frey (Jinglebell)"        
  [3] "Aegon I Targaryen"              
  [4] "Aegon Targaryen"                
  [5] "Aemon Targaryen"                
  [6] "Aeron Greyjoy"                  
  [7] "Aerys II Targaryen"             
  [8] "Alebelly"                       
  [9] "Alester Florent"                
 [10] "Alfyn"                          
 [11] "Allar Deem"                     
 [12] "Amory Lorch"                    
 [13] "Antario Jast"                   
 [14] "Areo Hotah"                     
 [15] "Arianne Martell"                
 [16] "Arnolf Karstark"                
 [17] "Aron Santagar"                  
 [18] "Arthur Dayne"                   
 [19] "Arya Stark"                     
 [20] "Arys Oakheart"                  
 [21] "Asha Greyjoy"                   
 [22] "Aurane Waters"                  
 [23] "Axell Florent"                  
 [24] "Balman Byrch"                   
 [25] "Balon Greyjoy"                  
 [26] "Balon Swann"                    
 [27] "Barra"                          
 [28] "Barristan Selmy"                
 [29] "Belwas"                         
 [30] "Ben Plumm"                      
 [31] "Benfred Tallhart"               
 [32] "Benjen Stark"                   
 [33] "Beric Dondarrion"               
 [34] "Biter"                          
 [35] "Boros Blount"                   
 [36] "Bowen Marsh"                    
 [37] "Bran Stark"                     
 [38] "Brandon Stark"                  
 [39] "Brienne of Tarth"               
 [40] "Bronn"                          
 [41] "Brynden Tully"                  
 [42] "Catelyn Tully"                  
 [43] "Cersei Lannister"               
 [44] "Chayle"                         
 [45] "Chett"                          
 [46] "Chiswyck"                       
 [47] "Cleos Frey"                     
 [48] "Coldhands"                      
 [49] "Colemon"                        
 [50] "Cortnay Penrose"                
 [51] "Cotter Pyke"                    
 [52] "Cragorn"                        
 [53] "Craster"                        
 [54] "Daario Naharis"                 
 [55] "Dacey Mormont"                  
 [56] "Daenerys Targaryen"             
 [57] "Dalla"                          
 [58] "Dareon"                         
 [59] "Daryn Hornwood"                 
 [60] "Davos Seaworth"                 
 [61] "Dick Crabb"                     
 [62] "Dirk"                           
 [63] "Donal Noye"                     
 [64] "Donella Manderly"               
 [65] "Dontos Hollard"                 
 [66] "Doran Martell"                  
 [67] "Khal Drogo"                     
 [68] "Dywen"                          
 [69] "Ebben"                          
 [70] "Eddard Karstark"                
 [71] "Eddard Stark"                   
 [72] "Eddison Tollett"                
 [73] "Edmure Tully"                   
 [74] "Edric Storm"                    
 [75] "Elia Martell"                   
 [76] "Emmon Cuy"                      
 [77] "Emmon Frey"                     
 [78] "Eon Hunter"                     
 [79] "Euron Greyjoy"                  
 [80] "Falyse Stokeworth"              
 [81] "Gared"                          
 [82] "Garlan Tyrell"                  
 [83] "Garse Goodbrook"                
 [84] "Garth Greyfeather"              
 [85] "Gendry"                         
 [86] "Gerris Drinkwater"              
 [87] "Gilly"                          
 [88] "Gladden Wylde"                  
 [89] "Gregor Clegane"                 
 [90] "Grenn"                          
 [91] "Groleo"                         
 [92] "Guncer Sunglass"                
 [93] "Guyard Morrigen"                
 [94] "Hake"                           
 [95] "Hal"                            
 [96] "Haldon"                         
 [97] "Harlan Hunter"                  
 [98] "Harwin"                         
 [99] "Helman Tallhart"                
[100] "Hizdahr zo Loraq"               
[101] "Hodor"                          
[102] "Hosteen Frey"                   
[103] "Hoster Tully"                   
[104] "Hot Pie"                        
[105] "Hubard Rambton"                 
[106] "Hugh"                           
[107] "Hullen"                         
[108] "Iggo"                           
[109] "Illyrio Mopatis"                
[110] "Ilyn Payne"                     
[111] "Irri"                           
[112] "Jacelyn Bywater"                
[113] "Jack Bulwer"                    
[114] "Jafer Flowers"                  
[115] "Jaime Lannister"                
[116] "Janos Slynt"                    
[117] "Jaqen H'ghar"                   
[118] "Jared Frey"                     
[119] "Jaremy Rykker"                  
[120] "Jeor Mormont"                   
[121] "Jeyne Poole"                    
[122] "Jeyne Westerling"               
[123] "Jhiqui"                         
[124] "Jhogo"                          
[125] "Joffrey Baratheon"              
[126] "Jojen Reed"                     
[127] "Jon Arryn"                      
[128] "Jon Connington"                 
[129] "Jon Snow"                       
[130] "Jon Umber (Greatjon)"           
[131] "Jorah Mormont"                  
[132] "Jory Cassel"                    
[133] "Kevan Lannister"                
[134] "Kindly Man"                     
[135] "Kraznys mo Nakloz"              
[136] "Kyra"                           
[137] "Lancel Lannister"               
[138] "Lark"                           
[139] "Lollys Stokeworth"              
[140] "Lommy Greenhands"               
[141] "Loras Tyrell"                   
[142] "Walder Frey"                    
[143] "Lothar Mallery"                 
[144] "Lothor Brune"                   
[145] "Lucas Blackwood"                
[146] "Luton"                          
[147] "Luwin"                          
[148] "Lyanna Stark"                   
[149] "Lyman Darry"                    
[150] "Lymond Vikary"                  
[151] "Lysa Tully"                     
[152] "Mace Tyrell"                    
[153] "Aemon"                          
[154] "Cressen"                        
[155] "Mag Mar Tun Doh Weg"            
[156] "Mance Rayder"                   
[157] "Mandon Moore"                   
[158] "Margaery Tyrell"                
[159] "Meera Reed"                     
[160] "Melisandre"                     
[161] "Mero"                           
[162] "Merrett Frey"                   
[163] "Meryn Trant"                    
[164] "Mikken"                         
[165] "Mirri Maz Duur"                 
[166] "Missandei"                      
[167] "Moqorro"                        
[168] "Mordane"                        
[169] "Mycah"                          
[170] "Myrcella Baratheon"             
[171] "Nan"                            
[172] "Nymeria"                        
[173] "Oberyn Martell"                 
[174] "Olenna Redwyne"                 
[175] "Ollo Lophand"                   
[176] "Orton Merryweather"             
[177] "Osha"                           
[178] "Osmund Kettleblack"             
[179] "Osney Kettleblack"              
[180] "Othor"                          
[181] "Oznak zo Pahl"                  
[182] "Pate"                           
[183] "Paxter Redwyne"                 
[184] "Paul"                           
[185] "Penny"                          
[186] "Petyr Baelish"                  
[187] "Podrick Payne"                  
[188] "Polliver"                       
[189] "Prendahl na Ghezn"              
[190] "Pycelle"                        
[191] "Pyg"                            
[192] "Pypar"                          
[193] "Qhorin Halfhand"                
[194] "Qotho"                          
[195] "Quaithe"                        
[196] "Quaro"                          
[197] "Quentyn Martell"                
[198] "Qyburn"                         
[199] "Rafford"                        
[200] "Ralf Kenning"                   
[201] "Ramsay Snow"                    
[202] "Randyll Tarly"                  
[203] "Rattleshirt"                    
[204] "Raymun Darry"                   
[205] "Raymund Frey"                   
[206] "Reek"                           
[207] "Renly Baratheon"                
[208] "Reznak mo Reznak"               
[209] "Rhaegar Frey"                   
[210] "Rhaegar Targaryen"              
[211] "Rhaego"                         
[212] "Rhaenys Targaryen"              
[213] "Rickard Karstark"               
[214] "Rickard Stark"                  
[215] "Rickon Stark"                   
[216] "Robar Royce"                    
[217] "Robb Stark"                     
[218] "Robert Arryn"                   
[219] "Robert Baratheon"               
[220] "Robett Glover"                  
[221] "Rodrik Cassel"                  
[222] "Roland Crakehall"               
[223] "Roose Bolton"                   
[224] "Rorge"                          
[225] "Rupert Brax"                    
[226] "Ryles"                          
[227] "Ryman Frey"                     
[228] "Salladhor Saan"                 
[229] "Sallor the Bald"                
[230] "Samwell Tarly"                  
[231] "Sandor Clegane"                 
[232] "Sansa Stark"                    
[233] "Satin"                          
[234] "Sawane Botley"                  
[235] "Selyse Florent"                 
[236] "Senelle"                        
[237] "Shae"                           
[238] "Shagga"                         
[239] "Shagwell"                       
[240] "Shireen Baratheon"              
[241] "Skahaz mo Kandaq"               
[242] "Softfoot"                       
[243] "Stafford Lannister"             
[244] "Stannis Baratheon"              
[245] "Stevron Frey"                   
[246] "Stiv"                           
[247] "Styr"                           
[248] "Symond Frey"                    
[249] "Syrio Forel"                    
[250] "Taena of Myr"                   
[251] "Theon Greyjoy"                  
[252] "Thistle"                        
[253] "Thoros of Myr"                  
[254] "Tickler"                        
[255] "Timeon"                         
[256] "Timett"                         
[257] "Tion Frey"                      
[258] "Tom of Sevenstreams"            
[259] "Tommen Baratheon"               
[260] "Tormund"                        
[261] "Torrhen Karstark"               
[262] "Tyrion Lannister"               
[263] "Tysha"                          
[264] "Tytos Frey"                     
[265] "Tywin Lannister"                
[266] "Utt"                            
[267] "Val"                            
[268] "Varamyr"                        
[269] "Vardis Egen"                    
[270] "Vargo Hoat"                     
[271] "Varly"                          
[272] "Varys"                          
[273] "Vayon Poole"                    
[274] "Victarion Greyjoy"              
[275] "Viserys Targaryen"              
[276] "Walder Frey (Little)"           
[277] "Waymar Royce"                   
[278] "Weeper"                         
[279] "Weese"                          
[280] "Will"                           
[281] "Willas Tyrell"                  
[282] "Willem Lannister"               
[283] "Wylis Manderly"                 
[284] "Wyman Manderly"                 
[285] "Yellow Dick"                    
[286] "Ygritte"                        
[287] "Yohn Royce"                     
[288] "Yoren"                          
[289] "First High Septon (the fat one)"
[290] "Talbert Serry"                  
[291] "Dagon Codd"                     
[292] "Adrack Humble"                  
[293] "Oppo"                           
[294] "Kerwin"                         
[295] "Khrazz"                         
[296] "Yurkhaz zo Yunzak"              
[297] "the Others"                     
[298] "Alliser Thorne"                 
#blockmodel with community membership- doesn't work bc it is one group
#print(blockmodel(network_statnet, got.lab$membership)$block.model, digits=2)

#add community membership as a node attribute
network_nodes$got.lab<-got.lab$membership

#summarize node statistics by community
nodes.by.gp(network_nodes,"got.lab")
     got.lab Appear pov majorpov major got.fg got.wt   n
[1,]       1     13 0.1    0.087  0.14      2    2.7 298
#plot network with community coloring
plot(got.lab,network_igraph, layout=layout_with_graphopt, max.sa.movement=10 )

Edge Betweenness Detection

Look, I tried (see code below if you want to figure out what went wrong) but this amost crashed my computer and it created over 100 groups, so I think its safe to say that this will not be a valid set of communities.

#Run clustering algorithm: edge betweeness
got.edge<-edge.betweenness.community(network_igraph)

#to get an idea of who is in what group
igraph::groups(network_igraph)

#blockmodel with community membership
print(blockmodel(network_statnet,got.edge$membership)$block.model, digits=2)

#add community membership as a node attribute
network_nodes$got.edge<-got.edge$membership

#summarize node statistics by community
nodes.by.gp(network_nodes,"got.edge")

#plot network with community coloringy
plot(got.edge,network_igraph, layout=layout_with_graphopt, max.sa.movement=10 )

Eigenvector Detection

#Run clustering algorithm: eigenvector
got.eigen<-leading.eigenvector.community(network_igraph)

#to get an idea of who is in what group
igraph::groups(got.eigen)
$`1`
  [1] "Addam Marbrand"                 
  [2] "Alester Florent"                
  [3] "Allar Deem"                     
  [4] "Amory Lorch"                    
  [5] "Antario Jast"                   
  [6] "Aron Santagar"                  
  [7] "Arys Oakheart"                  
  [8] "Aurane Waters"                  
  [9] "Balman Byrch"                   
 [10] "Balon Swann"                    
 [11] "Barra"                          
 [12] "Beric Dondarrion"               
 [13] "Biter"                          
 [14] "Boros Blount"                   
 [15] "Brienne of Tarth"               
 [16] "Bronn"                          
 [17] "Brynden Tully"                  
 [18] "Catelyn Tully"                  
 [19] "Cersei Lannister"               
 [20] "Cleos Frey"                     
 [21] "Colemon"                        
 [22] "Cortnay Penrose"                
 [23] "Dick Crabb"                     
 [24] "Dontos Hollard"                 
 [25] "Edmure Tully"                   
 [26] "Edric Storm"                    
 [27] "Emmon Cuy"                      
 [28] "Emmon Frey"                     
 [29] "Eon Hunter"                     
 [30] "Falyse Stokeworth"              
 [31] "Garlan Tyrell"                  
 [32] "Gendry"                         
 [33] "Gladden Wylde"                  
 [34] "Gregor Clegane"                 
 [35] "Guyard Morrigen"                
 [36] "Harlan Hunter"                  
 [37] "Helman Tallhart"                
 [38] "Hoster Tully"                   
 [39] "Hugh"                           
 [40] "Ilyn Payne"                     
 [41] "Jacelyn Bywater"                
 [42] "Jaime Lannister"                
 [43] "Jeyne Westerling"               
 [44] "Joffrey Baratheon"              
 [45] "Jon Arryn"                      
 [46] "Kevan Lannister"                
 [47] "Lancel Lannister"               
 [48] "Lollys Stokeworth"              
 [49] "Loras Tyrell"                   
 [50] "Walder Frey"                    
 [51] "Lothar Mallery"                 
 [52] "Lothor Brune"                   
 [53] "Lucas Blackwood"                
 [54] "Lyman Darry"                    
 [55] "Lymond Vikary"                  
 [56] "Lysa Tully"                     
 [57] "Mace Tyrell"                    
 [58] "Mandon Moore"                   
 [59] "Margaery Tyrell"                
 [60] "Meryn Trant"                    
 [61] "Mordane"                        
 [62] "Mycah"                          
 [63] "Myrcella Baratheon"             
 [64] "Olenna Redwyne"                 
 [65] "Orton Merryweather"             
 [66] "Osmund Kettleblack"             
 [67] "Osney Kettleblack"              
 [68] "Paxter Redwyne"                 
 [69] "Petyr Baelish"                  
 [70] "Podrick Payne"                  
 [71] "Polliver"                       
 [72] "Pycelle"                        
 [73] "Pyg"                            
 [74] "Qyburn"                         
 [75] "Randyll Tarly"                  
 [76] "Raymun Darry"                   
 [77] "Renly Baratheon"                
 [78] "Rickard Karstark"               
 [79] "Robar Royce"                    
 [80] "Robert Arryn"                   
 [81] "Robert Baratheon"               
 [82] "Robett Glover"                  
 [83] "Roland Crakehall"               
 [84] "Rorge"                          
 [85] "Rupert Brax"                    
 [86] "Ryman Frey"                     
 [87] "Sandor Clegane"                 
 [88] "Sansa Stark"                    
 [89] "Senelle"                        
 [90] "Shae"                           
 [91] "Shagga"                         
 [92] "Shagwell"                       
 [93] "Stafford Lannister"             
 [94] "Taena of Myr"                   
 [95] "Thoros of Myr"                  
 [96] "Tickler"                        
 [97] "Timeon"                         
 [98] "Timett"                         
 [99] "Tom of Sevenstreams"            
[100] "Tommen Baratheon"               
[101] "Tyrion Lannister"               
[102] "Tywin Lannister"                
[103] "Vardis Egen"                    
[104] "Vargo Hoat"                     
[105] "Varly"                          
[106] "Varys"                          
[107] "Weese"                          
[108] "Willas Tyrell"                  
[109] "Wylis Manderly"                 
[110] "Yohn Royce"                     
[111] "First High Septon (the fat one)"

$`2`
 [1] "Aegon Frey (Jinglebell)" "Aeron Greyjoy"          
 [3] "Alebelly"                "Arya Stark"             
 [5] "Asha Greyjoy"            "Balon Greyjoy"          
 [7] "Benfred Tallhart"        "Bran Stark"             
 [9] "Brandon Stark"           "Chayle"                 
[11] "Chiswyck"                "Cragorn"                
[13] "Dacey Mormont"           "Donella Manderly"       
[15] "Eddard Stark"            "Garse Goodbrook"        
[17] "Harwin"                  "Hodor"                  
[19] "Hosteen Frey"            "Hot Pie"                
[21] "Hullen"                  "Iggo"                   
[23] "Jaqen H'ghar"            "Jared Frey"             
[25] "Jeyne Poole"             "Jojen Reed"             
[27] "Jon Umber (Greatjon)"    "Jory Cassel"            
[29] "Kindly Man"              "Kyra"                   
[31] "Lommy Greenhands"        "Luton"                  
[33] "Luwin"                   "Meera Reed"             
[35] "Merrett Frey"            "Mikken"                 
[37] "Osha"                    "Rafford"                
[39] "Ralf Kenning"            "Ramsay Snow"            
[41] "Raymund Frey"            "Reek"                   
[43] "Rhaegar Frey"            "Rickard Stark"          
[45] "Rickon Stark"            "Robb Stark"             
[47] "Rodrik Cassel"           "Roose Bolton"           
[49] "Sawane Botley"           "Stevron Frey"           
[51] "Stiv"                    "Symond Frey"            
[53] "Syrio Forel"             "Theon Greyjoy"          
[55] "Tion Frey"               "Tytos Frey"             
[57] "Utt"                     "Vayon Poole"            
[59] "Victarion Greyjoy"       "Walder Frey (Little)"   
[61] "Willem Lannister"        "Wyman Manderly"         
[63] "Yellow Dick"             "Yoren"                  
[65] "Talbert Serry"           "Dagon Codd"             
[67] "Adrack Humble"          

$`3`
 [1] "Aegon I Targaryen"  "Aegon Targaryen"    "Aerys II Targaryen"
 [4] "Areo Hotah"         "Arianne Martell"    "Arthur Dayne"      
 [7] "Barristan Selmy"    "Belwas"             "Ben Plumm"         
[10] "Daario Naharis"     "Daenerys Targaryen" "Doran Martell"     
[13] "Khal Drogo"         "Elia Martell"       "Euron Greyjoy"     
[16] "Gerris Drinkwater"  "Groleo"             "Haldon"            
[19] "Hizdahr zo Loraq"   "Illyrio Mopatis"    "Irri"              
[22] "Jhiqui"             "Jhogo"              "Jon Connington"    
[25] "Jorah Mormont"      "Kraznys mo Nakloz"  "Lyanna Stark"      
[28] "Cressen"            "Mirri Maz Duur"     "Missandei"         
[31] "Moqorro"            "Oberyn Martell"     "Pate"              
[34] "Penny"              "Prendahl na Ghezn"  "Qotho"             
[37] "Quaithe"            "Quaro"              "Quentyn Martell"   
[40] "Reznak mo Reznak"   "Rhaegar Targaryen"  "Rhaego"            
[43] "Rhaenys Targaryen"  "Sallor the Bald"    "Skahaz mo Kandaq"  
[46] "Tysha"              "Viserys Targaryen"  "Oppo"              
[49] "Kerwin"             "Khrazz"             "Yurkhaz zo Yunzak" 

$`4`
 [1] "Aemon Targaryen"     "Alfyn"               "Arnolf Karstark"    
 [4] "Axell Florent"       "Benjen Stark"        "Bowen Marsh"        
 [7] "Chett"               "Coldhands"           "Cotter Pyke"        
[10] "Craster"             "Dalla"               "Dareon"             
[13] "Daryn Hornwood"      "Davos Seaworth"      "Dirk"               
[16] "Donal Noye"          "Dywen"               "Ebben"              
[19] "Eddard Karstark"     "Eddison Tollett"     "Gared"              
[22] "Garth Greyfeather"   "Gilly"               "Grenn"              
[25] "Guncer Sunglass"     "Hake"                "Hal"                
[28] "Hubard Rambton"      "Jack Bulwer"         "Jafer Flowers"      
[31] "Janos Slynt"         "Jaremy Rykker"       "Jeor Mormont"       
[34] "Jon Snow"            "Lark"                "Aemon"              
[37] "Mag Mar Tun Doh Weg" "Mance Rayder"        "Melisandre"         
[40] "Nan"                 "Nymeria"             "Ollo Lophand"       
[43] "Othor"               "Paul"                "Pypar"              
[46] "Qhorin Halfhand"     "Rattleshirt"         "Ryles"              
[49] "Salladhor Saan"      "Samwell Tarly"       "Satin"              
[52] "Selyse Florent"      "Shireen Baratheon"   "Softfoot"           
[55] "Stannis Baratheon"   "Styr"                "Thistle"            
[58] "Tormund"             "Torrhen Karstark"    "Val"                
[61] "Varamyr"             "Waymar Royce"        "Weeper"             
[64] "Will"                "Ygritte"             "the Others"         
[67] "Alliser Thorne"     

$`5`
[1] "Mero"          "Oznak zo Pahl"
#blockmodel with community membership
print(blockmodel(network_statnet,got.eigen$membership)$block.model, digits=2)
        Block 1 Block 2 Block 3 Block 4 Block 5
Block 1   0.411   0.174   0.180   0.100   0.018
Block 2   0.174   0.319   0.074   0.160   0.000
Block 3   0.180   0.074   0.518   0.052   0.137
Block 4   0.100   0.160   0.052   0.465   0.015
Block 5   0.018   0.000   0.137   0.015   1.000
#add community membership as a node attribute
network_nodes$got.eigen<-got.eigen$membership

#summarize node statistics by community
nodes.by.gp(network_nodes,"got.eigen")
     got.eigen Appear   pov majorpov major got.fg got.wt got.lab   n
[1,]         1     16 0.072    0.072  0.15    3.0    2.4       1 111
[2,]         2     13 0.119    0.104  0.15    1.4    2.8       1  67
[3,]         3     11 0.157    0.118  0.14    2.0    2.8       1  51
[4,]         4     12 0.104    0.075  0.12    1.0    3.0       1  67
[5,]         5      1 0.000    0.000  0.00    2.0    3.0       1   2
#plot network with community coloring
plot(got.eigen,network_igraph, layout=layout_with_graphopt, max.sa.movement=10 )

Spinglass Community Detection

#Run clustering algorithm: spinglass
got.spin<-spinglass.community(network_igraph)

#to get an idea of who is in what group
igraph::groups(got.spin)
$`1`
 [1] "Addam Marbrand"                 
 [2] "Aegon Targaryen"                
 [3] "Aerys II Targaryen"             
 [4] "Allar Deem"                     
 [5] "Antario Jast"                   
 [6] "Areo Hotah"                     
 [7] "Arianne Martell"                
 [8] "Aron Santagar"                  
 [9] "Arys Oakheart"                  
[10] "Aurane Waters"                  
[11] "Balman Byrch"                   
[12] "Balon Swann"                    
[13] "Barra"                          
[14] "Boros Blount"                   
[15] "Brienne of Tarth"               
[16] "Bronn"                          
[17] "Cersei Lannister"               
[18] "Colemon"                        
[19] "Cortnay Penrose"                
[20] "Dick Crabb"                     
[21] "Dontos Hollard"                 
[22] "Doran Martell"                  
[23] "Elia Martell"                   
[24] "Emmon Cuy"                      
[25] "Eon Hunter"                     
[26] "Falyse Stokeworth"              
[27] "Garlan Tyrell"                  
[28] "Gregor Clegane"                 
[29] "Guyard Morrigen"                
[30] "Haldon"                         
[31] "Harlan Hunter"                  
[32] "Hugh"                           
[33] "Ilyn Payne"                     
[34] "Jacelyn Bywater"                
[35] "Jaime Lannister"                
[36] "Joffrey Baratheon"              
[37] "Jon Arryn"                      
[38] "Jon Connington"                 
[39] "Kevan Lannister"                
[40] "Lancel Lannister"               
[41] "Lollys Stokeworth"              
[42] "Loras Tyrell"                   
[43] "Lothor Brune"                   
[44] "Lyanna Stark"                   
[45] "Lymond Vikary"                  
[46] "Lysa Tully"                     
[47] "Mace Tyrell"                    
[48] "Mandon Moore"                   
[49] "Margaery Tyrell"                
[50] "Meryn Trant"                    
[51] "Myrcella Baratheon"             
[52] "Oberyn Martell"                 
[53] "Olenna Redwyne"                 
[54] "Orton Merryweather"             
[55] "Osmund Kettleblack"             
[56] "Osney Kettleblack"              
[57] "Paxter Redwyne"                 
[58] "Petyr Baelish"                  
[59] "Podrick Payne"                  
[60] "Pycelle"                        
[61] "Pyg"                            
[62] "Qyburn"                         
[63] "Randyll Tarly"                  
[64] "Renly Baratheon"                
[65] "Rhaegar Targaryen"              
[66] "Rhaenys Targaryen"              
[67] "Robar Royce"                    
[68] "Robert Arryn"                   
[69] "Robert Baratheon"               
[70] "Roland Crakehall"               
[71] "Rupert Brax"                    
[72] "Sandor Clegane"                 
[73] "Sansa Stark"                    
[74] "Senelle"                        
[75] "Shae"                           
[76] "Shagga"                         
[77] "Shagwell"                       
[78] "Stafford Lannister"             
[79] "Taena of Myr"                   
[80] "Timeon"                         
[81] "Timett"                         
[82] "Tommen Baratheon"               
[83] "Tyrion Lannister"               
[84] "Tysha"                          
[85] "Tywin Lannister"                
[86] "Vardis Egen"                    
[87] "Varly"                          
[88] "Varys"                          
[89] "Willas Tyrell"                  
[90] "Yohn Royce"                     
[91] "First High Septon (the fat one)"
[92] "Oppo"                           

$`2`
  [1] "Aegon Frey (Jinglebell)" "Aeron Greyjoy"          
  [3] "Alebelly"                "Amory Lorch"            
  [5] "Arthur Dayne"            "Arya Stark"             
  [7] "Asha Greyjoy"            "Balon Greyjoy"          
  [9] "Benfred Tallhart"        "Beric Dondarrion"       
 [11] "Biter"                   "Bran Stark"             
 [13] "Brandon Stark"           "Brynden Tully"          
 [15] "Catelyn Tully"           "Chayle"                 
 [17] "Chiswyck"                "Cleos Frey"             
 [19] "Cragorn"                 "Dacey Mormont"          
 [21] "Donella Manderly"        "Eddard Stark"           
 [23] "Edmure Tully"            "Emmon Frey"             
 [25] "Euron Greyjoy"           "Garse Goodbrook"        
 [27] "Gendry"                  "Gladden Wylde"          
 [29] "Harwin"                  "Helman Tallhart"        
 [31] "Hodor"                   "Hosteen Frey"           
 [33] "Hoster Tully"            "Hot Pie"                
 [35] "Hullen"                  "Iggo"                   
 [37] "Jaqen H'ghar"            "Jared Frey"             
 [39] "Jeyne Poole"             "Jeyne Westerling"       
 [41] "Jojen Reed"              "Jon Umber (Greatjon)"   
 [43] "Jory Cassel"             "Kindly Man"             
 [45] "Kyra"                    "Lommy Greenhands"       
 [47] "Walder Frey"             "Lothar Mallery"         
 [49] "Lucas Blackwood"         "Luton"                  
 [51] "Luwin"                   "Lyman Darry"            
 [53] "Meera Reed"              "Merrett Frey"           
 [55] "Mikken"                  "Mordane"                
 [57] "Mycah"                   "Osha"                   
 [59] "Polliver"                "Rafford"                
 [61] "Ralf Kenning"            "Ramsay Snow"            
 [63] "Raymun Darry"            "Raymund Frey"           
 [65] "Reek"                    "Rhaegar Frey"           
 [67] "Rickard Karstark"        "Rickard Stark"          
 [69] "Rickon Stark"            "Robb Stark"             
 [71] "Robett Glover"           "Rodrik Cassel"          
 [73] "Roose Bolton"            "Rorge"                  
 [75] "Ryman Frey"              "Sawane Botley"          
 [77] "Stevron Frey"            "Stiv"                   
 [79] "Symond Frey"             "Syrio Forel"            
 [81] "Theon Greyjoy"           "Thoros of Myr"          
 [83] "Tickler"                 "Tion Frey"              
 [85] "Tom of Sevenstreams"     "Tytos Frey"             
 [87] "Utt"                     "Vargo Hoat"             
 [89] "Vayon Poole"             "Victarion Greyjoy"      
 [91] "Walder Frey (Little)"    "Weese"                  
 [93] "Willem Lannister"        "Wylis Manderly"         
 [95] "Wyman Manderly"          "Yellow Dick"            
 [97] "Yoren"                   "Talbert Serry"          
 [99] "Dagon Codd"              "Adrack Humble"          

$`3`
 [1] "Aegon I Targaryen"  "Barristan Selmy"    "Belwas"            
 [4] "Ben Plumm"          "Daario Naharis"     "Daenerys Targaryen"
 [7] "Khal Drogo"         "Gerris Drinkwater"  "Groleo"            
[10] "Hizdahr zo Loraq"   "Illyrio Mopatis"    "Irri"              
[13] "Jhiqui"             "Jhogo"              "Jorah Mormont"     
[16] "Kraznys mo Nakloz"  "Mero"               "Mirri Maz Duur"    
[19] "Missandei"          "Moqorro"            "Nymeria"           
[22] "Oznak zo Pahl"      "Pate"               "Penny"             
[25] "Prendahl na Ghezn"  "Qotho"              "Quaithe"           
[28] "Quaro"              "Quentyn Martell"    "Reznak mo Reznak"  
[31] "Rhaego"             "Sallor the Bald"    "Skahaz mo Kandaq"  
[34] "Viserys Targaryen"  "Khrazz"             "Yurkhaz zo Yunzak" 

$`4`
[1] "Kerwin"

$`5`
 [1] "Aemon Targaryen"     "Alester Florent"     "Alfyn"              
 [4] "Arnolf Karstark"     "Axell Florent"       "Benjen Stark"       
 [7] "Bowen Marsh"         "Chett"               "Coldhands"          
[10] "Cotter Pyke"         "Craster"             "Dalla"              
[13] "Dareon"              "Daryn Hornwood"      "Davos Seaworth"     
[16] "Dirk"                "Donal Noye"          "Dywen"              
[19] "Ebben"               "Eddard Karstark"     "Eddison Tollett"    
[22] "Edric Storm"         "Gared"               "Garth Greyfeather"  
[25] "Gilly"               "Grenn"               "Guncer Sunglass"    
[28] "Hake"                "Hal"                 "Hubard Rambton"     
[31] "Jack Bulwer"         "Jafer Flowers"       "Janos Slynt"        
[34] "Jaremy Rykker"       "Jeor Mormont"        "Jon Snow"           
[37] "Lark"                "Aemon"               "Cressen"            
[40] "Mag Mar Tun Doh Weg" "Mance Rayder"        "Melisandre"         
[43] "Nan"                 "Ollo Lophand"        "Othor"              
[46] "Paul"                "Pypar"               "Qhorin Halfhand"    
[49] "Rattleshirt"         "Ryles"               "Salladhor Saan"     
[52] "Samwell Tarly"       "Satin"               "Selyse Florent"     
[55] "Shireen Baratheon"   "Softfoot"            "Stannis Baratheon"  
[58] "Styr"                "Thistle"             "Tormund"            
[61] "Torrhen Karstark"    "Val"                 "Varamyr"            
[64] "Waymar Royce"        "Weeper"              "Will"               
[67] "Ygritte"             "the Others"          "Alliser Thorne"     
#blockmodel with community membership
print(blockmodel(network_statnet,got.spin$membership)$block.model, digits=2)
        Block 1 Block 2 Block 3 Block 4 Block 5
Block 1   0.496   0.197   0.168   0.011   0.116
Block 2   0.197   0.286   0.047   0.030   0.125
Block 3   0.168   0.047   0.576   0.056   0.035
Block 4   0.011   0.030   0.056     NaN   0.000
Block 5   0.116   0.125   0.035   0.000   0.445
#add community membership as a node attribute
network_nodes$got.spin<-got.spin$membership

#summarize node statistics by community
nodes.by.gp(network_nodes,"got.spin")
     got.spin Appear  pov majorpov major got.fg got.wt got.lab
[1,]        1   17.8 0.11    0.109 0.217    2.8    2.5       1
[2,]        2   11.9 0.09    0.080 0.110    1.9    2.7       1
[3,]        3    9.9 0.11    0.083 0.083    2.0    2.7       1
[4,]        4    1.0 0.00    0.000 0.000    2.0    5.0       1
[5,]        5   11.7 0.12    0.072 0.116    1.1    3.0       1
     got.eigen   n
[1,]       1.3  92
[2,]       1.7 100
[3,]       3.1  36
[4,]       3.0   1
[5,]       3.9  69
#plot network with community coloring
plot(got.spin,network_igraph, layout=layout_with_graphopt, max.sa.movement=10 )

Compare Modularity

mods<-c(fastgreedy=modularity(got.fg), walktrap=modularity(got.wt), llp=modularity(got.lab), eigen=modularity(got.eigen), spinglass=modularity(got.spin))

mods
fastgreedy   walktrap        llp      eigen  spinglass 
0.25236488 0.02662821 0.00000000 0.26589769 0.14355753 

Reuse

Text and figures are licensed under Creative Commons Attribution CC BY-NC 4.0. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".

Citation

For attribution, please cite this work as

Bean (2022, March 27). Data Analytics and Computational Social Science: Assignment7. Retrieved from https://github.com/DACSS/dacss_course_website/posts/httpsrpubscombean22z881579/

BibTeX citation

@misc{bean2022assignment7,
  author = {Bean, Zoe},
  title = {Data Analytics and Computational Social Science: Assignment7},
  url = {https://github.com/DACSS/dacss_course_website/posts/httpsrpubscombean22z881579/},
  year = {2022}
}