[R] lagged variables

Rui Barradas
Wed, 25 Jul 2012 09:08:11 -0000

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:
> hi guys,
> i have some trouble in creating lagged variables to use as external
> regressors.
> i'm trying to use lag(x) but it gives me as result the same time series
(x),
> adding this part at the end:
>
> attr(,"tsp")
> [1]    0 2323    1
>
> where do i wrong?are there other functions to be used?
> thanks
> sara
>
>
>
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/lagged-variables-tp4637734.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.