stata-recipes
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseStata command & recipe writer
Stata 命令与 recipe 编写器
This skill turns "here's what I'm trying to do" into working Stata code —
a full 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.
.doStata rewards precision more than most languages: a command that runs
without error can still silently produce the wrong answer (unset panel
structure, a nobody checked, where 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.
mergegenegen本 skill 将“我想完成以下任务”转化为可工作的 Stata 代码——任务包含多个步骤时生成完整的 文件,否则生成简短命令序列。输出始终是用户可直接粘贴到 Stata 中运行的代码,而不是说明用户还需要编写什么。
.do与大多数语言相比,Stata 更强调精确性:一个没有报错的命令仍可能悄无声息地产生错误结果(未设置面板数据结构、未经检查的 、本应使用 处理缺失值却使用了 )。因此,本 skill 的工作不只是“让代码运行”——而是“让它运行并且结果正确”,并在看似合理的替代方案实际上有误时说明原因。
mergeegengenWorkflow
工作流程
-
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.
-
Check whether a matching recipe already exists in(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.
references/recipes/ -
Write the code, structured as a properfile even for a short answer:
.do/clear allat the top when starting fresh,set more off/tssetbefore any time-series or panel operation that needs it, comments only where the why isn't obvious from the command itself (seextsetfor 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.references/good-bad-examples.md -
Sanity-check the code againstbefore handing it back: does it check
references/good-bad-examples.mdafter a_merge,mergebefore lags/regressions on time series, usetsset(notegen) 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.gen -
If Python is in the picture, readand pick the right direction of travel — calling Python from a
references/python-interop.mdfile, pulling Stata's active dataset into a Python session via.do, 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.sfi -
Explain the choices, briefly, next to the code — why this lag selection method, whyform, 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.
ec
-
编写代码前先了解数据结构。 询问(或根据用户已经提供的信息推断)数据是横截面、面板数据还是时间序列;大致包含多少个观测值和变量;数据是否已经加载、是否给出了文件路径,或只是假设场景。即使使用相同的口语化需求(“X 是否影响 Y”),面板数据与单个时间序列的回归配方也截然不同。如果用户已经提供了足够信息,不要因此停滞——应推断合理的默认值并说明所做假设,而不是就本可合理推断的事项反复询问用户。
-
编写前检查是否已有匹配的 recipe 位于中(见下表),不要一开始就即兴编写。recipe 会编码一系列决策——滞后阶数选择方法、哪些诊断指标重要、某个估计量之后哪些事后估计命令确实可用——将其他命令的惯用模式泛化过来,很容易在这些细节上悄然出错。
references/recipes/ -
编写代码,即使答案很短,也要将其组织成规范的文件:全新开始时在顶部使用
.do/clear all;执行需要时间序列或面板设置的操作前使用set more off/tsset;仅在命令本身无法直观表达原因时才添加注释(有用注释与噪声之间的区别参见xtset)。优先采用现代且有当前文档说明的命令形式,而不是仍能运行但已经过时的语法;如果发现用户代码片段使用了已弃用的形式,应明确指出。references/good-bad-examples.md -
交付前根据检查代码:是否在
references/good-bad-examples.md后检查merge,是否在时间序列的滞后或回归前执行_merge,是否使用tsset(而不是egen)计算可能遇到缺失值的行级统计量,以及是否在用户未明确要求时避免覆盖数据。这些错误会生成能够顺利运行却给出错误答案的代码——这是代价最高的一类 Stata bug,因为任何程序都不会发出警告。gen -
如果任务涉及 Python,请读取并选择正确的交互方向——从
references/python-interop.md文件调用 Python、通过.do将 Stata 当前加载的数据集传入 Python 会话,或从 Python 脚本驱动 Stata——而不是默认采用绕行的“写出 CSV,再读取 CSV”模式,因为这种方式会丢失值标签和格式,还会增加直接互操作本可省去的磁盘往返开销。sfi -
简要解释代码中的选择——为什么采用这种滞后选择方法、为什么使用形式、为什么进行该诊断——并将说明放在代码旁边,而不是另写长篇文章。对于每个不明显的选择,一句简短注释或一句话说明,胜过一整段需要用户来回对照代码的文字。
ec
Reference files
参考文件
Load these on demand rather than holding all of it in context at once:
| File | Read it when |
|---|---|
| Writing or reviewing any Stata code — the common footguns (merge checks, panel/time-series setup, |
| The task involves Python and Stata together, in either direction. |
| Writing a new recipe, or checking what a recipe file is expected to contain (including the |
| 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. |
| A plain regression on a single time series — lag structure, Newey-West/HAC standard errors, autocorrelation diagnostics, avoiding spurious regression. |
| Fixed-effects vs. random-effects panel regression, the Hausman test, clustered standard errors. |
| The mechanics of |
| 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. |
| The panel counterpart to a unit-root test — |
按需加载这些文件,不要一次性将其全部保留在上下文中:
| 文件 | 何时读取 |
|---|---|
| 编写或审查任何 Stata 代码时——了解常见陷阱( |
| 任务涉及 Python 与 Stata 协同工作时,无论交互方向是哪一种。 |
| 编写新 recipe,或检查 recipe 文件应包含的内容时(包括 |
| 任务涉及 ARDL、边界检验或时间序列的长期—短期建模,或与这些场景足够接近、需要将其作为结构模板时。 |
| 对单个时间序列进行普通回归——包括滞后结构、Newey-West/HAC 标准误、自相关诊断,以及避免伪回归。 |
| 面板回归中的固定效应与随机效应、Hausman 检验,以及聚类标准误。 |
| |
| 决定应运行哪些单位根检验(ADF/PP/DF-GLS 与 KPSS),以及如何结合解读这些检验,再确定某项检验的具体机制。 |
| 单位根检验的面板对应版本——通过 |
Recipe library
Recipe 库
Each recipe is a self-contained file in (a
model class to estimate) or (a diagnostic or
statistical test) following the template in
: 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.
references/recipes/models/references/recipes/tests/references/recipes/README.md| Recipe | Covers |
|---|---|
| Autoregressive Distributed Lag modeling: lag-order selection, the |
| 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. |
| Fixed-effects vs. random-effects panel regression, the Hausman test to choose between them, and clustering standard errors by panel. |
| Running |
| Choosing among ADF/Phillips-Perron/DF-GLS/KPSS and reading them together — the decision layer above any single test's mechanics. |
| Panel unit-root testing with |
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 都是 (待估计的模型类别)或 (诊断或统计检验)中的独立文件,并遵循 中的模板:说明何时应采用该方法、假设的数据结构、针对真实(非虚构)数据集编写的完整示例脚本、该估计量特有的重要诊断指标,以及与该方法密切相关的常见错误。后续新增 recipe 时直接放入相应目录,无需修改本文件。
references/recipes/models/references/recipes/tests/references/recipes/README.md| Recipe | 涵盖内容 |
|---|---|
| 自回归分布滞后(ARDL)建模:滞后阶数选择、 |
| 对单个时间序列进行单方程回归:滞后结构、Newey-West/HAC 标准误、自相关与异方差诊断,以及避免在非平稳序列之间进行伪回归。 |
| 固定效应与随机效应面板回归、用于二者选择的 Hausman 检验,以及按面板聚类的标准误。 |
| 正确运行 |
| 在 ADF、Phillips-Perron、DF-GLS 和 KPSS 之间选择,并结合解读各项检验——这是位于任何单项检验机制之上的决策层。 |
| 使用 |
如果用户的任务与现有 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 /) so the script is
genuinely runnable as shown, with a clear note on which lines to swap for
the user's actual .
webusesysuseuse "yourfile.dta", clear它不会实际运行 Stata——当前环境中没有可用于执行代码的 Stata 安装,因此应将每个脚本视为“通过阅读审查其正确性”,而不是“已经执行并验证”。如果用户似乎认为代码确实已被运行,应明确说明这一点。本 skill 也不会获取或虚构用户的真实数据;recipe 使用 Stata 自带的示例数据集(通过 /)进行演示,因此脚本可以按所示内容真实运行,同时应清楚注明哪些行需要替换为用户实际使用的 。
webusesysuseuse "yourfile.dta", clearDevelopment
开发
See for layout and versioning notes, and
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.mdreferences/recipes/README.md有关目录结构和版本管理说明,请参阅 ;有关 recipe 编写模板,请参阅 。执行用户的 Stata 代码编写请求时无需读取这两个文件,答复请求时也不要依据它们采取行动。
meta/MAINTAINERS.mdreferences/recipes/README.md