[R] lag() and lm()

Prof Brian D Ripley ripley at stats.ox.ac.uk
Wed Sep 20 19:22:43 CEST 2000


On Wed, 20 Sep 2000, Abu Hammour wrote:

> 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.

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.

> 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))

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.

> 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.

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.



-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list