Hello community,

I've started working with R just a few weeks ago in university and was wondering about nested loop functions with multiple sums. In particular I have an ARMA(2,4) process, which I want to simulate. I found all neccesary parameters and first simulated the time series with a simple for loop:

for (i in 5:length(t)) {

Y[i] = a + phi.1 * Y[i-1] + phi.2 * Y[i-2] + Z[i] + theta.4 * Z[i-4] + theta.3 * Z[i-3] +theta.2 * Z[i-2] + theta.1 * Z[i-1]

}

where phi.1 and so on are the parameters I defined before. Because of the lag up to [i-4] I start the loop at 5. t is the time vector with 1,000 values and Z[i] is just white noise.

Since this is a pretty sh.. code, I tried to simplify the summation for phi and theta with 2 other for-loops, but unfortunately it doesn't work in the slightest. I think I do understand why it is not working the way Iam trying it, but I wasn't able to come up with a suitable soluation until now.

My try:

for (i in 5:length(t)) {

for (j in 1:2){

for (k in 1:4){

Y[i] = sum(phi[j]*Y[i-j]) + Z[i] + sum(theta[k]*Z[i-k])

}

}

}

I think it is not equivalent because R tries to do the k-loop (1:4) at i=5 and j=1, then goes to i=5 and j=2 and repeats k=1:4. But I want j=1:2 and k=1:4 at the same time for i=5.

Can anyone give me at least a hint, how I can handle this procedure, so that R computes the AR and MA part simultaneously for every i-th value I try to compute?

Sincerely

FactOREO