Stata连享会 主页 || 视频 || 推文 || 知乎 || Bilibili 站
温馨提示: 定期 清理浏览器缓存,可以获得最佳浏览体验。
New!
lianxh
命令发布了:
随时搜索推文、Stata 资源。安装:
. ssc install lianxh
详情参见帮助文件 (有惊喜):
. help lianxh
连享会新命令:cnssc
,ihelp
,rdbalance
,gitee
,installpkg
⛳ Stata 系列推文:
作者:秦范 (北京大学)
邮箱:qf13032861571@163.com
编者按:本文主要整理自「replication-template」,特此致谢!
目录
为提高论文重现效率,有必要遵循一定规则为代码设定良好的运行环境。本文主要介绍一种常用代码模板,读者可根据个人习惯修改。
文件管理:主要有两种数据和代码文件管理结构。一是所有代码置于同一文件夹;二是主要代码单独放置,重现论文过程中创建的其他代码文件则置于文件夹中。
/*
Structure of the code, two scenarios:
- Code looks like this (simplified, Scenario A)
directory/
code/
main.do
01_dosomething.do
data/
data.dta
otherdata.dta
- Code looks like this (simplified, Scenario B)
directory/
main.do
scripts/
01_dosomething.do
data/
data.dta
otherdata.dta
*/
路径设定:设定工作路径和根路径。在打开 Stata 软件后显示的路径即当前工作路径,可用 pwd
获取当前路径作为之后的工作路径。根路径可以设定为研究项目的根文件夹存放位置,如上文代码中的 directory/
。
local pwd : pwd // the pwd after you just open stata
cd .. // change to the program directory and set as rootdir
global rootdir : pwd
cd `pwd' // return to the initial directory
为保证每次运行结果得以高效记录备份,有必要使用日志文件。首先,单独创建一个文件来存放所有日志文件。其次,将具体运行时间作为日志文件名称后缀。代码运行的具体时间可使用 c()
函数(可运行 creturn list
查看该函数的具体内容)。
global logdir "${rootdir}/logs"
cap mkdir "$logdir"
local c_date = c(current_date)
local cdate = subinstr("`c_date'", " ", "_", .) // repace all " " in "`c_date'" with "_"
local c_time = c(current_time)
local ctime = subinstr("`c_time'", ":", "_", .)
log using "$logdir/logfile_`cdate'-`ctime'-`c(username)'.log", name(ldi) replace text
此时还需要 creturn
来告知目前代码运行的 Stata 版本、处理器甚至计算机系统等信息,而这些信息对于是否能成功、高效运行具体代码至关重要。
/* SYSTEM DIAGNOSTICS */
di "=== SYSTEM DIAGNOSTICS ==="
di "Stata version: `c(stata_version)'"
di "Updated as of: `c(born_date)'"
di "Edition: `c(edition_real)'"
di "Processors: `c(processors)'"
di "OS: `c(os)' `c(osdtl)'"
di "Machine type: `c(machine_type)'"
di "=========================="
首先,有必要在根路径下新建 ado
文件夹,以存放为了重现某篇论文额外需要的所有命令包。
capture mkdir "$rootdir/ado"
sysdir set PERSONAL "$rootdir/ado/personal"
sysdir set PLUS "$rootdir/ado/plus"
sysdir set SITE "$rootdir/ado/site"
sysdir
其次,明确所需命令包可由 ssc install
还是 net install
安装。前者指从官方统计软件组 (Statistical Software Components) 中下载,后者则只需要命令包下载链接。
* ssc install
if !missing("`ssc_packages'") {
foreach pkg in `ssc_packages' {
cap `pkg'
if _rc == 199 { // _rc the return code from the most recent capture command
dis "Installing `pkg'"
ssc install `pkg', replace
}
which `pkg'
}
}
* net install pkgname, from(directory_or_url)
最后,在所有命令下载完成后,运行 mata: mata mlib index
将命令包集合成一个新的库。并且,要让软件在之后的代码运行中,不仅在 Stata 的 ado 中寻找包,还要在新的命令库中寻找。
/* Template config.do */
* NOTE: you should always put "config.do" in the same directory as "main.do"
* *** Add required packages from SSC to this list ***
local ssc_packages "winsor winsor2" // ensure pkgs can be ssc installed
* *** Set directory
local pwd : pwd // Captures the current directory, the one when you just open Stata
cd .. // the directory of program
global rootdir : pwd // Now capture the directory to use as rootdir
/*You normally need to make no further changes below this, unless "net install" packages */
set more off //Tell Stata to pause or not pause for --more-- messages
cd `pwd' // Return to the initial directory and never again use cd
/* Create log file including info about how and when the program was run */
global logdir "${rootdir}/logs"
cap mkdir "$logdir"
local c_date = c(current_date) // you can use creturn list to check what it is
local cdate = subinstr("`c_date'", " ", "_", .) // repace all " " in "`c_date'" with "_"
local c_time = c(current_time)
local ctime = subinstr("`c_time'", ":", "_", .)
cap log close _all
log using "$logdir/logfile_`cdate'-`ctime'-`c(username)'.log", name(ldi) replace text
/* SYSTEM DIAGNOSTICS */
di "=== SYSTEM DIAGNOSTICS ==="
di "Stata version: `c(stata_version)'"
di "Updated as of: `c(born_date)'"
di "Edition: `c(edition_real)'"
di "Processors: `c(processors)'"
di "OS: `c(os)' `c(osdtl)'"
di "Machine type: `c(machine_type)'"
di "=========================="
/* install any packages locally */
capture mkdir "$rootdir/ado"
sysdir set PERSONAL "$rootdir/ado/personal"
sysdir set PLUS "$rootdir/ado/plus"
sysdir set SITE "$rootdir/ado/site"
sysdir
/* add packages to the macro */
if !missing("`ssc_packages'") {
foreach pkg in `ssc_packages' {
cap `pkg'
if _rc == 199 { // _rc the return code from the most recent capture command
dis "Installing `pkg'"
ssc install `pkg', replace
}
which `pkg'
}
}
/* "net install" packages */
net install outreg2, from("http://fmwww.bc.edu/RePEc/bocode/o") replace force // Egs
mata: mata mlib index
//the LAST command after installing all packages, let Mata know to search the added function library
log close _all
Note:产生如下推文列表的 Stata 命令为:
lianxh 重现 复现, m
安装最新版lianxh
命令:
ssc install lianxh, replace
免费公开课
最新课程-直播课
专题 | 嘉宾 | 直播/回看视频 |
---|---|---|
⭐ 最新专题 | 文本分析、机器学习、效率专题、生存分析等 | |
研究设计 | 连玉君 | 我的特斯拉-实证研究设计,-幻灯片- |
面板模型 | 连玉君 | 动态面板模型,-幻灯片- |
面板模型 | 连玉君 | 直击面板数据模型 [免费公开课,2小时] |
⛳ 课程主页
⛳ 课程主页
关于我们
课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法
等
连享会小程序:扫一扫,看推文,看视频……
扫码加入连享会微信群,提问交流更方便
✏ 连享会-常见问题解答:
✨ https://gitee.com/lianxh/Course/wikis
New!
lianxh
和songbl
命令发布了:
随时搜索连享会推文、Stata 资源,安装命令如下:
. ssc install lianxh
使用详情参见帮助文件 (有惊喜):
. help lianxh