r - 如何在 R 中的多个模拟图形上应用一个函数

标签 r igraph

我需要一个函数在多个模拟图(例如分类性)上的多个结果的向量(或列表)。 这是为了模拟蒙特卡罗程序,用随机 block 模型而不是 Erdos-Renyi 模型执行分离分析。由于数据收集的设计,这允许使用类似于条件统一图的方法来区分两种类型的参与者之间的链接概率。 我尝试使用以下循环失败:

library(igraph)
k <- cbind(c(.2,.2),c(.2,0))  # one block without data of its links 
attrib <- c(1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,5,5,5,5,5,5,5,5,5,1,1,2,2,2,2,2,2,2,3,3,4,4,4,4,4,4,5,5,5,5,5,5,5,5)  # observed data attributes for each block

for(i in 1:1000) {
  sbm[i] <- sample_sbm(49, pref.matrix=k,block.sizes=c(24,25))
  sbm[i] <- set_vertex_attr(sbm[i], "att",value=attrib)
  sbm_ass[i] <- assortativity_nominal(sbm[i],V(sbm[i])$att,directed=F)
 }

我也试过只在最后一行使用 [i]:“sbm_ass[i]...”。 我想需要一个功能,但我一直无法实现它。

最佳答案

我认为 replicate 是您可能需要的东西

sbm_ass <- replicate(
  1000,
  sample_sbm(49, pref.matrix = k, block.sizes = c(24, 25)) %>%
    set_vertex_attr(name = "att", value = attrib) %>%
    assortativity_nominal(V(.)$att, directed = F)
)

关于r - 如何在 R 中的多个模拟图形上应用一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70979920/

相关文章:

c++ - Netbeans 无法识别 igraph 函数

igraph - 如果我们在 python Igraph v1.7 中有顶点对象,我如何找到顶点 id?

r - 在 R 中,为什么 is.integer(1) 返回 FALSE?

r - ggplot : reduce the space reserved for y-axis text

r - 在 Forecast() 和 ar() 中传递参数时 R 中的预测错误

R - 如何修改图例?

r - 在R中使用igraph获取连接的组件

r - 为什么 R 将 UTF-8 header 读取为文本?

c++ - 使用 Rcpp 开发现有的 R 包

python - 将 neo4j 查询结果加载到 python 的 `igraph` 图中