lag

Create a Lagged Time Series

Returns a time series like the input but shifted in time.

lag(x, ...) ## Default S3 method: lag(x, k = 1, ...)

x | a vector or matrix, usually univariate or multivariate regular time series. Missing values (NAs) are allowed. |

k | an integer number, indicates the number of positions the new series is to lead the input series, i.e., the resulting series is shifted backwards in time; negative values lag the series forwards in time. Non-integer, values of k are rounded to the nearest integer. |

Value

object of the same length and the same number of channels
as x, with the "tsp" attribute, but lagged by k positions.
Only the start and end dates are changed;

Differences between TIBCO Enterprise Runtime for R and Open-source R

In open-source R, the return value keeps the attribute of input object, whereas in TIBCO Enterprise Runtime for R, the return value is a "ts" object.

Note

Many think that k should have the opposite sign,
but due to its pervasive use in other functions,
lag it is as described.

Also, if the input is not of class "ts", neither will the output,
although it will have the "tsp" attribute that "ts" objects have.

References

Becker, R. A., Chambers, J. M., and Wilks, A. R. 1988. The New S Language: A Programming Environment for Data Analysis and Graphics. Pacific Grove, CA: Wadsworth & Brooks/Cole Advanced Books and Software.

See Also

hasTsp,
ts.union.

Examples

lag12co2 <- lag(co2, 12) # co2 lagged by 12 months ts.union(co2, lag(co2, 12))