2016-06-24 5 views
0

내가 재무 데이터에 아리마 모델에 맞게 노력하고 있어요,하지만 난 오류 얻을 : 다음 코드ARIMA 모델 선택

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

: 나는했습니다

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") 
} 

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

가 나는 또한 trycatch를 사용하여 시도하고 코드가 실행 유지 :뿐만 아니라 아리마 기능에 MLE 방법을 시도, 여전히 오류 MSG를 얻을!

어떻게 해결할 수 있습니까?

다음 코드는 내가 요구 한 것을 수행
# 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

발언은 내가 믿는 몇 가지 가능한 변환을 언급 http://stats.stackexchange.com/questions/56794/system-is-exactly-singular-in-r-function-boxcox-ar이 관련 될 수있다 –

답변

0

는 읽기와 제안에 감사드립니다 : 응답에서

가 해킹-R

다음 코드에 의해 언급하는 작업을 수행합니다.

#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")