在R中多次在节点之间生成概率的随机边缘?

时间:2018-03-08 07:19:18

标签: r random igraph network-analysis

所以我想生成一个包含3个节点的随机网络。边缘必须在两个节点之间以特定的概率或强度随机。

选择具有以下概率的节点: 节点1:0.6 节点2:0.3 节点3:0.1

我想多次这样做,因为这是时态数据的一部分。所以在每个时间戳一个连接。

我在R中用igraph做它。但是ER模型无法做到。

知道我该怎么办?

编辑:边缘是定向的。 (注意:我是一名生物学学生,而不是一个与网络合作的核心人士。所以任何方向和建议都会有所帮助。)

1 个答案:

答案 0 :(得分:1)

你想要什么似乎是不可能的。有三种可能的边缘:1-2,1-3和2-3。设p_ij表示选择边i-j的概率。注意,给定节点出现在随机选择的边缘上的概率是涉及该节点的两个边缘概率的总和,例如,

p(1) = p_12 + p_13

您似乎希望p_ij满足:

p_12 + p_13 = 0.6
p_12 + p_23 = 0.3
p_13 + p_23 = 0.1
p_12 + p_13 + p_23 = 1

进一步限制每个p_ij >= 0

这根本不可能。前三个方程产生p_12 = 0.4p_13 = 0.2p_23 = -0.1,这违反了第四个方程和非负性约束。

如果这个论点没有暗示你想要的东西是不可能的,请解释你想要做的更清楚。

开启编辑:如果您想模拟节点之间的移动(根据您的评论),您可以执行以下操作:

#the following function takes a vector, nodes of
#populations and picks a random move of an individual
#from one node to another. Chosen source node is
#is picked with probability which is proportional
#to the population. Return value is a vector of
#the form c(from,to) 

move <- function(nodes){
  total.pop <- sum(nodes)
  n <- length(nodes)
  from <- sample(1:n,1,prob = nodes/total.pop)
  to <- sample((1:n)[-from],1)
  c(from,to)
}

#The following function takes an initial population
#distribution and simulates n moves according to the above
#it returns a dataframe with one row per step

moves <- function(nodes,steps){
  n <- length(nodes)
  current <- nodes #current nodes
  A <- matrix(0,steps+1,n+3)
  A[1,] <- c(0,c(NA,NA),nodes)
  for(i in 1:steps){
    v <- move(current)
    current[v[1]] <- current[v[1]] - 1
    current[v[2]] <- current[v[2]] + 1
    A[i+1,] <- c(i,v,current)
  }
  df <- data.frame(A)
  names(df) <- c("Step","From","To",paste("Node",1:n,sep = ""))
  df
}

例如:

> sim <- moves(c(60,30,10),1000)
> plot(sim$Step,sim$Node1,type = "l",ylim = c(0,100),xlab = "Time Step", ylab = "populations")
> points(sim$Step,sim$Node2,type = "l",col = "blue")
> points(sim$Step,sim$Node3,type = "l",col = "red")

输出:

enter image description here