# Stata: VAR (向量自回归) 模型简介

Stata 连享会   主页 || 视频 || 推文

E-mail: m.xu@fs.de

Stata 连享会时间序列专题：

## 2. 数据和估计

``````. varsoc inflation unrate ffr, maxlag(8)

Selection-order criteria
Sample:  41 - 236                            Number of obs      =       196
+---------------------------------------------------------------------------+
|lag |    LL      LR      df    p      FPE       AIC      HQIC      SBIC    |
|----+----------------------------------------------------------------------|
|  0 | -1242.78                      66.5778    12.712   12.7323   12.7622  |
|  1 | -433.701  1618.2    9  0.000  .018956   4.54796   4.62922   4.74867  |
|  2 | -366.662  134.08    9  0.000  .010485   3.95574   4.09793   4.30696* |
|  3 | -351.034  31.257    9  0.000  .009801    3.8881   4.09123   4.38985  |
|  4 | -337.734    26.6    9  0.002  .009383   3.84422    4.1083    4.4965  |
|  5 | -319.353  36.763    9  0.000  .008531    3.7485   4.07351    4.5513  |
|  6 | -296.967   44.77*   9  0.000  .007447*  3.61191*  3.99787*  4.56524  |
|  7 | -292.066  9.8034    9  0.367  .007773   3.65373   4.10063   4.75759  |
|  8 |  -286.45  11.232    9  0.260  .008057   3.68826    4.1961   4.94265  |
+---------------------------------------------------------------------------+
Endogenous:  inflation unrate ffr
Exogenous:  _cons
``````

`varsoc` 展示了之后滞后阶数选择检验的主要结果，检验的细节可以通过 `help varsoc` 得到。似然比和 AIC 都推荐选择六阶滞后，因此本文选择六阶滞后。

``````. var inflation unrate ffr, lags(1/6) dfk small

Vector autoregression

Sample:  39 - 236                               Number of obs     =        198
Log likelihood =  -298.8751                     AIC               =   3.594698
FPE            =   .0073199                     HQIC              =    3.97786
Det(Sigma_ml)  =   .0041085                     SBIC              =   4.541321

Equation           Parms      RMSE     R-sq        F       P > F
----------------------------------------------------------------
inflation            19     .430015   0.9773   427.7745   0.0000
unrate               19     .252309   0.9719    343.796   0.0000
ffr                  19     .795236   0.9481   181.8093   0.0000
----------------------------------------------------------------

