# Stata：时间序列数据的回归和预测

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

## 2. 数据验证

``````varsoc realcons
dfuller realcons,trend lags(#)
varsoc realdpi
dfuller realdpi,trend lags(#)

varsoc D.realcons
dfuller D.realcons,lags(#)
varsoc D.realdpi
dfuller D.realdpi,lags(#)
``````

Note：`#` 表示滞后阶数，由 `varsoc` 命令基于信息准则确定。

``````reg realcons realdpi if tin(1950q1,1983q4)
``````
``````   Source |       SS       df       MS              Number of obs =     136
----------+------------------------------           F(  1,   134) =       .
Model |  76256873.3     1  76256873.3           Prob > F      =  0.0000
Residual |  84604.0843   134  631.373763           R-squared     =  0.9989
Total |  76341477.4   135  565492.425           Root MSE      =  25.127
---------------------------------------------------------------------------
realcons |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
----------+----------------------------------------------------------------
realdpi |   .8579124   .0024686   347.53   0.000     .8530299    .8627948
_cons |   67.65482   6.270548    10.79   0.000     55.25277    80.05687
---------------------------------------------------------------------------
``````

``````regress realcons realdpi if tin(1950q1,1983q4)
estimates store consmodel
forecast create expostforecast, replace
forecast estimates consmodel
set seed 1
forecast solve, prefix(f_)  ///
begin(tq(1984q1)) end(tq(1985q4)) static  ///
simulate(betas, statistic(stddev,prefix(sd_)) reps(100))
``````

``````list realcons f_realcons if tin(1984q1, 1985q4)
``````
``````     +---------------------+
| realcons   f_real~s |
|---------------------|
137. |   3579.7    3587.67 |
138. |   3628.3    3655.79 |
139. |   3653.5    3721.33 |
140. |   3700.9    3744.75 |
141. |   3756.8    3746.04 |
|---------------------|
142. |   3791.5    3815.62 |
143. |   3860.9    3796.66 |
144. |   3874.2    3832.43 |
+---------------------+
``````

``````gen f_y_up = f_realcons + invnormal(0.975)*sd_realcons
gen f_y_dn = f_realcons + invnormal(0.025)*sd_realcons
``````

``````twoway (line realcons   year)  ///
(line f_realcons year)  ///
(line f_y_up     year, lpattern(dash)) ///
(line f_y_dn     year, lpattern(dash)) ///
if tin(1984q1,1985q4)
``````

• 我们可以插入已知的 ${y}_{T+1}$ 来代表 ${y}_{t-1}$ 在预测公式中 (与 ${x}_{3,T+2},{x}_{4,T+2}\dots etc$ 一起)。
• 我们也可以插入原来预测值 ${y}_{T+1}$, 也叫做 ${y}_{T+1}^{\ast }$ 在公式 (4)，与相应的 ${\mathbit{x}}_{\mathbit{t}}$ 值一起。

``````reg realcons L.realcons realdpi if tin(1950q1,1983q4)
``````
``````      Source |       SS       df       MS              Number of obs =     135
-------------+------------------------------           F(  2,   132) =       .
Model |  75172507.6     2  37586253.8           Prob > F      =  0.0000
Residual |  47085.9217   132  356.711528           R-squared     =  0.9994
Total |  75219593.5   134   561340.25           Root MSE      =  18.887

------------------------------------------------------------------------------
realcons |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
realcons |
L1. |   .6729567   .0662463    10.16   0.000     .5419149    .8039985
|
realdpi |   .2840548   .0564912     5.03   0.000     .1723096    .3958001
_cons |   26.23553   6.328403     4.15   0.000     13.71732    38.75373
------------------------------------------------------------------------------
``````

``````. reg realcons L.realcons realdpi if tin(1950q1,1983q4)
. estimates store consmodel
. forecast create forecaststat, replace
. forecast estimates consmodel
. set seed 1
. forecast solve, prefix(sf_) static  ///
begin(tq(1984q1)) end(tq(1985q4)) ///
simulate(betas, statistic(stddev, prefix(ssd_)) ///
reps(100))
``````

``````. forecast create forecastdy, replace
. forecast estimates consmodel
. set seed 1
. forecast solve, prefix(df_)   ///
begin(tq(1984q1)) end(tq(1985q4)) ///
simulate(betas, statistic(stddev, prefix(dsd_)) ///
reps(100))
``````

``````list realcons sf_realcons df_realcons if tin(1984q1,1985q4)
``````
``````     +--------------------------------+
| realcons   sf_rea~s   df_rea~s |
|--------------------------------|
137. |   3579.7    3575.46    3575.46 |
138. |   3628.3    3623.25     3620.4 |
139. |   3653.5    3677.66    3672.34 |
140. |   3700.9    3702.37    3715.05 |
141. |   3756.8    3734.69    3744.22 |
|--------------------------------|
142. |   3791.5    3795.35    3786.88 |
143. |   3860.9    3812.42    3809.31 |
144. |   3874.2    3870.97    3836.26 |
+--------------------------------+
``````

``````gen sf_y_up = sf_realcons + >invnormal(0.975)*ssd_realcons
gen sf_y_dn = sf_realcons + >invnormal(0.025)*ssd_realcons
gen df_y_up = df_realcons + >invnormal(0.975)*dsd_realcons
gen df_y_dn = df_realcons + >invnormal(0.025)*dsd_realcons
``````

``````twoway (line realcons    year)  ///
(line sf_realcons year)  ///
(line sf_y_up     year, lpattern(dash)) ///
(line sf_y_dn     year, lpattern(dash)) ///
if tin(1984q1,1985q4)
``````
``````twoway (line realcons year)     ///
(line df_realcons year)  ///
(line df_y_up year,lpattern(dash))  ///
(line df_y_dn year,lpattern(dash))  ///
if tin(1984q1,1985q4)
``````

## 相关课程

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