R中nnfor包的mlp函数,相当于NARX。此函数提供外部回归量和滞后量:
xreg:外生回归量。每列是一个不同的回归量,并且
样本大小必须至少与目标样本内集一样长,
但可以更长。
xreg.lags:这是一个包含滞后的列表
每个外生变量。每个列表都是一个数值向量,包含
滞后。如果 xreg 有 3 列,则 xreg.lags 列表必须包含三个
元素。如果为 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!