2

我正在尝试使用 xts 滞后一个(弱)常规时间序列。zoo:::lag.zooreg 提供了正确的行为,但如果可能的话,我更愿意坚持使用 xts。关于如何在下面使用 xts 的任何建议?

#create a multivariate regular time series
tmp <- zooreg(data.frame(a=1:10,b=20:11),start=as.yearmon("2012-05-01"),frequency=12)
#july is missing
tmp <- tmp[-3,]
tmp2 <- xts(tmp)

#lag using xts. this doesn't use the weak regularity of the time-series
tmp2$lag1 <- lag(tmp2$a,1)
#what I really want is this behavior with xts (for speed, consistency, etc)
tmp3 <- merge(tmp,lag1=lag(tmp$a,-1),all=c(TRUE,FALSE))

#below is more elegant than merge, but I don't want the extra row for march
#is merge the only way to take care of this?
tmp$lag1 <- lag(tmp$a,-1)

谢谢您的帮助。

4

0 回答 0