time series - R, lag( ) has inconsistent behavior for xts and ts objects -
i take lag of xts variable, , lag() function returns lag. however, if use on ts variable, gives lead. bug, or working intended?
library('xts') = as.xts(ts(c(5,3,7,2,4,8,3), start=c(1980,1), freq=4)) cbind(a, lag(a)) # provides lag 1 # ..1 ..2 # 1980 q1 5 na # 1980 q2 3 5 # 1980 q3 7 3 # 1980 q4 2 7 # 1981 q1 4 2 # 1981 q2 8 4 # 1981 q3 3 8 b = ts(c(5,3,7,2,4,8,3), start=c(1980,1), freq=4) cbind(b, lag(b)) # provides lead 1 # b lag(b) # 1979 q4 na 5 # 1980 q1 5 3 # 1980 q2 3 7 # 1980 q3 7 2 # 1980 q4 2 4 # 1981 q1 4 8 # 1981 q2 8 3 # 1981 q3 3 na
as pointed out in documentation ?lag.xts, intended behavior.
Comments
Post a Comment