我是 R 的新手,刚刚开始使用它。我有三年的每周数据。我想把这个时间序列数据分解成趋势、季节等成分。我有以下疑问:
- 我应该使用哪个函数 -
ts()
或decompose()
- 如何处理闰年的情况。
如有错误请指正,频率为52
提前致谢。如果能提供任何帮助,我将不胜感激。
最佳答案
欢迎来到 R!
是的,频率是52。
如果数据尚未归类为时间序列,您将需要 ts()
和 decompose()
。要查找数据集的类,请使用 class(data)
。如果它返回 "ts"
,就 R 而言,您的数据已经是一个时间序列。如果它返回其他内容,例如 "data.frame"
,那么您需要将其更改为时间序列。将变量分配给 ts(data)
并再次检查该类以确保。
R 中已经加载了月度时间序列数据集 sunspot.month
,您可以在上面练习。这是一个例子。您还可以通过编写 ?decompose
decompose
的帮助文件
class(sunspot.month)
[1] "ts"
> decomp <- decompose(sunspot.month)
> summary(decomp)
Length Class Mode
x 2988 ts numeric
seasonal 2988 ts numeric
trend 2988 ts numeric
random 2988 ts numeric
figure 12 -none- numeric
type 1 -none- character
> names(decomp)
[1] "x" "seasonal" "trend" "random" "figure" "type"
> plot(decomp) # to see the plot of the decomposed time-series
调用 names
表示您还可以访问单个组件数据。这可以通过 $
运算符来完成。例如,如果您只想查看季节性成分,请使用 decomp$seasonal
。
关于r - 每周数据的时间序列分解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21815802/