stata-recipes

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Stata command & recipe writer

Stata 命令与 recipe 编写器

This skill turns "here's what I'm trying to do" into working Stata code — a full
.do
file when the task has multiple steps, or a short command sequence when it doesn't. The output is always something the user can paste into Stata and run, not a description of what they'd need to write.
Stata rewards precision more than most languages: a command that runs without error can still silently produce the wrong answer (unset panel structure, a
merge
nobody checked,
gen
where
egen
was needed for missing values). So this skill's job isn't just "make it run" — it's "make it run and be right," and to say why when a plausible-looking alternative would have been wrong.
本 skill 将“我想完成以下任务”转化为可工作的 Stata 代码——任务包含多个步骤时生成完整的
.do
文件,否则生成简短命令序列。输出始终是用户可直接粘贴到 Stata 中运行的代码,而不是说明用户还需要编写什么。
与大多数语言相比,Stata 更强调精确性:一个没有报错的命令仍可能悄无声息地产生错误结果(未设置面板数据结构、未经检查的
merge
、本应使用
egen
处理缺失值却使用了
gen
)。因此,本 skill 的工作不只是“让代码运行”——而是“让它运行并且结果正确”,并在看似合理的替代方案实际上有误时说明原因。

Workflow

工作流程

  1. Understand the data shape before writing anything. Ask (or infer from what the user already said) whether the data is cross-sectional, panel, or time series; roughly how many observations/variables; and whether it's already loaded, a file path, or hypothetical. A regression recipe for panel data and one for a single time series look nothing alike even when the plain-language ask ("does X affect Y") is the same. Don't stall on this if the user already gave enough to proceed — infer sensible defaults and say what you assumed, rather than interrogating them over something you can reasonably guess.
  2. Check whether a matching recipe already exists in
    references/recipes/
    (see the table below) before improvising from scratch. A recipe encodes decisions — lag selection method, which diagnostics matter, which postestimation commands actually work after that estimator — that are easy to get subtly wrong by generalizing from a different command's usual pattern.
  3. Write the code, structured as a proper
    .do
    file even for a short answer:
    clear all
    /
    set more off
    at the top when starting fresh,
    tsset
    /
    xtset
    before any time-series or panel operation that needs it, comments only where the why isn't obvious from the command itself (see
    references/good-bad-examples.md
    for the difference between a useful comment and noise). Prefer the modern, currently-documented form of a command over an older syntax that still happens to run — flag it explicitly if you're aware the user's snippet uses a deprecated form.
  4. Sanity-check the code against
    references/good-bad-examples.md
    before handing it back: does it check
    _merge
    after a
    merge
    ,
    tsset
    before lags/regressions on time series, use
    egen
    (not
    gen
    ) for row-wise stats that might hit missing values, and avoid overwriting data without the user clearly wanting that? These are the mistakes that produce code which runs cleanly and gives a wrong answer — the most expensive kind of Stata bug because nothing flags it.
  5. If Python is in the picture, read
    references/python-interop.md
    and pick the right direction of travel — calling Python from a
    .do
    file, pulling Stata's active dataset into a Python session via
    sfi
    , or driving Stata from a Python script — rather than defaulting to the roundabout "write a CSV, read the CSV" pattern, which loses value labels/formats and adds a disk round-trip that direct interop skips.
  6. Explain the choices, briefly, next to the code — why this lag selection method, why
    ec
    form, why this diagnostic — not as a separate essay. A short comment or one sentence per non-obvious choice beats a wall of prose the user has to cross-reference against the code.
  1. 编写代码前先了解数据结构。 询问(或根据用户已经提供的信息推断)数据是横截面、面板数据还是时间序列;大致包含多少个观测值和变量;数据是否已经加载、是否给出了文件路径,或只是假设场景。即使使用相同的口语化需求(“X 是否影响 Y”),面板数据与单个时间序列的回归配方也截然不同。如果用户已经提供了足够信息,不要因此停滞——应推断合理的默认值并说明所做假设,而不是就本可合理推断的事项反复询问用户。
  2. 编写前检查是否已有匹配的 recipe 位于
    references/recipes/
    中(见下表),不要一开始就即兴编写。recipe 会编码一系列决策——滞后阶数选择方法、哪些诊断指标重要、某个估计量之后哪些事后估计命令确实可用——将其他命令的惯用模式泛化过来,很容易在这些细节上悄然出错。
  3. 编写代码,即使答案很短,也要将其组织成规范的
    .do
    文件:全新开始时在顶部使用
    clear all
    /
    set more off
    ;执行需要时间序列或面板设置的操作前使用
    tsset
    /
    xtset
    ;仅在命令本身无法直观表达原因时才添加注释(有用注释与噪声之间的区别参见
    references/good-bad-examples.md
    )。优先采用现代且有当前文档说明的命令形式,而不是仍能运行但已经过时的语法;如果发现用户代码片段使用了已弃用的形式,应明确指出。
  4. 交付前根据
    references/good-bad-examples.md
    检查代码
    :是否在
    merge
    后检查
    _merge
    ,是否在时间序列的滞后或回归前执行
    tsset
    ,是否使用
    egen
    (而不是
    gen
    )计算可能遇到缺失值的行级统计量,以及是否在用户未明确要求时避免覆盖数据。这些错误会生成能够顺利运行却给出错误答案的代码——这是代价最高的一类 Stata bug,因为任何程序都不会发出警告。
  5. 如果任务涉及 Python,请读取
    references/python-interop.md
    并选择正确的交互方向——从
    .do
    文件调用 Python、通过
    sfi
    将 Stata 当前加载的数据集传入 Python 会话,或从 Python 脚本驱动 Stata——而不是默认采用绕行的“写出 CSV,再读取 CSV”模式,因为这种方式会丢失值标签和格式,还会增加直接互操作本可省去的磁盘往返开销。
  6. 简要解释代码中的选择——为什么采用这种滞后选择方法、为什么使用
    ec
    形式、为什么进行该诊断——并将说明放在代码旁边,而不是另写长篇文章。对于每个不明显的选择,一句简短注释或一句话说明,胜过一整段需要用户来回对照代码的文字。

