Stata:原始聚类自助法(wild cluster bootstrap)-boottest

发布时间:2021-12-21 阅读 3532

Stata连享会   主页 || 视频 || 推文 || 知乎 || Bilibili 站

温馨提示: 定期 清理浏览器缓存,可以获得最佳浏览体验。

New! lianxh 命令发布了:
随时搜索推文、Stata 资源。安装:
. ssc install lianxh
详情参见帮助文件 (有惊喜):
. help lianxh
连享会新命令:cnssc, ihelp, rdbalance, gitee, installpkg

课程详情 https://gitee.com/lianxh/Course

课程主页 https://gitee.com/lianxh/Course

⛳ Stata 系列推文:

PDF下载 - 推文合集

作者:王乔 (中南财经政法大学)
邮箱zuelerqiao@foxmail.com

编者按:本文主要摘译自下文,特此致谢!
Source:Roodman D, Nielsen M Ø, MacKinnon J G, et al. Fast and wild: Bootstrap inference in Stata using boottest[J]. The Stata Journal, 2019, 19(1): 4-60. -PDF-


目录


1. 引言

先说说编写 boottest 命令的因缘。几年前,Roodman 教授读了 Kevin Croke 的一篇构思巧妙的论文,从中学习到了 wild (cluster) bootstrap 法。Roodman 发现,Stata 在应用上述方法时,运行速度具有很大的改进空间。于是,Roodman 编写了命令 boottest。相较于标准的应用程序,比如 Kevin 的程序,该命令的处理速度非常快。

那么什么是 wild cluster bootstrap?

编者注: 对于「wild bootstrap」的中文翻译,目前并不定论。陈强老师的《高级计量经济学及Stata应用》将其译为“野自助法”,应该是取了「wild」一词的字面意思;伍德里奇教授的《横截面与面板数据的计量经济分析》中译为“原始自助法”;知网上部分文献也译为“原始自助法”。由于「wild bootstrap」考虑到了原始样本中观测到的残差方差和变量间的关系,所以我认为“原始自助法”较为合理。

wild cluster bootstrap 最初是为异方差形式未知的回归模型开发的。在过去三十年里,它已经扩展到工具变量和最大似然估计模型,以及误差项 (可能是多种方式) 聚集在一起的情况。与 bootstrap 方法类似,当传统推理方法因大样本假设不成立时,wild bootstrap 法就特别有用。例如存在较少的集群,或者较少的实验组、以及弱工具变量。

在社会科学研究中,假设回归模型中的误差项在集群 (cluster) 中相关是很常见的。这也是为什么许多 Stata 估计命令提供了一个聚类选项,来实现聚类-稳健方差矩阵估计。该方法对集群内相关性和未知形式的异方差都是稳健的。

当聚类的样本数量很大、聚类相当均匀 (在大小和方差矩阵方面相似)、以及对照组聚类 (treated clusters) 的数量不太少时,基于聚类标准误的推断具有较好的效果。但当这些条件不成立时,推断可能是不太准确的。

一种改进的方法是使用 bootstrap。其主要思想是通过生成大量的 bootstrap 样本,来模拟实际样本的分布。通常来说,bootstrap 数据生成过程 (DGP) 越接近真的 DGP,估计就越可靠。对于许多回归模型,wild bootstrap 经常能很好地匹配真实的 DGP。关于 wild bootstrap 的详细介绍,请参考原文。

在此基础上,本文将简要介绍 wild cluster bootstrap 方法的 Stata 命令 boottest

2. boottest 简介

boottest 提供了几种 bootstrap 的算法 (用于生成模拟数据集的算法),以及在数据集上运行的几种测试。其中 bootstrap 的算法包含以下三种:

  • wild bootstrap (Wu 1986):在 (有约束的) OLS 估计后可用;
  • WRE (Wild restricted efficient bootstrap):将 wild bootstrap 扩展到工具变量估计,包括 2SLS、LIML、Fuller LIML 和 k 类估计;
  • score bootstrap:对一般极值估计 (包括 2SLS、LIML、ML 和 GMM) 的一种 wild bootstrap 的适应性处理方法。score bootstrap 适用于残差的广义类比。

boottest 使用了前两种方法对零假设的 Wald 检验的经验分布进行 bootstrap。对于工具变量估计,WRE 方法也可以用于对 Anderson-Rubin (1949) 检验做 bootstrap,这本身就是基于辅助 OLS 回归的 Wald 检验。score bootstrap 则可以视为对 Rao score/LM 检验的 bootstap。

