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.
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.
On Wed, Jul 25, 2012 at 4:10 AM, saraberta <sara.bertapelle at hotmail.it> wrote:> 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 >The dyn and dynlm packages address this problem. -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
There is a Lag function in Hmisc and I found this on StackExchange
shift <- function (x, shift_by) { #similar to lag function
stopifnot(is.numeric(shift_by))
stopifnot(is.numeric(x))
if (length(shift_by)>1)
return(sapply(shift_by,shift, x=x))
out<-NULL
abs_shift_by=abs(shift_by)
if (shift_by > 0 )
out<-c(tail(x,-abs_shift_by),rep(NA,abs_shift_by))
else if (shift_by < 0 )
out<-c(rep(NA,abs_shift_by), head(x,-abs_shift_by))
else
out<-x
out
}
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of saraberta
Sent: Wednesday, July 25, 2012 4:10 AM
To: r-help at r-project.org
Subject: [R] lagged variables
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.