首页 > 解决方案 > 分解时间序列数据:“不超过 2 个周期”

问题描述

我有一个要分解的时间序列数据字符串。每个数据点对应于给定月份的开始日期,如下所示:

A <- c(5,6,7,8,9,8,5,6,10,11)

我使用以下方法将数据转换为时间序列:

A1 <- as.ts(A, frequency=12)

然后我尝试使用分解:

decompose(A1)

我收到以下错误:

时间序列的周期不少于或少于 2 个

我还使用该zoo包创建了类似的时间序列,但得到了相同的结果。

有什么想法吗?

标签: rtime-seriesdecomposition

解决方案


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.

推荐阅读