## 5.8 Forecasting using transformations

Some common transformations which can be used when modelling were discussed in Section 3.1. When forecasting from a model with transformations, we first produce forecasts of the transformed data. Then, we need to reverse the transformation (or back-transform) to obtain forecasts on the original scale. The reverse Box-Cox transformation is given by $$$\tag{5.1} y_{t} = \begin{cases} \exp(w_{t}) & \text{if \lambda=0};\\ (\lambda w_t+1)^{1/\lambda} & \text{otherwise}. \end{cases}$$$

The fable package will automatically back-transform the forecasts whenever a transformation has been used in the model definition.

### Prediction intervals with transformations

If a transformation has been used, then the prediction interval is first computed on the transformed scale, then the end points are back-transformed to give a prediction interval on the original scale. This approach preserves the probability coverage of the prediction interval, although it will no longer be symmetric around the point forecast.

The back-transformation of prediction intervals is done automatically for fable models, provided that you have used a transformation in the model formula.

Transformations sometimes make little difference to the point forecasts but have a large effect on prediction intervals.

### Forecasting with constraints

One common use of transformations is to ensure the forecasts remain on the appropriate scale. For example, log transformations constrain the forecasts to stay positive.

Another useful transformation is the scaled logit, which can be used to ensure that the forecasts are kept within a specific interval. A scaled logit that ensures the forecasted values are between $$a$$ and $$b$$ (where $$a<b$$) is given by: $f(x) = \log\left(\dfrac{x-a}{b-x}\right).$ Inverting this transformation gives the appropriate back-transformation of: $f^{-1}(x) = \dfrac{a + be^x}{1 + e^x} = \dfrac{(b-a)e^x}{1 + e^x} + a.$

To use this transformation when modelling, we can create a new transformation with the new_transformation() function. This allows us to define two functions that accept the same parameters, where the observations are provided as the first argument. The first function is used to transform the data, the second is used to back-transform forecasts.

With this new transformation function defined, it is now possible to restrict forecasts to be within a specified interval. For example, to restrict the forecasts to be between 0 and 100 you could use scaled_logit(y, 0, 100) as the model’s left hand side formula.

For a Box-Cox transformation, the back-transformed mean is given by $$$\tag{5.2} y_t = \begin{cases} \exp(w_t)\left[1 + \frac{\sigma_h^2}{2}\right] & \text{if \lambda=0;}\\ (\lambda w_t+1)^{1/\lambda}\left[1 + \frac{\sigma_h^2(1-\lambda)}{2(\lambda w_t+1)^{2}}\right] & \text{otherwise;} \end{cases}$$$ where $$\sigma_h^2$$ is the $$h$$-step forecast variance on the transformed scale. The larger the forecast variance, the bigger the difference between the mean and the median.
To see how much difference this bias-adjustment makes, consider the following example, where we forecast average annual price of eggs using the drift method with a log transformation $$(\lambda=0)$$. The log transformation is useful in this case to ensure the forecasts and the prediction intervals stay positive.