skills/66-zheng-siyao-empirical-research-skills/codebook-pass/SKILL.md
调查数据清洗Skill。处理调查数据(CGSS/CHIP/CSS等)时的标准化清洗流程,包括缺失值处理、变量编码统一、数据异常值检测。触发词:数据清洗/调查数据/codebook/数据清洗流程/问卷数据处理
npx skillsauth add brycewang-stanford/Awesome-Agent-Skills-for-Empirical-Research codebook-passInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
本 Skill 是处理中国综合社会调查(CGSS)、中国家庭收入调查(CHIP)、中国社会科学调查(CSS)等大型调查数据的标准化清洗流程。确保从 codebook 到可用面板数据的每一步都可审计、可复现。
调查数据清洗的核心原则:原始数据只读不修改,所有转换操作记录在 processed/ 文件夹中,且必须更新 provenance.json。
三个关键规则:
processed/ 文件夹中进行原始数据 → 导入检查 → 变量命名规范化 → 缺失值处理 → 异常值处理 → 编码统一 → 合并面板 → 输出报告
必须执行:
import pandas as pd
import numpy as np
# 读取原始数据
df = pd.read_stata("data/raw/cgss2021.dta")
# 基础检查
print(f"样本量: {len(df)}")
print(f"变量数: {len(df.columns)}")
print(f"\n缺失值概况:")
print(df.isnull().sum()[df.isnull().sum() > 0])
检查项:
| 检查项 | 预期结果 | |--------|----------| | 样本量 | 与 codebook 描述一致 | | 变量数 | 与 codebook 描述一致 | | 极端缺失变量 | 缺失率 >50% 的变量需标注 | | 数据类型 | 数值型/字符型与 codebook 一致 |
统一命名规则(建议采用):
{数据库缩写}_{年份}_{变量原名}
例如:cgss_2021_income, chip_2018_urban
变量编码记录(必须写入 provenance.json):
{
"variable": "cgss_2021_urban",
"description": "城乡户籍类型",
"original_codes": {"1": "城市", "2": "农村", "9": "未知"},
"transformed_codes": {"1": "urban", "2": "rural"},
"missing_handling": "9 → NaN"
}
必须明确处理的缺失类型:
| 缺失类型 | 调查数据常见编码 | 处理方式 |
|----------|-----------------|----------|
| 真实缺失 | . / NA | 保持 NaN |
| 不适用 | 97/98/99 | 保持 NaN,不填0 |
| 拒绝回答 | 97 | 保持 NaN |
| 不知道 | 98 | 保持 NaN |
| 数据缺失 | 99 | 保持 NaN |
⚠️ 常见错误:
97/98/99 填为 0(改变了变量的均值)正确做法:
# 识别调查数据中的特殊编码
missing_codes = [97, 98, 99, 999, 9999]
for var in categorical_vars:
df[var] = df[var].replace(missing_codes, np.nan)
常用方法:
# 描述统计 + 分布检查
print(df[numeric_vars].describe(percentiles=[.01, .05, .25, .50, .75, .95, .99]))
# 极端值检测(IQR方法)
def detect_outliers_iqr(series, factor=3):
Q1 = series.quantile(0.25)
Q3 = series.quantile(0.75)
IQR = Q3 - Q1
lower = Q1 - factor * IQR
upper = Q3 + factor * IQR
return (series < lower) | (series > upper)
outliers = detect_outliers_iqr(df['income'])
print(f"极端值数量: {outliers.sum()}")
处理方式:
| 异常类型 | 处理方式 | |----------|----------| | 数据录入错误(明显不合理值) | 修正或标记为缺失 | | 合理极端值 | 保持,报告中位数替代均值 | | 离群点( winsorize) | 双边 winsorize(1%和99%分位) |
Winsorize 示例:
from scipy.stats import mstats
for var in ['income', 'education_years']:
df[var] = mstats.winsorize(df[var], limits=[0.01, 0.01])
纵向合并(多年份 CGSS):
# 确保变量名一致
cgss_2017 = pd.read_stata("data/raw/cgss2017.dta")
cgss_2021 = pd.read_stata("data/raw/cgss2021.dta")
# 重命名统一
cgss_2017 = cgss_2017.rename(columns={
"a2017_income": "income",
"a2017_urban": "urban"
})
cgss_2021 = cgss_2021.rename(columns={
"a2021_income": "income",
"a2021_urban": "urban"
})
# 合并
df_combined = pd.concat([cgss_2017, cgss_2021], ignore_index=True)
df_combined['year'] = 2017 # 添加年份变量
横向合并(CHIP + CGSS):
# 统一变量定义和编码
# CHIP: income_2018; CGSS: income_2021
# 需标注:不同数据库收入定义可能不同
长宽格式转换:
# 宽 → 长
df_panel = df.melt(
id_vars=['id', 'year'],
value_vars=['income', 'education', 'employment'],
var_name='variable',
value_name='value'
)
必须输出:
data/processed/cleaning_report.md)data/processed/variable_definition.csv)清洗报告模板:
## 数据清洗报告
**原始数据**:CGSS 2021 (N=10,000)
**清洗后数据**:processed/cgss_2021_clean.dta (N=9,847)
**清洗时间**:[日期]
### 样本变动
| 处理类型 | 样本数 | 原因 |
|----------|--------|------|
| 原始样本 | 10,000 | |
| 删除重复记录 | -15 | ID重复 |
| 核心变量缺失 | -138 | income/education 缺失 |
| 清洗后 | 9,847 | |
### 变量处理记录
| 变量名 | 原始编码 | 处理方式 | 备注 |
|--------|----------|----------|------|
| income | 99999→NaN | 特殊编码替换 | 拒绝回答 |
| urban | 1/2 | 保持 | 城市=1,农村=2 |
在 Claude Code 对话窗口输入:
/codebook-pass
或完整 Prompt:
按调查数据清洗Skill处理本项目的CGSS/CHIP数据:原始数据导入检查→变量命名规范化→缺失值处理→异常值检测→编码统一→构建面板→输出清洗报告和provenance.json
processed/ 文件夹中进行tools
Recommend AND run open-source AI tools, agents, Claude Code / Codex skills, and MCP servers for any stage of a literature review — searching, reading, extracting, synthesizing, screening, citation-checking, and paper writing. Use when the user asks "what tool should I use to..." OR "install/run/use <tool> to ..." for research/lit-review work: automating a survey or related-work section, PDF→Markdown extraction for LLMs (MinerU/marker/docling), PRISMA / systematic review (ASReview), citation-backed Q&A over PDFs (PaperQA2), wiring papers into Claude/Cursor via MCP (arxiv/paper-search/zotero servers), or chatting with a Zotero library. Ships a launcher (scripts/litrun.py) that installs each tool in an isolated venv and runs it. Curated catalog of 70+ vetted projects. 支持中英文(用于「文献综述工具选型」与「一键安装/运行」)。
development
Route empirical-research requests through the Auto-Empirical Research Skills catalog when this whole repository is installed as one skill in Codex, CodeBuddy, Claude Code, or another IDE. Use to choose and load the right vendored AERS skill for causal inference, econometrics, replication, data acquisition, manuscript writing, peer review and referee responses, citation checking, de-AIGC editing, or full empirical-paper workflows without reading the entire repository at once.
documentation
Use when the project collects primary data or runs a field, lab, or survey experiment, before the intervention begins — write the pre-analysis plan, size the sample from a power calculation, and register with the AEA RCT Registry. Apply after the design is chosen in aer-identification and before any outcome data are seen.
tools
Guide economists to authoritative data sources with explicit, confirmed data specifications before retrieval; interfaces with Playwright MCP to navigate portals and extract real data, not articles about data.