網(wǎng)絡(luò)數(shù)據(jù)的統(tǒng)計分析-R語言實戰(zhàn)

資料:《Statistical Analysis of Network Data with R》

語言R常見的網(wǎng)絡(luò)分析包:

  • 基礎(chǔ)網(wǎng)絡(luò)操作、可視化于特征化: igraph、network、sna
  • 網(wǎng)絡(luò)建模:igraph、eigenmodel、ergm、mixer
  • 網(wǎng)絡(luò)建模:glasso、huge

網(wǎng)絡(luò)分析研究大部分是描述性的工作。
網(wǎng)絡(luò)的可視化 即是一門藝術(shù),也是一門科學(xué)。

聚類系數(shù) Clustering cofficient

三元閉包體現(xiàn)了社會網(wǎng)絡(luò)的“傳遞性”(transitivity),枚舉所有節(jié)點三元組中構(gòu)成三角形的比值來表征。

網(wǎng)絡(luò)的可視化和數(shù)值特征化是網(wǎng)絡(luò)分析的首要步驟之一。
網(wǎng)絡(luò)可視化視圖將數(shù)據(jù)的多個重要反面整合在一個圖表中。


同配性 assortativity

該節(jié)點在多大程度上會與同類型或者不同類型的其他節(jié)點進(jìn)行匹配,可以通過一種相關(guān)性統(tǒng)計量(所謂的同配系數(shù))進(jìn)行量化。

將復(fù)雜系統(tǒng)中感興趣的問題與合適的網(wǎng)絡(luò)概括性度量匹配起來,是網(wǎng)絡(luò)特征化方法起作用的關(guān)鍵所在。

模體 motif

網(wǎng)絡(luò)中的頻繁子圖模式

網(wǎng)絡(luò)聚類系數(shù)的分布,用來檢驗社會網(wǎng)路的聚集性上

sand安裝包
網(wǎng)絡(luò)數(shù)據(jù)統(tǒng)計分析 statistical analysis of network data
在CRAN上

install.packages("sand")
# 安裝包statistical analysis of network data 網(wǎng)絡(luò)數(shù)據(jù)統(tǒng)計分析
install.packages("igraph")
library(igraph)
library(igraphdata)
library(sand)
install_sand_packages()

?sand

第2章 操作網(wǎng)絡(luò)數(shù)據(jù)

G=(V,E)
節(jié)點 :vertices 或者 nodes
邊:edges 或者 links
節(jié)點數(shù)量:圖的階數(shù) order
邊的數(shù)量:圖的規(guī)模 size

同構(gòu)圖 isomorphic

無向 undirected
有向 directed graph 或者 digraph
邊:有向邊 directed edges 或 弧 arcs
雙向 mutual

小的圖形用 formulate來創(chuàng)建

# 2.1
library(igraph) # 載入igraph包

# 創(chuàng)建一個圖對象g,包含Nv=7個節(jié)點
g <- graph.formula(1-2, 1-3, 2-3, 2-4, 3-5, 4-5, 4-6, 4-7, 5-6, 6-7)

# 2.2
## 節(jié)點系列:Vertex sequence
V(g)


# 2.3
## 邊序列:Edge squence
E(g)

# 2.4
## 顯示圖的結(jié)構(gòu)
str(g)

# 2.5
## 可視化圖
plot(g)
# 2.6
## 有向圖的創(chuàng)建
dg <- graph.formula(1-+2, 1-+3, 2++3)
plot(dg)
# 2.7
## 1,2,3,...,n的編號是默認(rèn)給定的標(biāo)簽,也可以自己給定標(biāo)簽
dg2 <- graph.formula(Sam-+Mary, Sam-+Tom, Mary++Tom)
str(dg2)
summary(dg2)
str(dg2) # 屬性調(diào)用不清楚
?str()

# 2.8
## 通過節(jié)點的name屬性進(jìn)行修改。
V(dg)$name <- c("小明", "小藍(lán)", "小西")
plot(dg)

