title: EWMA covariance using R
body: <p>I'm trying to implement in R the covariance function for the Exponential Weighted Moving Average (EWMA) model, given by:</p>
  
  <p>Cov(x,y;t+1) = lambda * Cov(x,y;t) + (1-lambda) * r(x;t) * r(y;t)</p>
  
  <p>for t=1,2,...,n.</p>
  
  <p>I have the following code:</p>
  
  <pre><code>ewma.cov &lt;- function(v1,v2,lambda) {
       n &lt;- length(v1)
       cv &lt;- double()
       cv[1] &lt;- 0
       for(i in 1:n)
           cv[i+1] &lt;- cv[i]*lambda + (1-lambda)*v1[i]*v2[i]
       cv
  }
  </code></pre>
  
  <p>However, something goes wrong when I call the function since I get the following error:</p>
  
  <blockquote>
    <p>Error in cv[i] * lambda : non-numeric argument to binary operator</p>
  </blockquote>