在R中使用NARX进行时间序列预测

时间:2018-09-05 09:11:32

标签: r machine-learning neural-network time-series forecasting

我想使用NARX(具有外源输入的非线性自回归网络)进行时间序列预测,我急切地在R中寻找正确的软件包和功能。

提出了对该论坛中相同问题的其他答案
    nnetTs()包中的
  • tsDyn
  • nnetar()包中的
  • forecast。另外,我找到了
  • elm()包中的
  • nnfor函数。

但是它们似乎都不等同于Matlab中的NARX模型,我试图避免这种情况。

nnetTs()没有对于NARX模型必不可少的外部输入变量(也就是外部回归变量)的参数。 nnetar()具有这些外部回归变量,并且是自回归的,但是没有提供参数来设置回归变量的滞后时间。

最后一个问题:R中是否有等效的NARX等效物?

1 个答案:

答案 0 :(得分:0)

R中mlp包的

nnfor函数,它等效于NARX。此函数提供了外部回归和滞后:

  

xreg:外生回归变量。每列都是不同的回归变量,   样本数量必须至少与目标样本集中的长度相同,   但可以更长。

     

xreg.lags:这是一个列表,其中包含   每个外生变量。每个列表都是一个数字向量,包含   滞后。如果xreg有3列,则xreg.lags列表必须包含3列   元素。如果为NULL,则会自动指定。

请参见下面的example by Nikolaos Kourentzes

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!