gpt4 book ai didi

r - 在 xts 对象中设置频率

转载 作者:行者123 更新时间:2023-12-01 20:09:16 25 4
gpt4 key购买 nike

我想在 R 中创建一个 xts 对象,然后将其分解为季节性和趋势。

> 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")

enter image description here

当我尝试分解它时,我收到此错误消息..

> plot(decompose(ctr.ts))
Error in decompose(ctr.ts) : time series has no or less than 2 periods

我的观察是每天进行的,时间跨度为 2014 年 12 月 1 日至 2015 年 2 月 25 日。我是否设置了错误的频率

最佳答案

对于 xts 类型的时间序列的频率:默认情况下,xts 有每日频率,因此如果是每日,则无需包含任何频率:

 ctr.xts <- xts(chicos[, 7], order.by = chicos[, 8])

R 函数 decompose() 仅适用于 ts 类型的对象。因此,您可能希望通过发出以下行将 xts 对象转换为 ts:

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

此外,您可能想尝试不同的频率:

  • 每月:12
  • 每年:365

希望这会有所帮助。

关于r - 在 xts 对象中设置频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28744218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com