boottest 命令的主要功能

  • OLS 回归估计后的检验;
  • Davidson 和 MacKinnon (2010) 对两阶段最小二乘、有限信息最大似然和其他工具变量 (IV) 方法进行的 wild restricted efficient bootstrap 后的检验,如 ivregressivreg2
  • 通过 score bootstrap 使用基于 ML 的命令如 probitlogitglmsemgsem 和用户编写的 cmp 进行最大似然 (ML) 估计后的推断和检验;
  • 多路径聚类 (multi-way clustering),即使估计命令不支持它;
  • 单向固定效应模型,用带有 fe 选项的 aregreghdfextregxtivreg,或 xtivreg2 去估计;
  • 反推一维假设的检验,以得出置信集;
  • 绘制相应的置信函数。

boottest 命令中的五种权重分布

  • 默认的 Rademacher 权重以相等的概率取 +/-1。在 wild bootstrap 方法中,这意味着对于每一个复制值和每一个集群 (或在非集群估计中的每一个观察值) 来说,合成因变量是 xβ^±e。其中 xβ^ 和 e 分别是基础回归的拟合值和残差。由于 Rademacher 权重的均值为 0,方差为 1,因此用该权重与 e 相乘后不会改变基础回归中残差的方差;
  • Mammen 权重使得三阶矩等于 1,从理论上改进了 Rademacher 权重,因此保持了偏态;
  • Rademacher 和 Mammen 分布的一个缺点是它们只取两个点,即 +1 或 1。Cameron 等 (2008) 在本案例中所运行的数值量已经超过了 32 个复制值,这会产生虚假的精度。Webb (2014) 的权重采用了均匀的 6 点分布,极大地缓解了这个问题;
  • 第四种权重类型是正态分布;
  • 第五种权重类型是 gamma 分布。这和 Mammen 分布很相似,它在理论上改进了正态分布,三阶矩也等于 1。

尽管非对称的 Mammen 分布和 gamma 分布看起来具有优越性,但对称分布如 Rademacher 分布和 Webb 分布在蒙特卡罗模拟中会有更好的表现,即在某种意义上来说,该方法可以更精准的检验。

3. 命令介绍

*命令安装
cnssc install boottest, replace
*命令语法
boottest [indeplist] [, options]

where indeplist is one of
    jointlist
    {jointlist} [{jointlist} ...]

jointlist is one of
    test
    (test) [(test) ...]

and test is
    exp = exp | coeflist

其中,indeplist 是一系列独立检验的假设。如果有多个,那么每个都必须用花括号括起来,即 {jointlist}。每个独立的假设依次由一个或多个联合检验的约束条件组成,约束条件即 (test)。如果有不止一个,那么每一个都必须用括号括起来。最后,每个约束表达式必须符合约束定义的语法,即 Exp=exp | coeflist

