Alex
Alex

Reputation: 19863

lagging xts objects in R

I am unable to find the answer to this question in the documentation and I think this works since zoo is underlying the xts object but:

  1. can one use the usual lag and diff functions on xts objects?
  2. if i want to rollapply a function to an xts objects in the zoo sense - i know that xts has the period.apply function which works on non-overlapping regions - should one just use rollapply on xts objects and it should work once again since there is a zoo underneath?

EDIT: I just looked up details and it doesn't seem that zoo is actually underlying the xts. Can someone comment on this/verify?

Upvotes: 3

Views: 10224

Answers (2)

Joshua Ulrich
Joshua Ulrich

Reputation: 176718

xts objects are not zoo objects "underneath". xts is a subclass of zoo, which means zoo methods are called if an xts method doesn't exist for a generic function (e.g. $.zoo and $<-.zoo). Both zoo and xts objects are a matrix with an ordered index attribute. xts requires that the index be time-based.

Yes you can use lag and diff on xts objects. As mrdwab noted, lag.xts intentionally behaves differently than lag.ts and lag.zoo.

You can also use zoo's rollapply family of functions on xts objects, but you may have to as.xts the result.

Upvotes: 4

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193677

You can, but you may not get the results you were expecting.

Here's a one-year lag with lag applied to a ts object (ldeaths is an inbuilt dataset, used as an example in the help file for lag, so you can try it too). The lagged set starts one year earlier (and shows all the data).

ldeaths
#       Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
# 1974 3035 2552 2704 2554 2014 1655 1721 1524 1596 2074 2199 2512
# 1975 2933 2889 2938 2497 1870 1726 1607 1545 1396 1787 2076 2837
# 1976 2787 3891 3179 2011 1636 1580 1489 1300 1356 1653 2013 2823
# 1977 3102 2294 2385 2444 1748 1554 1498 1361 1346 1564 1640 2293
# 1978 2815 3137 2679 1969 1870 1633 1529 1366 1357 1570 1535 2491
# 1979 3084 2605 2573 2143 1693 1504 1461 1354 1333 1492 1781 1915
lag(ldeaths, 12)
#       Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
# 1973 3035 2552 2704 2554 2014 1655 1721 1524 1596 2074 2199 2512
# 1974 2933 2889 2938 2497 1870 1726 1607 1545 1396 1787 2076 2837
# 1975 2787 3891 3179 2011 1636 1580 1489 1300 1356 1653 2013 2823
# 1976 3102 2294 2385 2444 1748 1554 1498 1361 1346 1564 1640 2293
# 1977 2815 3137 2679 1969 1870 1633 1529 1366 1357 1570 1535 2491
# 1978 3084 2605 2573 2143 1693 1504 1461 1354 1333 1492 1781 1915

Now, convert the dataset to an xts object and try.

ldeaths.x = as.xts(ldeaths)
head(lag(ldeaths.x, 12), n=14)
#          [,1]
# Jan 1974   NA
# Feb 1974   NA
# Mar 1974   NA
# Apr 1974   NA
# May 1974   NA
# Jun 1974   NA
# Jul 1974   NA
# Aug 1974   NA
# Sep 1974   NA
# Oct 1974   NA
# Nov 1974   NA
# Dec 1974   NA
# Jan 1975 3035
# Feb 1975 2552

Oops. Not what you expected. But you should expect it, because the help file for lag.xts says:

... it was decided that lag's default behavior should match the common time-series interpretation of that operator — specifically that a value at time ‘t’ should be the value at time ‘t-1’ for a positive lag. This is different than lag.zoo as well as lag.ts.

So, let's try k = -12.

head(lag(ldeaths.x, -12), n=14)
#          [,1]
# Jan 1974 2933
# Feb 1974 2889
# Mar 1974 2938
# Apr 1974 2497
# May 1974 1870
# Jun 1974 1726
# Jul 1974 1607
# Aug 1974 1545
# Sep 1974 1396
# Oct 1974 1787
# Nov 1974 2076
# Dec 1974 2837
# Jan 1975 2787
# Feb 1975 3891

For fun, what happens with a zoo object? First, convert to zoo and then try it.

ldeaths.z = zoo(ldeaths.x)
head(lag(ldeaths.z, 12), n=14)
# 
# Jan 1974 2933
# Feb 1974 2889
# Mar 1974 2938
# Apr 1974 2497
# May 1974 1870
# Jun 1974 1726
# Jul 1974 1607
# Aug 1974 1545
# Sep 1974 1396
# Oct 1974 1787
# Nov 1974 2076
# Dec 1974 2837
# Jan 1975 2787
# Feb 1975 3891

Upvotes: 11

Related Questions