jpm miao
2012-Apr-11 03:21 UTC
[R] What is a better way to deal with lag/difference and loops in time series using R?
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)> PI1Qtr1 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 Qtr41982 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.747365One 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 Qtr41983 -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. Michael Weylandt
2012-Apr-11 03:36 UTC
[R] What is a better way to deal with lag/difference and loops in time series using R?
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.
Gabor Grothendieck
2012-Apr-11 11:32 UTC
[R] What is a better way to deal with lag/difference and loops in time series using R?
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) > > ? ?If I would like to create a time series vector containing the data in 4 > quarters ahead> > ? ?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. >This takes the difference between each point and the point 4 quarters back and then lags the result forward. lag(diff(PI1, 4), 4) In zoo you can do this: library(zoo) z <- as.zoo(PI1) time(z) <- as.yearqtr(time(z)) # quarter after '83 Q2 z[as.yearqtr("1983 Q2")+1/4] You might also want to look at rollapply in the zoo package. xts which is related to zoo has flexible subscripting. If your data is regular as in your example then you could also use the tis package which is related to the fame package but can be used without it. zoo has several vignettes which give many examples of processing time series. The timeSeries package is another alternative. -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com