看起来自从@G5W发布这篇优秀的答案之后,igraph
可能已经发生了变化。 closeness
的igraph文档已更改:
一个顶点的中心性定义为到所有其他顶点的距离之和的倒数:
如果顶点v和i之间没有(有向)路径,则i在计算中被省略。如果没有其他顶点可以从v到达,则将其中心度作为NaN返回。
输出也已更改。使用原始示例:
g <- graph(c("Amy", "Ram",
"Ram", "Li",
"Li", "Amy",
"Amy", "Li",
"Kate", "Li"), directed=TRUE)
closeness(g, mode = "out")
Amy Ram Li Kate
0.5000000 0.3333333 0.3333333 0.1666667
这个结果与上面的例子不同,且正确答案并未在接受的答案中得到复现(链接在上方,并在此处重现),可能是因为函数 igraph::closeness 发生了改变:
Dist <- distances(g, mode="out")
Dist[Dist == Inf] <- vcount(g)
1/rowSums(Dist)
Amy Ram Li Kate
0.1666667 0.1428571 0.1428571 0.1666667
注意:这仍然与@G5W的输出匹配,但不再与匹配。 相反,与OP更相似的东西,除了使用而不是,可以匹配:
Dist <- distances(g, mode="out")
Dist[Dist == Inf] <- NA
1/rowSums(Dist,na.rm=T)
Amy Ram Li Kate
0.5000000 0.3333333 0.3333333 0.1666667
会话信息:
> sessionInfo()
R version 4.2.2 (2022-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.utf8
[2] LC_CTYPE=English_United States.utf8
[3] LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.utf8
attached base packages:
[1] stats graphics grDevices utils datasets
[6] methods base
other attached packages:
[1] qgraph_1.9.2 igraph_1.3.2 tidyr_1.2.0 dplyr_1.0.9
[5] ggplot2_3.4.0
loaded via a namespace (and not attached):
[1] splines_4.2.2 gtools_3.9.2.2
[3] Formula_1.2-4 assertthat_0.2.1
[5] stats4_4.2.2 latticeExtra_0.6-29
[7] yaml_2.3.5 pbivnorm_0.6.0
[9] pillar_1.7.0 backports_1.4.1
[11] lattice_0.20-45 glue_1.6.2
[13] digest_0.6.29 RColorBrewer_1.1-3
[15] checkmate_2.1.0 colorspace_2.0-3
[17] sandwich_3.0-2 htmltools_0.5.2
[19] Matrix_1.5-1 plyr_1.8.7
[21] psych_2.2.5 pkgconfig_2.0.3
[23] purrr_0.3.4 xtable_1.8-4
[25] corpcor_1.6.10 mvtnorm_1.1-3
[27] scales_1.2.0 glasso_1.11
[29] jpeg_0.1-9 fdrtool_1.2.17
[31] emmeans_1.8.2 tibble_3.1.7
[33] htmlTable_2.4.0 generics_0.1.2
[35] farver_2.1.0 ellipsis_0.3.2
[37] TH.data_1.1-1 withr_2.5.0
[39] pbapply_1.5-0 nnet_7.3-18
[41] mnormt_2.1.0 cli_3.6.0
[43] survival_3.4-0 magrittr_2.0.3
[45] crayon_1.5.1 estimability_1.4.1
[47] evaluate_0.15 fansi_1.0.3
[49] nlme_3.1-160 MASS_7.3-58.1
[51] foreign_0.8-83 tools_4.2.2
[53] data.table_1.14.2 lifecycle_1.0.3
[55] multcomp_1.4-20 stringr_1.4.0
[57] munsell_0.5.0 cluster_2.1.4
[59] compiler_4.2.2 rlang_1.0.6
[61] grid_4.2.2 rstudioapi_0.13
[63] htmlwidgets_1.5.4 lavaan_0.6-11
[65] base64enc_0.1-3 labeling_0.4.2
[67] rmarkdown_2.14 gtable_0.3.0
[69] codetools_0.2-18 abind_1.4-5
[71] DBI_1.1.2 reshape2_1.4.4
[73] R6_2.5.1 gridExtra_2.3
[75] zoo_1.8-10 knitr_1.39
[77] performance_0.10.1 fastmap_1.1.0
[79] utf8_1.2.2 Hmisc_4.7-0
[81] insight_0.18.8 stringi_1.7.6
[83] parallel_4.2.2 Rcpp_1.0.8.3
[85] vctrs_0.5.1 rpart_4.1.19
[87] png_0.1-7 tidyselect_1.1.2
[89] xfun_0.31 coda_0.19-4