[R] lag function doesn't work - what am i doing wrong?

Gabor Grothendieck ggrothendieck at gmail.com
Fri Oct 10 20:24:08 CEST 2008


Note that there is actually a good reason why this is so.
ts can only represent regularly spaced series but the
result  of a subscripting operation might not be a regularly
spaced series so you would not be able to guarantee that
it could be a ts series as well.  That's the reason for window.ts
too since window.ts is constructed in such a way that the result
is always a regular series so it can be presented as a ts object.

The reason that zoo and xts can handle this is that they work
with irregularly spaced series and subscripting an irregularly spaced
series always creates another.

For example:

library(zoo)
z <- zoo(11:15)
z[c(1, 3, 4)]

The result of the last line is another zoo object but it would be
impossible to do that with ts since the result in this case the
result is irregularly spaced.


On Fri, Oct 10, 2008 at 2:11 PM, Scotty Nelson <poorboy44 at hotmail.com> wrote:
>
> thanks.  i'll try zoo or xts.  ts is a p.i.t.a.
>
>
> Gabor Grothendieck wrote:
>>
>> lag1resid is a time series, try
>>
>> str(residsq)
>> str(lag1residsq)
>>
>> The problem is that when you subscript lag1resid you
>> don't get a time series out from that.  See ?window.ts
>> or try the zoo or xts packages where subscripting of time
>> series works.
>>
>> On Fri, Oct 10, 2008 at 1:27 PM, Scotty Nelson <poorboy44 at hotmail.com>
>> wrote:
>>>
>>> I am trying to lag a time series.  My data is in a matrix, but I coerce
>>> it
>>> into a ts object.
>>> But when I lag it and then look at the result, nothing has changed.  What
>>> am
>>> I doing wrong?????
>>>
>>> residsq<-resid^2
>>> residsq<-as.ts(residsq)
>>> lag1residsq<-lag(residsq,-1)
>>>
>>>> residsq[1:5]
>>>          1           2           3           4           5
>>>     87.759  329882.188 5325849.333   31512.334   70865.228
>>>>  lag1residsq[1:5]
>>>          1           2           3           4           5
>>>     87.759  329882.188 5325849.333   31512.334   70865.228
>>>>
>>>
>>> PS: I tried to lag the series manually using rbind(0, residsq), but the
>>> result ended up returning me [ 0, residsq(1), 0, residsq(2), 0, ....]
>>> WTF!!!!  how do you stack 1 element onto another in R?
>>> --
>>> View this message in context:
>>> http://www.nabble.com/lag-function-doesn%27t-work---what-am-i-doing-wrong--tp19922651p19922651.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> 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.
>>>
>>
>> ______________________________________________
>> 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.
>>
>>
>
> --
> View this message in context: http://www.nabble.com/lag-function-doesn%27t-work---what-am-i-doing-wrong--tp19922651p19923295.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list