Network_HW2_ego r09228001 地理所碩一 楊宇翔 2021/3/19 (Fri.)

提供實習資料:soc-tribes.txt ok 資料為新幾內亞東部高地Gahuku-Gama聯盟的部落社會網絡。網絡格式為edge-list;節點表示為一部落,連線表示部落間建立的友誼關係。 繪製每個部落的ego-network,計算每個部落的個體網絡指標(如下),並討論哪些的網絡結構是較具競爭力的部落。 [1]Size [2]Density [3]Effective size [4]Efficiency [5]Constraint

library(igraph)
library(egor)

load in and visulize network data

setwd("~/Desktop/109-2 碩班/109-2 Network  Models/week4 Ego-Networks/lab2")
data<- "soctribes.csv"
edges <- read.table(data, header=T, sep=",") 
data3<- "tribesnode.csv"
nodes <- read.table(data3, header=T, sep=",")
#visualize network
g1 = graph.data.frame (edges, directed=F, nodes) 
plot(g1,vertex.color="gold", main="Gahuku-Gama Tribe-wise network")

#transform from edge list to full matrix(adjacency)
m <- as_adjacency_matrix(g1,type="both",names=TRUE,sparse=FALSE)#,attr="weight")
#Extracting ego-network from the whole network
s=ego(g1,order=1,nodes = V(g1),mode="all",mindist=0)
sg=make_ego_graph(g1,order=1,nodes = V(g1),mode="all",mindist=0)

1. size

Now that we have a list of networks, we can apply the same function to each network using a single line of code, again with the help of lapply. Network size is the number of nodes in a network. To find this, we use the vcount() function. We can also find the number of edges using ecount()

#size
network_sizes <- lapply(sg, vcount)
network_sizes <- unlist(network_sizes)-1
size=cbind(c(1:16),network_sizes)
size
##          network_sizes
##  [1,]  1             8
##  [2,]  2             8
##  [3,]  3             6
##  [4,]  4             3
##  [5,]  5             7
##  [6,]  6            10
##  [7,]  7             7
##  [8,]  8             7
##  [9,]  9             7
## [10,] 10             5
## [11,] 11             9
## [12,] 12             8
## [13,] 13             8
## [14,] 14             5
## [15,] 15             9
## [16,] 16             9
#The average network has a little over one and a half people in it. We could similarly plot the distribution.
hist(network_sizes, main = "Histogram of Ego Network Sizes", xlab = "Network Size", col = "gold")

#We can take the mean of one of these results simply by turning the list into a vector and using the mean function on the resulting vector.
mean(network_sizes, na.rm = T)
## [1] 7.25
median(network_sizes, na.rm = T)
## [1] 7.5

2.density

#density
network_densities <- lapply(sg, graph.density)
network_densities <- unlist(network_densities)
density=cbind(c(1:16),network_densities)
density
##          network_densities
##  [1,]  1         0.6111111
##  [2,]  2         0.6388889
##  [3,]  3         0.6666667
##  [4,]  4         0.8333333
##  [5,]  5         0.5714286
##  [6,]  6         0.5818182
##  [7,]  7         0.6071429
##  [8,]  8         0.6071429
##  [9,]  9         0.7142857
## [10,] 10         0.8666667
## [11,] 11         0.6444444
## [12,] 12         0.6388889
## [13,] 13         0.6111111
## [14,] 14         0.6000000
## [15,] 15         0.6444444
## [16,] 16         0.6444444
hist(network_densities,, main = "Histogram of Ego Network Density", xlab = "Network Density", col = "gold" )

mean(network_densities, na.rm = T)
## [1] 0.6551136
median(network_densities, na.rm = T)
## [1] 0.6388889

3. Effective size 

#install.packages("influenceR")
library(influenceR)
?ens

Burt’s Effective Network Size

ways to measure the actors’ access to structural holes in a network.

Structural holes “provide opportunities to broker connections between people” (Burt 2008)

ens(g1)
##        1        2        3        4        5        6        7        8 
## 4.500000 4.250000 3.333333 1.666667 4.428571 5.600000 4.142857 4.142857 
##        9       10       11       12       13       14       15       16 
## 3.285714 1.800000 4.555556 4.250000 4.500000 3.400000 4.555556 4.555556

4. Efficiency

Efficiency is the effective size divided by the observed size.

