[R] lagged variables

Federman, Douglas Douglas.Federman at utoledo.edu
Wed Jul 25 19:43:52 CEST 2012


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.



More information about the R-help mailing list