簡體   English   中英

如何分解xts半小時時間序列數據

[英]How to decompose xts half hourly time-series data

我有下面的數據集,其中包含半小時時間序列數據。

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)

當我嘗試根據這個 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

正如我在評論中提到的,您需要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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM