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