[R] What is a better way to deal with lag/difference and loops in time series using R?
R. Michael Weylandt
michael.weylandt at gmail.com
Wed Apr 11 05:36:48 CEST 2012
Two ways around this:
I = Easy) Just use zoo/xts objects. ts objects a real pain in the
proverbial donkey because of things like this.
Something like:
library(xts)
PI1.yq <- as.xts(PI1) # Specialty class for quarterly data (or regular
zoo works)
lag(PI1.yq)
II = Hard) lag on a ts actually changes the time indices while keeping
all the data, [so the fourth data point is the same value -- just a
different time point] what you may want to do is cbind() the objects
to see how they line up now.
cbind(PI1, lag(PI1,4))
Hope this helps,
Michael
On Tue, Apr 10, 2012 at 11:21 PM, jpm miao <miaojpm at gmail.com> wrote:
> Hello,
>
> I am writing codes for time series computation but encountering some
> problems
>
> Given the quarterly data from 1983Q1 to 1984Q2
>
> PI1<-ts(c(2.747365190,2.791594762, -0.009953715, -0.015059485,
> -1.190061246, -0.553031799, 0.686874720, 0.953911035),
> start=c(1983,1), frequency=4)
>
>> PI1
> Qtr1 Qtr2 Qtr3 Qtr4
> 1983 2.747365190 2.791594762 -0.009953715 -0.015059485
> 1984 -1.190061246 -0.553031799 0.686874720 0.953911035
>
>
> If I would like to create a time series vector containing the data in 4
> quarters ahead
>
>> PI4<-lag(PI1,4)> PI4 Qtr1 Qtr2 Qtr3 Qtr4
> 1982 2.747365190 2.791594762 -0.009953715 -0.015059485
> 1983 -1.190061246 -0.553031799 0.686874720 0.953911035
>
>
> Confusingly, PI1[1] and PI4[1] are exactly the same! I usually would
> like to calculate the difference between the vector of interest and the
> corresponding values 4 quarters ahead, but it is zero!
>
>> PI1[1][1] 2.747365> PI4[1][1] 2.747365
>
>
> One remedy that comes into my mind is to use window,but a warning message
> emerges
>
>> PI4w<-window(PI4, start=start(PI1), end=end(PI1))Warning message:In window.default(x, ...) : 'end' value not changed> PI4w Qtr1 Qtr2 Qtr3 Qtr4
> 1983 -1.1900612 -0.5530318 0.6868747 0.9539110
>
>
> Similar problems happen with the usage of the function "diff", which
> calculate the difference. I wonder if it is better to work with the dates
> (1983Q1, 1983Q2,.....) directly?
>
> If I want to write a loop, say, to conduct some computation from 1983Q1
> to 2011Q4, the only way I know is to convert the dates to the ordinal
> indices, 1, 2, 3...... Can we work with the dates? Is there any built-in
> equality that provides the computation like
> 1983Q1 +1 equals 1983Q2?
> In EViews, it is easy to do that. We can let %s run from 1983Q1 to
> 2011Q4, and he knows that 1983Q1+1 is exactly 1983Q2.
>
> Thanks very much for your reply!
>
> miao
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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