# Why can't my (auto.)arima-model forecast my time series?

For testing I generated a very simple time series with a clear recurring pattern. I expected that auto.arima will generate a model, that can forecast that pattern, but óbviously it doesn't. Can anyone give me some hints how I can improve the model in order to predict that pattern correctly?

library(forecast)

ts<-c(1,1,1,1,1,0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0,1,1,1,1,1)
fit <- auto.arima(ts)
plot(forecast(fit,h=20))

• Your data is rather binary than continuous (most time-series models are designed for non-binary data) and 0.7 is a mean of (1,1,1,1,1,0,0), so you got mean forecast... – Tim Jul 28 '15 at 19:57
• yes it just takes the mean, but I expected that the autocorrelation is detected and the pattern is forecasted correctly, what is wrong? – MikeHuber Jul 28 '15 at 20:12
• Check stats.stackexchange.com/questions/77374/… your data does not really seems to be ARIMA-friendly: binary, deterministic, with no noise. – Tim Jul 28 '15 at 20:35
• Can you recommend a better approach to forecast that "simple" pattern? – MikeHuber Jul 28 '15 at 20:40
• As @Tim rightly points out your data is deterministic in nature and can be easily modeled using simple rule. If you have five 1s followed by two 0s why do you need to do any modeling? Deterministic series can be modeled simply using linear regression. – forecaster Jul 28 '15 at 23:25

Your both examples concern with deterministic time series, with no noise and no trend. Deterministic time series, with no trend is not really the kind of data that ARIMA was designed for (see this question to learn more on ARIMA assumptions).

Actually to forecast future trend given your data what you could do is simply to take averages of different time lags and then repeat them in the same order. The problem in here is to determine the number of lags to use, that is, to find out the length of the window that repeats itself. This could be simply achieved, using sum of squared errors or other error measure. If you define average $k$'th lag value as

$$\overline x_{(k)} = \frac{1}{N/K} \sum_{i=0}^{(N/K)-1} x_{k+i \times K}$$

and sum of squared errors is

$$\mathrm{SSE} = \sum_{k=1}^K \sum_{i=0}^{(N/K)-1} \left( x_{k+i \times K} - \overline x_{(k)} \right)^2$$

then you can use SSE to choose the best window size. Below you can find an example in R.

tsPattern <- function(x, kmax = ceiling((length(x)/2))) {

stopifnot(is.numeric(x))
kmax <- min(round(length(x)/2), kmax)

predPattern <- function(x, k) {
n <- length(x)
pattern <- rep(1:k, times = ceiling(n/k), length.out = n)
pred <- rep(tapply(x, pattern, mean), times = ceiling(n/k), length.out = n)
as.numeric(pred)
}

out <- NULL
for (k in 1:kmax) {
xhat <- predPattern(x, k)
out[k] <- sum((x - xhat)^2)
}

list(fitted = predPattern(x, which.min(out)),
sumsq = which.min(out))
}

ts<-c(1,1,1,1,1,0,0,
1,1,1,1,1,0,0,
1,1,1,1,1,0,0,
1,1,1,1,1,0,0,
1,1,1,1,1,0,0,
1,1,1,1,1,0,0,
1,1,1,1,1,0,0,
1,1,1,1,1,0,0,
1,1,1,1,1)

ts2 <-rep(c(1,1,2,3,4,4,5,5,6,5,5,4,3,3,2),10)
ts2 <- ts2 * round(runif(length(ts2), 0.95, 1.0), digits=2)

tsPattern(ts, 10)
tsPattern(ts2, 20)


Below you can see the results plotted (red points are estimated values, lines are actual data). This primitive approach works very good for your examples of deterministic time series with no trend and no noise, it would fail however with real life data, i.e. exactly in such cases as ARIMA (and other similar time-series methods) were designed for. Below you can see results of using this method and auto.arima for real life data (WWWusage dataset in forecast library). A bit late, but you can specify your frequency and tell arima that you have seasonality:

library(forecast)

ts<-c(1,1,1,1,1,0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0,1,1,1,1,1)

ts <- ts(ts, frequency = 7)
fit <- auto.arima(ts, D = 1)
plot(forecast(fit,h=20))


This works as desired and ARIMA finds the structure. 