如何使用nnetar进行一步到位的样本外预测?

时间:2018-04-09 19:27:17

标签: r time-series forecasting

我想评估R中nnetar时间序列预测模型的样本外一步性能。我正在寻找类似于以下代码的东西,但是使用nnetar。

#prepare train and test set
train <- lynx[1:100]
test <- lynx[101:length(lynx)]

# fitting out of sample
train.fit <- auto.arima(train)
plot(forecast(train.fit, h = 20))
test.fit <- Arima(test, model = train.fit)
one.step <- fitted(test.fit)

使用nnetar进行以下尝试不会产生任何错误,但one.step结果有8个NAs。

fit <- nnetar(train)
plot(forecast(fit, h = 20))
fit2 <- nnetar(test, model = fit)
one.step <- fitted(fit2)

我的实际数据甚至没有达到那么远,它拥有700个工作日仅5天频率训练点数据。 test.series持有28天。

fit <- nnetar(train.series)
fit2 <- nnetar(test.series, model = fit)

在上面的fit2行,我收到错误:

Error in nnet.default(x = c(0.223628229573182, -0.783157335744087, -0.560497369997497,  : 
  weights vector of incorrect length
In addition: Warning message:
In nnetar(test.series, model = fit) :
  Reducing number of lagged inputs due to short series

任何帮助/示例都将不胜感激。

1 个答案:

答案 0 :(得分:3)

以下使用预测包的v8.3:

library(forecast)
series <- ts(rnorm(728), freq=5)
train.series <- subset(series, end=700)
test.series <- subset(series, start=701)
fit <- nnetar(train.series)
fit2 <- nnetar(test.series, model = fit)
one.step <- fitted(fit2)

但请注意,您无法获得前几个拟合值,因为对nnetar的第二次调用对早期数据一无所知。

以下是更好的

fit2 <- nnetar(series, model = fit)
one.step <- subset(fitted(fit2), start=701)