Skip to content

Instantly share code, notes, and snippets.

@ivannp
Created June 12, 2012 00:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save ivannp/2913657 to your computer and use it in GitHub Desktop.
Save ivannp/2913657 to your computer and use it in GitHub Desktop.
Determine the best ARMA model
armaSearch = function(
xx,
minOrder=c(0,0),
maxOrder=c(5,5),
trace=FALSE )
{
bestAic = 1e9
len = NROW( xx )
for( p in minOrder[1]:maxOrder[1] ) for( q in minOrder[2]:maxOrder[2] )
{
if( p == 0 && q == 0 )
{
next
}
formula = as.formula( paste( sep="", "xx ~ arma(", p, ",", q, ")" ) )
fit = tryCatch( armaFit( formula, data=xx ),
error=function( err ) FALSE,
warning=function( warn ) FALSE )
if( !is.logical( fit ) )
{
fitAic = fit@fit$aic
if( fitAic < bestAic )
{
bestAic = fitAic
bestFit = fit
bestModel = c( p, q )
}
if( trace )
{
ss = paste( sep="", "(", p, ",", q, "): AIC = ", fitAic )
print( ss )
}
}
else
{
if( trace )
{
ss = paste( sep="", "(", p, ",", q, "): None" )
print( ss )
}
}
}
if( bestAic < 1e9 )
{
return( list( aic=bestAic, fit=bestFit, model=bestModel ) )
}
return( FALSE )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment