分位数回归及Stata实现

发布时间:2021-11-01 阅读 15684

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下载 - 推文合集

作者: 胡雨霄 (伦敦政治经济学院)


目录


0. 简介

最小二乘法 (OLS) 是在实证中最常用到的回归方法。标准的最小二乘线性回归,关注解释变量 x 对被解释变量 y 的 条件均值 E(Y|X) 的影响。OLS 回归关注的是因变量的条件均值函数。然而,实证中,研究者可能对 y|x 分布的其它重要分位数感兴趣。例如,劳动经济学家研究性别收入差距不平等问题时,若想探究高收入女性是否更难获得升职机会,就需要更关注女性群体收入的 90% 分位数水平。

与 OLS 不同,分位数回归估计的是解释变量 x 与被解释变量 y 的 分位数 之间线性关系。OLS 回归以残差平方最小化 iei2 作为目标,中位数回归则目标最小化离差绝对值 i|ei|。对于分位数回归,其目标为最小化非对称性绝对值残值。

以 q 表示分位数水平,分位数回归估计量 βq 最小化目标函数

相较于 OLS, 分位数回归的优点主要是:更全面描述解释变量 x 和被解释变量 y 的关系。实际上,在不同分位数上,因为解释变量 x 对被解释变量 y 可能影响不同,因此分位数回归系数可能和 OLS 的回归系数不同。

1. 分位数回归基本命令:qreg

分位数回归最基本的命令为 qreg,其语法格式如下:

qreg depvar [indepvars] [if] [in] [weight] [, qreg_options]

其中,

  • depvar 为被解释变量
  • [indepvars] 为解释变量
  • [, qreg_options] 具化分位数水平,例如 quantile (.25) 。如不特别设定,则 Stata 默认为中位数,即 quantile (.50)

2. 分位数回归绘图命令: grqreg

若想绘制不同分位数的回归系数,则可使用 grqreg 命令。该命令的基本语法如下:

grqreg [varlist] [weight], qmin(integer) qmax(integer)  ///
     qstep(integer) cons ci ols olsci

其中:

  • varlist 为选择的要被绘制的变量。若无特别设定,则默认为所有的估计系数。
  • qmin 为估计的分位数的最小值。若无特别设定,则默认为 50%。
  • qmax 为估计的分位数的最大值。若无特别设定,则默认为 95%。
  • qstep 为分位数间距。若无特别设定,则默认为 5%。
  • cons 为绘制截 (intercept) 的估计系数。若无特别设定,则默认为 off 。
  • ci 为绘制分位数回归的置信区间水平。若无特别设定,则默认为 off 。
  • ols 为绘制 OLS 的估计系数。若无特别设定,则默认为 off 。
  • olsci 为绘制 OLS 回归的置信区间水平。若无特别设定,则默认为 off 。
  • title 为命名参数。

举例


ssc install  grqreg, replace //下载外部命令

. qreg price mpg  headroom

Iteration  1:  WLS sum of weighted deviations =  70255.799

Iteration  1: sum of abs. weighted deviations =  70990.429
Iteration  2: sum of abs. weighted deviations =  67518.917
note:  alternate solutions exist
Iteration  3: sum of abs. weighted deviations =      64777
Iteration  4: sum of abs. weighted deviations =  64587.083
Iteration  5: sum of abs. weighted deviations =  64574.074
Iteration  6: sum of abs. weighted deviations =  64552.093
Iteration  7: sum of abs. weighted deviations =  64549.833

Median regression                           Number of obs =       74
  Raw sum of deviations  71102.5 (about 4934)
  Min sum of deviations 64549.83              Pseudo R2   =   0.0922

--------------------------------------------------------------------
   price |     Coef.   Std. Err.    t    P>|t|  [95% Conf. Interval]
---------+----------------------------------------------------------
     mpg | -127.3333   60.78234  -2.09   0.040 -248.5299   -6.136791
headroom | -130.6667   415.6721  -0.31   0.754 -959.4933      698.16
   _cons |  8272.333   2158.205   3.83   0.000  3968.995    12575.67
--------------------------------------------------------------------

. grqreg, ci title(Fig.1a Fig.1b)

3. 广义分位数回归 (generalized quantile regression) 命令: genqreg

genqreg 命令基于广义的分位数回归 ( Powell (2016) ),可以用来估计非条件的分位数回归。该命令的基本语法为

