神经网络可用于给时间序列变量生成预测。
R中的forecast
包可用于实现有单层隐藏层的前馈神经网络,并以滞后输入值预测一元时间序列。
使用R中内置数据集Air Passengers
forecast::nnetar函数的用法和参数
获取帮助
library(forecast)
?nnetar
1.用法
nnetar(y, p, P=1, size, repeats=20, xreg=NULL, lambda=NULL, model=NULL,
subset=NULL, scale.inputs=TRUE, x=y, ...)
2.参数
y
A numeric vector or time series.
数值向量或时间序列
p
Embedding dimension for non-seasonal time series. Number of non-seasonal lags used as inputs. For non-seasonal time series, the default is the optimal number of lags (according to the AIC) for a linear AR(p) model. For seasonal time series, the same method is used but applied to seasonally adjusted data (from an stl decomposition).
输入非季节性的滞后阶数
P
Number of seasonal lags used as inputs.
输入季节性的滞后阶数
size
Number of nodes in the hidden layer. Default is half of the number of input nodes (including external regressors, if given) plus 1.
隐藏层节点的个数
repeats
Number of networks to fit with different random starting weights. These are then averaged when producing forecasts.
以不同随机权重拟合的网络个数
xreg
Optionally, a vector or matrix of external regressors, which must have the same number of rows as y. Must be numeric.
用于拟合模型的外部回归
lambda
Box-Cox transformation parameter.
称作box-cox转换参数
建立模型
## 查看下数据
str(AirPassengers)
# fit
fit <- nnetar(AirPassengers,p = 9,P = ,size = 10,repeats = 50,lambda = 0)
# plot
plot(forecast(fit,10))
一个基于神经网络的预测模型生成如下输出结果:
summary(fit)
结果如下:
Length Class Mode
x 144 ts numeric
m 1 -none- numeric
p 1 -none- numeric
P 1 -none- numeric
scalex 2 -none- list
size 1 -none- numeric
lambda 1 -none- numeric
subset 144 -none- numeric
model 50 nnetarmodels list
nnetargs 0 -none- list
fitted 144 ts numeric
residuals 144 ts numeric
lags 10 -none- numeric
series 1 -none- character
method 1 -none- character
call 6 -none- call
预测后续的10个区间,图形如下: