[R] How to use "lag"?

Spencer Graves spencer.graves at pdf.com
Sat Mar 5 17:14:15 CET 2005


      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




More information about the R-help mailing list