三種表示圖的格式:鄰接列表、邊列表、領(lǐng)接矩陣

  • 鄰接列表 adjacency list


  • 邊列表 edge list

E(dg)

+ 4/4 edges from 2ec3d7f (vertex names):
[1] 小明->小藍(lán) 小明->小西 小藍(lán)->小西 小西->小藍(lán)
  • 領(lǐng)接矩陣 adjacency matrix 數(shù)據(jù) 0 和1
# 2.10
## 鄰接矩陣
get.adjacency(g)

7 x 7 sparse Matrix of class "dgCMatrix"
  1 2 3 4 5 6 7
1 . 1 1 . . . .
2 1 . 1 1 . . .
3 1 1 . . 1 . .
4 . 1 . . 1 1 1
5 . . 1 1 . 1 .
6 . . . 1 1 . 1
7 . . . 1 . 1 .

子圖 subgraph

導(dǎo)出子圖 induced subgraph

# 2.11 
## 導(dǎo)出子圖
h <- induced.subgraph(g, 1:5)
plot(h)

## 導(dǎo)出子圖 刪除節(jié)點6和節(jié)點7
h1 <- g - vertices(c(6,7))
plot(h1)
# 2.13
# 給h1增加兩個新節(jié)點,然后在增加邊
h1 <- h1 + vertices(c(6,7))
plot(h1)
g <- h1 + edges(c(4,5), c(4,7), c(5,6), c(6,7))
plot(g)

# 圖的合并
h1 <- h
h2 <- graph.formula(4-6, 4-7, 5-6, 6-7)
g <- graph.union(h1, h2)
plot(g)

2.3 網(wǎng)絡(luò)的修飾

> library(sand)
載入需要的程輯包:igraphdata

Statistical Analysis of Network Data with R
Type in C2 (+ENTER) to start with Chapter 2.
> g.lazega <- graph.data.frame(elist.lazega, directed = "FALSE", vertices = v.attr.lazega)
> g.lazega$name <- "Lazega Lawyers" # 給圖命名
> 
> vcount(g.lazega) # 節(jié)點個數(shù)
[1] 36
> ecount(g.lazega) # 邊的個數(shù)
[1] 115
> 
> list.vertex.attributes(g.lazega) # 節(jié)點的屬性
[1] "name"      "Seniority" "Status"    "Gender"    "Office"    "Years"    
[7] "Age"       "Practice"  "School"   
> is.simple(g) # 判斷是否是簡答圖
[1] TRUE
> E(mg)$weight <- 1 # 給所有的邊賦值為1
> wg2 <- simplify(mg)
> is.simple(wg2)
[1] TRUE
> E(wg2)$weight
 [1] 1 1 2 1 1 1 1 1 1 1
plot(mg)
plot(wg2)

把mg轉(zhuǎn)化為wg2


mg
wg2
> # 圖g中節(jié)點5的鄰居
> neighbors(g,5)
+ 3/7 vertices, named, from bb8e27b:
[1] 3 4 6
> 
> degree(g)
1 2 3 4 5 6 7 
2 3 3 4 3 3 2 
> 
> degree(dg, mode = "in") # 有向圖,入度數(shù)
小明 小藍(lán) 小西 
   0    2    2 
> degree(dg, mode = "out") # 無向圖,出度數(shù)
小明 小藍(lán) 小西 
   2    1    1 
g.full <- graph.full(7)
g.ring <- graph.ring(7)
g.tree <- graph.tree(7, children = 2, mode = "undirected")
g.star <- graph.star(7, mode = "undirected")
par(mfrow=c(2,2))
plot(g.full)
plot(g.ring)
plot(g.tree)
plot(g.star)
# 二部圖
g.bip <- graph.formula(actor1:actor2:actor3, movie1:movie2,
                       actor1:actor2-movie1, actor2:actor3 - movie2)
V(g.bip)$type <- grepl("^movie", V(g.bip)$name)
plot(g.bip)

