noob
noob

Reputation: 3811

ARIMA Number of regressors does not match fitted model , Error in forecast.forecast_ARIMA(fit, xreg = ) in R

I have a time series object named timeseries2 which is as shown below:

timeseries2

timeseries2
Time Series:
Start = 1 
End = 49 
Frequency = 1 
   sum_profit sum_quantity sum_discount sum_Segment sum_Ship_mode
 1  2424.1125          269         9.45         145           105
 2   866.1925          163         8.05         100            79
 3   123.4122          527        23.15         329           223
 4  3313.2568          543        17.20         352           207
 5  2636.2171          468        18.65         277           208
 6  5316.8660          506        21.42         245           212

I fit the time series where y = sum_profits column and x = columns other than profit which is sum_quantity, sum_discount, sum_Segment and sum_Ship_mode. I fit these and then try to forecast for nexxt 8 periods. I am getting error as shown

 (fit <- auto.arima(timeseries2[,"sum_profit"],
                    xreg=timeseries2[,c(2:5)]))

  fcast <- forecast(fit, xreg=rep(mean(timeseries2[,c(2:5)]),8))

 Error in forecast.forecast_ARIMA(fit, xreg = rep(mean(timeseries2[,
 c(2:5)]),  :    Number of regressors does not match fitted model

Upvotes: 1

Views: 688

Answers (1)

Jake
Jake

Reputation: 96

This error appears because the result from rep(mean(timeseries2[,c(2:5)]),8) is a 1-dimensional vector, whereas your ARIMA model requires a 4-dimensional matrix of values. The following adjustment will run:

 fcast <- forecast(fit, xreg=matrix(rep(mean(timeseries2[,c(2:5)]),8),ncol=4))

Of course, this will only give you a 2 period forecast since it is really 2 observations but that is easily solved. You will get a warning unless you provide names to the matrix columns that match your original data, but this is safely ignored if you check your input properly.

Upvotes: 2

Related Questions