Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ShishirShakya/e0b484295b723aa91579dc68f95718b3 to your computer and use it in GitHub Desktop.
Save ShishirShakya/e0b484295b723aa91579dc68f95718b3 to your computer and use it in GitHub Desktop.
Auto Arima InSample Forecast Accuracy Optimization
rm(list=ls())
dev.off(dev.list()["RStudioGD"])
#################
par(mfrow=c(1,2)) # I want to plot 3 graphs in 1 column
set.seed(2017)
nos <- 1000
my_ar <- -0.6
my_ma <- 0.5
y <- arima.sim(n = nos, list(ar = my_ar, ma = my_ma))
plot(y)
hist(y)
fit <- forecast::auto.arima(y, trace = T, ic ='aic')
fit
forecast::accuracy(fit)
##########
auto.arima_ins_acc <- function(y, p, d, q, acc_coef){
mx_p <- p+1
mx_q <- q+1
x <- matrix(data = NA, nrow=mx_p, ncol=mx_q)
for(i in 0:p){
for(j in 0:q){
fit <- arima(y, order=c(i,d,j))
acc <- forecast::accuracy(fit)
x[i+1,j+1] <- acc[[acc_coef]]
print(i);print(j)
}
}
rownames(x) <- 0:p
colnames(x) <- 0:q
opt <- which(x==min(x), arr.ind=T)-1
rownames(opt) <- 'optimum lags'
colnames(opt) <- c('p', 'q')
val <- list(x=x, optimum <- opt)
val
}
xx <- auto.arima_ins_acc(y, p=5, d=0, q=2, acc_coef = 5)
xx
tseries::adf.test(y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment