简体   繁体   English

R,求度、接近度和介数的误差

[英]R, error in finding degree, closeness and betweenness

Trying to apply the common measures of centrality to a simple data set of non-directed like this:尝试将常见的中心性度量应用于一个简单的无向数据集,如下所示:

在此处输入图像描述

It gives error:它给出了错误:

Error in closeness(net, gmode = "graph") : unused argument (gmode = "graph")

When I removed the argument (gmode = "graph), it gives:当我删除参数 (gmode = "graph) 时,它给出:

Error in degree(W) : Not a graph object 

I have tried to use this lines to convert them, but still doesn't work:我曾尝试使用这些行来转换它们,但仍然无法正常工作:

W <- graph_from_adjacency_matrix(df)
W <- graph_from_data_frame(df)

How can I correct them?我该如何纠正它们? thank you.谢谢你。

Here are the lines:以下是这些行:

Bob <- c(0,1,0,0,0)
Kate <- c(0,0,0,1,0)
David <- c(0,0,0,1,0)
Jack <- c(0,0,1,0,0)
Peter <- c(0,1,0,0,1)
df <- data.frame(Bob, Kate, David, Jack, Peter)

library(igraph)

W <- data.frame(df)
net <- network(W)

net %v% 'vertex.names'

degree(W, gmode="graph")
closeness(net, gmode="graph")
betweenness(net, gmode="graph")

Add-on after this question is answered, in case it can help someone - to convert Excel format into a adjacency_matrix, use below lines.回答此问题后的附加组件,以防它可以帮助某人 - 将 Excel 格式转换为 adjacency_matrix,请使用以下行。

df <- readxl::read_excel("spreadsheet.xlsx", sheet = "Sheet1")
W <- as.matrix(df)
W <- graph_from_adjacency_matrix(W)

Your code is somewhat mysterious, suggesting perhaps a use of of some other package?您的代码有些神秘,暗示可能使用了其他包? There is no such function network in igraph , functions degree , closeness , and betweenness don't have argument gmode . igraph中没有这样的函数network ,函数degreegmodebetweenness没有参数closeness I believe the following is what you were after:我相信以下是您所追求的:

library(igraph)
# We are going to use graph_from_adjacency_matrix, so we need a matrix
# rather than a data frame
df <- cbind(Bob, Kate, David, Jack, Peter)

W <- graph_from_adjacency_matrix(df)

V(W)$name
# [1] "Bob"   "Kate"  "David" "Jack"  "Peter"

degree(W)
#   Bob  Kate David  Jack Peter 
#     1     3     2     3     3 
closeness(W)
#        Bob       Kate      David       Jack      Peter 
# 0.05000000 0.08333333 0.11111111 0.16666667 0.05000000 
# Warning message:
# In closeness(W) :
#   At centrality.c:2784 :closeness centrality is not well-defined for disconnected graphs
betweenness(W)
#   Bob  Kate David  Jack Peter 
#     0     4     0     3     0 

The function degree exists both in igraph and sna package.功能度同时存在于 igraph 和 sna 包中。 However, gmode argument only exists in the sna package version of it.但是,gmode 参数只存在于它的 sna 包版本中。 A basic solution for this can be using sna::degree(net, gmode="graph") can be helpful for this issue.一个基本的解决方案是使用sna::degree(net, gmode="graph")可以帮助解决这个问题。

Source:资源:

degree {sna} R Documentation学位 {sna} R 文档

Compute the Degree Centrality Scores of Network Positions计算网络位置的度中心性分数

Description Degree takes one or more graphs (dat) and returns the degree centralities of positions (selected by nodes) within the graphs indicated by g.描述 Degree 采用一个或多个图 (dat) 并返回由 g 指示的图中位置(由节点选择)的度中心性。 Depending on the specified mode, indegree, outdegree, or total (Freeman) degree will be returned;根据指定的模式,将返回入度、出度或总(弗里曼)度; this function is compatible with centralization, and will return the theoretical maximum absolute deviation (from maximum) conditional on size (which is used by centralization to normalize the observed centralization score).此函数与集中化兼容,并将返回以大小为条件的理论最大绝对偏差(与最大值)(集中化用于标准化观察到的集中化分数)。

Usage用法

degree(dat, g=1, nodes=NULL, gmode="digraph", diag=FALSE,
    tmaxdev=FALSE, cmode="freeman", rescale=FALSE, ignore.eval=FALSE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM