如何分解xts半小时时间序列数据

问题描述 投票:1回答:1

我有下面的数据集,有半小时的时间序列数据。

Date <- c("2018-01-01 08:00:00", "2018-01-01 08:30:00", 
          "2018-01-01 08:59:59","2018-01-01 09:29:59")
Volume <- c(195, 188, 345, 123)
Dataset <- data.frame(Date, Volume)

我转换为日期格式:

Dataset$Date <- as.POSIXct(Dataset$Date)

创建xts对象

library(xts)
Dataset.xts <- xts(Dataset$Volume, order.by=Dataset$Date)

当我尝试基于this Q分解它时:

attr(Dataset.xts, 'frequency')<- 48
decompose(ts(Dataset.xts, frequency = 48))

我明白了:

Error in decompose(ts(Dataset.xts, frequency = 48)) : 
  time series has no or less than 2 periods
r time-series xts
1个回答
1
投票

正如我在评论中提到的,你需要as.ts而不是ts。您还指定的频率高于您拥有的记录数。两者都会导致错误。

此代码有效:

library(xts)

df1 <- data.frame(date = as.POSIXct(c("2018-01-01 08:00:00", "2018-01-01 08:30:00", 
                                          "2018-01-01 08:59:59","2018-01-01 09:29:59")),
                      volume =  c(195, 188, 345, 123))

df1_xts <- xts(df1$volume, order.by = df1$date)

attr(df1_xts, 'frequency') <- 2
decompose(as.ts(df1_xts))

这不是(频率高于记录数):

attr(df1_xts, 'frequency') <- 48
decompose(as.ts(df1_xts))
Error in decompose(as.ts(df1_xts)) : 
  time series has no or less than 2 periods

这也不是(ts而不是as.ts):

attr(df1_xts, 'frequency') <- 2
decompose(ts(df1_xts))
Error in decompose(ts(df1_xts)) : 
  time series has no or less than 2 periods
© www.soinside.com 2019 - 2024. All rights reserved.