如何在R中使用ergm构建无孤立节点的全连接网络?
解决ergm模拟无孤立节点网络的问题
要在ergm中直接生成无孤立节点的网络,不能简单给isolates项设系数0——这只是让模型对孤立节点的偏好中性,没法强制消除。正确做法是用硬约束来限制网络必须满足“无孤立节点”的条件,而非把isolates作为模型项。
具体实现步骤
- 移除原模型公式里的
isolates项,改用constraints = ~isolates(0)强制要求孤立节点数量为0 - 保留
edges和nodematch("wearable")作为模型核心项,维持你需要的网络连接特征
修改后的完整代码
N <- 100 # 初始化空有向网络 nett <- network(N, directed=TRUE, density=0) # 设置节点wearable属性 nett <- set_vertex_attribute(nett, "wearable", rep(c("Y","N"), times=c(N*0.21, N*0.79))) # 模拟带无孤立节点约束的网络 nt.sim <- simulate(nett ~ edges + nodematch("wearable"), coef=c(qlogis(0.05), qlogis(0.1)), constraints = ~isolates(0)) # 定义节点颜色并绘图 nodeColors <- ifelse(get.vertex.attribute(nt.sim, "wearable") == "Y", "red", "blue") plot(nt.sim, vertex.col=nodeColors)
关键说明
constraints = ~isolates(0)是硬约束,模拟过程会直接排除所有含孤立节点的网络结构,从根源保证生成的网络无孤立节点- 若你需要的是每个节点同时有入边和出边的有向网络,可以把约束改成
constraints = ~outdegree(min=1) + indegree(min=1),进一步强化节点的连接要求
内容的提问来源于stack exchange,提问作者mxiao11
相关产品推荐
相关产品推荐

