简体   繁体   中英

How to decompose xts half hourly time-series data

I have the dataset below with half hourly timeseries 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)

I convert to Date format with:

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

Create xts object

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

When I try to decompose it based on this Q with:

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

I get:

Error in decompose(ts(Dataset.xts, frequency = 48)) : 
  time series has no or less than 2 periods

As I mentioned in the comments you need as.ts instead of ts . Also you are specifying a frequency higher than the number of records you have. Both lead to errors.

This code works:

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))

This doesn't (frequency higher than number of records):

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

Neither does this ( ts instead of 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM