[R] What is the best way to lag a time series?

Patrick Burns pburns at pburns.seanet.com
Sun Dec 26 11:06:54 CET 2010

First off, there are data manipulation
techniques that will beat doing it in
a spreadsheet.  For example:

head(x, -1)

is lagged 1 relative to

tail(x, -1)

But I think you are really looking for
'Lag' in the 'quantmod' package.

On 26/12/2010 07:49, Christian Schoder wrote:
> Dear R-users,
> I've been using R for a while and I am very satisfied! Unfortunately, I
> still have not figured out an efficient and general way to construct and
> use lags of time series, especially when I need to work with different
> packages.
> Let me give an example. I have two time series x and y and I want to
> estimate a variaty of distributed lags models and run different tests
> (autocorrelation, etc). It is obvious that I need to be able to lag x
> and y in a flexible way. So far, my temporary solution was to construct
> the lags manually (x1,..,xn and y1,..,yn) in a spreadsheet and import it
> to R, which is not very satisfactory because it does not allow for much
> flexibility.
> Is there a straighforward command which allows me to easily construct a
> lag when required and which allows me to, for example, use the lm()
> command to fit a dynamic model and the bgtest() command to perform the
> breusch-godfrey test on the same model?
> Is it adviseable to use time series objects which consist of many time
> series (like a dataframe) or is it better to have it contain only one
> time series?
> I would be grateful for any hints and links.
> Thx!
> Christian
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Patrick Burns
pburns at pburns.seanet.com
twitter: @portfolioprobe
(home of 'Some hints for the R beginner'
and 'The R Inferno')

More information about the R-help mailing list