如何使用预测包但不引用整个数据?

How do I use forecast package but not refering to the entire data?

我正在做一个项目,我试图评估不同的 ARIMA 模型估计股票价格的好坏程度。我有一个全年的股票价格数据。假设我想使用 (0,1,0) ARIMA 模型中前 100 天的数据预测第 101 天的价格。我该怎么做?我只知道如何引用整个数据集

fit <- auto.arima(data)
prediction <- forecast(fit,h=20)

这让我使用全年的数据估算了未来 20 天的情况。我的目的是将估计值与实际数据进行比较。

看这里:https://robjhyndman.com/hyndsight/rolling-forecasts/

library(fpp)
h <- 5
train <- window(hsales,end=1989.99)
test <- window(hsales,start=1990)
n <- length(test) - h + 1
fit <- auto.arima(train)
order <- arimaorder(fit)
fcmat <- matrix(0, nrow=n, ncol=h)
for(i in 1:n) {  
  x <- window(hsales, end=1989.99 + (i-1)/12)
  refit <- Arima(x, order=order[1:3], seasonal=order[4:6])
  fcmat[i,] <- forecast(refit, h=h)$mean
}

如果您只想根据前 100 个值进行简单预测,您可以尝试先对数据进行子集化,然后再拟合模型。如果希望预测函数只预测下一个值,则需要将预测范围设置为h = 1

# Only select the first 100 obs from your dataset
subset <- data[1:100]
fit <- auto.arima(subset)
prediction <- forecast(fit, h=1)

编辑:如果你想在之后比较数据,你可以提取第 101 个索引并使用 forecast

中的 accuracy 函数进行比较
library(forecast)
# Extract the 101th index
test_value <- data[101]
accuracy(prediction, test_value)

另请参阅 ?accuracy 以获得帮助