Search code examples
rtime-seriesmodelingforecasting

auto.arima using xreg and forecasting several ts together


I am trying to run auto.arima given a set of variables in xreg. My code is:

xregvars <- cbind(df$V1,df$V2,df$V3) 
xregvars1 <- as.matrix(sapply(xregvars , as.numeric))
sales <- ts(df$sales, frequency=7)
arimaxFits <- group_by(df, df$region) %>% do(fit=auto.arima((sales), xreg = xregvars1))

where df has variables: week, region, sales, V1, V2, V3. There are 65 different regions and I am trying to forecast sales for each region. Also,there are 120 observations per region. Data looks something like this:

head(df)
  week     region  sales     V1       V2         V3             
02/01/2011 Albany 55.48295 32.08712 42.02820 0.62777576        
09/01/2011 Albany 56.27815 32.13170 42.12087 0.00000000      
16/01/2011 Albany 58.38205 32.13313 42.20314 1.45937474 
23/01/2011 Albany 55.40233 32.12635 42.08676 0.01351126 
30/01/2011 Albany 57.09780 32.10210 42.04645 0.92708561 
06/02/2011 Albany 59.62308 32.10204 42.12536 0.47682755 

I referred to Prof. Rob Hyndman's solution as given here but I still get this error:

Error in model.frame.default(formula = x ~ xreg, drop.unused.levels = TRUE) : 
  variable lengths differ (found for 'xreg')

What am I doing wrong?


Solution

  • I don't think you need the as.matrix(sapply(xregvars, as.numeric)) conversion. When I read in your example data (which I put in a CSV), xregvars was already a matrix of numeric. The as.matrix call made it a 18-element vector instead of a 6x3 matrix I got from your data. Maybe that is why auto.arima complained about the length. If you do need the conversion, try:

    xregvars1 <- matrix(as.numeric(xregvars), ncol=3)