[R] How to use "lag"?

From: Spencer Graves <spencer.graves_at_pdf.com>
Date: Sun 06 Mar 2005 - 03:14:15 EST

      Is it possible to fit a lagged regression, "y[t]=b0+b1*x[t-1]+e", using the function "lag"? If so, how? If not, of what use is the function "lag"? I get the same answer from y~x as y~lag(x), whether using lm or arima. I found it using y~c(NA, x[-length(x)])). Consider the following:

> set.seed(1)
> x <- rep(c(rep(0, 4), 9), len=9)
> y <- (rep(c(rep(0, 5), 9), len=9)+rnorm(9)) # y[t] = x[t-1]+e
>
> lm(y~x)

(Intercept)            x 
     1.2872      -0.1064 

> lm(y~lag(x))
(Intercept) lag(x) 1.2872 -0.1064
> arima(y, xreg=x)
intercept x 1.2872 -0.1064 s.e. 0.9009 0.3003

sigma^2 estimated as 6.492: log likelihood = -21.19, aic = 48.38
> arima(y, xreg=lag(x))
      intercept   lag(x)
         1.2872  -0.1064
s.e.     0.9009   0.3003

> arima(y, xreg=c(NA, x[-9]))
intercept c(NA, x[-9]) 0.4392 0.8600 s.e. 0.2372 0.0745

sigma^2 estimated as 0.3937: log likelihood = -7.62, aic = 21.25
> arima(ts(y), xreg=lag(ts(x)))

arima(x = ts(y), xreg = lag(ts(x)))
      intercept  lag(ts(x))
         1.2872     -0.1064
s.e.     0.9009      0.3003

sigma^2 estimated as 6.492: log likelihood = -21.19, aic = 48.38  
      Thanks for your help. 
      Spencer Graves

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sun Mar 06 03:24:05 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:30:41 EST