Skip to content

What is the best way to lag a time series?

4 messages · Christian Schoder, Liviu Andronic, Patrick Burns +1 more

#
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
#
On Sun, Dec 26, 2010 at 8:49 AM, Christian Schoder
<schoc152 at newschool.edu> wrote:
Perhaps ?diff.

Liviu

  
    
  
#
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:

  
    
#
The correct answer to "How to lag..?" is almost certainly, "Don't."
The functionality of  numerous time series packages and functions take
care of this automatically for you (using suitable data structures,
probably). Rather than trying to reinvent wheels, it might be wiser to
consult the Time Series Task View on Cran to see what's there first.

Incidentally, my limited understanding is that modern time series
methods tend to use more appropriately specified covariance structures
(e.g. arima models) rather than the lagged models  of e.g. classical
econometrics. But on this, I would happily stand correction.

-- Cheers,
 Bert
On Sun, Dec 26, 2010 at 12:21 AM, Liviu Andronic <landronimirc at gmail.com> wrote: