r - 网络同质性的计算
问题描述
我对网络分析中网络同质性的计算感到非常困惑。现在,我正在通过以下函数计算同质性,该函数已由以下 URL 编写和描述:http: //dappls.umasscreate.net/networks/calculating-network-homophily-part-1/。该技术的目标是通过网络中所有边的比例来衡量网络中的同质性。我的目标是测量有向网络中的同质性。
功能
homophily <- function(graph,vertex.attr,attr.val=NULL,prop=T){
#Assign names as vertex attributes for edgelist output#
V(graph)$name<-vertex_attr(graph,vertex.attr)
#Get the basic edgelist#
ee<-get.data.frame(graph)
#If not specifying on particular attribute value, get percentage (prop=T)#
#or count (prop=F) of all nodes tied with matching attribute#
if(is.null(attr.val)){
ifelse(prop==T,sum(ee[,1]==ee[,2])/nrow(ee),sum(ee[,1]==ee[,2]))
#If not null, get proportion (prop=T) or count (prop=F) of#
#edges among nodes with that particular node attribute value#
} else {
ifelse(prop==T,sum(ee[,1]==attr.val & ee[,2]==attr.val)/nrow(ee[ee[,1]==attr.val|ee[,2]==attr.val,]),
sum(ee[,1]==attr.val & ee[,2]==attr.val))
}
}
样本数据
set.seed(5165)
#Random directed graph with 100 nodes and 30% chance of a tie#
gg<-random.graph.game(100,0.3,"gnp",directed=T)
#Randomly assign the node attribute (group numbers 0:3)#
V(gg)$group<-sample(1:5,100,replace=T)
输出
通过对样本数据应用该函数,我收到以下输出,这意味着网络中 20% 的关系位于同一组中的参与者之间。也可以按百分比计算特定组的同质性。homophily(graph = abc, vertex.attr = "group")
[1] 0.1971504
然而,我也注意到igraph包还包含一个名为“ assortativity() ”的同质方法,此处描述。执行此函数会收到基于范围(-1, 1)中的分类系数的完全其他结果。分类系数是正的,相似的顶点(基于某些外部属性)倾向于连接到每个顶点,否则为负。
library(igraph)
assortativity(abc, V(abc)$group, directed=T)
[1] -0.02653782
问题
所以现在我很困惑,这些方法中哪一种是衡量网络中同质性的正确方法,因为这两个函数收到了不同的结果。我还注意到 igraph 方法不支持特定组的计算。在我看来,我宁愿选择第一个自编码的(不确定是否有错误),因为解释更有意义。所以我的问题是,以下哪种方法适合测量网络中的同质性?
解决方案
你能澄清一下“但它在某种程度上不一样吗?”?
我最初无法访问该链接并且说错了。上面的homophily()
函数并不完全相同,需要不同的解释。
原始设置和数据:
library(igraph)
set.seed(5165)
gg <- random.graph.game(100, 0.3, "gnp", directed = TRUE)
V(gg)$group <- sample(1:5, 100, replace = TRUE)
原始功能:
homophily <- function(graph,vertex.attr,attr.val=NULL,prop=T){
V(graph)$name<-vertex_attr(graph,vertex.attr)
ee<-get.data.frame(graph)
if(is.null(attr.val)){
ifelse(prop==T,sum(ee[,1]==ee[,2])/nrow(ee),sum(ee[,1]==ee[,2]))
} else {
ifelse(prop==T,sum(ee[,1]==attr.val & ee[,2]==attr.val)/nrow(ee[ee[,1]==attr.val|ee[,2]==attr.val,]),
sum(ee[,1]==attr.val & ee[,2]==attr.val))
}
}
原始结果:
homophily(gg, "group")
#> [1] 0.2017368
返回所有相关详细信息的新函数:
new_homophily <- function(graph, vertex.attr) {
V(graph)$name <- vertex_attr(graph, vertex.attr)
edges <- get.data.frame(graph)
# heterophilous ties where vertices have different `"group"` attributes
external <- length(which(edges$from != edges$to))
# homophilous ties where vertices have the same `"group"` attributes
internal <- length(which(edges$from == edges$to))
list(
n_external = external,
n_internal = internal,
prop_external = external / nrow(edges), # proportion of ties that are heterophilous
prop_internal = internal / nrow(edges), # proportion of ties that are homophilous (the results of your initial function)
ei_index = (external - internal) / nrow(edges) # (EL - IL) / (EL + IL)
)
}
新结果:
new_homophily(gg, "group")
#> $n_external
#> [1] 2390
#>
#> $n_internal
#> [1] 604
#>
#> $prop_external
#> [1] 0.7982632
#>
#> $prop_internal
#> [1] 0.2017368 # the results of your initial function ===================
#>
#> $ei_index
#> [1] 0.5965264
解释$ei_index
应该比$prop_internal
. 接近 +1 的值表示更多的异质性,而接近 -1 的值表示更多的同质性。
如果这符合您的目标,这里有一些替代的 E/I 指数选项:
Michal Bojanowski 的日常。它不在 CRAN 上,但可以在https://github.com/mbojan/isnar上找到
isnar::ei(gg, "group")
#> [1] 0.5965264
完全披露:这是我自己的例行公事。这个包肯定是未完成的,它绝对不在 CRAN 上。https://knapply.github.io/homophily/reference/ei_index.html
homophily::ei_index(gg, node_attr_name = "group")
#> [1] 0.5965264
推荐阅读
- python - 如何定义一个只改变父类的新子类?
- python - 如何在 Django Admin 的用户添加页面导航栏中更改“添加用户”?
- firefox - Firefox 扩展开发:如何修复 XMLHttpRequest 中错误的“Referer”
- python - Featuretools:跳过目标特征
- c# - 根据 DataGridView 文本框值更改事件搜索 ListView 项。应选择匹配的项目,同时文本框焦点不应离开
- javascript - React:使用 .map 不会显示任何数据
- javascript - 修改 observableArray 不会立即更新选择 UI
- arrays - 通过字符串值访问数组
- r - 使用贝叶斯预测 R
- blazor - asp.net core mvc blazor 自动刷新列表