[R] Regression and time series

Gabor Grothendieck ggrothendieck at gmail.com
Tue Apr 12 02:43:40 CEST 2005


On Apr 11, 2005 8:18 PM, Fernando Saldanha <fsaldan1 at gmail.com> wrote:
> Can someone shed some light on this obscure portion of the help for lm?
> 
>    "Considerable care is needed when using 'lm' with time series.
> 
>     Unless 'na.action = NULL', the time series attributes are stripped
>     from the variables before the regression is done.  (This is
>     necessary as omitting 'NA's would invalidate the time series
>     attributes, and if 'NA's are omitted in the middle of the series
>     the result would no longer be a regular time series.)
> 
>     Even if the time series attributes are retained, they are not used
>     to line up series, so that the time shift of a lagged or
>     differenced regressor would be ignored.  It is good practice to
>     prepare a 'data' argument by 'ts.intersect(..., dframe = TRUE)',
>     then apply a suitable 'na.action' to that data frame and call 'lm'
>     with 'na.action = NULL' so that residuals and fitted values are
>     time series."
> 
> I found that ts.intersect does not shorten a set of time series just
> because the series has NAs. It only shortens a set of time series to
> the length of the shortest time series (with NAs counting for the
> length calculation). That being the case, the utility of ts.inersect
> seems limited to me, unless I am missing something (which I probably
> am).
> 
> In particular, I am currently having to pad the beginning of a time
> series when I call diff. For example,
> 
> > a <- ts(c(1, 2, 4))
> > b <- ts(c(NA, diff(a)))
> > ab <- ts.intersect(a, b)
> > Time Series:
>   Start = 1
>   End = 3
>   Frequency = 1
>  a  b
> 1 1 NA
> 2 2  1
> 3 4  2
> 
> I was hoping that something like ts.intersect would spare me the
> trouble of explicitly padding b in the example above. However, if I
> don't pad b the time series get misaligned:
> 
> > a <- ts(c(1, 2, 4))
> > b <- ts(diff(a))
> > ab <- ts.intersect(a, b)
> Time Series:
> Start = 1
> End = 2
> Frequency = 1
>  a b
> 1 1 1
> 2 2 2
> 
> Any comments, suggestions?

b <- ts(diff(a)) should be 

   b <- diff(a) 

By applying ts to diff(a) you are resetting the time.




More information about the R-help mailing list