Reference files

参考文件

Load these on demand rather than holding all of it in context at once:
FileRead it when
references/good-bad-examples.md
Writing or reviewing any Stata code — the common footguns (merge checks, panel/time-series setup,
gen
vs
egen
, deprecated syntax, unsafe overwrites, output tables) with a bad/good pair and why for each.
references/python-interop.md
The task involves Python and Stata together, in either direction.
references/recipes/README.md
Writing a new recipe, or checking what a recipe file is expected to contain (including the
models/
/
tests/
split and naming pattern).
references/recipes/models/ardl.md
The task is ARDL / bounds-testing / long-run-short-run time-series modeling, or is close enough to it to use as a structural template.
references/recipes/models/regression-timeseries.md
A plain regression on a single time series — lag structure, Newey-West/HAC standard errors, autocorrelation diagnostics, avoiding spurious regression.
references/recipes/models/regression-panel.md
Fixed-effects vs. random-effects panel regression, the Hausman test, clustered standard errors.
references/recipes/tests/test-dickey-fuller.md
The mechanics of
dfuller
specifically — trend/drift specification, lag-length choice, testing level vs. first difference.
references/recipes/tests/test-unit-root.md
Deciding which unit-root test(s) to run (ADF/PP/DF-GLS vs. KPSS) and how to read them together, before committing to one test's mechanics.
references/recipes/tests/test-im-pesaran-shin.md
The panel counterpart to a unit-root test —
xtunitroot ips
, its heterogeneous alternative vs. Levin-Lin-Chu's homogeneous one, and cross-sectional-dependence caveats.
按需加载这些文件,不要一次性将其全部保留在上下文中:
文件何时读取
references/good-bad-examples.md
编写或审查任何 Stata 代码时——了解常见陷阱(
merge
检查、面板或时间序列设置、
gen
与
egen
的区别、已弃用的语法、不安全的覆盖操作、输出表格),其中包含错误与正确示例的对照及各自的原因。
references/python-interop.md
任务涉及 Python 与 Stata 协同工作时,无论交互方向是哪一种。
references/recipes/README.md
编写新 recipe,或检查 recipe 文件应包含的内容时(包括
models/
/
tests/
目录划分及命名模式)。
references/recipes/models/ardl.md
任务涉及 ARDL、边界检验或时间序列的长期—短期建模,或与这些场景足够接近、需要将其作为结构模板时。
references/recipes/models/regression-timeseries.md
对单个时间序列进行普通回归——包括滞后结构、Newey-West/HAC 标准误、自相关诊断,以及避免伪回归。
references/recipes/models/regression-panel.md
面板回归中的固定效应与随机效应、Hausman 检验,以及聚类标准误。
references/recipes/tests/test-dickey-fuller.md
dfuller
的具体使用机制——趋势或漂移项设定、滞后长度选择,以及检验水平值或一阶差分。
references/recipes/tests/test-unit-root.md
决定应运行哪些单位根检验(ADF/PP/DF-GLS 与 KPSS),以及如何结合解读这些检验,再确定某项检验的具体机制。
references/recipes/tests/test-im-pesaran-shin.md
单位根检验的面板对应版本——通过
xtunitroot ips
使用其异质备择假设,并与同质的 Levin-Lin-Chu 检验比较,同时了解横截面依赖方面的注意事项。

Recipe library

Recipe 库