------------------------------------------------------------------------------
|      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
inflation    |
inflation |
L1. |    1.37357   .0741615    18.52   0.000     1.227227    1.519913
L2. |   -.383699   .1172164    -3.27   0.001    -.6150029   -.1523952
L3. |   .2219455   .1107262     2.00   0.047     .0034489     .440442
L4. |  -.6102823   .1105383    -5.52   0.000    -.8284081   -.3921565
L5. |   .6247347   .1158098     5.39   0.000     .3962065    .8532629
L6. |  -.2352624   .0719141    -3.27   0.001    -.3771708    -.093354
|
unrate |
L1. |  -.4638928   .1386526    -3.35   0.001    -.7374967   -.1902889
L2. |   .6567903   .2370568     2.77   0.006     .1890049    1.124576
L3. |   -.271786   .2472491    -1.10   0.273     -.759684    .2161119
L4. |  -.4545188   .2473079    -1.84   0.068    -.9425328    .0334952
L5. |   .6755548   .2387697     2.83   0.005     .2043893     1.14672
L6. |  -.1905395    .136066    -1.40   0.163    -.4590393    .0779602
|
ffr |
L1. |   .1135627   .0439648     2.58   0.011     .0268066    .2003187
L2. |  -.1155366   .0607816    -1.90   0.059    -.2354774    .0044041
L3. |   .0356931   .0628766     0.57   0.571    -.0883817    .1597678
L4. |  -.0928074   .0620882    -1.49   0.137    -.2153263    .0297116
L5. |   .0285487   .0605736     0.47   0.638    -.0909816    .1480789
L6. |   .0309895   .0436299     0.71   0.478    -.0551055    .1170846
|
_cons |   .3255765   .1730832     1.88   0.062    -.0159696    .6671226
-------------+----------------------------------------------------------------
unrate       |
inflation |
L1. |   .0903987   .0435139     2.08   0.039     .0045326    .1762649
L2. |  -.1647856   .0687761    -2.40   0.018    -.3005019   -.0290693
L3. |   .0502256    .064968     0.77   0.440    -.0779761    .1784273
L4. |   .0919702   .0648577     1.42   0.158     -.036014    .2199543
L5. |  -.0091229   .0679508    -0.13   0.893    -.1432106    .1249648
L6. |  -.0475726   .0421952    -1.13   0.261    -.1308366    .0356914
|
unrate |
L1. |   1.511349   .0813537    18.58   0.000     1.350814    1.671885
L2. |  -.5591657   .1390918    -4.02   0.000    -.8336363   -.2846951
L3. |  -.0744788   .1450721    -0.51   0.608    -.3607503    .2117927
L4. |  -.1116169   .1451066    -0.77   0.443    -.3979565    .1747227
L5. |   .3628351   .1400968     2.59   0.010     .0863813     .639289
L6. |  -.1895388    .079836    -2.37   0.019    -.3470796    -.031998
|
ffr |
L1. |   -.022236   .0257961    -0.86   0.390    -.0731396    .0286677
L2. |   .0623818   .0356633     1.75   0.082    -.0079928    .1327564
L3. |  -.0355659   .0368925    -0.96   0.336    -.1083661    .0372343
L4. |   .0184223   .0364299     0.51   0.614    -.0534651    .0903096
L5. |   .0077111   .0355412     0.22   0.828    -.0624226    .0778449
L6. |  -.0097089   .0255996    -0.38   0.705    -.0602247     .040807
|
_cons |    .187617   .1015557     1.85   0.066    -.0127834    .3880173
-------------+----------------------------------------------------------------
ffr          |
inflation |
L1. |   .1425755   .1371485     1.04   0.300    -.1280603    .4132114
L2. |   .1461452   .2167708     0.67   0.501    -.2816098    .5739003
L3. |  -.0988776   .2047683    -0.48   0.630     -.502948    .3051928
L4. |  -.4035444   .2044208    -1.97   0.050    -.8069291   -.0001598
L5. |   .5118482   .2141696     2.39   0.018     .0892262    .9344702
L6. |  -.1468158   .1329922    -1.10   0.271      -.40925    .1156184
|
unrate |
L1. |  -1.411603   .2564132    -5.51   0.000    -1.917585   -.9056216
L2. |   1.525265   .4383941     3.48   0.001      .660179     2.39035
L3. |  -.6439154   .4572429    -1.41   0.161    -1.546195    .2583646
L4. |   .8175053   .4573517     1.79   0.076    -.0849893        1.72
L5. |   -.344484   .4415619    -0.78   0.436     -1.21582    .5268524
L6. |   .0366413   .2516297     0.15   0.884     -.459901    .5331835
|
ffr |
L1. |   1.003236   .0813051    12.34   0.000     .8427961    1.163676
L2. |  -.4497879   .1124048    -4.00   0.000    -.6715968   -.2279789
L3. |   .4273715   .1162791     3.68   0.000     .1979173    .6568256
L4. |  -.0775962    .114821    -0.68   0.500    -.3041731    .1489807
L5. |    .259904   .1120201     2.32   0.021     .0388542    .4809538
L6. |  -.2866806   .0806857    -3.55   0.000     -.445898   -.1274631
|
_cons |   .2580589   .3200865     0.81   0.421    -.3735695    .8896873
------------------------------------------------------------------------------

. matlist e(Sigma)

| inflation     unrate        ffr
-------------+---------------------------------
inflation |  .1849129
unrate | -.0064425   .0636598
ffr |  .0788766    -.09169      .6324

``````

`var`命令的报告结果以矩阵形式报告，每个方程以其因变量的名字命名，因此会报告三个方程：通胀方程、失业率方程以及利率方程。 e(Sigma) 中则保存 VAR 模型估计残差的协方差矩阵。注意各个方程的残差相关。

## 3. 解读 VAR 结果：格兰杰因果检验

``````. quietly var inflation unrate ffr, lags(1/6) dfk small

. vargranger

Granger causality Wald tests
+------------------------------------------------------------------------+
|          Equation           Excluded |     F      df    df_r  Prob > F |
|--------------------------------------+---------------------------------|
|         inflation             unrate |  3.5594     6     179   0.0024  |
|         inflation                ffr |  1.6612     6     179   0.1330  |
|         inflation                ALL |  4.6433    12     179   0.0000  |
|--------------------------------------+---------------------------------|
|            unrate          inflation |  2.0466     6     179   0.0618  |
|            unrate                ffr |  1.2751     6     179   0.2709  |
|            unrate                ALL |  3.3316    12     179   0.0002  |
|--------------------------------------+---------------------------------|
|               ffr          inflation |  3.6745     6     179   0.0018  |
|               ffr             unrate |  7.7692     6     179   0.0000  |
|               ffr                ALL |  5.1996    12     179   0.0000  |
+------------------------------------------------------------------------+
``````

``````. quietly regress unrate l(1/6).unrate l(1/6).ffr l(1/6).inflation

. test   l1.inflation=l2.inflation=l3.inflation
>       =l4.inflation=l5.inflation=l6.inflation=0

( 1)  L.inflation - L2.inflation = 0
( 2)  L.inflation - L3.inflation = 0
( 3)  L.inflation - L4.inflation = 0
( 4)  L.inflation - L5.inflation = 0
( 5)  L.inflation - L6.inflation = 0
( 6)  L.inflation = 0

F(  6,   179) =    2.05
Prob > F =    0.0618

. test l1.ffr=l2.ffr=l3.ffr=l4.ffr=l5.ffr=l6.ffr=0

( 1)  L.ffr - L2.ffr = 0
( 2)  L.ffr - L3.ffr = 0
( 3)  L.ffr - L4.ffr = 0
( 4)  L.ffr - L5.ffr = 0
( 5)  L.ffr - L6.ffr = 0
( 6)  L.ffr = 0

F(  6,   179) =    1.28
Prob > F =    0.2709
``````

## 4. 解读 VAR 模型结果：脉冲响应分析

``````. matlist e(Sigma)

| inflation     unrate        ffr
-------------+---------------------------------
inflation |  .1849129
unrate | -.0064425   .0636598
ffr |  .0788766    -.09169      .6324
``````

``````. quietly var inflation unrate ffr, lags(1/6) dfk small

. irf create var1, step(20) set(myirf) replace
(file myirf.irf now active)
(file myirf.irf updated)

. irf graph oirf, impulse(inflation unrate ffr)   ///
response(inflation unrate ffr)  ///
yline(0,lcolor(black)) ///
xlabel(0(4)20) ///
byopts(yrescale)
``````

`irf graph` 命令根据 .irf' 文件中的某些统计量画图。在该文件中的众多统计量中，我们对正交脉冲响应方程最感兴趣，因此我们在 `irf graph` 命令后指定 oirfimpulse( )response( ) 选项则分别指定对以哪些方程为自变量的方程造成冲击，以及对哪些变量需要绘图，这里我们将对所有变量进行冲击并绘图，脉冲响应图如下：

## 相关课程

http://lianxh.duanshu.com

### 课程一览

Stata数据清洗 游万海 直播, 2 小时，已上线

Note: 部分课程的资料，PPT 等可以前往 连享会-直播课 主页查看，下载。

#### 关于我们

• Stata连享会 由中山大学连玉君老师团队创办，定期分享实证分析经验。直播间 有很多视频课程，可以随时观看。
• 连享会-主页知乎专栏，300+ 推文，实证分析不再抓狂。
• 公众号推文分类： 计量专题 | 分类推文 | 资源工具。推文分成 内生性 | 空间计量 | 时序面板 | 结果输出 | 交乘调节 五类，主流方法介绍一目了然：DID, RDD, IV, GMM, FE, Probit 等。
• 公众号关键词搜索/回复 功能已经上线。大家可以在公众号左下角点击键盘图标，输入简要关键词，以便快速呈现历史推文，获取工具软件和数据下载。常见关键词：`课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法`

✏ 连享会学习群-常见问题解答汇总：
https://gitee.com/arlionn/WD