2016-06-24 4 views
0

Ich versuche ARIMA-Modelle auf Finanzdaten zu passen, aber ich erhalte eine Fehlermeldung:ARIMA Modellauswahl für Finanzdaten in R

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

für den folgenden Code:

library(quantmod) 


getSymbols("^GSPC", from="1950-01-01") 
spReturns = diff(log(Cl(GSPC))) 
#differenced logarithmic returns of the "Closing Price" of the S&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 <- vector(mode="character", length=foreLength) 

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


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

Ich habe MLE-Methode in arima Funktion versucht, so gut, noch erhalte ich eine Fehlermeldung:

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

ich habe auch versucht TryCatch mit und der Code in Betrieb gehalten!

Wie kann ich das beheben?

Als Antwort von Hack-R

Der folgende Code Kommentar funktioniert:

# Obtain the S&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 <- vector(mode="character", length=foreLength) 

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

    # Fit the ARIMA model 
    final.aic <- Inf 
    final.order <- c(0,0,0) 
    for (p in 0:5) for (q in 0:5) { 
     if (p == 0 && 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 <- AIC(arimaFit) 
      if (current.aic < final.aic) { 
       final.aic <- current.aic 
       final.order <- c(p, 0, q) 
       final.arima <- arima(spReturnsOffset, order=final.order) 
      } 
     } else { 
      next 
     } 
    } 
+0

Diese relevant sein können http://stats.stackexchange.com/questions/56794/system-is-exactly-singular-in-r-function-boxcox-ar Die Kommentare einige mögliche Transformationen erwähnen Ich glaube, –

Antwort

0

Der folgende Code tut, was ich fragte, danke für das Lesen und was darauf hindeutet.

#Get S&P 500 
getSymbols("^GSPC", from = "2000-01-01") 

#Compute the daily returns 
gspcRet<-diff(log(Cl(GSPC))) 

#Use only the last two years of returns 
gspcTail<-as.ts(tail(gspcRet,500)) 

#Searching for the best models 
order.matrix<-matrix(0,nrow = 3, ncol = 6 * 6) 
aic.vec<- numeric(6 * 6) 
k<-1 
for(p in 0:5) for(q in 0:5){ 
    order.matrix[,k]<-c(p,0,q) 
    aic.vec[k]<- AIC(arima(gspcTail, order=c(p,0,q))) 
    k<-k+1 
} 
ind<- order(aic.vec,decreasing=F) 
aic.vec<- aic.vec[ind] 
order.matrix<- order.matrix[,ind] 
order.matrix<- t(order.matrix) 
result<- cbind(order.matrix,aic.vec) 
colnames(result)<- c("p","d","q","AIC")