Why is this xts frequency always 1?

I create a xts object with a weekly (7-day) frequency for use in forecasting. However, even when using an argument frequency=7in a call, the xtsresulting xts object has a frequency of 1.

Here is 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 also tried using the above code frequency(x) <- 7. However, this xonly changes the class to zooregand zoo, losing the class xtsand messing around with time stamp formats.

Does xts use auto-select frequency based on data analysis in some way? If so, how can you override this to set a specific frequency for forecasting purposes (in this case, switching from a seasonal time series to etsfrom a forecast package)?

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

+4
source share
1 answer

Sequential dates of date dates always have a frequency of 1, since consecutive dates are 1. 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)

They will create a series whose time is 1, 1 + 1/7, 1 + 2/7, ...

If we have some zr index values

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

we can restore dates from zrdatesas follows:

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

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

+6
source

Source: https://habr.com/ru/post/1621633/


All Articles