[R] How to use "lag"?
Remigijus Lapinskas
remigijus.lapinskas at maf.vu.lt
Sat Mar 5 18:34:23 CET 2005
I use the following two function for a lagged regression:
lm.lag=function(y,lag=1) summary(lm(embed(y,lag+1)[,1]~embed(y,lag+1)[,2:(lag+1)]))
lm.lag.x=function(y,x,lag=1) summary(lm(embed(y,lag+1)[,1]~embed(x,lag+1)[,2:(lag+1)]))
for, respectively,
y_t=a+b_1*y_t-1+...+b_lag*y_t-lag
y_t=a+b_1*x_t-1+...+b_lag*x_t-lag
I am not quite sure whether this an answer to your question, but here
are two examples:
set.seed(7)
ar1=arima.sim(n=300,list(ar=0.8))
lm.lag(ar1)
lm.lag.x(ar1,ar1)
set.seed(8)
ar3=arima.sim(n = 200, list(ar = c(0.4, -0.5, 0.7)))
lm.lag(ar3,3)
lm.lag.x(ar3,ar3,3)
Best wishes,
Rem
Saturday, March 5, 2005, 6:14:15 PM, you wrote:
SG> Is it possible to fit a lagged regression, "y[t]=b0+b1*x[t-1]+e",
SG> using the function "lag"? If so, how? If not, of what use is the
SG> function "lag"? I get the same answer from y~x as y~lag(x), whether
SG> using lm or arima. I found it using y~c(NA, x[-length(x)])). Consider
SG> 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)
SG> (Intercept) x
SG> 1.2872 -0.1064
>> lm(y~lag(x))
SG> (Intercept) lag(x)
SG> 1.2872 -0.1064
>> arima(y, xreg=x)
SG> intercept x
SG> 1.2872 -0.1064
SG> s.e. 0.9009 0.3003
SG> sigma^2 estimated as 6.492: log likelihood = -21.19, aic = 48.38
>> arima(y, xreg=lag(x))
SG> intercept lag(x)
SG> 1.2872 -0.1064
SG> s.e. 0.9009 0.3003
>> arima(y, xreg=c(NA, x[-9]))
SG> intercept c(NA, x[-9])
SG> 0.4392 0.8600
SG> s.e. 0.2372 0.0745
SG> sigma^2 estimated as 0.3937: log likelihood = -7.62, aic = 21.25
>> arima(ts(y), xreg=lag(ts(x)))
SG> arima(x = ts(y), xreg = lag(ts(x)))
SG> intercept lag(ts(x))
SG> 1.2872 -0.1064
SG> s.e. 0.9009 0.3003
SG> sigma^2 estimated as 6.492: log likelihood = -21.19, aic = 48.38
SG> Thanks for your help.
SG> Spencer Graves
SG> ______________________________________________
SG> R-help at stat.math.ethz.ch mailing list
SG> https://stat.ethz.ch/mailman/listinfo/r-help
SG> PLEASE do read the posting guide!
SG> http://www.R-project.org/posting-guide.html
More information about the R-help
mailing list