简体   繁体   中英

Time series forecasting with NARX in R

I want to use a NARX (Non-linear AutoRegressive Network with eXogenous input) for time series forecasting and I am desperatly searching for the right package and function in R. Other answers to the same question in this forum came up with

  • nnetTs() of the tsDyn package or
  • nnetar() in the forecast package. Additionally I found the
  • elm() function in the nnfor package.

But non of them seems equivalent to for example the NARX model in Matlab, which I am trying to avoid.

nnetTs() has no argument for exogenous input variables (aka. external regressors) which is essential to the NARX model. nnetar() has these external regressors and is autoregressive, but provides no argument to set the lag of the regressors.

Finally the question: Is there any real NARX eqivalent in R?

mlp function of nnfor package in R, which is an equivalent to NARX. This function provides external regressors and lags:

xreg: Exogenous regressors. Each column is a different regressor and the sample size must be at least as long as the target in-sample set, but can be longer.

xreg.lags: This is a list containing the lags for each exogenous variable. Each list is a numeric vector containing lags. If xreg has 3 columns then the xreg.lags list must contain three elements. If NULL then it is automatically specified.

See the example by Nikolaos Kourentzes below:

library(nnfor)
# The objective is to forecast the Airline Passengers series with only deterministic trend and seasonality
# mlp does the deterministic seasonality internally, when needed, but not the trend.

# Let us prepare some data
y <- AirPassengers
plot(y)
h <- 2*frequency(y)
tt <- cbind(c(1:(length(y)+h),rep(0,2*h)))
plot(tt)
# Observe that the deterministic trend ends with zeros
print(tt)

# Fit a network with no differencing, no univariate lags, and fixed deterministic trend
fit1 <- mlp(y,difforder=0,lags=0,xreg=tt,xreg.lags=list(0),xreg.keep=TRUE)
print(fit1)
plot(fit1)
plot(forecast(fit1,h=h,xreg=tt))
# The forecast is reasonable

# Now let us shift the input so that the zeros are in the forecast period
tt2 <- tt[-(1:h),,drop=FALSE]
plot(forecast(fit1,h=h,xreg=tt2))
# The seasonality is there, but there is zero trend, as the inputs suggest. 
# Also note that the mlp modelled multiplicative seasonality on its own. NNs are cool. 

# Now let us fit a network on the shifted inputs
# I will ask for outplot=1 to see the model fit
fit2 <- mlp(y,difforder=0,lags=0,xreg=tt2,xreg.lags=list(0),xreg.keep=TRUE,outplot=1)
plot(fit2)
plot(forecast(fit2,h=h,xreg=tt2))
# Same as before

# Now lets fit with two inputs, the shifted (lead of 24 periods) and the original trend
fit3 <- mlp(y,difforder=0,lags=0,xreg=cbind(tt[1:192,,drop=FALSE],tt2),xreg.lags=list(0,0),xreg.keep=list(TRUE,TRUE),outplot=1)
print(fit3)
plot(fit3)
plot(forecast(fit3,h=h,xreg=cbind(tt[1:192,,drop=FALSE],tt2)))
# The network gets a bit confused with one of the trend vectors stopping!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM