giacomo moro
2005-Oct-15 15:33 UTC
[R] regression using a lagged dependent variable as explanatory variable
Hi, I would like to regress y (dependent variable) on x (independent variable) and y(-1). I have create the y(-1) variable in this way: ly<-lag(y, -1) Now if I do the following regression lm (y ~ x + ly) the results I obtain are not correct. Can someone tell me the code to use in R in order to perform a regression using as explanatory variable a lagged dependent variable? My best regards, Giacomo --------------------------------- [[alternative HTML version deleted]]
Gabor Grothendieck
2005-Oct-15 15:50 UTC
[R] regression using a lagged dependent variable as explanatory variable
Create time series from your data and then use lm with the dyn or dynlm package (as lm does not support time series directly). With the dyn package you just preface lm with dyn$ and then use lm as usual: library(dyn) yt <- ts(y) xt <- ts(x) dyn$lm(yt ~ xt + lag(yt, -1)) After loading dyn try this for more info: package?dyn On 10/15/05, giacomo moro <gmbegnis at yahoo.it> wrote:> Hi, > I would like to regress y (dependent variable) on x (independent variable) and y(-1). > I have create the y(-1) variable in this way: ly<-lag(y, -1) > Now if I do the following regression lm (y ~ x + ly) the results I obtain are not correct. > Can someone tell me the code to use in R in order to perform a regression using as explanatory variable a lagged dependent variable? > My best regards, > Giacomo > > > --------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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 >
Achim Zeileis
2005-Oct-15 16:04 UTC
[R] regression using a lagged dependent variable as explanatory variable
On Sat, 15 Oct 2005, giacomo moro wrote:> Hi, > I would like to regress y (dependent variable) on x (independent variable) and y(-1). > I have create the y(-1) variable in this way: ly<-lag(y, -1) > Now if I do the following regression lm (y ~ x + ly) the results I obtain are not correct.The reason is that lm() itself does not match time series by their time attribute (and apart from the "tsp" attribute x and lag(x, -1) are the same vector). On ?lm there is a description what you could do to create a time series and then apply lm(). As Gabor already wrote, there are two convenience interfaces available in the packages dyn and dynlm. Z> Can someone tell me the code to use in R in order to perform a regression using as explanatory variable a lagged dependent variable? > My best regards, > Giacomo > > > --------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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 >