Skip to content

lag() and lm()

2 messages · Ahmad, Brian Ripley

#
Hi,
I am using R interactively for estimation and even data manipulation. I want
to use lag() function within lm() function in a way looks like:

mymodel<- lm(y~x+I(lag(y,-1)-1, data=anydata)

What I get is always perfect fit; (that is, coefficient=1) which is not
true.

If the model looks like
mymodel<- lm(y~z+x+I(lag(x,-1)-1, data=anydata)

summary returns only one coefficient for x and I(lag(x,-1))

Is there any way to do both of these models without going through long
process of transforming of x and y and making ts.intersect before running
lm() function.

Thank you,
Ahmad Abu Hammour



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Wed, 20 Sep 2000, Abu Hammour wrote:

            
But it *is* true: the model is not what you meant I suspect.  The problem
is that lag(y, -1) is a time series with an altered time base, and as a
regressor it is unchanged from y. If you really mean this as a model, you
have random regressors and least squares is inappropriate. Use arima0 to
fit it.
^)
Actually, if drops the third term as completely aliased with the
second.  You need x to be a time series to have any hope here.

anydata <- data.frame(x = ts(rnorm(100)), y = rnorm(100), z = rnorm(100))
mymodel<- lm(y ~ z + cbind(x, lag(x,-1))[-101,] - 1, data=anydata)

might do what you want.
See above.  It's a short process, but you do need to apply time-series
functions to time series.

Perhaps simpler is to shift the series manually:

mymodel<- lm(y ~ z + x + c(NA, x[-100]) - 1, data=anydata)

or use embed if you want natural (positive) lags.