sna R包效率函数不一致

时间:2015-06-16 17:03:44

标签: r graph sna network-efficiency

我有三种不同的矩阵,他们的Krackhardt效率对我来说似乎不对。前两个矩阵在拓扑上是等价的,但它们的效率是不同的。任何人都有不一致的解释?

对于第一个矩阵,效率为1:

A <- matrix(c(0,1,0,0,0,0,0,0,0,0,
          0,0,1,0,0,0,0,0,0,0,
          0,0,0,1,0,0,0,0,0,0,
          0,0,0,0,1,0,0,0,0,0,
          0,0,0,0,0,1,0,0,0,1,
          0,0,0,0,0,0,1,0,0,0,
          0,0,0,0,0,0,0,1,0,0,
          0,0,0,0,0,0,0,0,0,0,
          0,0,0,0,0,0,0,0,0,0,
          0,0,0,0,0,0,0,0,1,0),ncol=10)
A_net <- network(A,directed=TRUE)
g_eff <- efficiency(A_net)
plot.network(A_net, vertex.col = "white", vertex.border = col_ama, 
         usearrows=FALSE, edge.col=col_gri, vertex.lwd = 3.5,
         vertex.cex = 3.5)
title(paste("Efficiency =",round(g_eff,3)))

效率不同的等效矩阵:

A <- matrix(c(0,0,0,0,1,0,0,0,0,0,
          1,0,1,0,0,0,0,0,0,0,  
          0,0,0,0,0,0,1,0,0,0,
          0,0,1,0,0,0,0,0,0,0,
          0,0,0,0,0,1,0,0,0,0,
          0,0,0,0,0,0,0,0,0,0,
          0,0,0,0,0,0,0,0,1,0,
          0,0,0,1,0,0,0,0,0,0,
          0,0,0,0,0,0,0,0,0,1,
          0,0,0,0,0,0,0,0,0,0),ncol=10)
A_net <- network(A,directed=FALSE)
g_eff <- efficiency(A_net)
plot.network(A_net, vertex.col = "white", vertex.border = col_ama, 
         usearrows=FALSE, edge.col=col_gri, vertex.lwd = 3.5,
         vertex.cex = 3.5)
title(paste("Efficiency =",round(g_eff,3)))

这个第三个矩阵有两个具有最小边数(n_i-1)的分量,但它们的效率不是一个。它既不匹配帮助中的公式:

(1 - [ |E(G)| - Sum(N_i-1,i=1,..,n) ]/[ Sum((N_i-1)^2,i=1,..,n) ]   = 1-[8-(2+4)]/[4+16] = .9)

第三个矩阵:

A <- matrix(c(0,0,0,0,1,0,0,0,0,0,
          1,0,0,0,0,0,0,0,0,0,  
          0,0,0,0,0,0,1,0,0,0,
          0,0,1,0,0,0,0,0,0,0,
          0,0,0,0,0,1,0,0,0,0,
          0,0,0,0,0,0,0,0,0,0,
          0,0,0,0,0,0,0,0,1,0,
          0,0,0,1,0,0,0,0,0,0,
          0,0,0,0,0,0,0,0,0,1,
          0,0,0,0,0,0,0,0,0,0),ncol=10)
A_net <- network(A,directed=FALSE)
g_eff <- efficiency(A_net)
g_eff
plot.network(A_net, vertex.col = "white", vertex.border = col_ama, 
         usearrows=FALSE, edge.col=col_gri, vertex.lwd = 3.5,
         vertex.cex = 3.5)
title(paste("Efficiency =",round(g_eff,3)))

1 个答案:

答案 0 :(得分:0)

看来你的例子是错误的:在第一种情况下,你正在使用定向网络,而在第二种情况下是一个无向网络(检查你的网络强制声明 - 顺便说一句,你可以只使用矩阵)。这是一个示例,前两个实际上是等价的:

> A <- matrix(c(0,1,0,0,0,0,0,0,0,0,
+           0,0,1,0,0,0,0,0,0,0,
+           0,0,0,1,0,0,0,0,0,0,
+           0,0,0,0,1,0,0,0,0,0,
+           0,0,0,0,0,1,0,0,0,1,
+           0,0,0,0,0,0,1,0,0,0,
+           0,0,0,0,0,0,0,1,0,0,
+           0,0,0,0,0,0,0,0,0,0,
+           0,0,0,0,0,0,0,0,0,0,
+           0,0,0,0,0,0,0,0,1,0),ncol=10)
> A
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    0    0    0    0    0    0    0    0    0     0
 [2,]    1    0    0    0    0    0    0    0    0     0
 [3,]    0    1    0    0    0    0    0    0    0     0
 [4,]    0    0    1    0    0    0    0    0    0     0
 [5,]    0    0    0    1    0    0    0    0    0     0
 [6,]    0    0    0    0    1    0    0    0    0     0
 [7,]    0    0    0    0    0    1    0    0    0     0
 [8,]    0    0    0    0    0    0    1    0    0     0
 [9,]    0    0    0    0    0    0    0    0    0     1
