title: Calculate the 20 steps sum of a variable
body: <p>I have a daily data base of retunrs from a portfolio. For a model I am replicating the authors calculate for each month, the realized variance RVt from daily returns in the previous 21 sessions.</p>
  <p>To do this here is a small example of how I am trying to calculate it:</p>
  <pre><code>x &lt;- rnorm(24795, 0, 0.2) #Generate random numbers to simulate my sample
  x_2 &lt;-x^2 #the model specify to work with the square returns
  # I need the monthly sum of the square returns. For this I create a matrix
  #with the length if x/20 because each month consist in 20 trading sessions
  
  rv &lt;- matrix(NA, nrow=(length(x_2)/20), ncol=1) 
  
  #I create the first step
  rv[1] &lt;- sum(x_2[1:20]) 
  
  #I create a loop to make the sum of from x_2[21:40] and continue
  # with this 20 steps sums
  for (i in 2:1239){
    rv[i] &lt;- sum(x_2[i+20:i+39]) 
  }
  rv
  </code></pre>
  <p>The problem is that my loop is summing as:</p>
  <pre><code> x_2[21:40]
   x_2[22:41]
   x_2[23:42]
  </code></pre>
  <p>instead of</p>
  <pre><code>x_2[21:40]
  x_2[41:60]
  x_2[61:80]
  </code></pre>
  <p>Does anyone knows what I a doing wrong?</p>
  <p>Here is a picture of the forula from the paper:
  <a href="https://i.stack.imgur.com/nctFL.png" rel="nofollow noreferrer">Formula</a></p>
  <p>Thanks</p>
  <p>Miguel</p>