2
votes

I want to evaluate the out-of-sample one-step-ahead performance of a nnetar time series forecasting model in R. I'm looking for something analogous to the following code, but using 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)

The following attempt with nnetar doesn't raise any errors, but the one.step result has 8 NAs.

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

I don't even get that far with my real data which holds 700 training points of 5 day frequency weekday-only data. The test.series holds 28 days.

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

On the fit2 line above, I get the error:

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

Any help/examples would be appreciated.

1
may be this book from the package author may help otexts.org/fpp2/nnetar.htmlSathish

1 Answers

3
votes

The following works using v8.3 of the forecast package:

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)

However, note that you can't get the first few fitted values that way because the second call to nnetar knows nothing about the earlier data.

The following is better

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