User Tools

Site Tools


r:social_network_analysis_tutorial

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
r:social_network_analysis_tutorial [2019/11/27 08:35] – [Edges] hkimscilr:social_network_analysis_tutorial [2023/11/23 08:50] (current) hkimscil
Line 1: Line 1:
 ====== T1. ====== ====== T1. ======
 +''%%install.packages("igraph")%%''
 +see [[https://igraph.org/r/doc/index.html]]
 ===== Dataset ===== ===== Dataset =====
 <file csv star-wars-network-edges.csv> <file csv star-wars-network-edges.csv>
Line 748: Line 750:
 </code> </code>
 {{:r:pasted:20191127-080437.png}} {{:r:pasted:20191127-080437.png}}
 +
 +cf. layout_with_fr: The Fruchterman-Reingold layout algorithm see [[https://www.rdocumentation.org/packages/igraph/versions/1.3.5/topics/layout_with_fr]] page
 +<code>
 +par(mfrow=c(1,2))
 +set.seed(777)
 +fr <- layout_with_fr(g, niter=1000)
 +par(mar=c(0,0,0,0)); plot(g, layout=fr)
 +set.seed(666)
 +fr <- layout_with_fr(g, niter=1000)
 +par(mar=c(0,0,0,0)); plot(g, layout=fr)
 +
 +</code>
 +{{:r:pasted:20191127-080758.png}}
 +
 +
 +===== Measurements =====
 +<code>
 +degree(g, mode = "all")
 +closeness(g, mode = "all", weights = NA, normalized = T)
 +betweenness(g, directed = F, weights = NA, normalized = T)
 +</code>
 +
 +<code>
 +> degree(g, mode = "all")
 +      R2-D2   CHEWBACCA       C-3PO        LUKE DARTH VADER       CAMIE       BIGGS 
 +          7                    10          15                               
 +       LEIA        BERU        OWEN     OBI-WAN       MOTTI      TARKIN         HAN 
 +         12                                                             
 +     GREEDO       JABBA     DODONNA GOLD LEADER       WEDGE  RED LEADER     RED TEN 
 +          1                                                             
 +  GOLD FIVE 
 +          0 
 +> closeness(g, mode = "all", weights = NA, normalized = T)
 +      R2-D2   CHEWBACCA       C-3PO        LUKE DARTH VADER       CAMIE       BIGGS 
 + 0.38181818  0.38888889  0.40384615  0.44680851  0.32812500  0.32307692  0.36842105 
 +       LEIA        BERU        OWEN     OBI-WAN       MOTTI      TARKIN         HAN 
 + 0.42000000  0.35000000  0.32812500  0.38181818  0.31343284  0.31343284  0.38888889 
 +     GREEDO       JABBA     DODONNA GOLD LEADER       WEDGE  RED LEADER     RED TEN 
 + 0.28767123  0.28767123  0.34426230  0.33870968  0.33870968  0.36842105  0.32307692 
 +  GOLD FIVE 
 + 0.04545455 
 +Warning message:
 +In closeness(g, mode = "all", weights = NA, normalized = T) :
 +  At centrality.c:2784 :closeness centrality is not well-defined for disconnected graphs
 +> betweenness(g, directed = F, weights = NA, normalized = T)
 +      R2-D2   CHEWBACCA       C-3PO        LUKE DARTH VADER       CAMIE       BIGGS 
 +0.011904762 0.035487528 0.057596372 0.297278912 0.011904762 0.000000000 0.032142857 
 +       LEIA        BERU        OWEN     OBI-WAN       MOTTI      TARKIN         HAN 
 +0.217120181 0.005442177 0.000000000 0.014852608 0.000000000 0.000000000 0.176190476 
 +     GREEDO       JABBA     DODONNA GOLD LEADER       WEDGE  RED LEADER     RED TEN 
 +0.000000000 0.000000000 0.011111111 0.003174603 0.003174603 0.032142857 0.000000000 
 +  GOLD FIVE 
 +0.000000000 
 +
 +</code>
 +
 +<code>
 +g.c.deg <- degree(g, mode = "all")
 +g.c.clo <- closeness(g, mode = "all", weights = NA, normalized = T)
 +g.c.bet <- betweenness(g, directed = F, weights = NA, normalized = T)
 +tmp <- data.frame(g.c.deg, g.c.clo, g.c.bet)
 +tmp[order(-g.c.clo, -g.c.bet),]
 +</code>
 +
 +<code>
 +> g.c.deg <- degree(g, mode = "all")
 +> g.c.clo <- closeness(g, mode = "all", weights = NA, normalized = T)
 +Warning message:
 +In closeness(g, mode = "all", weights = NA, normalized = T) :
 +  At centrality.c:2784 :closeness centrality is not well-defined for disconnected graphs
 +> g.c.bet <- betweenness(g, directed = F, weights = NA, normalized = T)
 +> tmp <- data.frame(g.c.deg, g.c.clo, g.c.bet)
 +
 +> tmp
 +            g.c.deg    g.c.clo     g.c.bet
 +R2-D2             7 0.38181818 0.011904762
 +CHEWBACCA         8 0.38888889 0.035487528
 +C-3PO            10 0.40384615 0.057596372
 +LUKE             15 0.44680851 0.297278912
 +DARTH VADER       5 0.32812500 0.011904762
 +CAMIE             2 0.32307692 0.000000000
 +BIGGS             7 0.36842105 0.032142857
 +LEIA             12 0.42000000 0.217120181
 +BERU              4 0.35000000 0.005442177
 +OWEN              3 0.32812500 0.000000000
 +OBI-WAN           7 0.38181818 0.014852608
 +MOTTI             3 0.31343284 0.000000000
 +TARKIN            3 0.31343284 0.000000000
 +HAN               8 0.38888889 0.176190476
 +GREEDO            1 0.28767123 0.000000000
 +JABBA             1 0.28767123 0.000000000
 +DODONNA           5 0.34426230 0.011111111
 +GOLD LEADER       5 0.33870968 0.003174603
 +WEDGE             5 0.33870968 0.003174603
 +RED LEADER        7 0.36842105 0.032142857
 +RED TEN           2 0.32307692 0.000000000
 +GOLD FIVE         0 0.04545455 0.000000000
 +
 +> tmp[order(-g.c.clo, -g.c.bet),]
 +            g.c.deg    g.c.clo     g.c.bet
 +LUKE             15 0.44680851 0.297278912
 +LEIA             12 0.42000000 0.217120181
 +C-3PO            10 0.40384615 0.057596372
 +HAN               8 0.38888889 0.176190476
 +CHEWBACCA         8 0.38888889 0.035487528
 +OBI-WAN           7 0.38181818 0.014852608
 +R2-D2             7 0.38181818 0.011904762
 +BIGGS             7 0.36842105 0.032142857
 +RED LEADER        7 0.36842105 0.032142857
 +BERU              4 0.35000000 0.005442177
 +DODONNA           5 0.34426230 0.011111111
 +GOLD LEADER       5 0.33870968 0.003174603
 +WEDGE             5 0.33870968 0.003174603
 +DARTH VADER       5 0.32812500 0.011904762
 +OWEN              3 0.32812500 0.000000000
 +CAMIE             2 0.32307692 0.000000000
 +RED TEN           2 0.32307692 0.000000000
 +MOTTI             3 0.31343284 0.000000000
 +TARKIN            3 0.31343284 0.000000000
 +GREEDO            1 0.28767123 0.000000000
 +JABBA             1 0.28767123 0.000000000
 +GOLD FIVE         0 0.04545455 0.000000000
 +
 +</code>
 +
 +<code>
 +g.c.bac <- distances(g, v=V(g)["R2-D2"], to=V(g), weights=NA)
 +g.c.bac
 +</code>
 +
 +<code>
 +> g.c.bac <- distances(g, v=V(g)["R2-D2"], to=V(g), weights=NA)
 +> g.c.bac
 +      R2-D2 CHEWBACCA C-3PO LUKE DARTH VADER CAMIE BIGGS LEIA BERU OWEN OBI-WAN MOTTI
 +R2-D2                    1                      1    2    2           2
 +      TARKIN HAN GREEDO JABBA DODONNA GOLD LEADER WEDGE RED LEADER RED TEN GOLD FIVE
 +R2-D2      2        2                                  2             Inf
 +</code>
 +
r/social_network_analysis_tutorial.1574811314.txt.gz · Last modified: 2019/11/27 08:35 by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki