2. 输出到 Excel / Word 中
将回归结果输出到 Excel / Word 中,使用的命令为 esttab。基本统计量表、相关系数矩阵表和回归结果表通常是论文的核心,这里仅详细介绍回归结果的输出。
help esttab
*-调入数据
sysuse nlsw88.dta, clear
*-设定存储变量名称的暂元 see P2_local.do
global xx "wage age tenure ttl_exp hours married"
*-Table 1: 基本统计量 see R5_logout.do
logout, save("Tab1_statis") excel replace: ///
tabstat $xx, stat(mean p50 sd min max) ///
format(%3.2f) column(statis)
*-Table 2: 相关系数矩阵 see R5_logout.do
logout, save("Tab2_corr") excel replace: ///
pwcorr_a $xx
*-Table 3: regression results
reg $xx
est store full
reg $xx if race==1
est store white
reg $xx if race==2
est store black
reg $xx i.occupation
est store occu
*-基本用法
esttab full white black occu, nogap
下图为 esttab full white black occu, nogap 命令输出的结果,full white black occu 为存储的回归结果,nogap 是使得行与行之间没有空格。但是,我们可以看出该输出结果有两个缺陷,一是列标题都是被解释变量,不容易区分;二是含有虚拟变量,通常在报告结果时,我们会在最后增加一行显示是否控制该变量,而非报告所有虚拟变量。
为克服以上缺陷,我们看一段完整代码和结果。在运行以下代码时,我们没运行 local s "using Tab3_reg.csv" 这行,因此结果会直接输出到屏幕上,而非 Excel。
*-完整设定
*-----------------------------------------------------------
local s "using Tab3_reg.csv" // 输出 Excel 文档的暂元
local m "full white black occu" // 放置模型名称的暂元
esttab `m' `s', nogap compress replace ///
mtitle("Full" "White" "Black" "with_occu") ///
b(%4.3f) t(%4.2f) ///
scalar(N r2_a) ///
star(* 0.1 ** 0.05 *** 0.01) ///
drop(*.*)
*-----------------------------------------------------------
*-Note: 两种执行方法, 分别为屏幕呈现和输出 Excel
*-----------------------选项的含义------------------------
*- nogap 去掉空行
*- compress 以比较紧凑的形式呈现结果
*- replace 覆盖已经存在的旧文件
*- b(%4.3f) 系数保留小数点后三位
*- t(%4.2f) t 值保留小数点后两位
*- scalar() 最后两行的统计量: N-样本数; r2_a-adj-R2
*- eret list //返回值列表
*- drop() 不需要呈现的系数
*---------------------------------------------------------
以下为输出结果,compress 是使得列与列间更加紧凑;mtitle 是给每列定义列名;b(%4.3f) t(%4.2f) 定义了系数和 t 值的显示格式,其中系数是小数后保留 3 位,t 值是小数后保留 2 位;scalar 设置输出统计量,这里输出了「样本数 \(N\)」和「调整后 \(R^2\)」;star 设置的显著水平对应几颗星,如 1 颗星代表 10% 的统计水平;drop(*.*) 是不呈现虚拟变量的回归系数。最后,要在结尾加上一行报告是否控制某个虚拟变量。