Each recipe is a self-contained file in
references/recipes/models/
(a model class to estimate) or
references/recipes/tests/
(a diagnostic or statistical test) following the template in
references/recipes/README.md
: when to reach for this approach, the data shape it assumes, a full worked script against a real (not invented) dataset, the diagnostics that matter for that estimator specifically, and common mistakes particular to it. New recipes get added there over time without touching this file.
RecipeCovers
models/ardl
Autoregressive Distributed Lag modeling: lag-order selection, the
ec
error-correction form, the Pesaran/Shin/Smith bounds test for a long-run relationship, and the postestimation gotcha where
ardl
's own results don't carry the full
regress
postestimation menu.
models/regression-timeseries
Single-equation regression on one time series: lag structure, Newey-West/HAC standard errors, autocorrelation/heteroskedasticity diagnostics, and avoiding spurious regression between non-stationary series.
models/regression-panel
Fixed-effects vs. random-effects panel regression, the Hausman test to choose between them, and clustering standard errors by panel.
tests/test-dickey-fuller
Running
dfuller
correctly: trend/drift specification, lag-length choice, and testing the level vs. the first difference to determine order of integration.
tests/test-unit-root
Choosing among ADF/Phillips-Perron/DF-GLS/KPSS and reading them together — the decision layer above any single test's mechanics.
tests/test-im-pesaran-shin
Panel unit-root testing with
xtunitroot ips
: its heterogeneous alternative vs. Levin-Lin-Chu, and cross-sectional-dependence caveats.
If the user's task doesn't match an existing recipe, write the code directly from Stata/econometric first principles and the guidance above — don't force-fit a recipe that doesn't actually apply. If it's a workflow likely to recur (the kind of thing worth a named recipe), mention that to the user as a possible addition rather than silently improvising every time.
每个 recipe 都是
references/recipes/models/
(待估计的模型类别)或
references/recipes/tests/
(诊断或统计检验)中的独立文件,并遵循
references/recipes/README.md
中的模板:说明何时应采用该方法、假设的数据结构、针对真实(非虚构)数据集编写的完整示例脚本、该估计量特有的重要诊断指标,以及与该方法密切相关的常见错误。后续新增 recipe 时直接放入相应目录,无需修改本文件。
Recipe涵盖内容
models/ardl
自回归分布滞后(ARDL)建模:滞后阶数选择、
ec
误差修正形式、用于检验长期关系的 Pesaran/Shin/Smith 边界检验,以及事后估计中的常见陷阱——
ardl
自身的结果并不包含完整的
regress
事后估计菜单。
models/regression-timeseries
对单个时间序列进行单方程回归:滞后结构、Newey-West/HAC 标准误、自相关与异方差诊断,以及避免在非平稳序列之间进行伪回归。
models/regression-panel
固定效应与随机效应面板回归、用于二者选择的 Hausman 检验,以及按面板聚类的标准误。
tests/test-dickey-fuller
正确运行
dfuller
:趋势或漂移项设定、滞后长度选择,以及通过检验水平值与一阶差分来确定积分阶数。
tests/test-unit-root
在 ADF、Phillips-Perron、DF-GLS 和 KPSS 之间选择,并结合解读各项检验——这是位于任何单项检验机制之上的决策层。
tests/test-im-pesaran-shin
使用
xtunitroot ips
进行面板单位根检验:其异质备择假设与 Levin-Lin-Chu 的同质备择假设,以及横截面依赖方面的注意事项。
如果用户的任务与现有 recipe 不匹配,应根据 Stata 和计量经济学的基本原理以及上述指导直接编写代码,不要强行套用实际不适用的 recipe。如果某种工作流很可能反复出现,值得为其创建命名 recipe,则应向用户提及这一潜在补充项,而不是每次都默默即兴编写。

What this skill doesn't do

本 skill 不具备的功能

It doesn't run Stata itself — there's no Stata installation in this environment to execute code against, so treat every script as reviewed- for-correctness-by-reading, not executed-and-verified. Say so if the user seems to assume the code was actually run. It also doesn't fetch or fabricate the user's real data; recipes are demonstrated against Stata's own bundled example datasets (via
webuse
/
sysuse
) so the script is genuinely runnable as shown, with a clear note on which lines to swap for the user's actual
use "yourfile.dta", clear
.
它不会实际运行 Stata——当前环境中没有可用于执行代码的 Stata 安装,因此应将每个脚本视为“通过阅读审查其正确性”,而不是“已经执行并验证”。如果用户似乎认为代码确实已被运行,应明确说明这一点。本 skill 也不会获取或虚构用户的真实数据;recipe 使用 Stata 自带的示例数据集(通过
webuse
/
sysuse
)进行演示,因此脚本可以按所示内容真实运行,同时应清楚注明哪些行需要替换为用户实际使用的
use "yourfile.dta", clear
。

Development

开发

See
meta/MAINTAINERS.md
for layout and versioning notes, and
references/recipes/README.md
for the recipe-writing template. Not read as part of carrying out a user's Stata code-writing request — don't act on either while answering one.
有关目录结构和版本管理说明,请参阅
meta/MAINTAINERS.md
;有关 recipe 编写模板,请参阅
references/recipes/README.md
。执行用户的 Stata 代码编写请求时无需读取这两个文件,答复请求时也不要依据它们采取行动。