Hi: I have two time series y(t) and x(t). I want to regress Y on X. Because Y is a time series and may have autocorrelation such as AR(p), so it is not efficient to use OLS directly. The model I am trying to fit is like Y(t)=beta0+beta1*X(t)+rho*Y(t-1)+e(t) e(t) is iid normal random error. Anybody know whether there is a function in R can fit such models? The function can also let me specify how many beta's and rho's I can have in the model. Thx a lot! liu
On 7/8/05, yyan liu <zhliur at yahoo.com> wrote:> Hi: > I have two time series y(t) and x(t). I want to > regress Y on X. Because Y is a time series and may > have autocorrelation such as AR(p), so it is not > efficient to use OLS directly. The model I am trying > to fit is like > Y(t)=beta0+beta1*X(t)+rho*Y(t-1)+e(t) > > e(t) is iid normal random error. Anybody know whether > there is a function in R can fit such models? The > function can also let me specify how many beta's and > rho's I can have in the model. >Make sure you have the latest version of packages dyn and zoo from CRAN and try this: library(dyn) # this also pulls in zoo set.seed(1) x <- zoo(rnorm(50)) y <- 1 + x + rnorm(49) # regress y on x y.lm.1 <- dyn$lm(y ~ x) y.lm.1 # regress y on x and lag of y. We use update to just add last term to model. y.lm.2 <- update(y.lm.1, . ~ . + lag(y, -1)) y.lm.2 anova(y.lm.1, y.lm.2) # difference not significant. Use y.lm.1. # regress y on x and its lag and on lags 1 and 2 of y # this shows that in lag(x,k) that k may be a vector y.lm.3 <- dyn$lm(y ~ x + lag(x,-1) + lag(y, -seq(2))) y.lm.3 For more info try: package?dyn vignette("zoo")
On Fri, 8 Jul 2005, yyan liu wrote:> Hi: > I have two time series y(t) and x(t). I want to > regress Y on X. Because Y is a time series and may > have autocorrelation such as AR(p), so it is not > efficient to use OLS directly. The model I am trying > to fit is like > Y(t)=beta0+beta1*X(t)+rho*Y(t-1)+e(t) > > e(t) is iid normal random error. Anybody know whether > there is a function in R can fit such models? The > function can also let me specify how many beta's and > rho's I can have in the model.If you want to estimate the model by ML, you can use arima() and specify further regressors via the `xreg' argument. Estimation by OLS can be done via lm(), but that typically requires setting up the lags yourself. More convenient interfaces are provided in the `dyn' package by Gabor Grothendieck and my `dynlm' package. Z> Thx a lot! > > liu > > ______________________________________________ > 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 >