# Time Collection Evaluation: Forecasting Gross sales Knowledge with Autoregressive (AR) Fashions Forecasting the longer term has all the time been one in every of man’s largest needs and plenty of approaches have been tried over the centuries. On this submit we’ll take a look at a easy statistical technique for time collection evaluation, referred to as AR for Autoregressive Mannequin. We are going to use this technique to foretell future gross sales information and can rebuild it to get a deeper understanding of how this technique works, so learn on!

Allow us to dive immediately into the matter and construct an AR mannequin out of the field. We are going to use the inbuilt `BJsales` dataset which accommodates 150 observations of gross sales information (for extra info seek the advice of the R documentation). Conveniently sufficient AR fashions may be constructed immediately in base R with the `ar.ols()` perform (OLS stands for Atypical Least Squares which is the tactic used to suit the precise mannequin). Take a look on the following code:

```information <- BJsales
##  200.1 199.5 199.4 198.9 199.0 200.2 N <- 3 # what number of intervals lookback
n_ahead <- 10 # what number of intervals forecast # construct autoregressive mannequin with ar.ols()
model_ar <- ar.ols(information, order.max = N) # ar-model
pred_ar <- predict(model_ar, n.forward = n_ahead)
pred_ar\$pred
## Time Collection:
## Begin = 151 ## Finish = 160 ## Frequency = 1 ##  263.0299 263.3366 263.6017 263.8507 264.0863 264.3145 264.5372
##  264.7563 264.9727 265.1868 plot(information, xlim = c(1, size(information) + 15), ylim = c(min(information), max(information) + 10))
strains(pred_ar\$pred, col = "blue", lwd = 5)
``` Nicely, this appears to be excellent news for the gross sales crew: rising gross sales! But, how does this mannequin arrive at these numbers? To grasp what’s going on we’ll now rebuild the mannequin. Mainly, every part is within the title already: auto-regressive, i.e. a (linear) regression on (a delayed copy of) itself (auto from Historical Greek self)!

So, what we’re going to do is create a delayed copy of the time collection and run a linear regression on it. We are going to use the `lm()` perform from base R for that (see additionally Studying Knowledge Science: Modelling Fundamentals). Take a look on the following code:

```# reproduce with lm()
df_data <- information.body(embed(information, N+1) - imply(information))
## X1 X2 X3 X4
## 1 -31.078 -30.578 -30.478 -29.878
## 2 -30.978 -31.078 -30.578 -30.478
## 3 -29.778 -30.978 -31.078 -30.578
## 4 -31.378 -29.778 -30.978 -31.078
## 5 -29.978 -31.378 -29.778 -30.978
## 6 -29.678 -29.978 -31.378 -29.778 model_lm <- lm(X1 ~., information = df_data) # lm-model
coeffs <- cbind(c(model_ar\$x.intercept, model_ar\$ar), coef(model_lm))
coeffs <- cbind(coeffs, coeffs[ , 1] - coeffs[ , 2])
spherical(coeffs, 12)
## [,1] [,2] [,3]
## (Intercept) 0.2390796 0.2390796 0
## X2 1.2460868 1.2460868 0
## X3 -0.0453811 -0.0453811 0
## X4 -0.2042412 -0.2042412 Zero data_pred <- df_data[nrow(df_data), 1:N]
colnames(data_pred) <- names(model_lm\$coefficients)[-1]
pred_lm <- numeric()
for (i in 1:n_ahead) { data_pred <- cbind(predict(model_lm, data_pred), data_pred) pred_lm <- cbind(pred_lm, data_pred[ , 1]) data_pred <- data_pred[ , 1:N] colnames(data_pred) <- names(model_lm\$coefficients)[-1]
} preds <- cbind(pred_ar\$pred, as.numeric(pred_lm) + imply(information))
preds <- cbind(preds, preds[ , 1] - preds[ , 2])
colnames(preds) <- NULL
spherical(preds, 9)
## Time Collection:
## Begin = 151 ## Finish = 160 ## Frequency = 1 ## [,1] [,2] [,3]
## 151 263.0299 263.0299 0
## 152 263.3366 263.3366 0
## 153 263.6017 263.6017 0
## 154 263.8507 263.8507 0
## 155 264.0863 264.0863 0
## 156 264.3145 264.3145 0
## 157 264.5372 264.5372 0
## 158 264.7563 264.7563 0
## 159 264.9727 264.9727 0
## 160 265.1868 265.1868 0
```

As you’ll be able to see, the coefficients and predicted values are the identical (aside from some negligible rounding errors)!

A number of issues warrant additional consideration: When constructing the linear mannequin in line 17 the formulation is created dynamically on the fly as a result of the dependent variable is within the final column which quantity is determined by `N` (the variety of lookback intervals). To be extra exact, it isn’t only a easy linear regression however a a number of regression as a result of every column (which symbolize completely different time delays) goes into the mannequin as a separate (unbiased) variable. Moreover, the regression is carried out on the demeaned information, that means that you just subtract the imply.

So, underneath the hood what sounds so spectacular (“Autoregressive mannequin”.. wow!) is nothing else however good ol’ linear regression. So, for this technique to work, there have to be some autocorrelation within the information, i.e. some repeating linear sample.

As you’ll be able to think about there are cases the place this is not going to work. For instance, in monetary time collection there’s subsequent to no autocorrelation (in any other case it will be too simple, proper! – see additionally my query and solutions on Quant.SE right here: Why aren’t econometric fashions used extra in Quant Finance?).

With a view to use this mannequin to foretell `n_ahead` intervals forward the predict perform first makes use of the final `N` intervals after which makes use of the brand new predicted values as enter for the following prediction, and so forth `n_ahead` occasions. After that, the imply is added once more. Clearly, the farther we predict into the longer term the extra unsure the forecast turns into as a result of the idea of the prediction contains an increasing number of values that had been predicted themselves. The values for each parameters had been taken right here for demonstration functions solely. A sensible situation could be to take extra lookback intervals than predicted intervals and you’d, in fact, take area information under consideration, e.g. when you could have month-to-month information take not less than twelve intervals as your `N`.

This submit solely barely scratched the floor of forecasting time collection information. Mainly, lots of the commonplace approaches of statistics and machine studying may be modified in order that they can be utilized on time collection information. But, even essentially the most refined technique just isn’t in a position to foresee exterior shocks (like the present COVID-19 pandemic) and suggestions loops when the very forecasts change the precise behaviour of individuals.

So, all strategies must be taken with a grain of salt due to these systematic challenges. You need to all the time hold that in thoughts whenever you get the most recent gross sales forecast!