0

我是新手R

我正在尝试创建一个时间循环,如果未通知节点,则节点属性的值会随着时间的推移而更新。到目前为止,我有以下内容:

#loading required packages 
library(igraph)
library(tidyverse)
library(ggraph)
library(ggnetwork)
library(tidygraph)

g <- play_erdos_renyi(10, .2)    
M<-matrix(0:1, nrow = 10, ncol = 10)
Periods=10
seeds=c(1,3)

#Seeds are informed 
V(g)$informed <- F
V(g)[seeds]$informed=T
#Seeds have prob 0.5 (just treat prob as any variable here)
V(g)$prob=0
V(g)[seeds]$prob=0.5

#Time loop for creating prob over time
#prob at [t+1]=M*prob at [t]
#calculate prob for vertex v in time t+1 by multiplying 10X10 matrix M with 10X1 vector V(g)$prob
#then considering the v-th row

for (t in 1:Periods) {
  for(v in V(g)) {
    if(!V(g)[v]$informed) {
      V(g)[v]$prob[t+1]= M %*% V(g)$prob[t][v]
    }}}

我正进入(状态

M %*% V(g)$prob[t][v] 中的错误:参数不一致

我想知道如何解决这个问题。

4

2 回答 2

0

我认为你应该使用

for (t in 1:Periods) {
  V(g)$prob <- M %*% matrix(V(g)$prob)
}

如果要跟踪动态,可以使用Reduce

Reduce(
  function(x, A) A %*% x, 
  rep(list(M), Periods),
  init = matrix(V(g)$prob), 
  accumulate = TRUE
)

这让你V(g)$prob随着时间的推移从0length(Periods)

[[1]]
      [,1]
 [1,]  0.5
 [2,]  0.0
 [3,]  0.5
 [4,]  0.0
 [5,]  0.0
 [6,]  0.0
 [7,]  0.0
 [8,]  0.0
 [9,]  0.0
[10,]  0.0

[[2]]
      [,1]
 [1,]    1
 [2,]    2
 [3,]    3
 [4,]    4
 [5,]    5
 [6,]    6
 [7,]    7
 [8,]    8
 [9,]    9
[10,]   10

[[3]]
      [,1]
 [1,]   55
 [2,]  110
 [3,]  165
 [4,]  220
 [5,]  275
 [6,]  330
 [7,]  385
 [8,]  440
 [9,]  495
[10,]  550

[[4]]
       [,1]
 [1,]  3025
 [2,]  6050
 [3,]  9075
 [4,] 12100
 [5,] 15125
 [6,] 18150
 [7,] 21175
 [8,] 24200
 [9,] 27225
[10,] 30250

[[5]]
         [,1]
 [1,]  166375
 [2,]  332750
 [3,]  499125
 [4,]  665500
 [5,]  831875
 [6,]  998250
 [7,] 1164625
 [8,] 1331000
 [9,] 1497375
[10,] 1663750

[[6]]
          [,1]
 [1,]  9150625
 [2,] 18301250
 [3,] 27451875
 [4,] 36602500
 [5,] 45753125
 [6,] 54903750
 [7,] 64054375
 [8,] 73205000
 [9,] 82355625
[10,] 91506250

[[7]]
            [,1]
 [1,]  503284375
 [2,] 1006568750
 [3,] 1509853125
 [4,] 2013137500
 [5,] 2516421875
 [6,] 3019706250
 [7,] 3522990625
 [8,] 4026275000
 [9,] 4529559375
[10,] 5032843750

[[8]]
              [,1]
 [1,]  27680640625
 [2,]  55361281250
 [3,]  83041921875
 [4,] 110722562500
 [5,] 138403203125
 [6,] 166083843750
 [7,] 193764484375
 [8,] 221445125000
 [9,] 249125765625
[10,] 276806406250

[[9]]
              [,1]
 [1,] 1.522435e+12
 [2,] 3.044870e+12
 [3,] 4.567306e+12
 [4,] 6.089741e+12
 [5,] 7.612176e+12
 [6,] 9.134611e+12
 [7,] 1.065705e+13
 [8,] 1.217948e+13
 [9,] 1.370192e+13
[10,] 1.522435e+13

[[10]]
              [,1]
 [1,] 8.373394e+13
 [2,] 1.674679e+14
 [3,] 2.512018e+14
 [4,] 3.349358e+14
 [5,] 4.186697e+14
 [6,] 5.024036e+14
 [7,] 5.861376e+14
 [8,] 6.698715e+14
 [9,] 7.536054e+14
[10,] 8.373394e+14

[[11]]
              [,1]
 [1,] 4.605367e+15
 [2,] 9.210733e+15
 [3,] 1.381610e+16
 [4,] 1.842147e+16
 [5,] 2.302683e+16
 [6,] 2.763220e+16
 [7,] 3.223757e+16
 [8,] 3.684293e+16
 [9,] 4.144830e+16
[10,] 4.605367e+16
于 2021-08-06T07:30:31.710 回答
0

只是我已经解决了问题的更新。这是答案:

#loading required packages 
library(igraph)
library(tidyverse)
library(ggraph)
library(ggnetwork)
library(tidygraph)

g <- play_erdos_renyi(10, .2)    
M<-matrix(0:1, nrow = 10, ncol = 10)
Periods=10
seeds=c(1,2)

#Seeds are informed 
V(g)$informed <- F
V(g)[seeds]$informed=T
#Seeds have prob 0.5 (just treat prob as any variable here)
V(g)$prob=0
V(g)[seeds]$prob=0.5


#Time loop for creating prob over time
#prob at [t+1]=M*prob at [t]
#calculate prob for vertex v in time t+1 by multiplying 10X10 matrix M with 10X1 vector V(g)$prob
#then considering the v-th row

#extract the probs that were just created 
prob <- V(g)$prob

#declare an initial matrix of simulated prob (called sim.prob) 
sim.prob <- matrix(nrow=10 , ncol = Periods+1) #Create empty matrix
sim.prob[,1] <- diag(ncol(M)) %*% c(1:10) #First column is just the id's

for (t in 2:Periods) {
  sim.prob[,t] <- diag(ncol(M)) %*% prob
  sim.prob[,t+1] <- M %*% sim.prob[,t]
  for(v in V(g)) {
    if(!V(g)[v]$informed) {
      V(g)$prob[v]= sim.prob[,t+1][v]
      prob <- V(g)$prob
    }}}
于 2021-08-07T16:34:20.220 回答