r - 分解时间序列数据:“不超过 2 个周期”
问题描述
我有一个要分解的时间序列数据字符串。每个数据点对应于给定月份的开始日期,如下所示:
A <- c(5,6,7,8,9,8,5,6,10,11)
我使用以下方法将数据转换为时间序列:
A1 <- as.ts(A, frequency=12)
然后我尝试使用分解:
decompose(A1)
我收到以下错误:
时间序列的周期不少于或少于 2 个
我还使用该zoo
包创建了类似的时间序列,但得到了相同的结果。
有什么想法吗?
解决方案
从decompose()
函数的源代码中可以看出,您的数据的频率必须高于 1,并且非缺失数据点的数量应至少是频率值的 2 倍:
> decompose
function (x, type = c("additive", "multiplicative"), filter = NULL) {
type <- match.arg(type)
l <- length(x)
f <- frequency(x)
if (f <= 1 || length(na.omit(x)) < 2 * f)
stop("time series has no or less than 2 periods")
...
在您的情况下,由于时间序列(构建方式)的频率为 1,因此引发了错误:
A <- c(5,6,7,8,9,8,5,6,10,11)
A1 <- as.ts(A, frequency=12)
> frequency(A1)
# 1
You can construct a time series object with the correct frequency calling ts
instead of as.ts
:
A1 <- ts(A, frequency=12)
> frequency(A1)
# 12
However in this case the same error will get triggered because you have 10 observations, when the required number is at least 24.
In order to make it work - have at least 24 observations:
A1 <- ts(runif(24, 1, 100), frequency=12)
decompose(A1)
# works.
推荐阅读
- java - Univocity - 如何动态提供日期格式
- python - Python:通过电子邮件字符串解析正则表达式函数并返回元组或如果输入无效则返回 ValueError
- r - 基于正则表达式的列表中的子集数据框
- ms-access - 使用 pyodbc 添加到 MS Access 数据库
- zig - 不能使用二维数组的 .len
- c++ - Eigen - 将向量重塑为矩阵
- r - 从包含单引号和双引号的字符创建字符串?
- c# - 自定义 ListView 中的 BaseAdapter,我如何使其在空视图中更健壮
- mysql - mysql用数据目录创建表:访问被拒绝
- c - 你能解决一个int的单个位吗?