# Plotting predicted values in ARIMA time series in R

There are likely more than one serious misunderstandings in this question, but it is not meant to get the computations right, but rather to motivate the learning of time series with some focus in mind.

In trying to understand the application of time series, it seems as though de-trending the data makes predicting future values implausible. For instance, the gtemp time series from the astsa package looks like this:

The trend upward in the past decades needs to be factored in when plotting predicted future values.

However, to evaluate the time series fluctuations the data need to be converted into a stationary time series. If I model it as an ARIMA process with differencing (I guess this is carried out because of the middle 1 in order = c(-, 1, -)) as in:

require(tseries); require(astsa)
fit = arima(gtemp, order = c(4, 1, 1))


and then try to predict future values ($50$ years), I miss the upward trend component:

pred = predict(fit, n.ahead = 50)
ts.plot(gtemp, pred$pred, lty = c(1,3), col=c(5,2))  Without necessarily touching on the actual optimization of the particular ARIMA parameters, how can I recover the upward trend in the predicted part of the plot? I suspect there is an OLS "hidden" somewhere, which would account for this non-stationarity? I have come across the concept of drift, which can be incorporated into the Arima() function of the forecast package, rendering a plausible plot: par(mfrow = c(1,2)) fit1 = Arima(gtemp, order = c(4,1,1), include.drift = T) future = forecast(fit1, h = 50) plot(future) fit2 = Arima(gtemp, order = c(4,1,1), include.drift = F) future2 = forecast(fit2, h = 50) plot(future2)  which is more opaque as to its computational process. I am aiming at some sort of understanding of how the trend is incorporated into the plot calculations. Is one of the problems that there no drift in arima() (lower case)? In comparison, using the dataset AirPassengers, the predicted number of passengers beyond the endpoint of the dataset is plotted accounting for this upward trend: The code is: fit = arima(log(AirPassengers), c(0, 1, 1), seasonal = list(order = c(0, 1, 1), period = 12)) pred <- predict(fit, n.ahead = 10*12) ts.plot(AirPassengers,exp(pred$pred), log = "y", lty = c(1,3))


rendering a plot that makes sense.

• I would say that if you think you have a series where the trend has changed over time, ARIMA models may not be the best way to approach prediction of them. In the absence of subject matter knowledge (which might lead to better models), I'd be inclined to look at state space models; in particular variants of the Basic Structural Model for something like this. Many discussions of state space models can be hard to follow, but Andrew Harvey's books and papers are quite readable (the book Forecasting, Structural Time Series Models and the Kalman Filter is pretty good, for example). ... ctd – Glen_b Aug 16 '16 at 0:14
• ctd ... There are a few other authors that do reasonably well, but even the better ones make it a bit more complicated than it really needs to be for a beginner. – Glen_b Aug 16 '16 at 0:15
• Thank you, @Glen_b. Just trying to get a flair for time series, and as in many math topics the lack of motivating preamble is a killer. All time series that we may really care about seem to trend up or down - populations, GOP, stock market, global temperatures. And I get that you want to get rid of the trends (may be for a second) to see cyclic and seasonal patterns. But the splicing back of the findings with the overarching trend to make predictions is either implied or not addressed as an objective. – Antoni Parellada Aug 16 '16 at 0:25
• Rob Hyndman's comments here are relevant. I may come back and expand on that a little. – Glen_b Aug 16 '16 at 1:54
• Rob J. Hyndman's blog post "Constants and ARIMA models in R" is probably all you need to know. I would be curious to hear you opinion once you explore the blog post. – Richard Hardy Aug 16 '16 at 18:24

## 1 Answer

That is why you shouldn't do ARIMA or anything on non stationary data.

Answer to a question why ARIMA forecast is getting flat is pretty obvious after looking at ARIMA equation and one of assumptions. This is simplified explanation, do not treat it as a math proof.

Let's consider AR(1) model, but that is true for any ARIMA(p,d,q).
Equation of AR(1) is:
$$y_t = \beta y_{t-1} + \alpha + \epsilon$$ and assumption about $\beta$ is that $|\beta| \le 1$. With such a β every next point is closer to 0 than the previous until $\beta y_{t-1} =0$, and $y_t = const = \alpha$.

In that case, how to deal with such a data? You have to make it stationary by differentiation ($new.data=y_t-y_{t-1}$) or calculating % change ($new.data=y_t/y_{t-1} -1$). You are modeling differences, not a data itself. Differences are getting constant with time, that is your trend.

 require(tseries)
require(forecast)
require(astsa)
dif<-diff(gtemp)
fit = auto.arima(dif)
pred = predict(fit, n.ahead = 50)
ts.plot(dif, pred$pred, lty = c(1,3), col=c(5,2)) gtemp_pred<-gtemp[length(gtemp)] for(i in 1:length(pred$pred)){
gtemp_pred[i+1]<-gtemp_pred[i]+pred$pred[i] } plot(c(gtemp,gtemp_pred),type="l")  • Thank you. In a nutshell,$\alpha$would be the slope of the final plot? – Antoni Parellada Jun 27 '18 at 13:47 • No. I think that you confused it, because the slope is often denoted as$\alpha$. However, if you would ask what is the relationship between this$\alpha$and a slope, answer won't be trivial. In a nutshell, if you had chosen differentiation,$\alpha$would be a tangent of a slope, if you had chosen % change there wouldn't be any slope, because trend won't be linear. – mbt Jun 27 '18 at 14:39 • OK. I'll have to play a bit with your code see what it is trying to illustrate in relation to the ts equation. I haven't worked with ts, and it's been a while I since I posted the question. – Antoni Parellada Jun 27 '18 at 16:19 • After playing a bit with the code, I see what's going on. Can you include the coefficients of fit, which are AR1 = 0.257; MA = - 0.7854, into the ARIMA model equation to fully appreciate the generating process of the projected or predicted tail sloped line at the end of your plot? – Antoni Parellada Jun 27 '18 at 19:29 • Sure. In my answer I put only AR(1) equation. Equation for whole ARMA(p, q) process is $$\hat y_t = \sum^p_i \beta_i y_{t-i} + \sum^q_j \gamma_j\epsilon_{t-j} + \alpha+\epsilon_t$$ where first sum is AR(p) part and second sum is MA(q) process. We have here ARMA(1,1), so it is less complex: $$\hat y_t = \beta y_{t-1} + \gamma\epsilon_{t-1} + \alpha+\epsilon_t$$ where$\beta =0.257$,$ \gamma =-0.7854$,$ \alpha=0.0064\$. – mbt Jun 27 '18 at 20:36