# [R] arima and xreg

Jose Capco cliomseerg at kriocoucke.mailexpire.com
Fri Sep 12 16:01:55 CEST 2008

```Thats nice thanks =) .. I can trick R to do multivariate armax with
lagged inputs as well and I bet R people didnt designed it that way
(but the idea is the same when doing MLE, it must work)..

anyway.. I wrote a small code (you can change it if you want) that
does armax with multiple inputs in matrix form and wherein you can
give lag for the inputs (a true-blooded ARMAX model
estimation). The function basically does an armax on the given
model .. but if it sees that the inputs are correlated, it deletes the
input columns (theres a bug there in my code.. I didnt have
time to improve it .. but I'll do it soon.. it works though if
inputOrder=1)

Code looks fine and works for me (I tried with your part and mort
data.. I did armax until inputOrder=4)
One thing that annoys me is that the matrix column get named as "blah
+i" instead of "blah+1" .. etc. -_-;

I'll write an improvement to the code soon.. Im having time management
difficulty yet, and I just finished reading 1/3 of your book in 1
week. So this is just as much as I could do for one week starting with
0 knowledge in the theory of time-series =)

#input is a matrix of doubles (for multivariate input)
#output is a vector of doubles having same length as input
#this function returns the one step ahead prediction (can be modified
for more steps
#inputOrder is the order of input in ARMAX in integer
armax = function(input,output, arOrder, maOrder,inputOrder){
N=1
n=length(output)
tempMat=0
if (inputOrder==1) {
tempMat=ts(input)
}
if (inputOrder > 1) {
tempMat=input
for (i in 1:(inputOrder-1)){
tempMat=cbind(tempMat, lag(input,-i))
}
tempMat=tempMat[-c((length(input)+1):(length(input)
+inputOrder-1)),]
}
if (length(tempMat) > length(input)) {
N=length(tempMat[1,])
}
for (i in 1:N){
if (length(tempMat)==1) {
return(arima(ts(output),order=c(arOrder,0,maOrder),optim.control =
list(maxit = 1000)))
}
else {
try(return(arima(ts(output),order=c(arOrder,
0,maOrder),xreg=tempMat,optim.control = list(maxit = 1000))), TRUE)
}
if (N!=1){
tempMat=tempMat[,-(N+1)]
tempMat=tempMat[,-1]
}
else { tempMat=0 }
}
}

```