[R] time function behavior for ts class objects

Prof Brian Ripley ripley at stats.ox.ac.uk
Sun Jun 14 08:56:56 CEST 2009


Try not reducing options(digit=)!  I get (with the default value 7)

> time(xts)
           Jan      Feb      Mar      Apr      May      Jun      Jul      Aug
1973 1973.000 1973.083 1973.167 1973.250 1973.333 1973.417 1973.500 1973.583
1974 1974.000 1974.083 1974.167 1974.250 1974.333 1974.417 1974.500 1974.583
           Sep      Oct      Nov      Dec
1973 1973.667 1973.750 1973.833 1973.917
1974 1974.667 1974.750 1974.833 1974.917

That is the 'correct linear sequence' in years, the units selected by 
'frequency = 12'.  It seems you printed it rounded to 4 digits 
(without telling us, so maybe you had forgotten that you had altered 
the setting).

Please do not confuse the printed representation of an object with its 
value -- this is especially important if you choose to reduce the 
default printing precision so much.

On Sat, 13 Jun 2009, Ron Burns wrote:

> 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

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list