[R] Lag operator in R does not work

Prof Brian Ripley ripley at stats.ox.ac.uk
Sun Mar 18 08:00:34 CET 2007


On Sun, 18 Mar 2007, adschai at optonline.net wrote:

> Hi - I'm quite wondering what makes the lag operator does not work for my time series. I have a time series of length about 200000 elements. I would like to have a lag 1 of this time series. I did the following:
>
> logprice = log(price, base=exp(1))
> # this is my log price which is a vector of price time series of length 200000
> ts_logprice = as.ts(logprice, frequency=1) # convert to time series
> lag_logprice = lag(ts_logprice, 1) # get the lag
>
> When I do:
>
> ts_logprice[1:10] == lag_logprice[1:10]
>
> The result returns TRUE for the first 10 elements which I do not expect that.

You should.  lag() changes the time base, not the values, as the help page 
says:

   Description

   Compute a lagged version of a time series, shifting the time base back
   by a given number of observations.

> Is there any reason to this? I'd appreciate if you could let me know.

The posting guide was not followed.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list