在 xts 对象中设置频率 [英] Set frequency in xts object
问题描述
我想在 R 中创建一个 xts
对象,然后我想将其分解为季节性和趋势.
I want to create an xts
object in R, which I then want to decompose to seasonal and trend.
> require(xts)
> require(lubridate)
> chicos$date <- ymd(chicos$date)
> ctr.ts <- xts(chicos[, 7], order.by = chicos[, 8], frequency = 365)
> plot(ctr.ts, main="Meaningful title")
当我尝试分解它时,我收到此错误消息..
When I try to decompose it, I get this error message..
> plot(decompose(ctr.ts))
Error in decompose(ctr.ts) : time series has no or less than 2 periods
我的观察是每天,从 2014-12-01 到 2015-02-25.我是否设置了错误的频率
?
My observations are daily, and span from 2014-12-01 to 2015-02-25. Did I set the wrong frequency
?
推荐答案
对于 xts 类型的时间序列的频率:默认情况下,xts 具有每日频率,因此如果是每日频率,则不需要包含任何频率:
For the frequency of the time series of type xts: By default xts has a daily frequency, So you don't need to include any frequency if it is daily:
ctr.xts <- xts(chicos[, 7], order.by = chicos[, 8])
R 函数分解()仅适用于 ts 类型的对象.因此,您可能希望通过发出以下几行将 xts 对象转换为 ts:
The R function decompose() works only with objects of type ts. So, you may like to convert the xts object to ts by issuing the following lines:
attr(ctr.xts, 'frequency') <- 7 # Set the frequency of the xts object to weekly
periodicity(ctr.xts) # check periodicity: weekly
plot(decompose(as.ts(ctr.xts))) # Decompose after conversion to ts
另外,您可能想尝试不同的频率:
Also, you may like to try different frequencies:
- 每月:12
- 每年:365
希望这会有所帮助.
这篇关于在 xts 对象中设置频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!