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
Last revisionBoth sides next revision
r:social_network_analysis_tutorial [2019/11/27 08:13] – [Using ifelse] hkimscilr:social_network_analysis_tutorial [2023/11/23 08:44] – [Edges] hkimscil
Line 1: Line 1:
 ====== T1. ====== ====== T1. ======
 +''%%install.packages("igraph")%%''
 ===== Dataset ===== ===== Dataset =====
 <file csv star-wars-network-edges.csv> <file csv star-wars-network-edges.csv>
Line 657: Line 658:
 </code> </code>
  
 +<code>
 +par(mar=c(0,0,0,0))
 +plot(g)
 +</code>
 +{{:r:pasted:20191127-074438.png}}
 +
 +===== %in% =====
 +
 +<code>
 +# what does %in% do?
 +1 %in% c(1,2,3,4)
 +1 %in% c(2,3,4)
 +</code>
 +
 +<code>
 +> 1 %in% c(1,2,3,4)
 +[1] TRUE
 +> 1 %in% c(2,3,4)
 +[1] FALSE
 +
 +</code>
 +
 +<code>
 +dark_side <- c("DARTH VADER", "MOTTI", "TARKIN")
 +light_side <- c("R2-D2", "CHEWBACCA", "C-3PO", "LUKE", "CAMIE", "BIGGS",
 +                "LEIA", "BERU", "OWEN", "OBI-WAN", "HAN", "DODONNA",
 +                "GOLD LEADER", "WEDGE", "RED LEADER", "RED TEN", "GOLD FIVE")
 +other <- c("GREEDO", "JABBA")
 +# node we'll create a new color variable as a node property
 +V(g)$color <- NA # V(g)에 color라는 컬럼을 만든다
 +V(g)$color[V(g)$name %in% dark_side] <- "red" 
 +# V(g)$name이 dark_side에 있는 이름이면 color 컬럼에 "red"를 적는다.
 +V(g)$color[V(g)$name %in% light_side] <- "gold"
 +V(g)$color[V(g)$name %in% other] <- "grey20"
 +vertex_attr(g)
 +</code>
 +
 +<code>
 +par(mar=c(0,0,0,0))
 +plot(g)
 +legend(x=.75, y=.75, legend=c("Dark side", "Light side", "Other"), 
 +       pch=21, pt.bg=c("red", "gold", "grey20"), pt.cex=2, bty="n")
 +</code>
 +{{:r:pasted:20191127-075029.png}}
 +
 +===== Edges =====
 +<code>
 +E(g)$width <- log(E(g)$weight) + 1.5 # create width column in the graph, g
 +edge_attr(g)
 +</code>
 +<code>
 +> E(g)$width <- log(E(g)$weight) + 1.5 # create width column in the graph, g
 +> edge_attr(g)
 +$weight
 + [1] 17 13  6  5  5  3  1  7  5 16 19 11  1  1  2  2
 +[17]  4  1  3  3  2  3 18  2  6 17  1 19  6  1  2  1
 +[33]  7  9 26  1  1  6  1  1 13  1  1  1  1  1  1  2
 +[49]  1  1  3  3  1  1  3  1  2  1  1  1
 +
 +$width
 + [1] 4.333213 4.064949 3.291759 3.109438 3.109438
 + [6] 2.598612 1.500000 3.445910 3.109438 4.272589
 +[11] 4.444439 3.897895 1.500000 1.500000 2.193147
 +[16] 2.193147 2.886294 1.500000 2.598612 2.598612
 +[21] 2.193147 2.598612 4.390372 2.193147 3.291759
 +[26] 4.333213 1.500000 4.444439 3.291759 1.500000
 +[31] 2.193147 1.500000 3.445910 3.697225 4.758097
 +[36] 1.500000 1.500000 3.291759 1.500000 1.500000
 +[41] 4.064949 1.500000 1.500000 1.500000 1.500000
 +[46] 1.500000 1.500000 2.193147 1.500000 1.500000
 +[51] 2.598612 2.598612 1.500000 1.500000 2.598612
 +[56] 1.500000 2.193147 1.500000 1.500000 1.500000
 +</code>
 +
 +<code>
 +par(mar=c(0,0,0,0))
 +plot(g)
 +</code>
 +{{:r:pasted:20191127-080219.png}}
 +
 +<code>
 +par(mfrow=c(2, 3), mar=c(0,0,1,0))
 +plot(g, layout=layout_randomly, main="Random")
 +plot(g, layout=layout_in_circle, main="Circle")
 +plot(g, layout=layout_as_star, main="Star")
 +plot(g, layout=layout_as_tree, main="Tree")
 +plot(g, layout=layout_on_grid, main="Grid")
 +plot(g, layout=layout_with_fr, main="Force-directed")
 +par(mfrow=c(1, 1), mar=c(0,0,0,0)) # set it back to the previous setting
 +</code>
 +{{: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.txt · Last modified: 2023/11/23 08:50 by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki