Error series is not periodic or has less than two periods. Anyone knows where the error is in the code?

require(dplyr)
require(lubridate)
require(readxl)
require(ggplot2)
require(fpp)
require(forecast)
be_se <- read.csv("be_se.csv", header =T)
be_se$Date <- dmy(be_se$Date)
be_se$Date

seasonal

ts_be_se <- ts(be_se$UC_Casino,frequency = 52)
ts_be_se
stl_be <- stl(ts_be_se, "periodic")
stl_be
seasonal_stl_be <- stl_be$time.series[,1]
trend_stl_be <- stl_be$time.series[,2]
random_stl_be <- stl_be$time.series[,3]

plot(ts_be_se)
plot(as.ts(seasonal_stl_be))
plot(trend_stl_be)
plot(random_stl_be)
plot(stl_be)

be_output <- bind_rows(list("date" = be_se$Date, "be_seasonal" = seasonal_stl_be, "be_trend" = trend_stl_be, "be_random" = random_stl_be))
write.csv(x = be_output, file = "be_output_week.csv")

When this happens to me, it’s because I needed at least one additional data points.

hey! thanks for the response! do you mean I'm missing a column or am I missing something in the code line, such as start/end window? @technocrat

or could it also be the format in excel- I might have the wrong format?

Sorry to be unclear; I was on a tablet.

I made a note from somewhere that explains this better

First thing STL would do is consume the data for 2 or more years (24 or 36 or 48 months) in order to calculate Seasonality, Trend etc. In this case we have exactly 24 data points. Now, STL would required atleast an additional data point ON which it would predict the seasonality. Since STL has already used your first 24 data points in learning the monthly seasonality, the next data point is absolutely required to extend what has been predicted earlier.

In other words, first 24 data points are being used to check out the seasonality while the next data points (greater than 24) will follow the previously calculated seasonal patterns of two years.

It shouldn't have anything to do with Excel as long as you've converted it into a time series object.

1 Like

Thanks so much I figured it out and yes it was because of insufficient data, kept throwing the same error.

Fixed by including 2 full cycle years.

Appreciate your help! Have a good one

1 Like