Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

lag.zoo

Lags and Differences of zoo Objects


Description

Methods for computing lags and differences of "zoo" objects.

Usage

## S3 method for class 'zoo'
lag(x, k = 1, na.pad = FALSE, ...)
## S3 method for class 'zoo'
diff(x, lag = 1, differences = 1, arithmetic = TRUE, na.pad = FALSE, ...)

Arguments

x

a "zoo" object.

k, lag

For lag the number of lags (in units of observations). Note the sign of k behaves as in lag. For diff it is the number of backward lags used (or if negative the number of forward lags.

differences

an integer indicating the order of the difference.

arithmetic

logical. Should arithmetic (or geometric) differences be computed?

na.pad

logical. If TRUE it adds any times that would not otherwise have been in the result with a value of NA. If FALSE those times are dropped.

...

currently not used.

Details

These methods for "zoo" objects behave analogously to the default methods. The only additional arguments are arithmetic in diff na.pad in lag.zoo which can also be specified in diff.zoo as part of the dots. Also, "k" can be a vector of lags in which case the names of "k", if any, are used in naming the result.

Value

The lagged or differenced "zoo" object.

Note

Note the sign of k: a series lagged by a positive k is shifted earlier in time.

lag.zoo and lag.zooreg can give different results. For a lag of 1 lag.zoo moves points to the adjacent time point whereas lag.zooreg moves the time by deltat. This implies that a point in a zoo series cannot be lagged to a time point that is not already in the series whereas this is possible for a zooreg series.

See Also

Examples

x <- zoo(11:21)

lag(x, k = 1)
lag(x, k = -1)
# this pairs each value of x with the next or future value
merge(x, lag1 = lag(x, k=1))
diff(x^3)
diff(x^3, -1)
diff(x^3, na.pad = TRUE)

zoo

S3 Infrastructure for Regular and Irregular Time Series (Z's Ordered Observations)

v1.8-10
GPL-2 | GPL-3
Authors
Achim Zeileis [aut, cre] (<https://orcid.org/0000-0003-0918-3766>), Gabor Grothendieck [aut], Jeffrey A. Ryan [aut], Joshua M. Ulrich [ctb], Felix Andrews [ctb]
Initial release
2022-04-15

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.