3 網(wǎng)絡(luò)數(shù)據(jù)可視化

# 3.1
library(sand)
## 數(shù)據(jù)一:5x5x5的網(wǎng)格(3D)
g.l <- graph.lattice(c(5, 5, 5))

# 3.2
## 數(shù)據(jù)二:博客網(wǎng)絡(luò) 數(shù)據(jù)記錄了146個獨立博客之間的引用關(guān)系
data(aidsblog)
summary(aidsblog)

str(aidsblog)
# 這個函數(shù)不能用就調(diào)用基礎(chǔ)的api
vcount(aidsblog) # 節(jié)點個數(shù)
ecount(aidsblog) # 邊的個數(shù)
## 節(jié)點系列:Vertex sequence
V(aidsblog)

## 邊序列:Edge squence
E(aidsblog)

# 3.3
igraph.options(vertex.size=3, vertex.label=NA, edge.arrow.size=0.5)
par(mfrow=c(1, 2))
plot(g.l, layout=layout.circle)
title("5x5x5 Lattice")
plot(aidsblog, layout=layout.circle)
title("Blog Network")
# 3.4
plot(g.l, layout=layout.fruchterman.reingold)
title("5x5x5 Lattice")
plot(aidsblog, layout=layout.fruchterman.reingold)
title("Blog Network")
# 3.5
plot(g.l, layout=layout.kamada.kawai)
title("5x5x5 Lattice")
plot(aidsblog, layout=layout.kamada.kawai)
title("Blog Network")
# 3.7 二部圖的可視化
plot(g.bip, layout=-layout.bipartite(g.bip)[,2:1],
     vertex.size=30, vertex.shapes=ifelse(V(g.bip)$type,
                                          "rectangle", "circlr"),
     vertex.color=ifelse(ifelse(V(g.bip)$type, "red", "cyan")))

Zachary 空手道俱樂部網(wǎng)絡(luò) (karate club network)
數(shù)據(jù)集合實際上只存在兩個社團(tuán),分別以教練為中心和以主管為中心。

# 3.8
library(igraphdata)
data(karate)

# 可重復(fù)的布局
set.seed(42)
l <- layout.kamada.kawai(karate)

# 首先回執(zhí)未修飾的圖
igraph.options(vertex.size=10)
par(mfrow = c(1,1))
plot(karate, layout=1, vertex.label=V(karate))

# 修飾圖,首先設(shè)定標(biāo)簽
V(karate)$label <- sub("Actor", "", V(karate)$name)

# 兩個領(lǐng)導(dǎo)者與其他俱樂部成員的節(jié)點形狀不同
V(karate)$shape <- "circle"
V(karate)[c("Mr Hi", "John A")]$shape <- "rectangle"

# 使用顏色區(qū)分不同的派別
V(karate)[Faction == 1]$color <- "red"
V(karate)[Faction == 2]$color <- "dodgerblue"

# 節(jié)點面積正比于節(jié)點強弱(即所關(guān)聯(lián)邊的權(quán)重值之和)
V(karate)$size <- 4 * sqrt(graph.strength(karate))
V(karate)$size <- v(karate)$size * .5

# 將共同活動的數(shù)量設(shè)定為邊的權(quán)重(粗細(xì))
E(karate)$width <- E(karate)$weight

# 使用顏色區(qū)分派別內(nèi)部和派別之間的邊
F1 <- V(karate)[Faction == 1]
F2 <- V(karate)[Faction == 2]
E(karate)[F1 %--% F1]$color <- "pink"
E(Karate)[F2 %--% F2]$color <- "lightblue"
E(karate)[F1 %--% F2]$color <- "yellow"
#這樣可以看出R語言繪圖確實有很多優(yōu)勢


# 較小節(jié)點的標(biāo)簽位置偏移量(初始為 0)
V(karate)$label.dist <- ifelse(V(karate)$size >= 10, 0, 0.75)

