# Loading packages
require(forecast)
require(quantmod)
# Loading OHLC xts object
getSymbols('SPY', from = '1950-01-01')
# Selecting weekly Close prices
x <- Cl(to.weekly(SPY))
# ARIMA(p,d,q) estimation and forecasting function
a.ari.fun <- function(x) {
a.ari <- auto.arima(x = x, d = 1, max.p = 50, max.q = 50, max.P = 50,
max.Q = 50, ic = 'aic', approximation = TRUE)
fore <- forecast.Arima(object = a.ari, h = 4, level = c(.9))
supp <- tail(fore$lower, 1)
rest <- tail(fore$upper, 1)
return(c(supp, rest))
}
# Roll apply ARIMA(p,d,q) in rolling window
rollapplyr(data = tail(x, 800), width = 750, FUN = a.ari.fun)
此代码返回一个错误,因为
return(c(supp, rest))
在a.ari.fun()
我写的函数结束时;我很确定,因为如果a.ari.fun()
只返回
return(rest)
它工作正常。
我必须如何安排a.ari.fun()
才能获得适合的对象rollapplyr()
?