[R] time function behavior for ts class objects

Ron Burns rrburns at cox.net
Sun Jun 14 04:23:35 CEST 2009


Hi all-

I am trying to use the time function for ts class objects and do not 
understand the return value.  I want to use it to set up a time trend in 
arima fits.  It does not seem to return a correct linear sequence that 
matches the underlying time series.  I am running:
R version 2.8.1 (2008-12-22).

For example:

R> ## create a time series
R> x <- rnorm(24)
R> (xts <- ts(x,start=c(1973),frequency=12))
          Jan      Feb      Mar      Apr      May      Jun      Jul      Aug
1973 -0.09220  0.29838 -0.48667 -1.08046 -0.65665 -0.11695  0.33333  0.00769
1974  1.23304  0.04211  0.88410  1.08454 -0.38852  0.05231  0.50548 -0.71105
          Sep      Oct      Nov      Dec
1973 -0.17923 -1.29722 -1.04519 -0.90785
1974  1.28810  0.43334 -0.71618  0.26681

R> plot(xts)
The plot of xts starts at 1973.0 as expected (as given by the plot 
x-axis first tick). 

R> ## check what time returns
R> (time.ts <- time(xts))
      Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
1973 1973 1973 1973 1973 1973 1973 1974 1974 1974 1974 1974 1974
1974 1974 1974 1974 1974 1974 1974 1974 1975 1975 1975 1975 1975

I would have expected the 1973 row to contain 1973 for all months and 
not to have a jump after Jun 1973 to Jul 1974.   That is  I  would 
expect something like:

R> ## const my own time specific to this xts
R> y <- c(rep(1973,12),rep(1974,12))
R> (tsp(y) <- attr(hasTsp(xts),"tsp"))
[1] 1973 1975   12
R> (my.time.ts <- as.ts(y))
      Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
1973 1973 1973 1973 1973 1973 1973 1973 1973 1973 1973 1973 1973
1974 1974 1974 1974 1974 1974 1974 1974 1974 1974 1974 1974 1974

I certainly don't think R expects me to construct this myself! So I must 
be missing something, doing something wrong, or there is bug somewhere.  
Looking a bit further, I tried adding an offset in the time() call:

R> time(xts,offset=-6)
      Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
1973 1972 1973 1973 1973 1973 1973 1973 1973 1973 1973 1973 1973
1974 1974 1974 1974 1974 1974 1974 1974 1974 1974 1974 1974 1974

which is close (i.e. Jan 1972) still not what I expect.  However

time(xts,offset=(-6+.1))
      Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
1973 1973 1973 1973 1973 1973 1973 1973 1973 1973 1973 1973 1973
1974 1974 1974 1974 1974 1974 1974 1974 1974 1974 1974 1974 1974

does it.  This behavior is not clear from the help pages (to me at 
least!)  and also (to me at least!) represents buggy behavior. It is 
even less clear now to me how to properly set up the time trends for an 
arima fit?  Does arma expect the results from time() without an offset, 
or what I think it should be? 

Thanks in advance for your consideration and help.

Best regards,
    Ron

-- 
R. R. Burns
Physicist (Retired)
Oceanside, CA




More information about the R-help mailing list