option 选项如下:

  • Weighttype(rademacher | mammen | webb | normal) | gamma)bootstrap 指定权重类型,默认权重类型为 rademacher
  • boottype(wild | score):指定 bootstrap 的类型,ML 估计后 score 是默认且唯一的选项;
  • statistic(t | c):指定 bootstrap 的统计类型,默认为 t
  • reps(#):指定基于 bootstrap 检验的复制值的数量,默认值是 999,Rao 或 Wald 检验则设置为 0;
  • nonull:是在进行 bootstrap 之前防止 null 选项的施加;
  • madjust(bonferroni | sidak):指定多个假设检验的调整;
  • level(#):指定置信区间的置信级别;
  • svmat[(t | numer)]:将 bootstrap 的准 t/z 分布或其分子保存到返回值 r(dist)
  • svv:请求在返回值 r(v) 中保存 wild 权重矩阵;
  • small:有限样本修正,重新对估计过程进行设置;
  • robust:只对异方差性进行稳健性检验,重新对估计过程进行设置;
  • cluster(varlist):请求对 (多路径) 聚类进行检验,重新对估计过程进行设置;
  • bootcluster(varname):设置进行 bootstrap 的集群变量,默认是所有 cluster() 变量;
  • ar:要求进行 Anderson - Rubin 检验;
  • seed(#):初始化随机数种子为 #
  • matsizegb(#):设置 wild 权重矩阵的最大值,单位为千兆字节;
  • quietly:带有 null 选项的估计值不显示出来,是基于 ML 估计;
  • cmdline(string):提供估算命令 line,只在进行自定义 ML 估计之后才需要;

其他的 option 选项:

  • gridmin(# [#]):对设置了搜索范围的置信集设置最低边界 end(s);
  • gridmax(# [#]):类似的上限 ends;
  • gridpoints(# [#]):设置等间距点的数目来计算拒绝置信度;
  • graphopt(string):要传递给图形命令的格式化选项;
  • noci:不允许从 inverted bootstrap 检验导出置信集;
  • ptolerance(#):设置置信集边界识别精度 (默认 1e - 6 );
  • graphname(name[, replace]):图的名称,对于多个独立假设,使用名称作为存根;
  • nograph:允许推导置信集,但不绘制置信函数;
  • ptype(symmetric | equaltail | lower | upper):对于一元假设,设置 p 值类型;

4. 案例应用

在这里,本文以数据库nlsw88.dta为例进行演示。

webuse nlsw88 

首先,我们对变量 wagetenurettl_expcollgrad 进行回归,并在行业层面 industry 进行聚类。

. webuse nlsw88.dta, clear
. regress wage tenure ttl_exp collgrad, cluster(industry)

Linear regression                               Number of obs     =      2,217
                                                F(3, 11)          =      56.79
                                                Prob > F          =     0.0000
                                                R-squared         =     0.1255
                                                Root MSE          =      5.402
                              (Std. err. adjusted for 12 clusters in industry)
------------------------------------------------------------------------------
             |               Robust
        wage | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
      tenure |      0.030      0.028     1.08   0.304       -0.032       0.093
     ttl_exp |      0.273      0.047     5.81   0.000        0.170       0.376
    collgrad |      3.255      0.397     8.21   0.000        2.382       4.129
       _cons |      3.420      0.285    11.98   0.000        2.792       4.048
------------------------------------------------------------------------------

利用 boottest 进行检验,并存储为模拟分布。

. boottest tenure, svmat seed(123)

Wild bootstrap-t, null imposed, 999 replications, Wald test, bootstrap clustering by industry, Rademacher weights:
  tenure
                           t(11) =     1.0772
                        Prob>|t| =     0.2943
95% confidence set for null hypothesis expression: [-.04069, .1864]

上述 boottest 检验所设定的前提条件为联合零假设的 wild bootstrap-t 检验, 施加了选项 null,使用权重为 Rademacher 权重。根据上述结果可知,由于遵循 Stata 约定,waldtest 使用 t(G−1) 分布,这里有 G = 12 个集群。变量 tenure 的 t(11) 统计值为 1.077。并利用 boottest 进行反向推断,得出 tenure 的 95% 置信区间为 [-0.041, 0.186]。

下面,我们请求将 bootstrap 的准 t 分布或其分子保存为返回值 r(dist),并绘制 t-statistic 统计柱状图。

. mat dist = r(dist)
. svmat dist 
. histogram dist1, xline(`r(t)') 

接着,对变量 ttl_exp 设置一个约束条件。具体条件为令 ttl_exp 的系数为固定值 0.02,并将该条件命名为 1。然后对变量 wagetenurettl_expcollgrad 进行约束线性回归,并在行业层面 industry 进行聚类。

. constraint 1 ttl_exp = .2 
. cnsreg wage tenure ttl_exp collgrad, constr(1) cluster(industry)

Constrained linear regression                           Number of obs =  2,217
                                                        F(2, 2214)    =  40.62
                                                        Prob > F      = 0.0000
                                                        Root MSE      = 5.4077
 ( 1)  ttl_exp = .2
                              (Std. err. adjusted for 12 clusters in industry)
------------------------------------------------------------------------------
             |               Robust
        wage | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
      tenure |      0.065      0.031     2.13   0.034        0.005       0.125
     ttl_exp |      0.200  (constrained)
    collgrad |      3.304      0.386     8.56   0.000        2.546       4.061
       _cons |      4.118      0.334    12.33   0.000        3.463       4.773
------------------------------------------------------------------------------

根据上述结果可知,在有约束的 OLS 回归中,变量 ttl_exp 的系数变成了固定值。然后,我们用 boottest 命令对变量 tenure 进行检验。默认情况下,boottest 使用 Rademacher 分布生成 999 个 wild cluster bootstrap 样本,并设置 null 假设。

. boottest tenure , seed(123)

Wild bootstrap-t, null imposed, 999 replications, Wald test, bootstrap clustering by industry, Rademacher weights:
  tenure
                           t(11) =     2.1249
                        Prob>|t| =     0.0010
95% confidence set for null hypothesis expression: [.008769, .186]

可以看到,变量 tenure 的 t(11) 统计值为 2.125,利用 boottest 进行反向推断,得出 tenure 的 95% 置信区间为 [0.009, 0.186]。

下面我们来看一下工具变量估计中, boottest 的作用。此处,我们以两阶段最小二乘法为例,变量 uniontenure 的工具变量,同样是在行业层面 industry 进行聚类。

. ivregress 2sls wage ttl_exp collgrad (tenure = union), cluster(industry)

Instrumental variables 2SLS regression            Number of obs   =      1,855
                                                  Wald chi2(3)    =     136.97
                                                  Prob > chi2     =     0.0000
                                                  R-squared       =          .
                                                  Root MSE        =     4.7943
                              (Std. err. adjusted for 12 clusters in industry)
------------------------------------------------------------------------------
             |               Robust
        wage | Coefficient  std. err.      z    P>|z|     [95% conf. interval]
-------------+----------------------------------------------------------------
      tenure |      0.736      0.283     2.60   0.009        0.182       1.290
     ttl_exp |     -0.232      0.209    -1.11   0.266       -0.641       0.177
    collgrad |      2.993      0.520     5.76   0.000        1.974       4.012
       _cons |      4.992      0.833     5.99   0.000        3.360       6.625
------------------------------------------------------------------------------
Instrumented: tenure
 Instruments: ttl_exp collgrad union

利用 boottest 进行检验,设置条件为 Wald 检验,使用 Rademacher 权重,应用于零假设,默认 999 复制值。其中 p 值类型使用的是等尾检验。

. boottest tenure, ptype(equaltail) seed(123)

Wild bootstrap-t, null imposed, 999 replications, Wald test, bootstrap clustering by industry, Rademacher weights:
  tenure
                               z =     2.6040
    2 * min(Prob>|z|, Prob<-|z|) =     0.0340
95% confidence set for null hypothesis expression: [.1647, 2.36]

根据结果可知, 对于变量 tenure 来说,等尾检验的 z 统计量为 2.604,boottest 推导的 95% 置信区间为 [0.165, 2.360]。

由于上面我们在进行 boottest 检验时,设置的条件是使用 bootstrap-t ,该条件也可以改成 bootstrap-c,只需要对语句稍作变换即可。

. boottest tenure, ptype(equaltail) seed(123) stat(c)

Wild bootstrap-c, null imposed, 999 replications, Wald test, bootstrap clustering by industry, Rademacher weights:
  tenure
                               z =     1.6728
    2 * min(Prob>|z|, Prob<-|z|) =     0.1041
95% confidence set for null hypothesis expression: [., .]
(A confidence interval could not be bounded. Try widening the search range with the gridmin() and gridmax() options.)
Note: denominator for z statistic computed from the bootstrap replications of the numerator.

上面的语句就改变了检验类型,从 bootstrap-t 变成了 bootstrap-c。但是在该种情况下,Stata 并没有计算出置信区间,并提示一个置信区间不能有界。尝试使用 gridmin()gridmax() 选项扩大搜索范围,即 [-2, 2]。

. boottest tenure, ptype(equaltail) seed(123) stat(c) gridmin(-2) gridmax(2)

Wild bootstrap-c, null imposed, 999 replications, Wald test, bootstrap clustering by industry, Rademacher weights:
  tenure
                               z =     1.6728
    2 * min(Prob>|z|, Prob<-|z|) =     0.1041
95% confidence set for null hypothesis expression: [., .]
(A confidence interval could not be bounded. Try widening the search range with the gridmin() and gridmax() options.)
Note: denominator for z statistic computed from the bootstrap replications of the numerator.

在扩大了坐标轴范围之后,我们还是没有找到置信区间范围,但是可以灵活使用这种方法不断放大或者缩小置信区间,以求得置信区间范围。在 boottest 中,我们通常进行的检验是 Wald 检验、Anderson-Rubin 检验以及 Rao 分数/拉格朗日乘子检验。以 Anderson-Rubin 检验为例,命令语句为 boottest, ar

在以上的分析中,我们对基本回归和工具变量回归分别进行了 boottest 检验,但是前提是它们都存在一个有待独立检验的假设条件。但是根据 boottest 命令语法的介绍可知,该命令也适用于多个有待独立检验的假设条件。下面我们就以存在两个假设条件为例,进行 boottest 检验。

. boottest tenure ttl_exp, seed(123)

Wild bootstrap-t, null imposed, 999 replications, Wald test, bootstrap clustering by industry, Rademacher weights:
  tenure ttl_exp
                   chi2(2) =   135.5836
               Prob > chi2 =     0.0040

在该语句中,我们使用的假设条件为:

由于原假设现在是二维的,boottest 使用 Stata 的双向轮廓命令来描述置信面。但是它并没有从数字上描述可信集的边界,可信集的边界是由这个曲面与 P = 0.05 定义的平面相交而产生的。

根据上述结果可知,卡方值为 135.5836,且此卡方值的 P 值为 0.004。根据用等高线绘制 p 值曲面图可知,颜色相同的区域,其相应的 P 值相等。当然我们也可以联合更为复杂的假设条件,此时我们需要用括号把相应的条件括起来。

. boottest (tenure) (ttl_exp = 2), seed(123)

Wild bootstrap-t, null imposed, 999 replications, Wald test, bootstrap clustering by industry, Rademacher weights:
  tenure
  ttl_exp = 2
                   chi2(2) =  3935.4395
               Prob > chi2 =     0.0010

基于此,我们用含有两个内生变量和两个工具变量的 LIML 回归进行分析。

. ivregress liml wage (collgrad tenure = ttl_exp union), cluster(industry) 

Instrumental variables LIML regression            Number of obs   =      1,855
                                                  Wald chi2(2)    =      66.00
                                                  Prob > chi2     =     0.0000
                                                  R-squared       =          .
                                                  Root MSE        =     4.5444
                              (Std. err. adjusted for 12 clusters in industry)
------------------------------------------------------------------------------
             |               Robust
        wage | Coefficient  std. err.      z    P>|z|     [95% conf. interval]
-------------+----------------------------------------------------------------
    collgrad |      8.685      8.839     0.98   0.326       -8.638      26.008
      tenure |      0.339      0.094     3.60   0.000        0.155       0.524
       _cons |      3.208      1.231     2.61   0.009        0.795       5.620
------------------------------------------------------------------------------
Instrumented: collgrad tenure
 Instruments: ttl_exp union

. boottest, ar seed(123)

Wild bootstrap-t, null imposed, 999 replications, Anderson-Rubin Wald test, bootstrap clustering by industry, Rademacher weights:
  collgrad tenure
                   chi2(2) =   477.2687
               Prob > chi2 =     0.0000

5. 相关推文

Note:产生如下推文列表的 Stata 命令为:
lianxh bootstrap 聚类, m
安装最新版 lianxh 命令:
ssc install lianxh, replace

相关课程

免费公开课

最新课程-直播课

专题 嘉宾 直播/回看视频
最新专题 文本分析、机器学习、效率专题、生存分析等
研究设计 连玉君 我的特斯拉-实证研究设计-幻灯片-
面板模型 连玉君 动态面板模型-幻灯片-
面板模型 连玉君 直击面板数据模型 [免费公开课,2小时]
  • Note: 部分课程的资料,PPT 等可以前往 连享会-直播课 主页查看,下载。

课程主页

课程主页

关于我们

  • Stata连享会 由中山大学连玉君老师团队创办,定期分享实证分析经验。
  • 连享会-主页知乎专栏,700+ 推文,实证分析不再抓狂。直播间 有很多视频课程,可以随时观看。
  • 公众号关键词搜索/回复 功能已经上线。大家可以在公众号左下角点击键盘图标,输入简要关键词,以便快速呈现历史推文,获取工具软件和数据下载。常见关键词:课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法

连享会小程序:扫一扫,看推文,看视频……

扫码加入连享会微信群,提问交流更方便

✏ 连享会-常见问题解答:
https://gitee.com/lianxh/Course/wikis

New! lianxhsongbl 命令发布了:
随时搜索连享会推文、Stata 资源,安装命令如下:
. ssc install lianxh
使用详情参见帮助文件 (有惊喜):
. help lianxh