[R] Time series misalignment
Fernando Saldanha
fsaldan1 at gmail.com
Tue Apr 12 22:53:53 CEST 2005
This maybe a basic question, but I have spent several hours
researching and I could not get an answer, so please bear with me. The
problem is with time series in the package tseries. As the example
below shows, the time series can get misaligned, so that bad results
are obtained when doing regressions. I found a way to do this
correctly, but I find it rather cumbersome. My question is: is there a
better way to do it?
Thanks for any help.
Suppose I define:
> y1<-ts(c(1,2,3,5,7))
> x1<-diff(y1)
> z1<-ts(c(1,1,2,2))
Then I get:
> x1
Time Series:
Start = 2
End = 5
Frequency = 1
[1] 1 1 2 2
> z1
Time Series:
Start = 1
End = 4
Frequency = 1
[1] 1 1 2 2
Notice that the Start values for x1 and z1 are different.
However, if I regress z1 on z1 I get:
> reg1 <- lm(z1 ~ x1, na.action = NULL)
> reg1
Call:
lm(formula = z1 ~ x1, na.action = NULL)
Coefficients:
(Intercept) x1
0 1
But this is the wrong answer. The time series z1 and x1 are
misaligned. lm is ignoring the fact that Start = 2 for x1 and Start =
1 for z1.
To fix this problem I did the following:
> tsf <- ts.intersect(y1, x1, z1)
> tsf
Time Series:
Start = 2
End = 4
Frequency = 1
y1 x1 z1
2 2 1 1
3 3 1 2
4 5 2 2
These versions of z1 and x1 are correctly aligned.
Now I can do:
> lm1 <- lm(tsf[,3] ~ tsf[,2])
> lm1
Call:
lm(formula = tsf[, 3] ~ tsf[, 2])
Coefficients:
(Intercept) tsf[, 2]
1.0 0.5
This is the correct answer. However, it is rather cumbersome to refer
to the aligned variables as columns of the time series object tsf.
As an observation, I also called ts.intersect with the option dframe =
t and got exactly the same results.
So my question is: is there a less cumbersome way to keep these time
series aligned?
Thanks again for any help.
More information about the R-help
mailing list