0
$\begingroup$

I am using tsCV function from Forecast package, I want to have log(y) as the dependent variable and then forecast y rather than forecasting log(y). I tried to obtain the forecast errors from tsCV and then exponentiate them as:


far2_xreg <- function(x, h, xreg, newxreg) { forecast(Arima(x, order=c(0,0,0), xreg=xreg), xreg=newxreg) }

# generating forecast errors
e1 <- mytsCV(log(data$y), far2_xreg, h=7, window = 1000, xreg_actual=xreg_actual, xreg_forecast=xreg_forecast)

data$e1 <- e1

data$e1<−shift(data$e1, n=1L, fill=NA, type=c("lag"), give.names=FALSE)


# exponentiating the forecast erros
data$e<− exp(data$e1)

mape <- mean(abs((data$e)/data$y), na.rm = TRUE)*100

But I know in this form, It is forecasting the log(y) and then it obtaines the errors, and I transform those errors to the original data. However, it seems it is not the correct approach. Can someone guide me about it please, is this possible within this function to use log(y) to forecast y? Thank you very much.

$\endgroup$
3
  • $\begingroup$ Could you indicate exactly where you exponentiate the forecast errors? Because your code lacks comments it is difficult to determine what it's doing, but it looks to me like you compute percentage errors directly on the last line--no exponentiation is involved. $\endgroup$
    – whuber
    Commented Oct 13, 2021 at 21:23
  • $\begingroup$ e is the series of forecast errors obtained from the tsCV function, here I exponentiate the forecast errors: data$e<− exp(data$e1), I edited the codes in the question. $\endgroup$
    – Shwan
    Commented Oct 13, 2021 at 21:25
  • $\begingroup$ You want to transform your data inside the forecast function, fit the model, then backtransform before returning, NOT transform the data before giving it to tsCV. You can't do this in general because forecast::tsCV requires you to return a forecast object rather than a ts or bare vector. Luckily, with forecast::Arima you can simply set lambda=0. Otherwise you can do it the way you're suggesting but you have to actually backtransform correctly (i.e. use the transformed actuals and the errors to get back the forecasts in logs, exponentiate and compute the untransformed errors). $\endgroup$
    – Chris Haug
    Commented Oct 13, 2021 at 21:40

1 Answer 1

3
$\begingroup$

Why would you want to exponentiate the errors? That will not give you anything useful. To see that, think about the equation you are fitting:

$$\log(y_t) = \beta_0 + \beta_1 x_t + \varepsilon_t$$

The errors on the transformed scale are simply the innovation residuals $\hat{\varepsilon}_t$. The errors on the original scale (called response residuals) are $y_t - \hat{y}_t$ where $\hat{y}_t = \exp(\hat\beta_0 + \hat\beta_1 x_t)(1 + \hat\sigma^2/2)$ and $\hat\sigma^2$ is the estimated variance of $\varepsilon_t$. You might use the innovation residuals to check model assumptions (homoscedasticity, normality, autocorrelation, etc). You might use the response residuals if you want to find where the largest forecast errors are, for example.

In any case, the package allows for transformations using the lambda argument (specifying a Box-Cox transformation). In this case, there is no need to explicitly take logs at all. The following code will give innovation residuals.

far2_xreg <- function(x, h, xreg, newxreg) { 
 forecast(Arima(x, order=c(0,0,0), xreg=xreg), xreg=newxreg, lambda=0) 
}

# generating forecast errors
e1 <- mytsCV(data$y, far2_xreg, h=7, window = 1000, xreg_actual=xreg_actual, xreg_forecast=xreg_forecast)
$\endgroup$
1
  • $\begingroup$ Thank you very much, however, when I include lambda=0 that trasforms the dependent variable to the logarithmic form, the generated errors will be Inf, I dont not have any zero or negative value in my data, further, when I use lambda="auto" the errors will be NA. Im wondering why this is happening? Thank you $\endgroup$
    – Shwan
    Commented Oct 14, 2021 at 13:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.