在窗口函数中使用频率参数

时间:2016-12-19 21:24:03

标签: r forecasting

假设我有以下循环,它使用ARMA模型通过模型重新拟合来计算滚动预测。

    library(forecast)
    set.seed(1)
    prices=rnorm(1963)
    USDlogreturns=diff(log(prices))
    h <- 1
    train <- window(USDlogreturns, end=1162, frequency=1)
    test <- window(USDlogreturns, start=1163, frequency=1)
    n <- length(test) - h + 1
    fc1 <- ts(numeric(n), start=1163+1, freq=1)
    fc2 <- ts(numeric(n), start=1163+1, freq=1)
    fc3 <- ts(numeric(n), start=1163+1, freq=1)
    fc4 <- ts(numeric(n), start=1163+1, freq=1)
    fit1 <- Arima(train, order=c(0,0,0), include.mean=TRUE, method="ML")
    fit2 <- Arima(train, order=c(0,0,1), include.mean=TRUE, method="ML")
    fit3 <- Arima(train, order=c(1,0,0), include.mean=TRUE, method="ML")
    fit4 <- Arima(train, order=c(1,0,1), include.mean=TRUE, method="ML")
    for(i in 1:n){  
      x <- window(USDlogreturns, end=1162 + i, frequency=100)
      refit1 <- Arima(x, model=fit1, include.mean=TRUE, method="ML")
      refit2 <- Arima(x, model=fit2, include.mean=TRUE, method="ML")
      refit3 <- Arima(x, model=fit3, include.mean=TRUE, method="ML")
      refit4 <- Arima(x, model=fit4, include.mean=TRUE, method="ML")
      fc1[i] <- forecast(refit1, h=h)$mean[h]
      fc2[i] <- forecast(refit2, h=h)$mean[h]
      fc3[i] <- forecast(refit3, h=h)$mean[h]
      fc4[i] <- forecast(refit4, h=h)$mean[h]
    }

当我在R中运行它时,我收到50条警告消息:

“在window.default中(USDlogreturns,结束= 1162 + i,频率= 100):'频率'未更改”

所以我的问题是我很难理解如何告诉R通过窗口函数每隔100天重新调整我的四个ARMA模型8次。

新手的任何提示?

1 个答案:

答案 0 :(得分:0)

我设法自己解决了我的问题。顺便说一下,使用“pos”命令可以提供更加灵活和优雅的解决方案。

length_training <- 1162
    start <- length_training + 1
    end <- length(USDlogreturns) 
    forecast_length <- 1
    for(pos in start:end) {
      fit000 <- Arima(USDlogreturns[(pos-length_training):(pos-1)], order=c(0,0,0), include.mean=TRUE, method="ML")
      fc000 <- forecast(fit000, h=forecast_length)$mean[forecast_length]
      fit001 <- Arima(USDlogreturns[(pos-length_training):(pos-1)], order=c(0,0,1), include.mean=TRUE, method="ML")
      fc001 <- forecast(fit001, h=forecast_length)$mean[forecast_length]
      fit100 <- Arima(USDlogreturns[(pos-length_training):(pos-1)], order=c(1,0,0), include.mean=TRUE, method="ML")
      fc100 <- forecast(fit100, h=forecast_length)$mean[forecast_length]
      fit101 <- Arima(USDlogreturns[(pos-length_training):(pos-1)], order=c(1,0,1), include.mean=TRUE, method="ML")
      fc101 <- forecast(fit101, h=forecast_length)$mean[forecast_length]
    }