Stata连享会 主页 || 视频 || 推文 || 知乎 || Bilibili 站
温馨提示: 定期 清理浏览器缓存,可以获得最佳浏览体验。
New!
lianxh
命令发布了:
随时搜索推文、Stata 资源。安装:
. ssc install lianxh
详情参见帮助文件 (有惊喜):
. help lianxh
连享会新命令:cnssc
,ihelp
,rdbalance
,gitee
,installpkg
⛳ Stata 系列推文:
作者:毕英睿 (中央财经大学)
邮箱:judybi2017@163.com
编者按:本文主要整理自下文,特此致谢!
Source:Evaluating the out-of-sample prediction performance of panel data models -PDF-
目录
在广泛的实际应用中,评估一个统计模型的预测准确性变得愈发普遍和重要。不过,现有应用都集中在一种数据结构上,要么是时间序列,要么是非结构化、截面数据或者合并数据。面板数据存在时间序列和个体两个维度,那么如何评价一个面板数据统计模型的预测性能呢?目前 Stata 中没有合适的命令来评估时间序列维度中模型的样本外性能。
此外,现有的交叉验证 (cross-validation) 程序 (如 crossfold
和 cvauroc
) 在分离样本内和样本外时,通常会对所有的观测值进行处理,而不考虑这些观测值是否属于不同的个体或来自同一个体的后续观测。因此很难引入一个动态效应模型或固定效应模型。
通常我们将一个模型的性能表示为与另一个替代模型的相对值。例如,在评估时间序列框架中的预测准确性时,模型的 RMSE 通常与“朴素 (naive)”预测的 RMSE 进行比较。其中样本内时期的最后一个观测值被用作对样本外观测值的直接预测。
但是,如果我们只是随机抽取观察结果,那么“朴素”的预测会是什么?本文认为,在面板数据的情况下,更有用的练习是使用个体而不是观察值的类似于交叉验证的练习。
因此,我们开发了4个新的命令 xtoos_t
,xtoos_i
,xtoos_bin_t
和 xtoos_bin_i
,分别评估面板数据模型在时间序列和截面维度上的样本外预测性能,还为不同类型的因变量 (连续变量和 0-1 变量) 开发了单独的程序。
ssc install xtoos, replace
时间序列程序 (xtoos_t
,xtoos_bin_t
) 从面板中每个人的估计样本中排除了一些由用户定义的时间段。截面程序 (xtoos_i
,xtoos_bin_i
) 将用户定义的一组个体从估计样本中排除 (包括他们在样本区间内的所有观测样本)。然后对于剩余的 (样本内) 子样本,他们拟合指定的模型,并使用所得到的参数来预测未使用的时期或个体 (样本外) 的因变量 (或结果为正的概率)。
未使用的时间段或个体集在时间序列的情况下在随后的每一步中递归地减少一个时期,在截面的情况下以随机或有序的方式,重复估计和预测评估,直到前面没有更多的时期或更多的个体可以被排除和评估。
在连续因变量的案例中,模型的预测性能既能以绝对值 (RMSE) 报告,又包括相对数据——相对于一个替代模型的 U-Theil ratio。在二元因变量的案例中,性能的评估是基于在训练样本和样本外评估的接收器操作特征统计量下的面积 (AUROC) 。
xtoos
程序的选项和特点足够灵活,可满足以下要求:
xtabond
/ xtdpsys
);
xtoos_t
报告指定模型的预测性能,包括绝对值 (均方根误差 RMSE)、相对数据——相对于一个替代模型的 U-Theil ratio。默认的估计方法是 xtreg
。
xtoos_t depvar [indepvars] [if], *indate(string) *cdate(string)
[met(string)] [mcomp(string)] [evalopt(varname)]
[fe] [xbu] [dum] [opar] [lags(numlist)] [hgraph(numlist)]
[model_options]
默认情况下,替代方法是一种“朴素 (naive)”的预测。在这种预测中,样本期的最后一次观测结果直接用作预测,没有任何变化。该过程还允许使用 AR1 模型作为比较的替代模型。如果样本是不平衡的,xtoos_t
会自动丢弃那些在定义的样本外周期内开始观察的个体。性能结果以两种不同的方式进行分解和报告。
接下来,我们举例说明如何使用 xtoos_t
进行评估。
(1) 使用 xtoos_t
评估第 15 期和第 20 期之间的预测性能 (在样本中共有 20 期,T=20,N=5)。
. webuse invest2, clear
. xtset company time
. xtoos_t invest market stock, indate(15) cdate(20)
Out of sample evaluation according to last in-sample date
| RMSE_oos RMSE_Al~s UTheil N
-------------+--------------------------------------------
15 | 197.5926 268.0517 .7371435 25
16 | 213.448 264.6098 .8066516 20
17 | 218.0811 242.715 .8985071 15
18 | 215.6059 237.3739 .9082966 10
19 | 189.9464 115.6006 1.643126 5
-------------+--------------------------------------------
Summary | 208.0358 250.6152 .8301006 75
Out of sample evaluation according to forecasting horizon
| RMSE_oos RMSE_Al~s UTheil N
-------------+--------------------------------------------
1 | 178.7174 112.7054 1.585704 25
2 | 202.2296 212.2442 .9528156 20
3 | 222.5415 287.1967 .774875 15
4 | 241.209 369.5434 .6527217 10
5 | 246.8993 421.7185 .5854599 5
-------------+--------------------------------------------
Summary | 208.0358 250.6152 .8301006 75
(2) 使用 xtoos_t
评估第 15 期和第 20 期之间的预测性能,但只限于对 1 号公司的评估。
. gen company1 = company == 1
. xtoos_t invest market stock, indate(15) cdate(20) evalopt(company1)
Out of sample evaluation according to last in-sample date
| RMSE_oos RMSE_Al~s UTheil N
-------------+--------------------------------------------
15 | 265.9022 563.972 .4714812 5
16 | 304.6125 553.1732 .5506639 4
17 | 319.0211 533.3001 .5982019 3
18 | 326.3651 512.5205 .6367845 2
19 | 243.6762 182.2999 1.336678 1
-------------+--------------------------------------------
Summary | 294.6642 530.7943 .5551382 15
Out of sample evaluation according to forecasting horizon
| RMSE_oos RMSE_Al~s UTheil N
-------------+--------------------------------------------
1 | 170.677 220.3413 .7746026 5
2 | 257.4415 435.1569 .5916061 4
3 | 333.4687 601.2803 .5545977 3
4 | 414.2776 797.9501 .5191773 2
5 | 463.4559 931.6 .4974838 1
-------------+--------------------------------------------
Summary | 294.6642 530.7943 .5551382 15
(3) 使用 xtoos_t
作为估计方法,使用 xtregar
命令,并使用 xtabond
估计一个 AR1 模型作为比较模型。
· xtoos_t invest market stock, indate(15) cdate(20) met(xtregar) mcomp(xtabond)
Out of sample evaluation according to last in-sample date
| RMSE_oos RMSE_Al~s UTheil N
-------------+--------------------------------------------
15 | 225.784 397.9647 .5673468 25
16 | 231.1073 409.8826 .5638378 20
17 | 234.0517 378.393 .6185414 15
18 | 230.5379 305.2529 .7552357 10
19 | 192.5137 111.7499 1.722719 5
-------------+--------------------------------------------
Summary | 227.4836 373.4778 .6090955 75
Out of sample evaluation according to forecasting horizon
| RMSE_oos RMSE_Al~s UTheil N
-------------+--------------------------------------------
1 | 184.2658 170.6574 1.079741 25
2 | 214.7247 330.6099 .6494805 20
3 | 243.642 447.3137 .5446781 15
4 | 276.5614 537.3367 .5146892 10
5 | 301.6278 575.9547 .5237005 5
-------------+--------------------------------------------
Summary | 227.4836 373.4778 .6090955 75
(4) 使用 xtoos_t
进行固定效应 (组内) 估计,预测结果包括估计的个体部分。
· xtoos_t invest market stock, indate(15) cdate(20) fe xbu
Out of sample evaluation according to last in-sample date
| RMSE_oos RMSE_Al~s UTheil N
-------------+--------------------------------------------
15 | 131.2454 268.0517 .4896273 25
16 | 146.6338 264.6098 .5541511 20
17 | 153.0876 242.715 .6307299 15
18 | 156.1486 237.3739 .6578173 10
19 | 145.2602 115.6006 1.256569 5
-------------+--------------------------------------------
Summary | 144.2926 250.6152 .5757537 75
Out of sample evaluation according to forecasting horizon
| RMSE_oos RMSE_Al~s UTheil N
-------------+--------------------------------------------
1 | 107.0292 112.7054 .9496369 25
2 | 134.963 212.2442 .6358857 20
3 | 158.8733 287.1967 .5531866 15
4 | 182.9497 369.5434 .4950697 10
5 | 198.7604 421.7185 .4713106 5
-------------+--------------------------------------------
Summary | 144.2926 250.6152 .5757537 75
上述命令等同于加入个体虚拟变量,并在预测中包括其估计值。
· xtoos_t invest market stock, indate(15) cdate(20) dum
(5) 使用 xtoos_t
进行固定效应 (组内) 估计,预测结果不包括估计的个体部分。
. xtoos_t invest market stock, indate(15) cdate(20) fe
Out of sample evaluation according to last in-sample date
| RMSE_oos RMSE_Al~s UTheil N
-------------+--------------------------------------------
15 | 200.6561 268.0517 .7485727 25
16 | 217.1251 264.6098 .820548 20
17 | 220.5621 242.715 .9087287 15
18 | 216.974 237.3739 .9140601 10
19 | 189.1054 115.6006 1.635851 5
-------------+--------------------------------------------
Summary | 210.673 250.6152 .8406234 75
Out of sample evaluation according to forecasting horizon
| RMSE_oos RMSE_Al~s UTheil N
-------------+--------------------------------------------
1 | 179.4424 112.7054 1.592138 25
2 | 204.1317 212.2442 .9617776 20
3 | 225.9538 287.1967 .7867565 15
4 | 246.3197 369.5434 .6665514 10
5 | 252.1064 421.7185 .5978073 5
-------------+--------------------------------------------
Summary | 210.673 250.6152 .8406234 75
上述命令等同于加入个体虚拟变量,但不在预测中包括其估计值。
. xtoos_t invest market stock, indate(15) cdate(20) dum opar
(6) 使用 xtoos_t
在模型中包括因变量的滞后项。
. xtoos_t invest market stock, indate(15) cdate(20) lags(1)
Out of sample evaluation according to last in-sample date
| RMSE_oos RMSE_Al~s UTheil N
-------------+--------------------------------------------
15 | 291.8545 268.0517 1.088799 25
16 | 249.6752 264.6098 .9435597 20
17 | 205.3792 242.715 .8461744 15
18 | 187.2163 237.3739 .7886982 10
19 | 92.79947 115.6006 .8027593 5
-------------+--------------------------------------------
Summary | 242.2808 250.6152 .966744 75
Out of sample evaluation according to forecasting horizon
| RMSE_oos RMSE_Al~s UTheil N
-------------+--------------------------------------------
1 | 100.2124 112.7054 .8891535 25
2 | 189.6657 212.2442 .8936205 20
3 | 268.4804 287.1967 .934831 15
4 | 366.9452 369.5434 .9929693 10
5 | 448.165 421.7185 1.062711 5
-------------+--------------------------------------------
Summary | 242.2808 250.6152 .966744 75
(7) 使用动态模型方法的 xtoos_t
,即 xtabond
或 xtdpdsys
。默认规格包括因变量的一个滞后期。
. xtoos_t invest market stock, indate(15) cdate(20) lags(2) met(xtabond)
Out of sample evaluation according to last in-sample date
| RMSE_oos RMSE_Al~s UTheil N
-------------+--------------------------------------------
15 | 166.3427 268.0517 .6205622 25
16 | 164.3264 264.6098 .621014 20
17 | 138.9675 242.715 .5725541 15
18 | 114.9347 237.3739 .4841925 10
19 | 139.9054 115.6006 1.210248 5
-------------+--------------------------------------------
Summary | 152.8161 250.6152 .609764 75
Out of sample evaluation according to forecasting horizon
| RMSE_oos RMSE_Al~s UTheil N
-------------+--------------------------------------------
1 | 110.7552 112.7054 .9826966 25
2 | 161.4334 212.2442 .7606023 20
3 | 177.9804 287.1967 .6197162 15
4 | 175.9378 369.5434 .4760952 10
5 | 166.6603 421.7185 .3951931 5
-------------+--------------------------------------------
Summary | 152.8161 250.6152 .609764 75
(8) 使用 xtoos_t
来画一个 "hair "图,其中包括个体 1 到 5 的每个预测水平的所有模型预测值。
. xtoos_t invest market stock, indate(15) cdate(20) lags(1) hgraph(1/5)
xtoos_i
报告指定模型的预测性能,既包括绝对值 (RMSE),也包括通过 U-Theil 比率与替代模
型的相对值。默认的估计方法是 xtreg
。
xtoos_i depvar [indepvars] [if], *ous(integer) *ksmpl(integer)
[rsmpl(integer)] [evalopt(varname)] [met] [mcomp(string)] [fe]
[dum] [lags(numlist)] [hgraph] [model_options]
默认情况下,替代模型是一个“朴素”的预测,即在每个时间段内所有样本个体的平均值被用来预测被排除的个体。该程序还允许使用一个 AR1 模型作为比较的替代模型。它还报告了指定模型和比较模型的几个样本内和样本外的统计数据。
被排除在外 (样本外) 的个体可能是:
如果在选项 1 中,n=1,或在选项 2 中,k=1,都相当于“留一法交叉验证 (LOOCV)”。
接下来,我们举例说明如何使用 xtoos_i
进行评估。
(1) 使用 xtoos_i
来评估 40 个个体 (ous(40)
) 的 20 个随机子样本 (rsmpl(20)
) 和同样 40 个个体的有序子样本 (ksmpl(40)
) 的预测性能。
. webuse abdata, clear
. xtoos_i n w l.w k l.k ys l.ys, ous(40) rsmpl(20) ksmpl(40)
Out of sample evaluation: Random sampling
| RMSE_in RMSE_oos R2_in R2_oos RMSE_Al~n RMSE_Al~s
----- ----+-----------------------------------------------------------
Summary | .5671476 .5816968 .8210846 .8091391 1.323403 1.332992
| R2_Alt_in R2_Alt_~s Utheil_~t N
----------+--------------------------------------------
Summary | .0258183 -.0022564 .4363842 236.4064
Out of sample evaluation: Ordered partition
| RMSE_in RMSE_oos R2_in R2_oos RMSE_Al~n RMSE_Al~s
----------+-------------------------------------------------------
Summary |.5679359 .5890679 .8193944 .791674 1.318939 1.347322
| R2_Alt_in R2_Alt_~s Utheil_~t N
----------+-------------------------------------------
Summary | .0259486 -.0898223 .4372138 230.0393
(2) 使用 xtoos_i
来评估预测性能,只限制在前 6 个评估个体 (evalopt()
),而且没有随机抽样。
. gen idlto6 = id <= 6
. xtoos_i n w l.w k l.k ys l.ys, ous(40) rsmpl(0) ksmpl(40) evalopt(idlto6)
Out of sample evaluation: Ordered partition
| RMSE_in RMSE_oos R2_in R2_oos RMSE_Al~n RMSE_Al~s
--------+--------------------------------------------------------
Summary |.5679359 .5890679 .8193944 .791674 1.318939 1.347322
| R2_Alt_in R2_Alt_~s Utheil_~t N
--------+------------------------------------------
Summary | .0259486 -.0898223 .4372138 230.0393
Out of sample evaluation: Specific individuals: idlto6
| RMSE_in RMSE_oos R2_in R2_oos RMSE_Al~n RMSE_Al~s
--------+-------------------------------------------------------
Summary |.5769317 .5356993 .7970362 .8951593 1.262116 2.374768
| R2_Alt_in R2_Alt_~s Utheil_~t N
--------+---------------------------------------
Summary | .0286659 -1.060298 .2255797 36
(3) 使用 xtoos_i
来评估预测性能,只对前 6 个个体进行评估,同时为这 6 个个体中的每一个绘制预测图。
. xtoos_i n w l.w k l.k ys l.ys, ous(40) rsmpl(0) ksmpl(40) evalopt(idlto6) hgraph
二元因变量 & 时间序列维度:
xtoos_bin_t
根据在样本内和样本外评估的接收者操作特征 (ROC) 统计量下的面积来评估预测性能。默认的估计方法是 xtlogit
,但允许选择不同的估计方法 (如 logit
、probit
、xtprobit
)。
xtoos_bin_t
允许选择不同的估计方法 (如 logit
、probit
、xtprobit
),也可以只在时间序列数据集中使用。xtoos_bin_t
允许选择估计结果为正的概率的方法,这取决于所使用的估计方法 (如 prob 等) 。
二元因变量 & 截面维度:
xtoos_bin_i
根据在训练样本和样本外评估的接收者运算特征 (AUROC) 统计量下的面积来评估预测性能。默认的估计方法是 xtlogit
,但允许选择不同的估计方法 (如 logit
、probit
、xtprobit
)。
xtoos_bin_i
具有与连续情况下 (xtoos_i
) 相同的选择排除样本 (样本外) 的选项。允许选择估计结果为正的概率的方法。这取决于所使用的估计方法 (如 prob 等)。xtoos_bin_i
还报告了样本内个体的 AUROC,并且还估计了 AUROC 的标准误。
这些程序可以帮助我们实现以下目标:
Note:产生如下推文列表的 Stata 命令为:
lianxh 预测, m
安装最新版lianxh
命令:
ssc install lianxh, replace
免费公开课
最新课程-直播课
专题 | 嘉宾 | 直播/回看视频 |
---|---|---|
⭐ 最新专题 | 文本分析、机器学习、效率专题、生存分析等 | |
研究设计 | 连玉君 | 我的特斯拉-实证研究设计,-幻灯片- |
面板模型 | 连玉君 | 动态面板模型,-幻灯片- |
面板模型 | 连玉君 | 直击面板数据模型 [免费公开课,2小时] |
⛳ 课程主页
⛳ 课程主页
关于我们
课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法
等
连享会小程序:扫一扫,看推文,看视频……
扫码加入连享会微信群,提问交流更方便
✏ 连享会-常见问题解答:
✨ https://gitee.com/lianxh/Course/wikis
New!
lianxh
和songbl
命令发布了:
随时搜索连享会推文、Stata 资源,安装命令如下:
. ssc install lianxh
使用详情参见帮助文件 (有惊喜):
. help lianxh