I am working on a small project with one time series which measures the customer visit data (daily). My covariates are a continuous variable Day to measure how many days have been elapsed since the first day of data collection, and some dummy variables, such as whether that day is Christmas, and which day of the week it is, etc.

Part of my data looks like:

Date    Customer_Visit  Weekday Christmas       Day
11/28/11        2535       2        0            1   
11/29/11        3292       3        0            2   
11/30/11        4103       4        0            3   
12/1/11         4541       5        0            4   
12/2/11         6342       6        0            5  
12/3/11         7205       7        0            6   
12/4/11         3872       1        0            7   
12/5/11         3270       2        0            8   
12/6/11         3681       3        0            9   

My plan is to use ARIMAX model to fit the data. This can be done in R, with the function auto.arima(). I understand that I have to put my covariates into the xreg argument, but my code for this part always returns an error.

Here is my code:

xreg     <- c(as.factor(modelfitsample$Christmas), as.factor(modelfitsample$Weekday), 
modArima <- auto.arima(ts(modelfitsample$Customer_Visit, freq=7), allowdrift=FALSE, 

The error message returned by R is:

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

I learned a lot from How to fit an ARIMAX-model with R? But I am still not very clear how to set up the covariates or dummies in the xreg argument in auto.arima() function.


1 Answer 1


The main problem is that your xreg is not a matrix. I think the following code does what you want. I've used some artificial data to check that it works.

# create some artifical data
modelfitsample <- data.frame(Customer_Visit=rpois(49,3000),Weekday=rep(1:7,7),

# Create matrix of numeric predictors
xreg <- cbind(Weekday=model.matrix(~as.factor(modelfitsample$Weekday)), 

# Remove intercept
xreg <- xreg[,-1]

# Rename columns
colnames(xreg) <- c("Mon","Tue","Wed","Thu","Fri","Sat","Day","Christmas")

# Variable to be modelled
visits <- ts(modelfitsample$Customer_Visit, frequency=7)

# Find ARIMAX model
modArima <- auto.arima(visits, xreg=xreg)
  • $\begingroup$ Hi, Prof.Rob, the code works perfect. Thanks so much for your solution. I really appreciate your help! $\endgroup$
    – Michelle
    Commented Oct 23, 2012 at 23:47
  • $\begingroup$ auto.arima(diff(visits), xreg=xreg) throws the same error. $\endgroup$ Commented Dec 29, 2015 at 9:34
  • $\begingroup$ @MdAzimulHaque - when you diff a ts object, you shorten its length by at least one observation. auto.arima(diff(visits), xreg = xreg) is asking auto.arima to fit an ARIMA model on 48 observations using external regressors with nrow of 49. $\endgroup$
    – Jubbles
    Commented Jan 6, 2016 at 19:08
  • $\begingroup$ @Jubbles i got the answer a while ago. there are 2 ways to handle this. 1st method: auto.arima(diff(diff(visits)),xreg=diff(diff(xreg))) 2nd method: auto.arima(visits,d=2,xreg) $\endgroup$ Commented Jan 7, 2016 at 3:39

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