Skip to content
Prev 301184 / 398506 Next

lagged variables

Hello,

You are doing nothing wrong. Follow this example.

x <- ts(1:5)
# Seems the same with different start and end
lag(x)
# But it's not
cbind(x, lag(x))

Are there other functions? I know of at least one. (In the end.)
These two, though not lags, might also be of interess to you.

?window
y <- window(x, start=2, end=5)
cbind(x, y)

?window.zoo # package zoo

#
# Package: TSA
# Title: Time Series Analysis
# Version: 0.98
# Date: 2010-7-31
# Author: Kung-Sik Chan
`zlag` <-
function (x, d = 1)
{
     if (d != as.integer(d) || d < 0)
         stop("d must be a non-negative integer")
     if (d == 0)
         return(x)
     else return(c(rep(NA, d), rev(rev(x)[-(1:d)])))
}

cbind(x, zlag(x))

Hope this helps,

Rui Barradas

Em 25-07-2012 09:10, saraberta escreveu: