一、向量自回归模型简介
经典回归模型都存在一个强加单向关系的局限性,即被解释变量受到解释变量的影响,但反之不成立。然而,在许多情况下所有变量都相互影响。向量自回归(VAR)模型允许这类双向反馈关系,所有变量都被平等对待,即所有变量都是内生的,变量之间平等地相互影响。VAR模型将单变量自回归的思想扩展到多元时间序列回归,是单变量自回归模型的一般化。它由系统中每个变量对应一个方程组成。每个方程的等式右边都包含一个常数项和系统中所有变量的滞后项。两变量P阶VAR模型的一般表达式如下:
如果序列是平稳的,我们直接根据数据拟合 VAR 模型(称为“水平 VAR”);如果序列非平稳,则将数据进行差分使其变得平稳,在此基础上拟合 VAR 模型(称为“差分 VAR”)。在这两种情况下,模型都是利用最小二乘法对方程逐一进行估计的。对于每个方程,通过最小化平方和的值来估计参数。
二、一个实例:使用VAR模型进行短期预测
(一)数据来源
本期应用案例为:Yash P. Mehra(1994)的文章《工资增长和通货膨胀过程:一种经验方法》所使用的时间序列数据集。
原文如下:
Mehra,Yash P. "Wage growth and the inflation process: an empiricalapproach." Cointegration. Palgrave Macmillan, London, 1994. 147-159.
CSV格式的数据集可以在下面网址下载。https://raw.githubusercontent.com/selva86/datasets/master/Raotbl6.csv
(二)变量
该数据集是8个变量的季度时间序列,变量如下:
1. rgnp :Real GNP.
2. pgnp :Potential real GNP.
3. ulc :Unit labor cost.
4. gdfco: Fixed weight deflator for personal consumption expenditure excluding food andenergy.
5. gdf :Fixed weight GNP deflator.
6. gdfim: Fixed weight import deflator.
7. gdfcf: Fixed weight deflator for food in personal consumption expenditure.
8. gdfce: Fixed weight deflator for energy in personal consumption expenditure.
三、计算过程与Python代码
#(1)载入包
importpandas as pd
importnumpy as np
importmatplotlib.pyplot as plt
%matplotlibinline
# ImportStatsmodels
fromstatsmodels.tsa.api import VAR
fromstatsmodels.tsa.stattools import adfuller
fromstatsmodels.tools.eval_measures import rmse, aic
#(2)导入数据
filepath= 'https://raw.githubusercontent.com/selva86/datasets/master/Raotbl6.csv'
df =pd.read_csv(filepath, parse_dates=['date'], index_col='date')
print(df.shape) # (123, 8)
df.tail()
#(3)时间序列可视化
# Plot
fig,axes = plt.subplots(nrows=4, ncols=2, dpi=120, figsize=(10,6))
for i,ax in enumerate(axes.flatten()):
data = df[df.columns[i]]
ax.plot(data, color='red', linewidth=1)
# Decorations
ax.set_ti