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

diff

Lags and Differences of xts Objects


Description

Methods for computing lags and differences on xts objects. This matches most of the functionality of zoo methods, with some default argument changes.

Usage

## S3 method for class 'xts'
lag(x, k = 1, na.pad = TRUE, ...)



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

Arguments

x

an xts object

k

period to lag over

lag

period to difference over

differences

order of differencing

arithmetic

should arithmetic or geometric differencing be used

log

should (geometric) log differences be returned

na.pad

pad vector back to original size

...

additional arguments

Details

The primary motivation for having methods specific to xts was to make use of faster C-level code within xts. Additionally, 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.

Another notable difference is that na.pad is set to TRUE by default, to better reflect the transformation visually and within functions requiring positional matching of data.

Backwards compatability with zoo can be achieved by setting options(xts.compat.zoo.lag=TRUE). This will change the defaults of lag.xts to k=-1 and na.pad=FALSE.

Value

An xts object reflected the desired lag and/or differencing.

Author(s)

Jeffrey A. Ryan

References

Examples

x <- xts(1:10, Sys.Date()+1:10)
lag(x)    # currently using xts-style positive k 


lag(x, k=2)


lag(x, k=-1, na.pad=FALSE) # matches lag.zoo(x, k=1)

diff(x)
diff(x, lag=1)
diff(x, diff=2)
diff(diff(x))

xts

eXtensible Time Series

v0.12.1
GPL (>= 2)
Authors
Jeffrey A. Ryan [aut, cph], Joshua M. Ulrich [cre, aut], Ross Bennett [ctb], Corwin Joy [ctb]
Initial release

We don't support your browser anymore

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