genqreg depvar indepvars [if] [in] [weight] , [quantile(#) ///
        instruments(varlist) proneness(varlist) technique(string)      

具体说明如下:

  • instruments(varlist) 列举外生变量。如果没有特别设定,则认为所有的变量都是外生的。
  • proneness(varlist) 允许加入一些额外的控制变量。
  • technique(string) 可以允许进行 Logit 或者 Probit 回归,technique("logit") 或者 technique("probit"),默认为 technique("linear")

例子

. ssc install genqreg, replace 
 
. genqreg price mpg headroom
Nelder-Mead optimization
initial:       f(p) = -.02029463
rescale:       f(p) = -.02029463
Iteration 0:   f(p) = -.02029463  
Iteration 1:   f(p) = -.00089208  
Iteration 2:   f(p) = -.00089208  
Iteration 3:   f(p) = -.00089208  
 
Generalized Quantile Regression (GQR)
     Observations:                74
 
--------------------------------------------------------------------
   price |     Coef.  Std. Err.    z    P>|z|   [95% Conf. Interval]
---------+----------------------------------------------------------
     mpg | -137.3333  64.03182  -2.14   0.032  -262.8334   -11.83326
headroom | -120.6667  327.3533  -0.37   0.712  -762.2673     520.934
   _cons |  8462.333  2063.802   4.10   0.000   4417.356    12507.31
--------------------------------------------------------------------
No excluded instruments and no proneness variables.
--> Estimation is equivalent to standard quantile regression.

4. 面板数据分位数回归命令:qregpd

该命令适用于面板数据,可参照 Powell (2015)。其具体命令为:

 qregpd depvar indepvars [if] [in] [weight] ,  
   [quantile(#) instruments(varlist)  
    identifier(varlist) fix(varlist)]

其中,

  • identifier(varlist) 类似于 xtset 中的 panelvar,是观测值单位的固定效应。
  • fix(varlist) 类似于 xtset 中的 timevar,是时间的固定效应。-

例子

.  webuse nlswork, clear 
(National Longitudinal Survey.  Young Women 14-26 years of age in 1968)

. qregpd ln_wage tenure union, id(idcode) fix(year)
Nelder-Mead optimization
initial:       f(p) = -298.32357
rescale:       f(p) = -1.2889814
Iteration 0:   f(p) = -1.2889814  
Iteration 1:   f(p) = -1.2889814  
Iteration 2:   f(p) = -1.2889814  
Iteration 3:   f(p) = -1.2889814  
Iteration 4:   f(p) = -1.2889814  
Iteration 5:   f(p) = -1.2889814  
Iteration 6:   f(p) = -1.2889814  
Iteration 7:   f(p) = -1.2889814  
Iteration 8:   f(p) = -1.2889814  
Iteration 9:   f(p) = -1.2889814  
Iteration 10:  f(p) =   -.657829  
Iteration 11:  f(p) =   -.657829  
Iteration 12:  f(p) = -.57754087  
Iteration 13:  f(p) = -.02146447  
Iteration 14:  f(p) = -.00537403  
Iteration 15:  f(p) = -.00537403  
Iteration 16:  f(p) = -.00171472  
Iteration 17:  f(p) = -.00164654  
Iteration 18:  f(p) = -.00164654  
Iteration 19:  f(p) = -.00164654  
Iteration 20:  f(p) = -.00164654  
Iteration 21:  f(p) = -.00164654  
Iteration 22:  f(p) = -.00164654  
Iteration 23:  f(p) = -.00164654  


Quantile Regression for Panel Data (QRPD)
     Number of obs:             19010
     Number of groups:           4134
     Min obs per group:             1
     Max obs per group:            12
-----------------------------------------------------------------
 ln_wage |    Coef.  Std. Err.    z   P>|z|  [95% Conf. Interval]
---------+-------------------------------------------------------
  tenure | .0207134  .0018152  11.41  0.000  .0171556    .0242712
   union | .0922837  .0122922   7.51  0.000  .0681915     .116376
-----------------------------------------------------------------
No excluded instruments - standard QRPD estimation.

4. 非条件分位数固定效应模型回归命令: xtrifreg

该命令可以用于估计非条件分位数回归固定效应模型。其基本命令如下

 xtrifreg depvar indepvars [if] [in] [weight], 
          fe i(varname)  quantile(#)

其中,

  • i(varname) 可用于表明固定效应变量。
. findit xtrifreg  //根据页面提示进行安装

. xtrifreg ln_wage tenure union, fe i(idcode)

------------------------------------------------------------------------
Model UQR
------------------------------------------------------------------------

Fixed-effects (within) regression         Number of obs      =  19010
Group variable: idcode                    Number of groups   =   4134

R-sq:  within  = 0.0588                   Obs per group: min =      1
       between = 0.1609                                  avg =    4.6
       overall = 0.1100                                  max =     12

                                          F(2,4133)          = 192.70
corr(u_i, Xb)  = 0.1377                   Prob > F           = 0.0000

                    (Std. Err. adjusted for 4,134 clusters in idcode)
----------------------------------------------------------------------
         |              Robust
  rif_50 |     Coef.   Std. Err.      t    P>|t|  [95% Conf. Interval]
---------+------------------------------------------------------------
  tenure |  .0276753   .0015301    18.09   0.000  .0246755    .0306752
   union |  .1120068   .0154023     7.27   0.000  .0818101    .1422035
   _cons |  1.596251   .0070095   227.73   0.000  1.582509    1.609994
---------+------------------------------------------------------------
 sigma_u | .45465567
 sigma_e | .37483862
     rho | .59534098   (fraction of variance due to u_i)
-------------------------------------------------------------------------
F test that all u_i=0: F(4133, 4133) = .                Prob > F =      .

相关课程

免费公开课

最新课程-直播课

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

课程主页

课程主页

关于我们

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

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

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

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

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