【发布时间】:2016-10-26 13:10:09
【问题描述】:
我正在尝试根据财务数据拟合 arima 模型,但出现错误:
arima 中的错误(spReturnsOffset, order = c(p, d, q)) : non-stationary 来自 CSS 的 AR 部分
以下代码:
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")
}
我也在 arima 函数中尝试过 MLE 方法,但仍然收到错误消息:
solve.default(res$hessian * n.used, A) 中的错误:Lapack 例程 dgesv:系统完全是奇异的:U[1,1] = 0
我也尝试过使用 trycatch!代码一直在运行!
我该如何解决这个问题?
回应 Hack-R 的评论
以下代码确实有效:
# 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
}
}
【问题讨论】:
-
这可能是相关的stats.stackexchange.com/questions/56794/… cmets 提到了我相信的一些可能的转换
标签: r time-series