[R] Forecast and xreg

Lorenzo Isella lorenzo.isella at gmail.com
Wed Jan 13 22:12:52 CET 2016


Dear All,
Please consider the small self-contained code at the end of the email.
It is an artificial arimax model with a matrix of regressors xreg.
In the script, xreg has as many rows as the number of data points in
the time series "visits" I want to model.
Now, my problem is the following: I am in a similar situation, as in
the example, but my matrix of auxiliary regressors is "shorter" than
the time series, e.g. I am trying to do something like (see the last
line of my script)

modArima <- auto.arima(visits, xreg=xreg[1:40, ])

Which is simply not allowed by the forecast package.
Is there any workaround?
I do not want to throw away the information, so I would prefer *not*
to disregard the predictors just because they are not synchronized to
the time series, nor shorten artificially the time series because it
is longer than my predictor matrix.
Any suggestion is appreciated.
Regards

Lorenzo


########################################################################
library(forecast)
# create some artifical data
modelfitsample <-
data.frame(Customer_Visit=rpois(49,3000),Weekday=rep(1:7,7),
                             Christmas=c(rep(0,40),1,rep(0,8)),Day=1:49)

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

# 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)



More information about the R-help mailing list