title: Calculating the means in a for loop in R
body: <p>Good afternoon all,</p>
  <p>I am trying to find the standard prediction error of a time-series that I generate through a simulation run which is defined through the function called sim_11 with 250 simulations. This is provided in the first batch of code below.</p>
  <p>The second batch creates a time-series model (AR(1)) and tries to predict the next 5 values, and I do this overall 250 simulations. For each simulation, I should be able to get 5 prediction errors and after 250 simulations I should have a resulting table of 250 rows and 5 columns. However, when I try to set this up in the for loop, I end up with only 250 single values when in fact I should end up with a 250 by 5 table/matrix. I believe the error to be in the</p>
  <pre><code>pred_error_AR1_100[i]&lt;-table((pre_AR1_100$se[1]),(pre_AR1_100$se[2]),
                             (pre_AR1_100$se[3]),(pre_AR1_100$se[4]),
                             (pre_AR1_100$se[5]), ncol=5) 
  </code></pre>
  <p>part however I am not able to figure out where or what the format should be.</p>
  <p>Thank you in advance.</p>
  <p>The two code batches are provided below for replication.</p>
  <pre><code># Setup the simulation run with 100 observations and 250 simulations
  sim_11=function(){
    e&lt;-rnorm(200, mean=0, sd=0.2) # Produces 200 white noise values
    Y_t=c(0,0)  # Fills in the first 2 observations as a lag of 2 can be handled
    for (i in 3:length(e)){
      f1&lt;- 0.138+(0.316+0.982*Y_t[i-1])*exp(-3.89*(Y_t[i-1])^2)
      f2&lt;- -0.437-(0.659+1.260*Y_t[i-1])*exp(-3.89*(Y_t[i-1])^2)
      Y_t[i]&lt;-f1*Y_t[i-1]+f2*Y_t[i-2]+e[i]
    }
    Y_t&lt;-Y_t[101:200] # Removes the first 100 observations
    Y_t # Prints the 100 observations
  }
  
  lapply(1:250, function(x) sim_11()) # Provides the results of the 250 simulations
  x_100_lstar=replicate(250,sim_11()) # Places all results into one matrix
  </code></pre>
  <pre><code>pred_error_AR1_100=0
  # controls&lt;-list(gammaInt=c(0.1,2000), nGamma=50)
  for (i in 1:ncol(x_100_lstar)){
    AR1_100&lt;-ar(x_100_lstar[,i])
    pre_AR1_100&lt;-predict(AR1_100, n.ahead=5)
    pred_error_AR1_100[i]&lt;-table((pre_AR1_100$se[1]),(pre_AR1_100$se[2]),
                             (pre_AR1_100$se[3]),(pre_AR1_100$se[4]),
                             (pre_AR1_100$se[5]), ncol=5)
  }
  pred_error_AR1_100
  </code></pre>