# 使用相同布局繪制修飾后的圖
plot(karate, layout=l)

Lazega律師網(wǎng)絡(luò)可視化

# 3.9
library(sand)
data(lazega)

# 使用顏色表示辦公地點
colbar <- c("red", "dodgerblue", "goldenrod")
v.colors <- colbar[V(lazega)$Office]

# 使用形狀表示執(zhí)業(yè)類型
v.shapes <- c("circle", "square")[V(lazega)$Practice]

# 節(jié)點大小正比于在公司工作了幾年
v.size <- 3.5 * sqrt(V(lazega)$Years)

# 節(jié)點標(biāo)簽為個人的資歷
v.label <- V(lazega)$Seniority

# 可重復(fù)布局
set.seed(42)
l <- layout.fruchterman.reingold(lazega)
plot(lazega, layout=l, vertex.color=v.colors, vertex.shape=v.shapes,
     vertex.size=v.size, vertex.label=v.label)

大型網(wǎng)絡(luò)可視化

srt() 不能用使用 upgrade_graph()d代替

library(sand)
summary(fblog)
upgrade_graph(fblog) 
# upgrade_graph(fblog) = summary(fblog) + str(fblog)
# 新api 舊api
library(sand)
summary(fblog)
upgrade_graph(fblog) 
# upgrade_graph(fblog) = summary(fblog) + str(fblog)
# 新api 舊api

list.vertex.attributes(fblog) # 節(jié)點的屬性

party.names <- sort(unique(V(fblog)$PolParty))

party.names

set.seed(42) # 設(shè)定隨機(jī)數(shù)種子,一個特定的種子可以產(chǎn)生一個特定的偽隨機(jī)序列,
l = layout.kamada.kawai(fblog)
party.nums.f <- as.factor(V(fblog)$PolParty)
party.nums <- as.numeric(party.names.f)
plot(fblog, layout=l, vertex.color=party.nums,
     vertex.size=3, vertex.label=NA)

DrL算法,針對大型網(wǎng)絡(luò)可視化設(shè)計的布局算法。

set.seed(42) # 設(shè)定隨機(jī)數(shù)種子,一個特定的種子可以產(chǎn)生一個特定的偽隨機(jī)序列
l <- layout.drl(fblog)
plot(fblog, layout=l, vertex.size=5, vertex.label=NA, vertex.color=party.nums)

元節(jié)點繪制

節(jié)點的節(jié)點,即社區(qū)節(jié)點(主題節(jié)點)

fblog.c <- contract.vertices(fblog, party.nums)
E(fblog.c)$weight <- 1
fblog.c <- simplify(fblog.c)
party.size <- as.vector(table(V(fblog)$PolParty))
plot(fblog.c, vertex.size=5*sqrt(party.size),
     vertex.label=party.names,vertex.color=V(fblog.c),
     edge.width=sqrt(E(fblog.c)$weight),
     vertex.label.dist=1.5,edge.arrow.size=0)

個體中心網(wǎng)(egocentric network)

即一個中心節(jié)點,一其直接相連的鄰居,以及這些節(jié)點至今的邊。

data("karate")
k.nbhds <- graph.neighborhood(karate, order = 1)

# k.nbhds形成了不同的圖

# 教練(Mr Hi, 節(jié)點1) 和 主管(John A,節(jié)點34)的鄰居規(guī)模最大

sapply(k.nbhds, vcount)  # 函數(shù)vcount 節(jié)點計數(shù)

# 提取連個最大的子網(wǎng)絡(luò)并繪制網(wǎng)絡(luò)
k.1 <- k.nbhds[[1]]
k.34 <- k.nbhds[[34]]
par(mfrow = c(1,2))

plot(k.1, vertex.label=NA,
     vertex.color=c("red", rep("lightblue", 16)))
plot(k.34, vertex.label=NA,
     vertex.color=c(rep("lightblue", 17),"red"))

第四章 網(wǎng)絡(luò)圖特征的描述性分析

4.2節(jié)點和邊

