ARIMA model selection for Financial data in R
<p>I am trying to fit arima models on financial data, but I get an error:</p>

<blockquote>
  <p>Error in arima(spReturnsOffset, order = c(p, d, q)) : non-stationary
  AR part from CSS</p>
</blockquote>

<p>for the following code:</p>

<pre><code>library(quantmod)


getSymbols("^GSPC", from="1950-01-01")
spReturns = diff(log(Cl(GSPC)))
#differenced logarithmic returns of the "Closing Price" of the S&amp;P500 and strip out the initial NA value:
spReturns[as.character(head(index(Cl(GSPC)),1))] = 0

# Create the forecasts vector to store the predictions
windowLength = 500
foreLength = length(spReturns) - windowLength
forecasts &lt;- vector(mode="character", length=foreLength)

for (f in 0:foreLength) {
    # Obtain the S&amp;P500 rolling window for this day
    spReturnsOffset&lt;- spReturns[(1+f):(windowLength+f)]


order.matrix &lt;- matrix(0, nrow = 3, ncol = 4 * 4)
aic.vec &lt;- numeric(4 * 4)
k &lt;- 1
for (p in 1:4) for (q in 1:4) {
  order.matrix[, k] &lt;- c(p,0,q)
  aic.vec[k] &lt;- AIC(arima(spReturnsOffset, order=c(p, 0, q)))
  k &lt;- k+1
  }
ind &lt;- order(aic.vec, decreasing = FALSE)
aic.vec &lt;- aic.vec[ind]
order.matrix &lt;- order.matrix[, ind]
rownames(order.matrix) &lt;- c("p", "d", "q")
order.matrix &lt;- t(order.matrix)
result &lt;- cbind(order.matrix, aic.vec)
colnames(result) &lt;- c("p", "d", "q", "AIC")
}
</code></pre>

<p>I've tried MLE method in arima function as well, still I get an error msg:</p>

<blockquote>
  <p>Error in solve.default(res$hessian * n.used, A) : Lapack routine
  dgesv: system is exactly singular: U[1,1] = 0</p>
</blockquote>

<p>I also tried using trycatch!And the code kept running!</p>

<p>how can I fix this?</p>

<p><strong>In response to comment by Hack-R</strong></p>

<p>The following code does work:</p>

<pre><code># Obtain the S&amp;P500 returns and truncate the NA value
getSymbols("^GSPC", from="1950-01-01")
spReturns = diff(log(Cl(GSPC)))
spReturns[as.character(head(index(Cl(GSPC)),1))] = 0

# Create the forecasts vector to store the predictions
windowLength = 500
foreLength = length(spReturns) - windowLength
forecasts &lt;- vector(mode="character", length=foreLength)

for (d in 0:foreLength) {
    # Obtain the S&amp;P500 rolling window for this day
    spReturnsOffset = spReturns[(1+d):(windowLength+d)]

    # Fit the ARIMA model
    final.aic &lt;- Inf
    final.order &lt;- c(0,0,0)
    for (p in 0:5) for (q in 0:5) {
        if ( p == 0 &amp;&amp; q == 0) {
            next
        }

        arimaFit = tryCatch( arima(spReturnsOffset, order=c(p, 0, q)),
                             error=function( err ) FALSE,
                             warning=function( err ) FALSE )

        if( !is.logical( arimaFit ) ) {
            current.aic &lt;- AIC(arimaFit)
            if (current.aic &lt; final.aic) {
                final.aic &lt;- current.aic
                final.order &lt;- c(p, 0, q)
                final.arima &lt;- arima(spReturnsOffset, order=final.order)
            }
        } else {
            next
        }
    }
</code></pre>