0

I used lag.xts() to create lags of my ts objects.

However, I am now trying to keep everyhting base R and replacing that with lag(). So same output needed with different function.

I found out that in lag() the parameter k generates leading data if positive, and lagging data if negative.

Which is in contrast to lag.xts(), where k=1 is a one period lag. So lag(..., k=-1) and lag.xts(..., k=1) must yield same results.

My data goes from Q1 1957 to Q1 2005. lag.xts(data, k=1) outputs lags until Q1 2005. However lag(data, k=-1) outputs an additional lag at Q2 2005.


              data   lag.xts()     lag()
1957 Q1         NA         NA         NA
1957 Q2  3.3937128         NA         NA
1957 Q3  3.5538940  3.3937128  3.3937128
1957 Q4  1.9295103  3.5538940  3.5538940
...
2004 Q4  3.5051154  1.6227116  1.6227116
2005 Q1  2.3660429  3.5051154  3.5051154
2005 Q2         NA         NA  2.3660429

Can someone please help? :)) Thanks a lot!

mohid
  • 1
  • 1
  • Please read the instructions on asking a question at the top of the [tag:r] tag home page and in particular provide the input in reproducible form using `dput` and also show what the expected output is. – G. Grothendieck Nov 09 '22 at 09:37

0 Answers0