我试图计算一些时间序列特征,当在R中使用tsfeatures函数时,我得到以下错误:
Warning messages:
1: In .f(.x[[i]], ...) : Insufficient data to compute STL decomposition
2: In .f(.x[[i]], ...) : Insufficient data to compute STL decomposition
对于每个时间序列,有365天的测量值,所以我对数据不足的警告感到困惑。
我怀疑这个错误可能与原始数据有关,我试图将面板数据强制转换为矩阵格式的时间序列,如下所示,但我可能搞砸了:
library(tsfeatures)
set.seed(25)
inds <- seq(as.Date("2022-05-01"), as.Date("2023-04-30"), by = "day")
values <- c(rnorm(length(inds), mean = 20, sd = 2),rnorm(length(inds), mean = 200, sd = 20))
products <- rep(c("A","B"), each = 365)
df <- data.frame(c(inds,inds),products,values)
df_mts <- ts(matrix(df$values, ncol = n_distinct(df$products), nrow = 365), frequency = 365, start = c(2022, as.numeric(format(as.Date("2022-05-01"), "%j"))))
features <- bind_cols(
tsfeatures(df_mts, c("acf_features","entropy","lumpiness","flat_spots","crossing_points")),
tsfeatures(df_mts,"stl_features", s.window = "periodic", robust = TRUE)
)
这是将每日数据转换为ts格式的正确方法吗?
1条答案
按热度按时间iqjalb3h1#
您有一年的每日数据,并将季节周期指定为365。因此,STL无法估计季节性,因为它需要整整两年的数据来估计季节性成分。
如果您想使用每周的季节性而不是每年的季节性,只需将频率设置为7。你的代码将工作。