Stata 回归表格输出之 esttab 详解
1. 简介
esttab
命令是由瑞士波恩大学社会学研究所(University of Bern, Institute of Sociology)的 Ben Jann 教授编写的 Stata
用户外部命令,主要用于生成满足用户需求的回归表格,这类命令已经成为量化实证分析中的基础性技能,兼具效率、规范与美观。本文是对该命令的详细介绍。
2. 安装
ssc install estout, replace //estout 是一个通用程序,是 esttab 命令实现其功能的引擎。
3. 使用 esttab 整理回归结果
3.1 基本语法及使用
esttab [ namelist ] [ using filename ] [, options estout_options ]
该命令的使用逻辑如下:
- 首先,将目标回归模型的估计结果进行存储;
- 其次,使用
esttab
命令将存储好的估计值或统计量编辑在一个回归表格中;
通过下面的示例进一步掌握:
** 保存模型结果之方法一:使用 eststo 命令存储回归模型。通过在回归模型前加上 eststo 前缀,esttab 命令能够自动找到储存的信息,并自动生成对应每个回归模型的变量。
sysuse auto, clear
eststo: regress price weight mpg //_est_est1
eststo: regress price weight mpg foreign //_est_est2
esttab // 为节省篇幅,提高阅读效率,除非必要,结果不再一一展示,下文不再说明
eststo clear //删除保存的模型结果,_est_est1 和 _est_est2 从变量窗口中被消失
** 保存模型结果之方法二:使用 Stata 官方自带的命令进行结果存储,即 estimates store(或缩写形式:est store)
sysuse auto, clear
regress price weight mpg
estimates store model1
regress price weight mpg foreign
estimates store model2
esttab model1 model2
*注:此时若使用 eststo clear,保存的结果无法删除
3.2 标准误、P值及概要统计量
esttab
的输出结果默认展示带有 t 统计量的的点估计,并在表格注脚处进行标记,一并默认输出的还有样本量。实证研究中,我们往往需要输出估计量的标准误以及其他特定的统计量,这时需要在esttab
后进行更为细致的设定。例如,我们需要报告标准误而非 t 统计量,并且需要汇报调整后的判决系数(adj. R-sq)。通过下面的例子,我们可以发现 esttab
命令输出回归结果的设定思路。
sysuse auto, clear
eststo: regress price weight mpg
eststo: regress price weight mpg foreign
esttab, se ar2 // 跑完回归模型,可以通过 ereturn list 获得该模型的的 scalars、macros、matrices以及functions
*上面的命令等价于
esttab, se scalars(r2_a) //r2_a 是存储在模型估计结果的 scalars中的一个标量参数
*也可以输出 p 值,置信区间(ci)或是任何存储在估计结果中的参数统计量,它们保存在 scalars 中。例如,我们需要输出p值、F统计量以及模型自由度。
esttab, p scalars(F df_m df_r)
eststo clear //注意,如果把该命令误写为 est sto clear,它表示将上一个回归模型命名为“clear”并储存起来,使用时要理解原理,不断试错才能避免出错
3.3 标准化系数
- 为什么要进行标准化?
同一回归模型中,即便两个自变量的单位一致(例如教育年限和工作经历都以年为计数单位),其回归系数也无法直接进行比较。事实上,研究中涉及的自变量往往具有不同的测度单位,回归系数也会受到影响。
多元回归模型经常涉及各自变量对因变量的相对作用大小进行比较,进而从多个因素中找出首要和次要因素,这时便可以采用标准化的回归系数(standardized coefficients)。所谓标准化回归系数,是将自变量转为一个无量纲的变量,使得不同标准化回归系数之间具有可比性。
- 随机变量的标准化处理方式
标准化回归系数是通过标准化回归模型估计得到的,需要将模型中所有的自变量和因变量都进行标准化。
将随机变量 X 进行标准化的表达式如下:
其中,E(X) 和 \\sigma(X) 分别表示期望和标准差。z 也叫标准化随机变量或标准分,是一个无量纲的纯数。变量的标准化形式表示:以标准差为单位,测量观测值与均值之间的距离。经过标准化处理后的新变量(z),其均值为0,方差为1。此外,我们还应该知道,标准化处理其实也是一个对中(centering)和测度转换(rescaling)的过程,经过标准化转换,不同变量的位置和尺度得以一致。
- 估计标准化模型
应用中,我们可以直接将标准化后的变量进行回归,但理论上还是要尽量知其所以然,以便更好理解对标准化模型的估计,很多细节上的问题也自然迎刃而解。假设标准化处理之前的原始回归模型设定如下:
y_i=\\beta_0+\\beta_1x_{i1}+\\beta_2x_{i2}+…+\\beta_px_{ip}+\\epsilon_i \\tag{1}
对模型两侧的变量进行标准化转换,即:y_i^*=\\frac{y_i-\\bar{y}}{S_y} ,x_{ik}^*=\\frac{x_{ik}-\\bar{x_k}}{S_{xk}}
对(1)式进行对中处理并除以因变量的标准差 S_y ,模型(1)可做如下变换:
\\begin{align}
\\frac{yi-\\bar{y}}{S_y}=& \\frac{\\beta_1}{S_y}(x{i1}-\\bar{x1})+\\frac{\\beta_2}{S_y}(x{i2}-\\bar{x2})+…+\\frac{\\beta_p}{S_y}(x{ip}-\\bar{x_p})+\\frac{\\epsilon_i}{S_y} \\\\
y_i^*=& \\beta1\\frac{S{x1}}{Sy}(\\frac{x{i1}-\\bar{x1}}{S{x1}})+\\beta2\\frac{S{x2}}{Sy}(\\frac{x{i2}-\\bar{x2}}{S{x2}})+…+\\beta_p\\frac{S{xp}}{Sy}(\\frac{x{ip}-\\bar{xp}}{S{xp}})+\\frac{\\epsilon_i-\\bar\\epsilon}{S_y} \\\\
y_i^*= & \\beta_1^*x{i1}^*+\\beta_2^*x{i2}^*+…+\\beta_p^*x_{ip}^*+\\epsilon_i^* \\tag{2}
\\end{align} \\begin{align}
\\end{align}此时,通过比较模型(1)和模型(2),可以得到标准化与非标准化回归系数之间的关系如下:
\\beta_k^*=\\beta_k\\times \\frac{S{xk}}{S_y} \\tag{3}
显然,利用(3)式,我们也可以通过计算样本中变量 y 与 x_k 的标准差,在获得非标准化系数后求得标准化系数。
- 两种回归系数的比较
标准化回归系数处于 -1, 1 的区间内,并且可以进行标准化尺度下的变量间系数比较。
相比而言,非标准化回归系数虽然不可比,但它将数据本身的信息提供给我们,估计系数具有明确的现实意义。
由此可见,并非进行标准化处理就显得“高大上”,标准化处理与否需要根据所研究的具体问题进行选择。但是,不论选择哪一种,尤其要关注对两种回归系数的解释。同是边际效应,标准化回归系数表示自变量每增加1个标准差,因变量平均增加 \\beta_k^*个标准差,具体的数学表达不再展示(提示:对(3)式进行变换)。
- 使用
esttab
汇报标准化回归系数
sysuse auto, clear
eststo: regress price weight mpg
eststo: regress price weight mpg foreign
esttab
/*--------------------------------------------
(1) (2)
price price
--------------------------------------------
weight 1.747** 3.465***
(2.72) (5.49)
mpg -49.51 21.85
(-0.57) (0.29)
foreign 3673.1***
(5.37)
_cons 1946.1 -5853.7
(0.54) (-1.73)
--------------------------------------------
N 74 74
--------------------------------------------
t statistics in parentheses
* p<0.05, ** p<0.01, *** p<0.001 */
esttab,beta //也可以汇报标准误,esttab,beta se
/*
--------------------------------------------
(1) (2)
price price
--------------------------------------------
weight 0.460** 0.913***
(2.72) (5.49)
mpg -0.097 0.043
(-0.57) (0.29)
foreign 0.573***
(5.37)
--------------------------------------------
N 74 74
--------------------------------------------
Standardized beta coefficients; t statistics in parentheses
* p<0.05, ** p<0.01, *** p<0.001*/
esttab,beta not //只汇报标准化系数
/*
--------------------------------------------
(1) (2)
price price
--------------------------------------------
weight 0.460** 0.913***
mpg -0.097 0.043
foreign 0.573***
--------------------------------------------
N 74 74
--------------------------------------------
Standardized beta coefficients
* p<0.05, ** p<0.01, *** p<0.001 */
3.4 将 t 统计量在回归系数的右侧汇报
sysuse auto, clear
eststo: regress price weight mpg
eststo: regress price weight mpg foreign
esttab, wide
/*
--------------------------------------------------------------------------------------------------------------------------------
(1) (2) (3) (4)
price price price price
--------------------------------------------------------------------------------------------------------------------------------
weight 1.747** (2.72) 3.465*** (5.49) 1.747** (2.72) 3.465*** (5.49)
mpg -49.51 (-0.57) 21.85 (0.29) -49.51 (-0.57) 21.85 (0.29)
foreign 3673.1*** (5.37) 3673.1*** (5.37)
_cons 1946.1 (0.54) -5853.7 (-1.73) 1946.1 (0.54) -5853.7 (-1.73)
--------------------------------------------------------------------------------------------------------------------------------
N 74 74 74 74
--------------------------------------------------------------------------------------------------------------------------------
t statistics in parentheses
* p<0.05, ** p<0.01, *** p<0.001 */
esttab, se wide //wide 和 se 的顺序可以互换
/*
--------------------------------------------------------------------------------------------------------------------------------
(1) (2) (3) (4)
price price price price
--------------------------------------------------------------------------------------------------------------------------------
weight 1.747** (0.641) 3.465*** (0.631) 1.747** (0.641) 3.465*** (0.631)
mpg -49.51 (86.16) 21.85 (74.22) -49.51 (86.16) 21.85 (74.22)
foreign 3673.1*** (684.0) 3673.1*** (684.0)
_cons 1946.1 (3597.0) -5853.7 (3377.0) 1946.1 (3597.0) -5853.7 (3377.0)
--------------------------------------------------------------------------------------------------------------------------------
N 74 74 74 74
--------------------------------------------------------------------------------------------------------------------------------
Standard errors in parentheses
* p<0.05, ** p<0.01, *** p<0.001 */
3.5 数值格式
esttab
汇报的回归结果是一个个数值,掌握数值的格式设定规则很有必要。该领命默认格式为a3,上述示例采取的就是默认的数值格式输出,是一种根据每一个统计量自身的数值大小进行的适应性呈现,因此统计量之间便没有统一的格式。可通过设定某一类统计量的数值格式实现更为个性化的输出,具体的实现方式为在要呈现的统计量后加适当的括号()
进行格式设定。
sysuse auto, clear
eststo: regress price weight mpg
eststo: regress price weight mpg foreign
esttab, b(a6) p(4) r2(4) nostar wide //a6表示一种更为精确的格式,更一般的为a#,#表示有效数字的最小个数,例子中都是7位有效数字;4和2分别表示p值和可决系数显示小数点后的位数;nostar 表示不显示显著性。
/*
----------------------------------------------------------------
(1) (2)
price price
----------------------------------------------------------------
weight 1.746559 (0.0081) 3.464706 (0.0000)
mpg -49.51222 (0.5673) 21.85360 (0.7693)
foreign 3673.060 (0.0000)
_cons 1946.069 (0.5902) -5853.696 (0.0874)
----------------------------------------------------------------
N 74 74
R-sq 0.2934 0.4996
----------------------------------------------------------------
p-values in parentheses
*/
* Stata 官方命令也有专门的格式设定规则,具体可以参见手册:[D] format Set variables' output format
3.6 标签、标题与注释
esttab
命令可以帮助我们在输出的结果中使用变量标签表示变量,这非常实用,也有利于我们在数据处理阶段重识对标签的管理,规范的标签设置可以帮助我们更清楚且更有效地呈现回归结果。
sysuse auto, clear
eststo: regress price weight mpg
eststo: regress price weight mpg foreign
esttab, se label title(Tabel. Regression results) ///
nonumbers mtitles("Model A" "Model B") ///
addnote("Source: auto.dta from Stata's System")
/*
Tabel. Regression results
----------------------------------------------------
Model A Model B
----------------------------------------------------
Weight (lbs.) 1.747** 3.465***
(0.641) (0.631)
Mileage (mpg) -49.51 21.85
(86.16) (74.22)
Car type 3673.1***
(684.0)
Constant 1946.1 -5853.7
(3597.0) (3377.0)
----------------------------------------------------
Observations 74 74
----------------------------------------------------
Standard errors in parentheses
Source: auto.dta from Stata's System
* p<0.05, ** p<0.01, *** p<0.001 */
* 注: [no]numbers:do not/do print model numbers in table header
此外,label
选项还支持因子变量(factor variables)和交互项(interaction terms )。
sysuse auto, clear
eststo: regress price mpg i.foreign
eststo: regress price c.mpg##i.foreign
esttab
/*
--------------------------------------------
(1) (2)
price price
--------------------------------------------
mpg -294.2*** -329.3***
(-5.28) (-4.39)
0.foreign 0 0
(.) (.)
1.foreign 1767.3* -13.59
(2.52) (-0.01)
0.foreign#~g 0
(.)
1.foreign#~g 78.89
(0.70)
_cons 11905.4*** 12600.5***
(10.28) (8.25)
--------------------------------------------
N 74 74
--------------------------------------------
t statistics in parentheses
* p<0.05, ** p<0.01, *** p<0.001 */
esttab, varwidth(20) //修改允许呈现的变量宽度,可根据呈现效果调整
/*
----------------------------------------------------
(1) (2)
price price
----------------------------------------------------
mpg -294.2*** -329.3***
(-5.28) (-4.39)
0.foreign 0 0
(.) (.)
1.foreign 1767.3* -13.59
(2.52) (-0.01)
0.foreign#c.mpg 0
(.)
1.foreign#c.mpg 78.89
(0.70)
_cons 11905.4*** 12600.5***
(10.28) (8.25)
----------------------------------------------------
N 74 74
----------------------------------------------------
t statistics in parentheses
* p<0.05, ** p<0.01, *** p<0.001 */
esttab, varwidth(25) label
/*
---------------------------------------------------------
(1) (2)
Price Price
---------------------------------------------------------
Mileage (mpg) -294.2*** -329.3***
(-5.28) (-4.39)
Domestic 0 0
(.) (.)
Foreign 1767.3* -13.59
(2.52) (-0.01)
Domestic # Mileage (mpg) 0
(.)
Foreign # Mileage (mpg) 78.89
(0.70)
Constant 11905.4*** 12600.5***
(10.28) (8.25)
---------------------------------------------------------
Observations 74 74
---------------------------------------------------------
t statistics in parentheses
* p<0.05, ** p<0.01, *** p<0.001 */
esttab, varwidth(25) label nobaselevels interaction(" X ")
/*
---------------------------------------------------------
(1) (2)
Price Price
---------------------------------------------------------
Mileage (mpg) -294.2*** -329.3***
(-5.28) (-4.39)
Foreign 1767.3* -13.59
(2.52) (-0.01)
Foreign X Mileage (mpg) 78.89
(0.70)
Constant 11905.4*** 12600.5***
(10.28) (8.25)
---------------------------------------------------------
Observations 74 74
---------------------------------------------------------
t statistics in parentheses
* p<0.05, ** p<0.01, *** p<0.001 */
* 还可以直接输出纯文本表格的结果
esttab, plain //需要注意的是,纯文本输出没有显著性标记,但这种方式也有其优点,例如,结果输出到excel后可直接开展进一步计算
*(output omitted)
3.7 压缩行距的表格
sysuse auto, clear
eststo: regress price weight mpg
eststo: regress price weight mpg foreign
eststo: regress price weight mpg foreign displacement
esttab
/*
------------------------------------------------------------
(1) (2) (3)
price price price
------------------------------------------------------------
weight 1.747** 3.465*** 2.458**
(2.72) (5.49) (2.82)
mpg -49.51 21.85 19.08
(-0.57) (0.29) (0.26)
foreign 3673.1*** 3930.2***
(5.37) (5.67)
displacement 10.22
(1.65)
_cons 1946.1 -5853.7 -4846.8
(0.54) (-1.73) (-1.43)
------------------------------------------------------------
N 74 74 74
------------------------------------------------------------
t statistics in parentheses
* p<0.05, ** p<0.01, *** p<0.001 */
esttab, compress //compress reduces the amount of horizontal spacing (so that more models fit on screen without line breaking).
/*
-------------------------------------------------
(1) (2) (3)
price price price
-------------------------------------------------
weight 1.747** 3.465*** 2.458**
(2.72) (5.49) (2.82)
mpg -49.51 21.85 19.08
(-0.57) (0.29) (0.26)
foreign 3673.1*** 3930.2***
(5.37) (5.67)
displace~t 10.22
(1.65)
_cons 1946.1 -5853.7 -4846.8
(0.54) (-1.73) (-1.43)
-------------------------------------------------
N 74 74 74
-------------------------------------------------
t statistics in parentheses
* p<0.05, ** p<0.01, *** p<0.001 */
3.8 系数显著性的星标设定
正如上面的示例所示,显著性标记默认的输出符号和阈值为:
* for p<.05
** for p<.01
*** for p<.001
事实上,我们也可以手动设定复合需要的显著性水平,亦可通过nostar
选项不显示显著性标记。
sysuse auto, clear
eststo: regress price weight mpg
eststo: regress price weight mpg foreign
esttab, star(& 0.10 * 0.05) // 其实,用什么符号完全可以自己随心所欲地设定,+、-、&、%.....均可。
/*
----------------------------------------
(1) (2)
price price
----------------------------------------
weight 1.747* 3.465*
(2.72) (5.49)
mpg -49.51 21.85
(-0.57) (0.29)
foreign 3673.1*
(5.37)
_cons 1946.1 -5853.7&
(0.54) (-1.73)
----------------------------------------
N 74 74
----------------------------------------
t statistics in parentheses
& p<0.10, * p<0.05 */
4. 使用 esttab 输出回归表格
4.1 输出为 Excel
通过设定一个后缀为.csv
文件格式的文件名,可以将结果输出为 excel 可读的的文档。
sysuse auto, clear
eststo: regress price weight mpg
eststo: regress price weight mpg foreign
esttab using excel_demmo.csv,wide se replace //可根据上文列出的相关命令进行更多定制化的设定
*(output written to excel_demmo.csv) // 单击 excel_demmo.csv 可以直接打开文件
如果要对回归结果进行其他处理(例如,计算变量之间的边际替代率,或是画一些简单的直方图等等),可以输出为纯文本格式:
esttab using excel_demmo.csv,wide se plain replace
4.2 输出为 Word
通过设定一个后缀为.rft
文件格式的文件名,可以将结果输出为 word 可读的的文档。
.rft 是Rich Text Format(RFT)的缩写,意即丰富的文本格式。 在rtf文档中可以嵌入图像等文件,RTF是word为了与其他字处理软件兼容而能够保存的文档格式,类似 doc 格式(Word文档)的文件,有很好的兼容性。
sysuse auto, clear
eststo: regress price weight mpg
eststo: regress price weight mpg foreign
esttab using word_demo.rft,se replace
进一步,可以使用append
命令将新的输出内容添加到已经输出的文档之中。
esttab using word_demo.rft,append wide se
另一个很实用的命令选项是onecell
,能够把点估计的t值和标准误直接置于位于同一个表格的格子内。
lab var mpg "The mgp variable has a really long label and that would disturb the table"
esttab using example.rtf, label onecell replace //上图
esttab using example.rtf, append label nogap onecell replace //下图
注:esttab 命令还可以输出为Latex 格式,这里不多做介绍。
4.3 非标准内容的输出
esttab
通过预先定义好的选项输出特定的参数统计量,但有时我们需要输出其他统计量。这时可以通过main()
和aux()
选项分别替换“点估计”
和“t值/标准误”
的位置,将回归后的Postestimation statistics
呈现在表格中。
sysuse auto, clear
eststo: regress price weight mpg foreign
estadd vif //estadd: Add results to (stored)estimates; estat vif 也可以计算vif
/*
Variable | VIF 1/VIF
-------------+----------------------
weight | 3.86 0.258809
mpg | 2.96 0.337297
foreign | 1.59 0.627761
-------------+----------------------
Mean VIF | 2.81
added matrix:
e(vif) : 1 x 4 */
estat vif
/*
Variable | VIF 1/VIF
-------------+----------------------
weight | 3.86 0.258809
mpg | 2.96 0.337297
foreign | 1.59 0.627761
-------------+----------------------
Mean VIF | 2.81 */
esttab, aux(vif 2) wide nopar //[no]parentheses: do not/do print parentheses around t-statistics
/*
-----------------------------------------
(1)
price
-----------------------------------------
weight 3.465*** 3.86
mpg 21.85 2.96
foreign 3673.1*** 1.59
_cons -5853.7
-----------------------------------------
N 74
-----------------------------------------
vif in second column
* p<0.05, ** p<0.01, *** p<0.001 */
更一般地,我们希望展示超过两个以上的参数统计量,例如,像之前一样汇报变量的点估计量和t值,同时汇报每个变量的方差膨胀因子(vif),这时需要转换到estout
语法规则,使用cells()
选项进行设定。estout
中所有的选项都可以在esttab
中使用,且相对于后者自身的选项而言优先被执行。例如,设定了cells()
,main()
,se()
,t()
,p()
,aux()
,star
,wide
,onecell
等上面使用过的选项都将失效。关于如何使用cell()
,下面做简要介绍:
sysuse auto, clear
regress price weight mpg foreign
estadd vif
esttab, cells("b(fmt(a3) star) vif(fmt(2))" t(par fmt(2))) // fmt:to specify the display format(s) of a statistic.
/*
-----------------------------------------
(1)
price
b/t vif
-----------------------------------------
weight 3.465*** 3.86
(5.49)
mpg 21.85 2.96
(0.29)
foreign 3673.1*** 1.59
(5.37)
_cons -5853.7
(-1.73)
-----------------------------------------
N 74
----------------------------------------- */
如果需要在上例中输出更多的概要统计量,可以使用estout
的stats()
选项进行设定。