度分布

library(igraph)
library(sand)
data(karate)
hist(degree(karate), col="lightblue", xlim = c(0,50),
     xlab = "Vertex Degree", ylab = "Frequency", main="")

節(jié)點強度分布

  • 強度strength 即與某個節(jié)點相連的邊的權(quán)重之和
hist(graph.strength(karate), col="pink",
     xlab = "Vertex Strength", ylab = "Frequency", main="")
library(igraphdata)
data(yeast)

# 邊的數(shù)量
ecount(yeast)

# 節(jié)點的數(shù)量
vcount(yeast)

d.yeast <- degree(yeast)

par(mfrow = c(1,2))

# 節(jié)點度分布的異質(zhì)性很強
hist(d.yeast, col="blue",
     xlab="Degree",ylab="Frequency",
     main="Degree Distribution")

#度的分布遞減,采用雙對數(shù)坐標(biāo)表達(dá)度的信息更為有效

dd.yeast <- degree.distribution(yeast)
d <- 1:max(d.yeast)-1
ind <- (dd.yeast != 0)
plot(d[ind], dd.yeast[ind], log="xy", col="blue",
     xlab=c("Log-Degree"), ylab=c("Log-Intensity"),
     mian="Log-Log Degree Distribution")

度值不同的節(jié)點以何種方式彼此連接

a.nn.deg.yeast <- graph.knn(yeast, V(yeast))$knn
plot(d.yeast, a.nn.deg.yeast, log="xy",
     col="goldenrod", xlab = c("Log Vertex Degree"),
     ylab = c("Log Average Neighbor Degree"))

節(jié)點中心性(centrality)

  • 接近中心性 closeness centrality

l <- layout.kamada.kawai(aidsblog)

par(mfrow=c(1,2))
plot(aidsblog, layout=l, mian="Hubs",
     vertex.label="", vertex.size=10 *
       sqrt(hub.score(aidsblog)$vector))

plot(aidsblog, layout=l, mian="Authorities",
     vertex.label="", vertex.size=10 *
       sqrt(authority.score(aidsblog)$vector))

4.3 子圖,完全子圖

library(igraphdata)
library(igraph)
data(karate)
# cliques 團(tuán) 完全子圖
table(sapply(cliques(karate), length))

cliques(karate)[sapply(cliques(karate), length) == 5]

data(yeast)
clique.number(yeast)

cores <- graph.coreness(karate)
sna::gplot.target(g, cores, circ.lab = FALSE,
                  circ.col = "skyblue", userrow = FALSE,
                  vertex.col = cores, edge.col="darkgray")
detach("package:network")
detach("package:sna")
plot(aidsblog)
aidsblog <- simplify(aidsblog)
> dyad.census(aidsblog)
$`mut`
[1] 3

$asym
[1] 177

$null
[1] 10405

圖的密度

> ego.instr <- induced.subgraph(karate,
+                               neighborhood(karate, 1, 1)[[1]])
> 
> ego.admin <- induced.subgraph(karate,
+                               neighborhood(karate, 1, 34)[[1]])
> 
> graph.density(karate)
[1] 0.1390374
> 
> graph.density(ego.instr)
[1] 0.25
> 
> graph.density(ego.admin)
[1] 0.2091503

全局聚類系數(shù)

> transitivity(karate)
[1] 0.2556818

局部聚類系數(shù)

> transitivity(karate, "local", vids = c(1,34))
[1] 0.1500000 0.1102941

互惠性 reciprocity
二元組普查

> reciprocity(aidsblog, mode = "default")
[1] 0.03278689
> 
> reciprocity(aidsblog, mode="ratio")
[1] 0.01666667

連通,割與流

> is.connected(yeast)
[1] FALSE
plot(yeast, vertex.label=NA, vertex.size=3,edge.arrow.size=1)

plot(yeast,  layout=layout.kamada.kawai,vertex.label=NA, vertex.size=3,edge.arrow.size=1)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容