Stata连享会 主页 || 视频 || 推文 || 知乎 || Bilibili 站
温馨提示: 定期 清理浏览器缓存,可以获得最佳浏览体验。
New!
lianxh
命令发布了:
随时搜索推文、Stata 资源。安装:
. ssc install lianxh
详情参见帮助文件 (有惊喜):
. help lianxh
连享会新命令:cnssc
,ihelp
,rdbalance
,gitee
,installpkg
⛳ Stata 系列推文:
作者:张迪 (中南财经政法大学)
邮箱:zhangdi_16@126.com
目录
对于参数估计问题,学者在研究时多选择将似然函数作为目标函数,即通过使似然函数最大化实现模型拟合最优。但是模型拟合精度最高并不意味着所选择的模型为最优模型。例如,较多的训练集可以提高模型精度,但随之而来的问题是模型变得更加复杂,甚至导致过度拟合。此时,我们需要借助能够平衡模型精度和复杂度的准则来进行判断。
本文主要介绍在最优模型选择时常用的 AIC 准则和 BIC 准则,以及 Stata 相关命令 aic_model_selection
和 stepwise
。
Akaike Information Criterion (AIC) 也被称为赤池信息准则,由日本统计学家赤池弘次在 1974 年提出。它建立在熵的概念上,同时兼顾模型拟合精度和模型参数个数 (复杂度),是拟合精度和参数未知个数的加权函数。AIC 的判断标准是,一般情况下,AIC 越小,模型拟合度越好,并且模型也越精确。AIC 的使用条件是,随机误差项服从正太分布,即
其中,
由此可知 AIC 准则的重要优点:AIC 准则在合理控制了自由参数的同时,也使得似然函数尽可能大,模型的拟合度尽可能高。
Bayesian Information Criterion (BIC) 也被称贝叶斯信息准则,它与 AIC 准则相似,也是用于模型选择。当增加参数
AIC 存在的不足之处:当样本容量很大时,在 AIC 准则中拟合误差提供的信息就要受到样本容量的放大,而参数个数的惩罚因子却和样本容量没关系 (一直是 2)。因此当样本容量很大时,使用 AIC 准则选择的模型不收敛于真实模型,它通常比真实模型所含的未知参数个数要多。
BIC 准则也引入了与参数相关的惩罚项,但是 BIC 的惩罚项会更大一点。相对 AIC 而言,BIC 考虑了样本量,样本量较大时,就有效的解决了由于模型精度过高导致的复杂度也较高的问题。
BIC 的一般表达式为:
其中,
AIC 和 BIC 前半部分是惩罚项,当
假设
其中,
构造的目标函数 (似然函数) 为:
上式的对数似然函数为:
对
在正态分布的情况,
将参数估计量代入可得:
其中,
* 命令安装
ssc install aic_model_selection, replace
* 命令语法
aic_model_selection command_name varlist, [ bic ]
其中,
command_name
:所有逐步回归法适用的命令都可以使用,包括 regress
和 logistic
;bic
:用 BIC 准则代替 AIC 准则,默认使用 AIC 准则。实际上,aic_model_selection
命令对一个模型序列执行回归,即按照指定的顺序,每次添加一个 aic_model_selection
都会输出 AIC 值,我们只需选取 AIC 最小的模型即可。
. sysuse "auto.dta", clear
. aic_model_selection reg price mpg rep78 headroom trunk weight length turn displacement gear_ratio
AIC Model
1377.079 price mpg
1281.659 price mpg rep78
1283.058 price mpg rep78 headroom
1283.717 price mpg rep78 headroom trunk
1272.309 price mpg rep78 headroom trunk weight
1266.748 price mpg rep78 headroom trunk weight length
1266.328 price mpg rep78 headroom trunk weight length turn
1266.429 price mpg rep78 headroom trunk weight length turn displacement
1265.682 price mpg rep78 headroom trunk weight length turn displacement gear_ratio
在本例中,对影响 price 的 9 个变量依次添加,并进行 9 次 OLS 回归,每次回归所计算的 AIC 值在最前方显示。选择 AIC 最小的模型,即最后一组模型 AIC=1265.682。如此选择存在一个问题,所选择的变量较多,模型较为复杂。换用 BIC 准则查看是否发生了变化。
. aic_model_selection reg price mpg rep78 headroom trunk weight length turn displacement gear_ratio, bic
BIC Model
1381.687 price mpg
1288.361 price mpg rep78
1291.995 price mpg rep78 headroom
1294.887 price mpg rep78 headroom trunk
1285.713 price mpg rep78 headroom trunk weight
1282.387 price mpg rep78 headroom trunk weight length
1284.201 price mpg rep78 headroom trunk weight length turn
1286.535 price mpg rep78 headroom trunk weight length turn displacement
1288.023 price mpg rep78 headroom trunk weight length turn displacement gear_ratio
在使用 BIC 准则之后,结果发生了变化。选择 BIC 最小的模型,即第五组模型 BIC=1282.387。相较于 AIC 准则,BIC 准则所选模型的变量更少,复杂度更低。
stepwise
是系统自带的命令,无需安装。
* 命令语法
stepwise [, options] : command
options
选项如下:
pr(#)
:从模型中移除的显著性水平;pe(#)
:从模型中添加的显著性水平;forward
:向前逐步选择:
pr()
、pe()
、以及 forward
向前逐步选择;pr()
和 pe()
向后逐步选择;pr()
向后选择;pe()
向前选择。hierarchical
:分层选择;lockterml
:保留第一项,使其包含在模型中,不受选择标准的约束;lr
:用似然比检验代替 Wald 检验。默认是 Wald 检验,即检验是基于估计量的估计方差-协方差矩阵;display_options
:控制显示结果的列、列格式和行宽。必须至少指定 pr(#)
和 pe(#)
中的一个,例如:
stepwise, pr(0.05): command
stepwise, pe(0.05): command
stepwise, pr(0.05) pe(0.05): command
对上述关于 forward
选项进一步展开,例如:
options | Description |
---|---|
pr(#) |
向后选择 |
pr(#) hierarchical |
向后层次选择 |
pr(#) pe(#) |
逐步向后 |
pe(#) |
向前选择 |
pe(#) hierarchical |
向前层次选择 |
pr(#) pe(#) forward |
逐步向前 |
command
是指 stepwise
支持的命令,包括:logit
、 poisson
、probit
、qreg
、regress
等。命令语法如下:
command_name [depvar] term [term ...] [if] [in] [weight] [, command_options]
term
:varname
或者 (varlist)
。其中 varlist
用括号框起来表示为一组变量;depvar
:当 command_name
为 stcox
、stcrreg
、stintreg
或 streg
时,depvar
不存在,其他情况下,假定 depvar
存在。特别地,对于 intreg
,depvar
实际上是两个因变量名 (depvar1
和 depvar2
)。. sysuse "auto.dta", clear
. //向前选择法
. stepwise, pe(.2) : reg price mpg rep78 headroom trunk weight length turn displacement gear_ratio
Wald test, begin with empty model:
p = 0.0000 < 0.2000, adding displacement
p = 0.0115 < 0.2000, adding rep78
p = 0.0573 < 0.2000, adding headroom
p = 0.1522 < 0.2000, adding weight
p = 0.0361 < 0.2000, adding turn
p = 0.1548 < 0.2000, adding length
p = 0.1550 < 0.2000, adding gear_ratio
p = 0.1415 < 0.2000, adding mpg
Source | SS df MS Number of obs = 69
-------------+---------------------------------- F(8, 60) = 7.84
Model | 294825617 8 36853202.1 Prob > F = 0.0000
Residual | 281971342 60 4699522.36 R-squared = 0.5111
-------------+---------------------------------- Adj R-squared = 0.4460
Total | 576796959 68 8482308.22 Root MSE = 2167.8
------------------------------------------------------------------------------
price | Coefficient Std. err. t P>|t| [95% conf. interval]
-------------+----------------------------------------------------------------
displacement | 16.982 8.939 1.90 0.062 -0.899 34.864
rep78 | 742.558 317.252 2.34 0.023 107.958 1377.157
headroom | -594.562 367.208 -1.62 0.111 -1329.088 139.963
weight | 3.935 1.518 2.59 0.012 0.899 6.972
turn | -208.551 130.892 -1.59 0.116 -470.374 53.273
length | -74.935 41.514 -1.81 0.076 -157.976 8.106
gear_ratio | 1645.675 1056.334 1.56 0.125 -467.307 3758.657
mpg | -117.295 78.724 -1.49 0.141 -274.767 40.178
_cons | 10076.632 7125.903 1.41 0.163 -4177.297 24330.560
------------------------------------------------------------------------------
. //向后选择法
. stepwise, pr(.2) : reg price mpg rep78 headroom trunk weight length turn displacement gear_ratio
Wald test, begin with full model:
p = 0.5013 >= 0.2000, removing trunk
Source | SS df MS Number of obs = 69
-------------+---------------------------------- F(8, 60) = 7.84
Model | 294825617 8 36853202.1 Prob > F = 0.0000
Residual | 281971342 60 4699522.36 R-squared = 0.5111
-------------+---------------------------------- Adj R-squared = 0.4460
Total | 576796959 68 8482308.22 Root MSE = 2167.8
------------------------------------------------------------------------------
price | Coefficient Std. err. t P>|t| [95% conf. interval]
-------------+----------------------------------------------------------------
mpg | -117.295 78.724 -1.49 0.141 -274.767 40.178
rep78 | 742.558 317.252 2.34 0.023 107.958 1377.157
headroom | -594.562 367.208 -1.62 0.111 -1329.088 139.963
gear_ratio | 1645.675 1056.334 1.56 0.125 -467.307 3758.657
weight | 3.935 1.518 2.59 0.012 0.899 6.972
length | -74.935 41.514 -1.81 0.076 -157.976 8.106
turn | -208.551 130.892 -1.59 0.116 -470.374 53.273
displacement | 16.982 8.939 1.90 0.062 -0.899 34.864
_cons | 10076.632 7125.903 1.41 0.163 -4177.297 24330.560
------------------------------------------------------------------------------
根据前面的示例,我们可以看出 aic_model_selection
命令和 stepwise
命令在使用既有共性,也有其特殊的使用场合。其相同点是,都是变量筛选的方法。不同点是:
aic_model_selection
选择 AIC 值或 BIC 值最小的模型。stepwise
根据指定的显著性水平进行筛选;aic_model_selection
只有向前选择。stepwise
有向前选择和向后选择两种选择方法。可针对不同的样本和模型进行选择。
Note:产生如下推文列表的 Stata 命令为:
lianxh aic bic 筛选, 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