I have a time series that I want to decompose using STL. The data has 1 row per min. I have the data for 10 days (the dataset is the one that comes pre-loaded with twitter's anomaly detection API) and I want to find seasonality within a day (e.g. activity peaks from 9pm to 11pm)
While decomposing with STL
however, I get an error
"series is not periodic or has less than 2 periods".
I understand this is because the time frame of data should be >2yrs. However since I want to check seasonality within a day , is there a way to tell STL
to look for seasonality within a day ?
I tried using frequency option in xts
while converting to time series format but doesn't work (1440 = no. of minutes in a day)
install.packages("devtools")
devtools::install_github("twitter/AnomalyDetection")
library(AnomalyDetection)
library(xts)
#data is part of the pacakage anomaly detection
data(raw_data)
View(raw_data)
#converting raw_data to xts format
raw_data_ts <- ts(raw_data$count, as.POSIXct(raw_data$timestamp, format='%m-%d-%y %H:%M:%S'), frequency = 1440)
raw_data_ts1<-as.ts(raw_data_ts)
# Using STL for seasonal decomposition
modelStl <- stl(raw_data_ts1, s.window = "periodic")
I tried to reproduce your error:
date_seq<-seq(as.Date("2000/1/1"), by = "day", length.out = 24)
data<-sin(1:24)+rnorm(24)
> stl(xts(x=data, date_seq, 2*pi),s.window="periodic")
This gives your error. It is in german but does mean the same.
Error in stl(xts(x = data, date_seq, 2 * pi), s.window = "periodic") : Zeitreihe ist nicht periodisch oder umfasst weniger als zwei Perioden
If I use ts
instead of xts
it works fine:
stl(ts(data=data, start = 1, frequency = 2 * pi), s.window="periodic")
*edit in response to updated question
This works for me:
install.packages("devtools")
devtools::install_github("twitter/AnomalyDetection")
library(AnomalyDetection)
time_series<-ts(data=raw_data[,2], start = 1, frequency = 1440)
plot(stl(time_series, s.window = "periodic"))