Skip to content

Newbie trying to Lag Variables in a regression

1 message · Pfaff, Bernhard

#
<rwatkins at cornerstonelp.com> writes:
Not sure the book will unstick you...

However, the simple way is to create a new variable which shifts the
response, i.e. 

yshft <- c(y[-1], NA) # pad with missing
summary(lm(yshft ~ x + y))

Alternatively, lag the regressors:

N <- length(x)
xlag <- c(NA, x[1:(N-1)])
ylag <- c(NA, y[1:(N-1)])
summary(lm(y ~ xlag + ylag))


Hello rwatkins,

in case you have to cope oftenly with lagged exogenous and/or endogenous
variables the following function might be handy, in particular if you want
to create longer lagged series (this is controlled by setting the argument
'd' to the relevant integer of the lagged period):  

#
# Function: tslag (lagging a vector)
#
tslag <- 
function(x, d=1)
{
  x <- as.vector(x)
  n <- length(x)
  c(rep(NA,d),x)[1:n]
}

HTH,
Bernhard