问题描述
我正在创建一个每周(7 天)频率的 xts 对象,用于预测.但是,即使在 xts
调用中使用 frequency=7
参数,生成的 xts 对象的频率为 1.
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
我也试过,在使用上面的代码后,frequency(x) .但是,这将
x
的类更改为只有 zooreg
和 zoo
,丢失了 xts
类并弄乱了时间戳格式.
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.
xts 是否会根据某种方式分析数据自动选择频率?如果是这样,您如何覆盖它以设置用于预测目的的特定频率(在这种情况下,将季节性时间序列从预测包中传递给 ets
)?
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)?
我知道 xts 可能不允许使用没有意义的频率,但是带有每日时间戳的 7 频率似乎很合乎逻辑.
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,因为连续日期相隔 1.使用 ts 或 zooreg 获得 7 的频率:
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)
这些将创建一个时间为 1, 1+1/7, 1+2/7, ...
These will create a series whose times are 1, 1+1/7, 1+2/7, ...
如果我们有一些 zr 的索引值
If we have some index values of zr
zrdates <- index(zr)[5:12]
我们可以像这样从 zrdates
恢复日期:
we can recover the dates from zrdates
like this:
days[match(zrdates, index(zr))]
正如评论中指出的,xts 不支持这种类型的系列.
As pointed out in the comments xts does not support this type of series.
这篇关于为什么这个 xts 频率总是 1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!