[R] time series regression
Gabor Grothendieck
ggrothendieck at gmail.com
Sat Jul 9 01:47:53 CEST 2005
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")
More information about the R-help
mailing list