Last active
May 15, 2023 12:21
-
-
Save shirayuca/9e9999d30d48629ea8460e69bcb27223 to your computer and use it in GitHub Desktop.
情報学特殊講義A_06回 (鈴木努『ネットワーク分析 第2版 (Rで学ぶデータサイエンス) 』共立出版、2017年参照)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### パッケージの準備 | |
install.packages("igraph") | |
library(igraph) | |
### 木グラフ(ケーリー・ツリー) | |
graph1 <- graph.tree(20, mode = "undirected") #graph1に、ノードの数20で木グラフを作成 | |
plot(graph1) #graph1をプロット | |
average.path.length(graph1) #graph1の平均経路長を算出 | |
#ノードの数を 50, 100, 200 と変化させ、作図と平均経路長を算出してみよう | |
### 正方格子 | |
graph2 <- graph.lattice(c(10,10)) #graph2に、10×10の正方格子のグラフを作成 | |
plot(graph2) #graph2をプロット | |
average.path.length(graph2) #graph2の平均経路長を算出 | |
#20×20の格子も、作図と平均経路長の算出をしてみよう | |
### ランダム・グラフ | |
graph3 <- random.graph.game(50, p=0.05) #graph3に、ランダム・グラフを作成する。ノード数は50、リンクを貼る確率は0.05 | |
plot(graph3) #graph3をプロット | |
average.path.length(graph3) #graph3の平均経路長を算出 | |
#p=0.1, 0.3と変化させ、作図と平均経路長の算出をしてみよう |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### パッケージの準備 | |
library(igraph) | |
### ワッツ・ストロガッツモデルのグラフ作成 | |
# 6行目から10行目までをまとめてコピー、ペースト(教科書と同じ図を作ってみるだけのコードのため、詳しい説明は割愛) | |
par(mfrow = c(1,3)) | |
for(prob in c(0, 0.2, 1)) { | |
plot(sample_smallworld(dim = 1, size = 16, nei = 2, p = prob),layout = layout_in_circle, vertex.size = 5, vertex.label = NA) | |
text(0, -1.3, paste("p=", prob), cex = 2) | |
} | |
### リンクの架け替えによる平均クラスター係数と平均経路長の変化シミュレーション | |
### ノード数1000、各ノードの次数が10となるレギュラーグラフから開始し、確率pでリンクを架け替えていくことでスモールワールド性が生まれることを確認 | |
# ノード数1000、各ノードの次数10のレギュラーグラフを作成し、平均クラスター係数と平均経路長を算出 | |
reg <- sample_smallworld(dim = 1, size = 1000, nei = 5, p = 0) #ノード数1000、各ノードの次数が10となるレギュラーグラフを作成 | |
(reg.clust <- transitivity(reg, type = "local")[1]) #平均クラスター係数を算出 | |
(reg.dist <- mean_distance(reg)) #平均経路長を算出 | |
# 確率pを0.0001から1まで変化させながら、平均クラスター係数と平均経路長を調べる。 | |
# 変化がよく見られるよう、pの値が小さい範囲ほど小刻みにpを変化させ、pの値ごとに20回試行した平均値を、確率pの平均クラスター係数と平均経路長として記録。 | |
# 25行目から38行目までをまとめてコピー、ペースト。計算に時間がかかるので、「>」が表示されるまで待ってください。 | |
prob <- 10^seq(-4, 0, by = 0.2) | |
mean.clust <- 1:length(prob) | |
mean.dist <- 1:length(prob) | |
for (i in 1:length(prob)) { | |
clust <- 1:20 | |
dist <- 1:20 | |
for (j in 1:20) { | |
g <- sample_smallworld(dim = 1, size = 1000, nei = 5, p = prob[i]) | |
clust[j] <- mean(transitivity(g, type = "local"), na.rm = TRUE) | |
dist[j] <- mean_distance(g) | |
} | |
mean.clust[i] <- mean(clust, na.rm = TRUE) | |
mean.dist[i] <- mean(dist, na.rm = TRUE) | |
} | |
# 各pの平均クラスター係数と平均経路長を、最初のレギュラーグラフにおける平均クラスター係数と平均経路長で割り、初期値を1とした比率として片対数グラフで表示 | |
# 42行目から44行目をまとめてコピー、ペースト | |
y <- cbind(mean.clust/reg.clust, mean.dist/reg.dist) | |
par(mfrow = c(1,1)) | |
matplot(prob, y, log = "x", pch = 1:2, xlab = "p", ylab = "") | |
legend("bottomleft", pch = 1:2, col = 1:2, c("clustering coefficient", "average distance")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment