Stata连享会 主页 || 视频 || 推文 || 知乎 || Bilibili 站
温馨提示: 定期 清理浏览器缓存,可以获得最佳浏览体验。
New!
lianxh
命令发布了:
随时搜索推文、Stata 资源。安装:
. ssc install lianxh
详情参见帮助文件 (有惊喜):
. help lianxh
连享会新命令:cnssc
,ihelp
,rdbalance
,gitee
,installpkg
⛳ Stata 系列推文:
作者:王胜文 (山东财经大学)
邮箱:sw8258@foxmail.com
编者按:本文主要摘译自「Packages for generating LaTeX output from Stata code」,特此致谢!
目录
在实际操作中,Stata 有很多自动输出命令,例如 estout
/esttab
可以高效地生成默认格式。但是,自动输出命令没办法对默认格式进行调整,所以使用者输出或者定制自己所需的格式是非常困难的。
很多半自动命令,例如 json-this
/coeftable
和 stata-tex
之类的命令,在输出过程中可通过手动创建
半自动命令在输出
* 命令安装
ssc install estout, replace
* 命令语法
estout [ namelist ] [ using filename ]
[, cells(array) stats(scalarlist) style(style) options ]
其中,namelist
是存储估计集的名称列表,using filename
是设置输出结果的文件名,cells()
和 stats()
选项决定了表格的主要内容,style()
选项确定表格的基本格式。
. sysuse auto, clear
. eststo: quietly regress price weight mpg
. eststo: quietly regress price weight mpg foreign
. estout
--------------------------------------
est1 est2
b b
--------------------------------------
weight 1.746559 3.464706
mpg -49.51222 21.8536
foreign 3673.06
_cons 1946.069 -5853.696
--------------------------------------
. estout using example.txt
. type example.txt
est1 est2
b b
weight 1.746559 3.464706
mpg -49.51222 21.8536
foreign 3673.06
_cons 1946.069 -5853.696
* 命令语法
esttab [ namelist ] [ using filename ]
[, options estout_options ]
在使用 esttab
输出回归结果时,首先存储多个模型的结果,然后使用 esttab
命令将存储的估计集组成一个回归表。esttab
和 estout
之间的主要区别是 esttab
会输出完全格式化的结果。
. sysuse auto, clear
. regress price weight mpg
. estimates store model1
. regress price weight mpg foreign
. estimates store model2
. esttab model1 model2 //括号里默认输出t统计量
--------------------------------------------
(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, p scalars(F df_m df_r) //括号里默认输出p值
--------------------------------------------
(1) (2)
price price
--------------------------------------------
weight 1.747** 3.465***
(0.008) (0.000)
mpg -49.51 21.85
(0.567) (0.769)
foreign 3673.1***
(0.000)
_cons 1946.1 -5853.7
(0.590) (0.087)
--------------------------------------------
N 74 74
F 14.74 23.29
df_m 2 3
df_r 71 70
--------------------------------------------
p-values in parentheses
* p<0.05, ** p<0.01, *** p<0.001
* 命令安装
ssc install outreg2, replace
* 命令语法
outreg2 [varlist] [estlist] using filename
[, options] [: command]
. sysuse auto, clear
. regress mpg foreign weight headroom trunk length turn displacement
. est store Full
. regress mpg foreign weight headroom
. est store Restricted1
. regress mpg foreign weight
. est store Restricted2
. outreg2 [Full Restricted1 Restricted2] using myfile.doc, replace
myfile.doc
dir : seeout
tabout
主要对象是描述性统计结果,而不是回归结果。tabout
的一大特点是提供定制化服务,即改变选项就能够对表格细节进行修改,以达到用户需要的样式。
* 命令安装
ssc install tabout, replace
* 命令语法
tabout [varlist] [if exp] [in range]
[weight = exp] using filename [ , options]
. sysuse auto, clear
. tabout foreign rep78 using table1.xls, c(mean mpg mean weight mean length ///
> median price median headroom) f(1c 1c 1c 2cm 1c) clab(MPG Weight_(lbs) ///
> Length_(in) Price Headroom_(in)) sum npos(tufte)
Table output written to: table1.xls
Mean Mean Mean Median Median
MPG Weight (lbs) Length (in) Price Headroom (in)
Car origin
Domestic (70%) 19.8 3,317.1 196.1 $4,782.50 3.5
Foreign (29%) 24.8 2,315.9 168.5 $5,759.00 2.5
Total (100%) 21.3 3,019.5 187.9 $5,006.50 3.0
Repair record 1978
1 (2%) 21.0 3,100.0 189.0 $4,564.50 1.8
2 (11%) 19.1 3,353.8 199.4 $4,638.00 3.8
3 (43%) 19.4 3,299.0 194.0 $4,741.00 3.5
4 (26%) 21.7 2,870.0 184.8 $5,751.50 3.0
5 (15%) 27.4 2,322.7 170.2 $5,397.00 2.5
Total (100%) 21.3 3,032.0 188.3 $5,079.00 3.0
orth_out
主要对象是汇总统计和正交表格,而不是回归结果。
* 命令安装
ssc install orth_out, replace
* 命令语法
orth_out varlist1 [using filename] [if] ,
by(varlist2) [options]
. sysuse auto, clear
. orth_out price mpg, by(foreign) se compare test count
Domestic: Foreign: (1) vs. (2): p-value f~y:
_ _ _ _
Price:mean 6072.423 6384.682 -312.259 0.680
se 429.491 558.994 754.449 .
Mileage(mpg):mean 19.827 24.773 -4.946 0.001
se 0.658 1.410 1.362 .
N:_ 52.000 22.000 74.000 .
命令 json-this
与 coeftable
计算的值保存为 .json
文件,然后插入到外部 .tex
模板中。该命令需要从「Gábor Nyéki 主页」手动安装,并需要安装 Python 相关包。
命令 stata-tex
将计算值保存为 .csv,然后插入到外部 .tex 模板中。该命令需从「Paul Novosad 主页」手动安装,并需要安装 Python 相关包。
命令 texresults
计算出的值被保存为 .tex
宏文件。
* 命令安装
ssc install texresults, replace
* 命令语法
texresults using filename, [options]
. sysuse auto, clear
. regress mpg trunk weight foreign
你可以使用 estout
命令将回归结果导出并放置在
如果想在文本中突出 foreign 的系数,你可以将其手动粘贴进文档,然而这种方法的问题在于系数无法因调整而发生变化,需要手动修改回归系数。
为了解决这一问题,你可以将 foreign
的系数保存在本地,将其四舍五入到合理的小数位。例如:
. local foreign = round(_b[foreign], 0.1)
接着,可以使用 file
命令创建一个包含存储系数的新
capture: file close myfile
file open myfile using "results.tex", write replace
file write myfile "\newcommand{\foreign}{$`foreign'$}" _n
file close myfile
请注意,foreign 的系数保存在分隔符 $...$
中,以便在
\documentclass{article}
\input{results.tex}
\begin{document}
Our main result is \foreign.
\end{document}
具体示例,参考「Export Stata results as LaTeX macros」。
命令 regsave
和 texsave
计算出的数值保存为 Stata 数据集,并导出 .tex
文件。
* 命令安装
ssc install regsave, replace
ssc install texsave, replace
. tempfile results
. sysuse auto, clear
. local replace replace
. foreach rhs in "mpg" "mpg weight" {
2. foreach type in "Domestic" "Foreign" {
3. reg price `rhs' if foreign=="`type'":origin, robust
4. regsave using "`results'", pval autoid `replace' ///
addlabel(rhs,"`rhs'",origin,"`type'")
5. local replace append
6. }
7. }
. use "`results'", clear
. list
命令 texdoc
可以将计算值直接导入到含有 .do 文件的 .tex 模板中。
* 命令安装
ssc install texdoc, replace
texdoc
的文档结构:
texdoc init myexample.tex, replace
/***
\documentclass{article}
\usepackage{graphicx}
\usepackage{stata}
\begin{document}
\section{Exercise 1}
Open the 1978 Automobile Data and summarize the variables.
***/
texdoc stlog
sysuse auto
summarize
texdoc stlog close
/***
\section{Exercise 2}
Run a regression of price on milage and weight.
***/
texdoc stlog
regress price mpg weight
texdoc stlog close
/***
\end{document}
***/
命令 write_mats
需要从「Alessandro Martinello 主页」手动安装。write_mats
将计算结果被保存为 .ster 文件,并将所需的数值排列为 Stata 矩阵,最后导出为 .tex 文件。
listtab
将表格设置为 Stata 数据集,然后导出 .tex 格式文件。
* 命令安装
ssc install listtab, replace
* 命令语法
listtab [ varlist ] [ using filename ] [if] [in] [ ,
begin(string) delimiter(string) end(string) missnum(string)
rstyle(rowstyle) vbegin(varname) vdelimiter(varname)
vend(varname) headlines(string_list) footlines(string_list)
headchars(namelist) footchars(namelist) nolabel type replace
appendto(filename) handle(handle_name) ]
. sysuse auto, clear
. listtab make weight mpg if foreign, type rstyle(markdown) ///
head("`tabhead'") headc(underline)
这些方法通常适用于将分析结果集成到新建的文档中,当然也可以用于输出
dyntext
命令 (文本,包括 tex 源码) 或 dyndoc
命令 (markdown) ;markstat
命令。
outsheet
导出 (根据「Damon Jones 的推特帖子」);file write
直接编写 .tex
代码 (根据「Paul Goldsmith-Pinkham 的例子」);extract-from-stata
(程序包) 将来自 Stata 日志文件的回归输出转换为 .tex 格式 ;stargazer
(程序包) 输出 LaTeX 格式 )。
Note:产生如下推文列表的 Stata 命令为:
lianxh latex, m
安装最新版lianxh
命令:
ssc install lianxh, replace
免费公开课
最新课程-直播课
专题 | 嘉宾 | 直播/回看视频 |
---|---|---|
⭐ 最新专题 | 文本分析、机器学习、效率专题、生存分析等 | |
研究设计 | 连玉君 | 我的特斯拉-实证研究设计,-幻灯片- |
面板模型 | 连玉君 | 动态面板模型,-幻灯片- |
面板模型 | 连玉君 | 直击面板数据模型 [免费公开课,2小时] |
⛳ 课程主页
⛳ 课程主页
关于我们
课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法
等
连享会小程序:扫一扫,看推文,看视频……
扫码加入连享会微信群,提问交流更方便
✏ 连享会-常见问题解答:
✨ https://gitee.com/lianxh/Course/wikis
New!
lianxh
命令发布了:
随时搜索连享会推文、Stata 资源,安装命令如下:
. ssc install lianxh
使用详情参见帮助文件 (有惊喜):
. help lianxh