[10,]    0    0    0    0    1    0    0    0    0     0
> B<-matrix(c(0,0,0,0,1,0,0,0,0,0,
+           1,0,1,0,0,0,0,0,0,0, 
+           0,0,0,0,0,0,1,0,0,0,
+           0,0,1,0,0,0,0,0,0,0,
+           0,0,0,0,0,1,0,0,0,0,
+           0,0,0,0,0,0,0,0,0,0,
+           0,0,0,0,0,0,0,0,1,0,
+           0,0,0,1,0,0,0,0,0,0,
+           0,0,0,0,0,0,0,0,0,1,
+           0,0,0,0,0,0,0,0,0,0),ncol=10)
> A
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    0    0    0    0    0    0    0    0    0     0
 [2,]    1    0    0    0    0    0    0    0    0     0
 [3,]    0    1    0    0    0    0    0    0    0     0
 [4,]    0    0    1    0    0    0    0    0    0     0
 [5,]    0    0    0    1    0    0    0    0    0     0
 [6,]    0    0    0    0    1    0    0    0    0     0
 [7,]    0    0    0    0    0    1    0    0    0     0
 [8,]    0    0    0    0    0    0    1    0    0     0
 [9,]    0    0    0    0    0    0    0    0    0     1
[10,]    0    0    0    0    1    0    0    0    0     0
> B
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    0    1    0    0    0    0    0    0    0     0
 [2,]    0    0    0    0    0    0    0    0    0     0
 [3,]    0    1    0    1    0    0    0    0    0     0
 [4,]    0    0    0    0    0    0    0    1    0     0
 [5,]    1    0    0    0    0    0    0    0    0     0
 [6,]    0    0    0    0    1    0    0    0    0     0
 [7,]    0    0    1    0    0    0    0    0    0     0
 [8,]    0    0    0    0    0    0    0    0    0     0
 [9,]    0    0    0    0    0    0    1    0    0     0
[10,]    0    0    0    0    0    0    0    0    1     0
> efficiency(A)
[1] 1
> efficiency(B)
[1] 1

如果您通过设置“directed = FALSE”来对称 - 这是您在示例中对其中一个网络所做的 - 那么您将获得0.889的效率。请注意,这与我们应该得到的相符:

1 - (18 - 9)/(选择(10,2)* 2-9)= 0.889

(请记住,Krackhardt效率会将所有网络视为有向图,因此相互边缘会被视为两条边。此外,我注意到您似乎错误地使用了手册中的公式,这可能是您混淆的一部分。)

你的第三个矩阵再次是效率1,因为它没有多余的边缘:

> C<-matrix(c(0,0,0,0,1,0,0,0,0,0,
+           1,0,0,0,0,0,0,0,0,0, 
+           0,0,0,0,0,0,1,0,0,0,
+           0,0,1,0,0,0,0,0,0,0,
+           0,0,0,0,0,1,0,0,0,0,
+           0,0,0,0,0,0,0,0,0,0,
+           0,0,0,0,0,0,0,0,1,0,
+           0,0,0,1,0,0,0,0,0,0,
+           0,0,0,0,0,0,0,0,0,1,
+           0,0,0,0,0,0,0,0,0,0),ncol=10)
> C
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    0    1    0    0    0    0    0    0    0     0
 [2,]    0    0    0    0    0    0    0    0    0     0
 [3,]    0    0    0    1    0    0    0    0    0     0
 [4,]    0    0    0    0    0    0    0    1    0     0
 [5,]    1    0    0    0    0    0    0    0    0     0
 [6,]    0    0    0    0    1    0    0    0    0     0
 [7,]    0    0    1    0    0    0    0    0    0     0
 [8,]    0    0    0    0    0    0    0    0    0     0
 [9,]    0    0    0    0    0    0    1    0    0     0
[10,]    0    0    0    0    0    0    0    0    1     0
> efficiency(C)
[1] 1

您的问题源于对称化(使用directed = FALSE将C强制转换为网络对象)。这会增加额外的边缘,导致

1-(16 - 3 - 5)/(选择(4,2)* 2 - 3 +选择(6,2)* 2 - 5)= 0.7647059

这相当于sna给你的东西:

> efficiency(symmetrize(C))
[1] 0.7647059

希望能够解决问题!