I'm trying to fit Arima model and see which order is the best based on AIC i Have the following for statement, my question is how to show the order of the model because it just gives me AIC values and can't determine which model,, mid.ts is time series created by xts the data is as follows:
mid.ts=
deltao
Jan 1751 -41.23
Jan 1754 -41.10
Jan 1756 -40.25
Jan 1759 -43.61
Jan 1761 -41.54
Jan 1764 -39.79
Jan 1766 -39.63
Jan 1769 -40.74
Jan 1771 -42.63
Jan 1774 -39.47
Jan 1776 -40.30
Jan 1778 -40.30
Jan 1781 -41.56
library(forecast)
for(d in 0:1){
for(p in 0:9){
for(q in 0:9){
fit=Arima(mid.ts,order=c(p,d,q))
print(AIC(fit))
}
}
}
You can do it in following two ways:
Based on printing the parameters along with AIC. Here you will have to visually inspect which model is best, which is time consuming and not a good way.
library(forecast)
for(d in 0:1){
for(p in 0:9){
for(q in 0:9){
fit=Arima(mid.ts,order=c(p,d,q))
print(paste0("AIC is ", AIC(fit), " for d = ", d, ", p = ", p, " and q = ", q))
}
}
}
Save model parameters in a data frame and then using code to find the optimum parameters. I will favor this method, because it involves less work.
library(forecast)
modelAIC <- data.frame()
for(d in 0:1){
for(p in 0:9){
for(q in 0:9){
fit=Arima(mid.ts,order=c(p,d,q))
modelAIC <- rbind(modelAIC, c(d,p,q,AIC(fit))) #
}
}
}
names(modelAIC) <- c("d", "p", "q", "AIC")
rowNum <- which(modelAIC$AIC==max(modelAIC$AIC))
modelAIC[rowNum,]#Required model parameters