title: Using R to solve a recursion function
body: <p>I am new to R, and I try to solve a recursion function: given </p>
  
  <pre><code>### creating variable R, a vector of length 10
  R = c(-1.70, 0.61, -0.54, -2.40, -1.50, -1.07, -2.42, -1.62, -1.65, -1.58)  
  </code></pre>
  
  <p>Then there is a model: <code>R[t] = A(t) + 0.5*A(t-1) + 0.3*A(t-2)</code>, where <code>A(0) = A(-1) = 0</code>, then calculate the <code>A(i)</code>, i=1,2...10. I wrote the code as following, but it always give me error and I am not sure where I am wrong. plz help me, many thanks.</p>
  
  <pre><code>ma &lt;- function(a){
                    r = NULL 
                    a = NULL
                    r[1]=1.70
                    r[2]=0.61
                    r[3]=-0.54
                    r[4]=-2.40
                    r[5]=-1.50
                    r[6]=-1.07
                    r[7]=-2.42
                    r[8]=-1.62
                    r[9]=-1.65
                    r[10]=-1.58
                    a[0] = 0
                    a[-1] = 0
                    for(i in 1:10){
                      r[i]=a[i]+0.5*a[i-1]+0.3*a[i-2]
                      return(a[i])
                    }
                   }
  </code></pre>