robjhyndman / forecast

Forecasting Functions for Time Series and Linear Models
http://pkg.robjhyndman.com/forecast
1.12k stars 342 forks source link

auto.arima gives error wrong length for 'fixed' #795

Closed chintan-sd closed 5 years ago

chintan-sd commented 5 years ago

Hi, When using auto.arima with lambda parameter, I have a specific case where it is giving error wrong length for 'fixed'

I have posted details and an example to reproduce issue here: https://stats.stackexchange.com/questions/403990/auto-arima-throws-error-wrong-length-for-fixed

Thanks Chintan

mitchelloharawild commented 5 years ago

I'd be careful when using BoxCox.lambda() (or lambda="auto") for this series. The chosen lambda parameter seems very extreme for the series. Nonetheless, it seems to be something which can be fixed by changes in the forecast package.


If narma = 0 and nxreg=0, then fixed should be NULL.

MRE:

library(forecast)

t <- c(5354969,5714384,6993786,8134965,7291882,7855863,8260187,10187979,8472624,9317541,8667358,10245900,8140618,7501393,7592070,9683767,8327092,9042875,9835730,10828364,8200433,8516591,8290409,8406130,6587574,8174418)
myts <- ts(t, frequency = 4)

f <- forecast(auto.arima(myts, stepwise = FALSE, approximation = FALSE, lambda = "auto"), h = 2)
#> Error in stats::arima(x = x, order = order, seasonal = seasonal, include.mean = include.mean, : wrong length for 'fixed'

Created on 2019-04-20 by the reprex package (v0.2.1)

mitchelloharawild commented 5 years ago

@robjhyndman I don't fully understand the logic here: https://github.com/robjhyndman/forecast/blob/master/R/newarima2.R#L290 What is this trying to achieve? Add a drift term to the model? It conflicts with the condition in https://github.com/robjhyndman/forecast/blob/master/R/arima.R#L715

Resulting in no drift term being included in the model.

robjhyndman commented 5 years ago

The first is inside an if statement for constant data after differencing. If the data are constant, and d+D=1, then we want to add a drift term so that the forecasts have the same property. If d+D=0, we want to add an intercept. If d+D > 1, it should probably not include the drift. So yes, this could be improved.

The second is inside an if statement which tests if the user wants a constant in the model (not the same as constant data). In that case, the constant is equivalent to a drift term only if d+D=1.

mitchelloharawild commented 5 years ago

So I guess the issue is that the first condition in newarima2.R#L290 is being satisfied as D=1 and d=1, when the condition originally intended to capture D=1 and d + D = 1? Otherwise the constant is being requested from the model with a fixed coefficient in newarima2.R#L290, but the regressor is never being added to the model as d + D > 1 in arima.R#L715