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. Is there any reason to this? I'd appreciate if you could let me know. Thank you so much. - adschai [[alternative HTML version deleted]]
What is happening is that by indexing the time series, you are losing the time series attributes. Here is a quick test that I ran.> xTime Series: Start = 1 End = 20 Frequency = 1 [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20> y <- lag(x,1) > yTime Series: Start = 0 End = 19 Frequency = 1 [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20> x==yTime Series: Start = 1 End = 19 Frequency = 1 [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [17] FALSE FALSE FALSE> str(x)Time-Series [1:20] from 1 to 20: 1 2 3 4 5 6 7 8 9 10 ...> str(x[1:10])int [1:10] 1 2 3 4 5 6 7 8 9 10> x[1:10] == y[1:10][1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE>Notice that 'x==y' comes up false, but x[1:10]==y[1:10] is true. Also notice the 'str(x[1:10])' as compared to 'str(x)' On 3/17/07, adschai@optonline.net <adschai@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. > > Is there any reason to this? I'd appreciate if you could let me know. > Thank you so much. > > - adschai > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@stat.math.ethz.ch 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]
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