library(influenceR)
effectivesize = as.array(ens(g1))
observedsize=as.array(network_sizes)
eff=effectivesize-observedsize
eff
##         1         2         3         4         5         6         7         8 
## -3.500000 -3.750000 -2.666667 -1.333333 -2.571429 -4.400000 -2.857143 -2.857143 
##         9        10        11        12        13        14        15        16 
## -3.714286 -3.200000 -4.444444 -3.750000 -3.500000 -1.600000 -4.444444 -4.444444

5.Constraint

constraint ()= Burt’s constraint function

Given a graph, constraint calculates Burt’s constraint for each vertex.

con=constraint(g1)
con
##         1         2         3         4         5         6         7         8 
## 0.2666565 0.2778649 0.3301328 0.4832806 0.2531944 0.2481491 0.2660553 0.2841825 
##         9        10        11        12        13        14        15        16 
## 0.3181476 0.3914684 0.2754184 0.2691893 0.2661417 0.2887259 0.2824314 0.2706770

plot each ego network

for (i in c(1:length(s))) 
  {plot(sg[[i]],main=paste("Tribe",i,"'s ego network", sep=" "))
}

DATA

adjacency matrix

m
##    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
## 1  0 1 1 1 1 1 0 0 0  0  0  1  0  0  1  1
## 2  1 0 1 0 1 1 0 0 1  1  0  0  0  0  1  1
## 3  1 1 0 1 0 1 1 1 0  0  0  0  0  0  0  0
## 4  1 0 1 0 0 0 0 1 0  0  0  0  0  0  0  0
## 5  1 1 0 0 0 0 1 0 1  0  0  0  0  1  1  1
## 6  1 1 1 0 0 0 1 1 1  0  1  1  1  0  0  1
## 7  0 0 1 0 1 1 0 1 0  0  1  1  1  0  0  0
## 8  0 0 1 1 0 1 1 0 0  0  1  1  0  1  0  0
## 9  0 1 0 0 1 1 0 0 0  1  1  0  1  0  1  0
## 10 0 1 0 0 0 0 0 0 1  0  1  0  1  0  1  0
## 11 0 0 0 0 0 1 1 1 1  1  0  1  1  0  1  1
## 12 1 0 0 0 0 1 1 1 0  0  1  0  0  1  1  1
## 13 0 0 0 0 0 1 1 0 1  1  1  0  0  1  1  1
## 14 0 0 0 0 1 0 0 1 0  0  0  1  1  0  0  1
## 15 1 1 0 0 1 0 0 0 1  1  1  1  1  0  0  1
## 16 1 1 0 0 1 1 0 0 0  0  1  1  1  1  1  0

edge graph

g1
## IGRAPH f9c461d UNW- 16 58 -- 
## + attr: name (v/c), weight (e/n)
## + edges from f9c461d (vertex names):
##  [1] 1 --2  1 --3  2 --3  1 --4  3 --4  1 --5  2 --5  1 --6  2 --6  3 --6 
## [11] 3 --7  5 --7  6 --7  3 --8  4 --8  6 --8  7 --8  2 --9  5 --9  6 --9 
## [21] 2 --10 9 --10 6 --11 7 --11 8 --11 9 --11 10--11 1 --12 6 --12 7 --12
## [31] 8 --12 11--12 6 --13 7 --13 9 --13 10--13 11--13 5 --14 8 --14 12--14
## [41] 13--14 1 --15 2 --15 5 --15 9 --15 10--15 11--15 12--15 13--15 1 --16
## [51] 2 --16 5 --16 6 --16 11--16 12--16 13--16 14--16 15--16

ego network nodes

