Q:

filling array with arima.sim in R

Couldn't find a similar question/answer to the following problem that I have.
I want to fill an array with simulations computed with arima.sim(). Because I have a 3-dimensional array that I want to fill, I used 3 nested for-loops. For some unknown reason (to me),  I get the error "number of items to replace is not a multiple of replacement length" when I use the following code:
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]
    }
  }
}

when I remove the "l" in Y[j,k,l], it suddenly does work and an array with the right dimensions is returned. I am however not sure if that's the right answer that R should return.

A:

I can't tell you if the result is correct or not, but here's a way to backtrack of what's going on inside your nested loop. Insert browser() after for (l in 1:nb), run the code, and inspect Y after every iteration (pressing c or Enter rolls the inner most loop once). This is what your result looks like after the first iteration.
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

Second iteration is overwriting the first result.
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

You will notice, however, that you're not using the l dimension at all. Remove it from your loop and you will probably get what you're after.
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, ])
  }
}

Notice that each row is added as the "3rd" dimension.
[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

