# fixing the r error – series is not periodic or has less than two periods

The “series is not periodic or has less than two periods” error message occurs when using the stl function to process a time series. It occurs because the data that you are giving either lacks a frequency or the frequency is greater than one-half of the length of the vector. Fixing these problems will fix our error message.

### Description of the error

When creating a time series with the stl function it is necessary to make sure that the data you are giving it has a stated frequency and that it results in at least two periods. If these conditions are not met, you will get our error message. If you give the stl function a vector that has no frequency associated with it, the function will produce this error message. If the vector has a frequency associated with it and it is greater than one-half of the length of the vector it will also produce this error message because it does not make two complete periods. Fixing this problem requires using two other functions. The cumsum function creates a new vector consisting of the cumulative sums of the original vector and the ts function which creates a time series object. This is basically a vector with a frequency associated with it. This new time series object can then be used with the stl function without getting our error message. It needs to be noted, that even when you get this error message the stl function will still produce results.

### Explanation of the error

Here we have two examples of code that produces our error message. In the first example, the series is not periodic and in the second example, it has less than two periods. Each of these examples produces one of the situations that cause this error message.

> t = as.numeric(Sys.time())
> set.seed(t)
> x = rnorm(15)
> fit= stl(x, t.window=15, s.window=”per”, robust=TRUE)
Error in stl(x, t.window = 15, s.window = “per”, robust = TRUE) :
series is not periodic or has less than two periods

In this example, the series is in an unaltered vector resulting in a series that is not periodic. As a result, the stl Function does not have a frequency on which to base its results.

> t = as.numeric(Sys.time())
> set.seed(t)
> a = rnorm(15)
> b = cumsum(a)
> x = ts(b, frequency = 8)
> fit = stl(x, “periodic”)
Error in stl(x, “periodic”) :
series is not periodic or has less than two periods

In this example, we turn the original vector into a cumulative sum and then into a time series object. However, despite doing this part correctly, we set the frequency to eight on a vector with a length of only fifteen. As a result, it does not make two complete periods.

### How to fix the error

Here are two code examples on how to fix our error message. In the first example, we use the cumsum and ts functions to turn the vector into a time series object to fix the problem of it not being a periodic series. Furthermore, in both cases, we set the frequency to five so that it has at least two periods.

> t = as.numeric(Sys.time())
> set.seed(t)
> a = rnorm(15)
> b = cumsum(a)
> x = ts(b, frequency = 5)
> fit= stl(x, t.window=5, s.window=”periodic”, robust=TRUE)

In this example, we use the cumsum and ts functions to turn the vector into a time series object. This fixes the problem of it not being a periodic series. We also set the frequency to five which produces a total of three periods.

> t = as.numeric(Sys.time())
> set.seed(t)
> a = rnorm(15)
> b = cumsum(a)
> x = ts(b, frequency = 5)
> fit = stl(x, “periodic”)

In this example, we fix the problem of its frequency of eight giving it less than two periods. We reduced the frequency to five giving it a total of three periods. This fixes the problem by putting it over the two-period limit.

Once you understand this error message and how to fix it, it is an easy problem to fix and avoid. To prevent this error message, it is a simple matter of making sure the data you enter is in the form of a time series object with a frequency that is at most one-half of its length.

Scroll to top