s
## [[1]]
## + 9/16 vertices, named, from f9c461d:
## [1] 1  2  3  4  5  6  12 15 16
## 
## [[2]]
## + 9/16 vertices, named, from f9c461d:
## [1] 2  1  3  5  6  9  10 15 16
## 
## [[3]]
## + 7/16 vertices, named, from f9c461d:
## [1] 3 1 2 4 6 7 8
## 
## [[4]]
## + 4/16 vertices, named, from f9c461d:
## [1] 4 1 3 8
## 
## [[5]]
## + 8/16 vertices, named, from f9c461d:
## [1] 5  1  2  7  9  14 15 16
## 
## [[6]]
## + 11/16 vertices, named, from f9c461d:
##  [1] 6  1  2  3  7  8  9  11 12 13 16
## 
## [[7]]
## + 8/16 vertices, named, from f9c461d:
## [1] 7  3  5  6  8  11 12 13
## 
## [[8]]
## + 8/16 vertices, named, from f9c461d:
## [1] 8  3  4  6  7  11 12 14
## 
## [[9]]
## + 8/16 vertices, named, from f9c461d:
## [1] 9  2  5  6  10 11 13 15
## 
## [[10]]
## + 6/16 vertices, named, from f9c461d:
## [1] 10 2  9  11 13 15
## 
## [[11]]
## + 10/16 vertices, named, from f9c461d:
##  [1] 11 6  7  8  9  10 12 13 15 16
## 
## [[12]]
## + 9/16 vertices, named, from f9c461d:
## [1] 12 1  6  7  8  11 14 15 16
## 
## [[13]]
## + 9/16 vertices, named, from f9c461d:
## [1] 13 6  7  9  10 11 14 15 16
## 
## [[14]]
## + 6/16 vertices, named, from f9c461d:
## [1] 14 5  8  12 13 16
## 
## [[15]]
## + 10/16 vertices, named, from f9c461d:
##  [1] 15 1  2  5  9  10 11 12 13 16
## 
## [[16]]
## + 10/16 vertices, named, from f9c461d:
##  [1] 16 1  2  5  6  11 12 13 14 15

ego network graph

