我正在准备使用statnet库在R中运行ERGM的一些网络数据。我想为运行ERGM时要使用的边缘分配一个属性。该矩阵包括网络中每个连接的0到1之间的数字。使用set.edge.attribute时出现错误,提示“ set.edge.attribute中给定的值不合适”。
我首先认为包含我要添加的属性的矩阵中的值可能存在问题。为了检查这一点,我创建了一个带有随机数的矩阵,然后再次运行set.edge.attribute代码,但仍然出现错误。
我将网络和edge属性作为CSV文件导入,将网络文件转换为网络对象,并将edge属性转换为矩阵。 edge属性与网络具有相同数量的边。
library(statnet)
NetworkGraph = network(NetworkData,type="adjacency", directed=FALSE)
EdgeInfo = as.matrix(EdgeInfo)
NetworkGraph<-set.edge.attribute(NetworkGraph,"edge_attribute", EdgeInfo)
要生成用于测试的属性矩阵,我使用runif制作了一个新矩阵,但仍然出现相同的错误):
Test = matrix(runif(23*23), nrow=23, ncol=23)
NetworkGraph<-set.edge.attribute(NetworkGraph,"edge_attribute", Test)
什么可以使这项工作?
答案 0 :(得分:0)
一种方法是直接从值矩阵创建网络:
> library(network)
# create a small test matrix
> gvalues<-matrix(runif(5*5),nrow=5,ncol=5)
> gvalues
[,1] [,2] [,3] [,4] [,5]
[1,] 0.6456140 0.88881086 0.2855281 0.79027269 0.01526509
[2,] 0.4825466 0.64184675 0.4456986 0.44277690 0.27018424
[3,] 0.5276330 0.04742485 0.7675878 0.05453299 0.36940432
[4,] 0.3188620 0.52574674 0.1642077 0.07034616 0.60229633
[5,] 0.3741370 0.22432400 0.8093938 0.24704229 0.29042967
# convert to network object, telling it to *not* ignore edge values
# and also name the edge values 'testValue'
> g <- as.network(gvalues,ignore.eval = FALSE, loops=TRUE, names.eval='testValue')
# check that the edge value was added. since it was a full matrix, it should be a 'complete' network with 25 edges connecting all nodes
> g
Network attributes:
vertices = 5
directed = TRUE
hyper = FALSE
loops = TRUE
multiple = FALSE
bipartite = FALSE
total edges= 25
missing edges= 0
non-missing edges= 25
Vertex attribute names:
vertex.names
Edge attribute names:
testValue
# convert network back to a matrix to check if it worked
> as.matrix(g,attrname = 'testValue')
1 2 3 4 5
1 0.6456140 0.88881086 0.2855281 0.79027269 0.01526509
2 0.4825466 0.64184675 0.4456986 0.44277690 0.27018424
3 0.5276330 0.04742485 0.7675878 0.05453299 0.36940432
4 0.3188620 0.52574674 0.1642077 0.07034616 0.60229633
5 0.3741370 0.22432400 0.8093938 0.24704229 0.29042967
我包括了loops=TRUE
参数,因为输入矩阵的对角线上有值,否则不需要此值。如果要从matrix
向现有网络添加边缘属性,则需要使用set.edge.value()
而不是set.edge.attribute()
。这似乎记录不充分。