mlp
функция nnfor
пакета в R, что эквивалентно NARX. Эта функция обеспечивает внешние регрессоры и лаги:
xreg: Экзогенные регрессоры. Каждый столбец является отдельным регрессором и
размер выборки должен быть не менее целевого набора выборок,
но может быть дольше.
xreg.lags: это список, содержащий лаги для
каждая экзогенная переменная. Каждый список представляет собой числовой вектор, содержащий
запаздывает. Если xreg имеет 3 столбца, то список xreg.lags должен содержать три
элементы. Если NULL, то он указывается автоматически.
См. Пример Николая Курентца ниже:
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!