我正在尝试格式化数据以运行ERGM模型。但是,我在组合我想用于ergm模型的两个数据集时遇到了问题。首先,我的实际数据集是一个二分网络,第一类节点(政客)作为行,第二类节点(政治家对一个主题的每一票)作为列。但是,在尝试向政治家添加属性时,我遇到了错误,即我的sna对象不是图形对象。为了找出问题所在,我使用了网站上的一个例子(链接贝娄)并稍微改了一下,以便它也是一个二分网络。我遇到了与原始数据集相同的问题。
http://www.mjdenny.com/Preparing_Network_Data_In_R.html
这里是可重现的例子
rm(list = ls())
# install.packages("statnet", dependencies = TRUE)
library(statnet)
num_nodes <- 10 # in my real data set, these would be the politicians
num_events <- 30 # in my real data set, these would be the votes
my_sociomatrix <- matrix(round(runif(num_nodes*num_events)), # edge values
nrow = num_events, #nrow must be same as ncol
ncol = num_nodes)
diag(my_sociomatrix) <- 0
class(my_sociomatrix)
net <- as.network(x = my_sociomatrix, # the network object
directed = TRUE, # specify whether the network is directed
bipartite = TRUE,
loops = FALSE, # do we allow self ties (should not allow them)
matrix.type = "adjacency" # the type of input
)
添加名称似乎有效:
network.vertex.names(net) <- LETTERS[1:10]
network.vertex.names(net) <- c("Susan","Rachel","Angela","Carly","Stephanie","Tom","Mike","Tony","Matt","Steven")
gender <- c(rep("Female",num_nodes/2),rep("Male",num_nodes/2))
print(gender)
以下是不起作用的部分:
set.vertex.attribute(net, # the name of the network object
"Gender", # the name we want to reference the variable by in that object
gender # the value we are giving that variable
)
这是需要解决的错误:
Error in i_set_vertex_attr(graph = graph, name = name, index = index, :
Not a graph object
有没有人知道如何向二分sna对象添加属性(sna对象应该允许我运行ERGM)?如果我按照网站上的示例(上面的链接)而不是二分网络,则代码可以正常工作。