用R中的arima.sim填充数组

时间:2014-03-16 12:55:25

标签: arrays r

无法找到与我遇到的以下问题类似的问题/答案。

我想用arima.sim()计算的模拟填充数组。因为我有一个我要填充的三维数组,所以我使用了3个嵌套的for循环。由于某些未知原因(对我而言),当我使用以下代码时,我收到错误“要替换的项目数不是替换长度的倍数”:

np <- 5
nd <- 4
nb <- 3


gamma000 <- 10   
gamma010 <- 0.3    
gamma100 <- 0.3 

SD.b <- 3 
SD.d <- 5 

Means <- matrix(c(gamma000, gamma010, gamma100), 3, 1, byrow = TRUE)

Sigma <- matrix(c(2, 0, 0,
              0, 0.01, 0,
              0, 0, 0.01), 3, 3, byrow = TRUE)

Theta <- rmvnorm(np, Means, Sigma)

in.mu <- Theta[,1]
in.beta <- Theta[,2] 
in.phi <- Theta[,3]

ind.day.mean <- matrix(0, np, nd)

for(i in 1:np){
  ind.day.mean[i,] <- as.matrix(as.numeric(arima.sim(n = nd, list(ar = c(in.beta[i]), 
order = c(1, 0, 0)), sd = SD.d)), nrow = 1) + in.mu[i]
}


Y <- array(0, c(np, nd, nb))

for(j in 1:np){
  for(k in 1:nd){
    for(l in 1:nb){
  Y[j,k,l] <- as.array(as.numeric(arima.sim(n = nb, list(ar = c(in.phi[j]), 
order = c(1, 0, 0)), sd = SD.b)), nrow = 1) + ind.day.mean[j,k]
    }
  }
}

当我删除Y [j,k,l]中的“l”时,它突然起作用并返回具有正确尺寸的数组。然而,我不确定这是否应该是R应该返回的正确答案。

1 个答案:

答案 0 :(得分:1)

我无法告诉您结果是否正确,但这是一种回溯嵌套循环内部内容的方法。在browser()之后插入for (l in 1:nb),运行代码,并在每次迭代后检查Y(按c或Enter滚动最内层循环一次)。这是您在第一次迭代后的结果。

Browse[1]> Y
, , 1

         [,1] [,2] [,3] [,4]
[1,] 9.315546    0    0    0
[2,] 0.000000    0    0    0
[3,] 0.000000    0    0    0
[4,] 0.000000    0    0    0
[5,] 0.000000    0    0    0

, , 2

         [,1] [,2] [,3] [,4]
[1,] 12.39103    0    0    0
[2,]  0.00000    0    0    0
[3,]  0.00000    0    0    0
[4,]  0.00000    0    0    0
[5,]  0.00000    0    0    0

, , 3

         [,1] [,2] [,3] [,4]
[1,] 10.97208    0    0    0
[2,]  0.00000    0    0    0
[3,]  0.00000    0    0    0
[4,]  0.00000    0    0    0
[5,]  0.00000    0    0    0

第二次迭代会覆盖第一个结果。

Browse[1]> Y
, , 1

         [,1] [,2] [,3] [,4]
[1,] 9.452485    0    0    0
[2,] 0.000000    0    0    0
[3,] 0.000000    0    0    0
[4,] 0.000000    0    0    0
[5,] 0.000000    0    0    0

, , 2

         [,1] [,2] [,3] [,4]
[1,] 10.92433    0    0    0
[2,]  0.00000    0    0    0
[3,]  0.00000    0    0    0
[4,]  0.00000    0    0    0
[5,]  0.00000    0    0    0

, , 3

         [,1] [,2] [,3] [,4]
[1,] 8.919828    0    0    0
[2,] 0.000000    0    0    0
[3,] 0.000000    0    0    0
[4,] 0.000000    0    0    0
[5,] 0.000000    0    0    0

但是,您会注意到您根本没有使用l维度。将它从你的循环中移除,你可能会得到你所追求的东西。

for(j in 1:np){
  for(k in 1:nd){
      Y[j,k, ] <- as.array(
        as.numeric(
          arima.sim(n = nb, list(ar = c(in.phi[j]), order = c(1, 0, 0)), sd = SD.b)), nrow = 1) + ind.day.mean[j,k]
      print(Y[j,k, ])
  }
}

请注意,每行都添加为&#34; 3rd&#34;尺寸。

[1] 14.75984 11.70245 11.62098
[1] 12.14670 11.02858 13.75324
[1] 11.39563 10.05482 13.34860
[1] 12.21004 11.26329 17.87310
[1] 18.02935 18.78355 16.81404
[1] 7.458101 8.310141 3.730183
[1] 3.241728 2.533293 3.297518
[1] 8.131038 7.312235 3.225820
[1] 22.16841 13.31824 14.55298
[1]  5.779292 11.445617 10.127335
[1] 3.497040 3.096024 4.156983
[1] -0.6772705 -3.9059121  1.7113603
[1] 11.93328 21.06519 16.55603
[1] 15.41917 16.85090 16.64554
[1]  8.122488  9.620335 11.441005
[1] 12.296554  4.616814  6.439327
[1] 7.971385 6.779664 1.041257
[1] 12.905093 11.989233  8.444227
[1]  9.783214 13.068641  5.656610
[1] 11.339195  5.454600  6.766433
> Y
, , 1

          [,1]      [,2]      [,3]       [,4]
[1,] 14.759842 12.146705 11.395633 12.2100370
[2,] 18.029350  7.458101  3.241728  8.1310377
[3,] 22.168413  5.779292  3.497040 -0.6772705
[4,] 11.933282 15.419173  8.122488 12.2965543
[5,]  7.971385 12.905093  9.783214 11.3391947

, , 2

          [,1]      [,2]      [,3]      [,4]
[1,] 11.702452 11.028582 10.054817 11.263291
[2,] 18.783552  8.310141  2.533293  7.312235
[3,] 13.318238 11.445617  3.096024 -3.905912
[4,] 21.065188 16.850896  9.620335  4.616814
[5,]  6.779664 11.989233 13.068641  5.454600

, , 3

          [,1]      [,2]      [,3]      [,4]
[1,] 11.620982 13.753242 13.348595 17.873103
[2,] 16.814041  3.730183  3.297518  3.225820
[3,] 14.552980 10.127335  4.156983  1.711360
[4,] 16.556032 16.645536 11.441005  6.439327
[5,]  1.041257  8.444227  5.656610  6.766433