# Fitting an SARIMA model of time series regressed on another

I'm fitting a $$ARIMA(p,d,q)\times (P,D,Q)_{12}$$ model. The first loop fits p and q. The second loop fits P and Q. Here, d and D are both assumed to be 0 since I'm looking at the interaction between two cycles.

<<arima,echo=FALSE, fig.show='hold', tidy=TRUE>>=
aic_table <- function(data,P,Q,xreg=NULL){
table <- matrix(NA,(P+1),(Q+1))
for(p in 0:P) {
for(q in 0:Q) {
table[p+1,q+1] <- arima(data,order=c(p,0,q),xreg=xreg, method="ML")$aic } } dimnames(table) <- list(paste("AR",0:P, sep=""),paste("MA",0:Q,sep="")) table } u_aic_table <- aic_table(cars_hp$cycle,5,4,xreg=steel_hp$cycle) @ #a and b are found from the previous step <<sarima,echo=FALSE, fig.show='hold', tidy=TRUE>>= aic_table2 <- function(data,P,Q,xreg=NULL){ table <- matrix(NA,(P+1),(Q+1)) for(p in 0:P) { for(q in 0:Q) { table[p+1,q+1] <- arima(data,order=c(5,0,3),xreg=xreg, seasonal = list(order = c(p,0,q), Period=12))$aic
}
}
dimnames(table) <- list(paste("AR",0:P, sep=""),paste("MA",0:Q,sep=""))
table
}
u_aic_table2 <- aic_table2(cars_hp$cycle,5,4,xreg=steel_hp$cycle)


The thing is when I try this, I can get ACF values under the ARIMA just fine. It's when I run the second loop that I get issues. Is there a workaround for this? Is it because I'm using the cyclic decompositions of each time series? I want to understand how my cars time series relates to the steel time series.

     Error in optim(init[mask], armafn, method = optim.method, hessian = TRUE,  :
non-finite finite-difference value [1]

• Could you describe verbally what you code does? It will help those of us who are too lazy to figure that out by reading the code carefully. – Richard Hardy May 6 '16 at 14:12
• I'm fitting a $$ARIMA(p,d,q)\times (P,D,Q)_{12}$$ model. The first loop fits p and q. The second loop fits P and Q. Here, d and D are both assumed to be 0 since I'm looking at the interaction between two cycles. – Glassjawed May 6 '16 at 14:24
• Thank you. It would be even better if you included that at the top of your original post. – Richard Hardy May 6 '16 at 15:06