4
votes

I'm creating an xts object with a weekly (7 day) frequency to use in forecasting. However, even when using the frequency=7 argument in the xts call, the resulting xts object has a frequency of 1.

Here's an example with random data:

> values <- rnorm(364, 10)
> days <- seq.Date(from=as.Date("2014-01-01"), to=as.Date("2014-12-30"), by='days')
> x <- xts(values, order.by=days, frequency=7)
> frequency(x)
[1] 1

I have also tried, after using the above code, frequency(x) <- 7. However, this changes the class of x to only zooreg and zoo, losing the xts class and messing with the time stamp formats.

Does xts automatically choose a frequency based on analyzing the data in some way? If so, how can you override this to set a specific frequency for forecasting purposes (in this case, passing a seasonal time series to ets from the forecast package)?

I understand that xts may not allow frequencies that don't make sense, but a frequency of 7 with daily time stamps seems pretty logical.

1
Thanks Deena. While it makes sense that the difference between successive times is 1, perhaps I should ask this: since the data I'm interested in has seasonal patterns where the period is 7, how can I imprint this info into xts to pass to forecast functions (mainly looking at ets)? A ts object could be used (with frequency=7), but xts would be nice due to its additional convenience functions. In effect, I'm looking to imitate the forecast on this page, but using xts in place of ts. - NathanC
xts<->ts conversions are not well-supported in xts (i.e. many things are not implemented or incorrect). It's on my to-do list of things to fix. - Joshua Ulrich
Thanks Joshua! As it is, xts will still be really useful. A quick ts(xts.object, frequency=7) call when passing to forecast functions takes care of the frequency issue for me. - NathanC

1 Answers

7
votes

Consecutive Date class dates always have a frequency of 1 since consecutive dates are 1 apart. Use ts or zooreg to get a frequency of 7:

tt <- ts(values, frequency = 7)

library(zoo)
zr <- as.zooreg(tt)
# or
zr <- zooreg(values, frequency = 7)

These will create a series whose times are 1, 1+1/7, 1+2/7, ...

If we have some index values of zr

zrdates <- index(zr)[5:12]

we can recover the dates from zrdates like this:

days[match(zrdates, index(zr))]

As pointed out in the comments xts does not support this type of series.