使用神经网络进行时间序列预测
对于此示例,我将对R中的时间序列进行建模。我将最后24个观察值保留为测试集,并将使用其余的观察值来拟合神经网络。
当前有两种类型的神经网络可用,多层感知器;和极限学习机。
可下载资源
1 2 3 4 5 |
# 拟合 多层感知器 mlp.fit <- mlp(y.in) plot(mlp.fit) print(mlp.fit) |
这是使MLP网络适合时间序列的基本命令。这将尝试自动指定自回归输入和时间序列的必要预处理。利用预先指定的参数,它训练了20个用于生成整体预测的网络和一个具有5个节点的隐藏层。print
是输出拟合网络的摘要:
1 2 3 4 5 6 |
MLP fit with 5 hidden nodes and 20 repetitions. Series modelled in differences: D1. Univariate lags: (1,3,4,6,7,8,9,10,12) Deterministic seasonal dummies included. Forecast combined using the median operator. MSE: 6.2011. |
该函数选择了自回归滞后,并将虚拟变量用于季节性趋势。使用plot
显示网络的体系结构(图1)。
图1.输出 plot(mlp.fit).
浅红色输入代表用于编码季节性的二进制虚拟变量,而灰色输入则是自回归滞后项。要生成预测,您可以输入:
1 2 |
forecast(mlp.fit,h=tst.n) |
图2显示了整体预测,以及各个神经网络的预测。
图2. plot
MLP预测的输出。
您还可以选择隐藏节点的数量。
1 2 |
# 自动拟合 MLP hd.auto.type="valid" |
这将评估1到10个隐藏节点,并选择验证集MSE上的最佳节点。也可以使用交叉验证。输出误差:
1 2 3 4 5 6 7 8 9 10 11 |
MSE H.1 0.0083 H.2 0.0066 H.3 0.0065 H.4 0.0066 H.5 0.0071 H.6 0.0074 H.7 0.0061 H.8 0.0076 H.9 0.0083 H.10 0.0076 |
ELM几乎以相同的方式工作。
1 2 3 |
# 可视化 ELM的结果 plot(elm.fit) |
以下是模型摘要:
1 2 3 4 5 6 7 |
ELM fit with 100 hidden nodes and 20 repetitions. Series modelled in differences: D1. Univariate lags: (1,3,4,6,7,8,9,10,12) Deterministic seasonal dummies included. Forecast combined using the median operator. Output weight estimation using: lasso. MSE: 83.0044. |