Loading...
Loading...
Compare original and translation side by side
..a.z* WRONG — includes observations where income is missing!
gen high_income = (income > 50000)
* RIGHT
gen high_income = (income > 50000) if !missing(income)
* WRONG — missing ages appear in this list
list if age > 60
* RIGHT
list if age > 60 & !missing(age)..a.z* 错误写法——会包含income为缺失值的观测!
gen high_income = (income > 50000)
* 正确写法
gen high_income = (income > 50000) if !missing(income)
* 错误写法——缺失的age会被包含在结果中
list if age > 60
* 正确写法
list if age > 60 & !missing(age)=========* WRONG — syntax error
gen employed = 1 if status = 1
* RIGHT
gen employed = 1 if status == 1===* 错误写法——语法错误
gen employed = 1 if status = 1
* 正确写法
gen employed = 1 if status == 1`name'$name${name}local controls "age education income"
regress wage `controls' // correct
regress wage `controls // WRONG — missing closing quote
regress wage 'controls' // WRONG — wrong quote characters`name'$name${name}local controls "age education income"
regress wage `controls' // 正确写法
regress wage `controls // 错误写法——缺少闭合引号
regress wage 'controls' // 错误写法——引号类型错误bybysortbybysort* WRONG — error if data not sorted by id
by id: gen first = (_n == 1)
* RIGHT — bysort sorts automatically
bysort id: gen first = (_n == 1)
* Also RIGHT — explicit sort
sort id
by id: gen first = (_n == 1)* 错误写法——如果数据未按id排序会报错
by id: gen first = (_n == 1)
* 正确写法——bysort会自动排序
bysort id: gen first = (_n == 1)
* 另一种正确写法——显式排序
sort id
by id: gen first = (_n == 1)i.c.i.c.i.c.i.* WRONG — treats race as continuous (e.g., race=3 has 3x effect of race=1)
regress wage race education
* RIGHT — creates dummies automatically
regress wage i.race education
* Interactions
regress wage i.race##c.education // full interaction
regress wage i.race#c.education // interaction only (no main effects)i.c.i.* 错误写法——将race视为连续变量(例如race=3的影响是race=1的3倍)
regress wage race education
* 正确写法——自动生成虚拟变量
regress wage i.race education
* 交互项
regress wage i.race##c.education // 完整交互项(包含主效应)
regress wage i.race#c.education // 仅交互项(无主效应)generatereplacegeneratereplacegeneratereplacegenerategen x = 1
gen x = 2 // ERROR: x already defined
replace x = 2 // correctgeneratereplacegenerategen x = 1
gen x = 2 // 错误:x已定义
replace x = 2 // 正确写法* May miss "Male", "MALE", etc.
keep if gender == "male"
* Safer
keep if lower(gender) == "male"* 可能会遗漏"Male"、"MALE"等情况
keep if gender == "male"
* 更安全的写法
keep if lower(gender) == "male"merge_mergemerge_mergetab _mergeassertmerge 1:1 id using other.dta
tab _merge // ALWAYS tab before assert
assert _merge == 3 // fails silently without tab output
drop _mergetab _mergeassertmerge 1:1 id using other.dta
tab _merge // 合并后务必执行此命令
assert _merge == 3 // 没有tab输出的话,断言失败时无提示
drop _mergepreserverestoretempfilepreserverestoretempfiletempfile stats
preserve
collapse (mean) avg_x=x, by(group)
save `stats'
restore
merge m:1 group using `stats'
tab _merge
assert _merge == 3
drop _mergebysort group: egen avg_x = mean(x)tempfile stats
preserve
collapse (mean) avg_x=x, by(group)
save `stats'
restore
merge m:1 group using `stats'
tab _merge
assert _merge == 3
drop _mergebysort group: egen avg_x = mean(x)fweightaweightpweightiweightfweightaweightpweightiweightcapturecapturecapture some_command
if _rc != 0 {
di as error "Failed with code: " _rc
exit _rc
}capture some_command
if _rc != 0 {
di as error "执行失败,错误码: " _rc
exit _rc
}//////regress y x1 x2 x3 ///
x4 x5 x6, ///
vce(robust)regress y x1 x2 x3 ///
x4 x5 x6, ///
vce(robust)r()e()s()r()e()s()r()e()s()e()regress y x1 x2
estimates store model1r()e()s()e()regress y x1 x2
estimates store model1.do.do.appundefined.appundefined
If Stata isn't on `$PATH`, find it with: `mdfind -name "stata-mp" | grep MacOS`
如果Stata不在`$PATH`中,可以使用以下命令查找:`mdfind -name "stata-mp" | grep MacOS`-b-bundefinedundefined
- `-b` = batch mode (non-interactive, no GUI)
- Output (everything Stata would display) is written to `analysis.log` in the working directory
- Exit code is 0 on success, non-zero on error
- The log file contains all output, including error messages — check it after execution
- `-b` = 批处理模式(无交互,无图形界面)
- 输出(Stata会显示的所有内容)会写入当前工作目录下的`analysis.log`
- 执行成功时退出码为0,失败时为非0值
- 日志文件包含所有输出,包括错误信息——执行后请务必检查.doundefined.doundefinedundefinedundefinedundefinedundefinedundefinedundefinedclear allclear allset more off--more--analysis.doanalysis.log.do.docd.doclear allclear allset more off--more--analysis.doanalysis.log.do.do.docd| File | Topics & Key Commands |
|---|---|
| |
| |
| |
| Variable types, |
| |
| |
| |
| 文件 | 主题与关键命令 |
|---|---|
| |
| |
| |
| 变量类型, |
| |
| |
| |
| File | Topics & Key Commands |
|---|---|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| 文件 | 主题与关键命令 |
|---|---|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| File | Topics & Key Commands |
|---|---|
| |
| DiD, parallel trends, event studies, staggered adoption |
| Sharp/fuzzy RD, bandwidth selection, |
| PSM, nearest neighbor, kernel matching, |
| |
| 文件 | 主题与关键命令 |
|---|---|
| |
| 双重差分法(DiD), 平行趋势, 事件研究, staggered adoption |
| 精确/模糊断点回归, 带宽选择, |
| 倾向得分匹配(PSM), 最近邻匹配, 核匹配, |
| |
| File | Topics & Key Commands |
|---|---|
| |
| |
| |
| |
| |
| 文件 | 主题与关键命令 |
|---|---|
| |
| |
| |
| |
| |
| File | Topics & Key Commands |
|---|---|
| |
| 文件 | 主题与关键命令 |
|---|---|
| |
| File | Topics & Key Commands |
|---|---|
| |
| |
| Mata basics, when to use Mata vs ado, data types |
| Mata functions, flow control, structures, pointers |
| Matrix creation, decompositions, solvers, |
| |
| 文件 | 主题与关键命令 |
|---|---|
| |
| |
| Mata基础, Mata与ado的适用场景, 数据类型 |
| Mata函数, 流程控制, 数据结构, 指针 |
| 矩阵创建, 矩阵分解, 求解器, |
| |
| File | Topics & Key Commands |
|---|---|
| |
| Project structure, master do-files, version control, debugging, common mistakes |
| Python via |
| User wants to report a Stata skill documentation gap or error to the repository |
| 文件 | 主题与关键命令 |
|---|---|
| |
| 项目结构, 主do文件, 版本控制, 调试, 常见错误 |
| 通过 |
| 用户需要向仓库报告Stata技能文档的漏洞或错误 |
| File | What It Does |
|---|---|
| High-dimensional fixed effects OLS (absorbs multiple FE sets efficiently) |
| |
| Alternative regression table exporter (Word, Excel, TeX) |
| One-command Word document creation for any Stata output |
| Cross-tabulations and summary tables to file |
| Coefficient plots from stored estimates |
| |
| Modern DiD: |
| |
| Robust RD estimation with optimal bandwidth ( |
| Propensity score matching (nearest neighbor, kernel, radius) |
| Synthetic control method ( |
| Enhanced IV/2SLS: |
| Dynamic panel GMM (Arellano-Bond/Blundell-Bond) |
| Binned scatter plots with CI ( |
| Nonparametric kernel estimation and inference |
| |
| Winsorizing and trimming: |
| |
| |
| 文件 | 功能描述 |
|---|---|
| 高维固定效应OLS(高效吸收多个固定效应集合) |
| |
| 替代回归表格导出工具(支持Word、Excel、TeX) |
| 一键将任意Stata输出生成Word文档 |
| 生成交叉表和汇总表到文件 |
| 从存储的估计结果绘制系数图 |
| |
| 现代双重差分法: |
| |
| 稳健断点回归估计与最优带宽选择( |
| 倾向得分匹配(最近邻、核、半径匹配) |
| 合成控制法( |
| 增强版IV/2SLS: |
| 动态面板GMM(Arellano-Bond/Blundell-Bond方法) |
| 带置信区间的分箱散点图( |
| 非参数核估计与推断 |
| |
| 缩尾与截尾处理: |
| |
| |
* Estimate models
eststo clear
eststo: regress y x1 x2, vce(robust)
eststo: regress y x1 x2 x3, vce(robust)
eststo: regress y x1 x2 x3 x4, vce(cluster id)
* Export table
esttab using "results.tex", replace ///
se star(* 0.10 ** 0.05 *** 0.01) ///
label booktabs ///
title("Main Results") ///
mtitles("(1)" "(2)" "(3)")* 估计模型
eststo clear
eststo: regress y x1 x2, vce(robust)
eststo: regress y x1 x2 x3, vce(robust)
eststo: regress y x1 x2 x3 x4, vce(cluster id)
* 导出表格
esttab using "results.tex", replace ///
se star(* 0.10 ** 0.05 *** 0.01) ///
label booktabs ///
title("主要结果") ///
mtitles("(1)" "(2)" "(3)")xtset panelid timevar // declare panel structure
xtdescribe // check balance
xtsum outcome // within/between variation
* Fixed effects
xtreg y x1 x2, fe vce(cluster panelid)
* Or with reghdfe (preferred for multiple FE)
reghdfe y x1 x2, absorb(panelid timevar) vce(cluster panelid)xtset panelid timevar // 声明面板结构
xtdescribe // 检查面板平衡性
xtsum outcome // 组内/组间变异
* 固定效应模型
xtreg y x1 x2, fe vce(cluster panelid)
* 或使用reghdfe(推荐用于多固定效应)
reghdfe y x1 x2, absorb(panelid timevar) vce(cluster panelid)* Classic 2x2 DiD
gen post = (year >= treatment_year)
gen treat_post = treated * post
regress y treated post treat_post, vce(cluster id)
* Event study (uniform timing — must interact with treatment group)
reghdfe y ib(-1).rel_time#1.treated, absorb(id year) vce(cluster id)
testparm *.rel_time#1.treated // pre-trend test
* Modern staggered DiD (Callaway & Sant'Anna)
csdid y x1 x2, ivar(id) time(year) gvar(first_treat) agg(event)
csdid_plot* 经典2x2双重差分
gen post = (year >= treatment_year)
gen treat_post = treated * post
regress y treated post treat_post, vce(cluster id)
* 事件研究(统一处理时间——必须与处理组交互)
reghdfe y ib(-1).rel_time#1.treated, absorb(id year) vce(cluster id)
testparm *.rel_time#1.treated // 平行趋势检验
* 现代多期双重差分(Callaway & Sant'Anna方法)
csdid y x1 x2, ivar(id) time(year) gvar(first_treat) agg(event)
csdid_plot* Publication-quality scatter with fit line
twoway (scatter y x, mcolor(navy%50) msize(small)) ///
(lfit y x, lcolor(cranberry) lwidth(medthick)), ///
title("Title Here") ///
xtitle("X Label") ytitle("Y Label") ///
legend(off) scheme(s2color)
graph export "figure1.pdf", replace as(pdf)
graph export "figure1.png", replace as(png) width(2400)* 符合出版要求的散点图与拟合线
twoway (scatter y x, mcolor(navy%50) msize(small)) ///
(lfit y x, lcolor(cranberry) lwidth(medthick)), ///
title("标题") ///
xtitle("X轴标签") ytitle("Y轴标签") ///
legend(off) scheme(s2color)
graph export "figure1.pdf", replace as(pdf)
graph export "figure1.png", replace as(png) width(2400)* Load and inspect
import delimited "raw_data.csv", clear varnames(1)
describe
codebook, compact
* Clean
rename *, lower // lowercase all varnames
destring income, replace force // convert string to numeric
replace income = . if income < 0
* Label
label variable income "Annual household income (USD)"
label define yesno 0 "No" 1 "Yes"
label values employed yesno
* Save
compress
save "clean_data.dta", replace* 加载并检查数据
import delimited "raw_data.csv", clear varnames(1)
describe
codebook, compact
* 数据清洗
rename *, lower // 所有变量名转为小写
destring income, replace force // 将字符串转为数值型
replace income = . if income < 0
* 添加标签
label variable income "家庭年收入(美元)"
label define yesno 0 "否" 1 "是"
label values employed yesno
* 保存数据
compress
save "clean_data.dta", replacemi set mlong
mi register imputed income education
mi impute chained (regress) income (ologit) education = age i.gender, add(20) rseed(12345)
mi estimate: regress wage income education age i.gendermi set mlong
mi register imputed income education
mi impute chained (regress) income (ologit) education = age i.gender, add(20) rseed(12345)
mi estimate: regress wage income education age i.genderreferences/filing-issues.mdreferences/filing-issues.md