title: calculating the autocorrelation of a time series in R
body: <p>As I understand, autocorrelation is a correlation of a set of number with itself.</p>
  
  <p>I know, that I can use <code>acf</code> function to calculate autocorrelation in R, but I want to implement it my self.</p>
  
  <p>I have a time series X[t] and I want to calculate correlation of this time series with X[t-1]</p>
  
  <p>I have done this in R:</p>
  
  <pre><code>ts &lt;- c(1,2,8,1,2,2,3,2,3,2,2,1,3,2,3,1,1,2)
  cor(ts,ts-1)
  </code></pre>
  
  <p>but the correlation is always 1, I think it is not the correct answer. How can I do this in R?</p>