sg
## [[1]]
## IGRAPH ebff906 UNW- 9 22 -- 
## + attr: name (v/c), weight (e/n)
## + edges from ebff906 (vertex names):
##  [1] 1 --2  1 --3  2 --3  1 --4  3 --4  1 --5  2 --5  1 --6  2 --6  3 --6 
## [11] 1 --12 6 --12 1 --15 2 --15 5 --15 12--15 1 --16 2 --16 5 --16 6 --16
## [21] 12--16 15--16
## 
## [[2]]
## IGRAPH d7551e3 UNW- 9 23 -- 
## + attr: name (v/c), weight (e/n)
## + edges from d7551e3 (vertex names):
##  [1] 1 --2  1 --3  2 --3  1 --5  2 --5  1 --6  2 --6  3 --6  2 --9  5 --9 
## [11] 6 --9  2 --10 9 --10 1 --15 2 --15 5 --15 9 --15 10--15 1 --16 2 --16
## [21] 5 --16 6 --16 15--16
## 
## [[3]]
## IGRAPH a6abf92 UNW- 7 14 -- 
## + attr: name (v/c), weight (e/n)
## + edges from a6abf92 (vertex names):
##  [1] 1--2 1--3 2--3 1--4 3--4 1--6 2--6 3--6 3--7 6--7 3--8 4--8 6--8 7--8
## 
## [[4]]
## IGRAPH 8e4a37d UNW- 4 5 -- 
## + attr: name (v/c), weight (e/n)
## + edges from 8e4a37d (vertex names):
## [1] 1--3 1--4 3--4 3--8 4--8
## 
## [[5]]
## IGRAPH db613d8 UNW- 8 16 -- 
## + attr: name (v/c), weight (e/n)
## + edges from db613d8 (vertex names):
##  [1] 1 --2  1 --5  2 --5  5 --7  2 --9  5 --9  5 --14 1 --15 2 --15 5 --15
## [11] 9 --15 1 --16 2 --16 5 --16 14--16 15--16
## 
## [[6]]
## IGRAPH 97b0c4d UNW- 11 32 -- 
## + attr: name (v/c), weight (e/n)
## + edges from 97b0c4d (vertex names):
##  [1] 1 --2  1 --3  2 --3  1 --6  2 --6  3 --6  3 --7  6 --7  3 --8  6 --8 
## [11] 7 --8  2 --9  6 --9  6 --11 7 --11 8 --11 9 --11 1 --12 6 --12 7 --12
## [21] 8 --12 11--12 6 --13 7 --13 9 --13 11--13 1 --16 2 --16 6 --16 11--16
## [31] 12--16 13--16
## 
## [[7]]
## IGRAPH 5e9a54a UNW- 8 17 -- 
## + attr: name (v/c), weight (e/n)
## + edges from 5e9a54a (vertex names):
##  [1] 3 --6  3 --7  5 --7  6 --7  3 --8  6 --8  7 --8  6 --11 7 --11 8 --11
## [11] 6 --12 7 --12 8 --12 11--12 6 --13 7 --13 11--13
## 
## [[8]]
## IGRAPH 1875428 UNW- 8 17 -- 
## + attr: name (v/c), weight (e/n)
## + edges from 1875428 (vertex names):
##  [1] 3 --4  3 --6  3 --7  6 --7  3 --8  4 --8  6 --8  7 --8  6 --11 7 --11
## [11] 8 --11 6 --12 7 --12 8 --12 11--12 8 --14 12--14
## 
## [[9]]
## IGRAPH 0187987 UNW- 8 20 -- 
## + attr: name (v/c), weight (e/n)
## + edges from 0187987 (vertex names):
##  [1] 2 --5  2 --6  2 --9  5 --9  6 --9  2 --10 9 --10 6 --11 9 --11 10--11
## [11] 6 --13 9 --13 10--13 11--13 2 --15 5 --15 9 --15 10--15 11--15 13--15
## 
## [[10]]
## IGRAPH b0a37f3 UNW- 6 13 -- 
## + attr: name (v/c), weight (e/n)
## + edges from b0a37f3 (vertex names):
##  [1] 2 --9  2 --10 9 --10 9 --11 10--11 9 --13 10--13 11--13 2 --15 9 --15
## [11] 10--15 11--15 13--15
## 
## [[11]]
## IGRAPH c616e21 UNW- 10 29 -- 
## + attr: name (v/c), weight (e/n)
## + edges from c616e21 (vertex names):
##  [1] 6 --7  6 --8  7 --8  6 --9  9 --10 6 --11 7 --11 8 --11 9 --11 10--11
## [11] 6 --12 7 --12 8 --12 11--12 6 --13 7 --13 9 --13 10--13 11--13 9 --15
## [21] 10--15 11--15 12--15 13--15 6 --16 11--16 12--16 13--16 15--16
## 
## [[12]]
## IGRAPH 2436ff2 UNW- 9 23 -- 
## + attr: name (v/c), weight (e/n)
## + edges from 2436ff2 (vertex names):
##  [1] 1 --6  6 --7  6 --8  7 --8  6 --11 7 --11 8 --11 1 --12 6 --12 7 --12
## [11] 8 --12 11--12 8 --14 12--14 1 --15 11--15 12--15 1 --16 6 --16 11--16
## [21] 12--16 14--16 15--16
## 
## [[13]]
## IGRAPH 4723875 UNW- 9 22 -- 
## + attr: name (v/c), weight (e/n)
## + edges from 4723875 (vertex names):
##  [1] 6 --7  6 --9  9 --10 6 --11 7 --11 9 --11 10--11 6 --13 7 --13 9 --13
## [11] 10--13 11--13 13--14 9 --15 10--15 11--15 13--15 6 --16 11--16 13--16
## [21] 14--16 15--16
## 
## [[14]]
## IGRAPH 440df86 UNW- 6 9 -- 
## + attr: name (v/c), weight (e/n)
## + edges from 440df86 (vertex names):
## [1] 8 --12 5 --14 8 --14 12--14 13--14 5 --16 12--16 13--16 14--16
## 
## [[15]]
## IGRAPH 399a198 UNW- 10 29 -- 
## + attr: name (v/c), weight (e/n)
## + edges from 399a198 (vertex names):
##  [1] 1 --2  1 --5  2 --5  2 --9  5 --9  2 --10 9 --10 9 --11 10--11 1 --12
## [11] 11--12 9 --13 10--13 11--13 1 --15 2 --15 5 --15 9 --15 10--15 11--15
## [21] 12--15 13--15 1 --16 2 --16 5 --16 11--16 12--16 13--16 15--16
## 
## [[16]]
## IGRAPH 8014765 UNW- 10 29 -- 
## + attr: name (v/c), weight (e/n)
## + edges from 8014765 (vertex names):
##  [1] 1 --2  1 --5  2 --5  1 --6  2 --6  6 --11 1 --12 6 --12 11--12 6 --13
## [11] 11--13 5 --14 12--14 13--14 1 --15 2 --15 5 --15 11--15 12--15 13--15
## [21] 1 --16 2 --16 5 --16 6 --16 11--16 12--16 13--16 14--16 15--16