科研 Skill
随机实验数据分析
rct-data-analysis
Analyzes RCT data for ITT, LATE/TOT, compliance, attrition, balance checks, and heterogeneous treatment effects. Use when analyzing results from a completed RCT.
SKILL.md
<skill_content>
Analysis must preserve the integrity of randomization through proper inference and transparent reporting.
<mandatory_requirements>
</mandatory_requirements>
<thinking_process> When analyzing RCT data:
- Check data quality and completeness
- Verify balance on baseline covariates
- Calculate and report attrition rates (overall and differential)
- Estimate ITT (primary analysis)
- Estimate LATE if non-compliance exists (secondary)
- Test for heterogeneous effects (pre-specified subgroups)
- Conduct robustness checks (alternative specifications)
- Report all results transparently (including null findings) </thinking_process>
<implementation_pattern>
<code_template>
@app.cell
def itt_analysis_rct(df):
# Intention-to-treat analysis: Effect of assignment to treatment
# This is the policy-relevant parameter even with imperfect compliance
import statsmodels.formula.api as smf
import pandas as pd
# Verify data structure
required = {'outcome', 'treatment', 'unit_id'}
assert required.issubset(df.columns), f"Missing columns: {required - set(df.columns)}"
# CRITICAL: Use cluster-robust or HC3-robust standard errors
if 'cluster_id' in df.columns:
# Cluster-robust (for cluster-randomized trials)
model = smf.ols("outcome ~ treatment", data=df).fit(
cov_type='cluster',
cov_kwds={'groups': df['cluster_id']}
)
se_type = "Cluster-robust"
else:
# HC3-robust (for individual randomization)
model = smf.ols("outcome ~ treatment", data=df).fit(cov_type='HC3')
se_type = "HC3-robust"
# Extract results
itt_effect = model.params['treatment']
itt_se = model.bse['treatment']
itt_pval = model.pvalues['treatment']
ci_low, ci_high = model.conf_int().loc['treatment']
# Context for interpretation
control_mean = df[df['treatment'] == 0]['outcome'].mean()
control_sd = df[df['treatment'] == 0]['outcome'].std()
effect_size = itt_effect / control_sd
print("INTENTION-TO-TREAT (ITT) ANALYSIS")
print("=" * 60)
print(f"Outcome: {df.columns[df.columns.get_loc('outcome')]}")
print(f"N: {len(df)} ({df['treatment'].sum()} treatment, {(~df['treatment'].astype(bool)).sum()} control)")
print(f"Standard errors: {se_type}")
print(f"\nITT Effect: {itt_effect:.4f}")
print(f"Std. Error: {itt_se:.4f}")
print(f"95% CI: [{ci_low:.4f}, {ci_high:.4f}]")
print(f"p-value: {itt_pval:.4f}")
print(f"\nControl mean: {control_mean:.4f}")
print(f"Effect size: {effect_size:.3f} SD")
print(f"Relative effect: {100*itt_effect/control_mean:.1f}%")
return model,
</code_template>
</implementation_pattern>
import pandas as pd
import numpy as np
from scipy import stats
# Baseline covariates (measured before randomization)
baseline_vars = ['age', 'education', 'income', 'baseline_outcome']
results = []
for var in baseline_vars:
# Means by treatment group
control_mean = df[df['treatment'] == 0][var].mean()
treat_mean = df[df['treatment'] == 1][var].mean()
# Normalized difference (Imbens & Rubin 2015)
control_var = df[df['treatment'] == 0][var].var()
treat_var = df[df['treatment'] == 1][var].var()
norm_diff = (treat_mean - control_mean) / np.sqrt((control_var + treat_var) / 2)
# T-test
t_stat, p_val = stats.ttest_ind(
df[df['treatment'] == 0][var].dropna(),
df[df['treatment'] == 1][var].dropna()
)
results.append({
'Variable': var,
'Control': f"{control_mean:.3f}",
'Treatment': f"{treat_mean:.3f}",
'Norm Diff': f"{norm_diff:.3f}",
'p-value': f"{p_val:.3f}"
})
balance_table = pd.DataFrame(results)
print("BALANCE TABLE")
print("=" * 60)
print(balance_table.to_string(index=False))
# Joint F-test
from statsmodels.formula.api import ols
formula = "treatment ~ " + " + ".join(baseline_vars)
joint_model = ols(formula, data=df).fit()
print(f"\nJoint F-test: F={joint_model.fvalue:.3f}, p={joint_model.f_pvalue:.4f}")
# Flag concerns
large_diffs = sum(abs(float(r['Norm Diff'])) > 0.25 for r in results)
if large_diffs > 0:
print(f"\nNote: {large_diffs} variables have |norm diff| > 0.25")
print("Consider including these as controls in regression")
return balance_table,
</code>
<lesson>
Balance checks use baseline (pre-randomization) covariates only. Large imbalances (|norm diff| > 0.25) don't invalidate randomization but suggest including those variables as controls to improve precision. Never use post-treatment variables for balance checks.
</lesson>
</example>
</examples>
<common_mistakes>
<mistake severity="critical">
<what>Not using cluster-robust or heteroskedasticity-robust standard errors</what>
<consequence>Type I error rates can be 45% instead of 5%, leading to massive false positive rates</consequence>
<prevention>ALWAYS use cov_type='cluster' or cov_type='HC3'. Never use default SEs.</prevention>
</mistake>
<mistake severity="critical">
<what>Not reporting ITT when non-compliance exists</what>
<consequence>Selection bias from analyzing compliers only, loss of causal interpretation</consequence>
<prevention>ALWAYS report ITT as primary result. LATE/TOT are secondary sensitivity analyses.</prevention>
</mistake>
<mistake severity="high">
<what>Ignoring attrition or not testing for differential attrition</what>
<consequence>Selection bias can completely invalidate results if attrition is differential</consequence>
<prevention>Report attrition by arm, test differential attrition, use Lee bounds if substantial</prevention>
</mistake>
<mistake severity="high">
<what>P-hacking through subgroups without adjustment</what>
<consequence>Finding "significant" effects that are just Type I errors from multiple testing</consequence>
<prevention>Pre-specify subgroups in PAP, use Bonferroni/Holm adjustment, report all tests</prevention>
</mistake>
<mistake severity="medium">
<what>Using baseline covariates that were measured post-randomization</what>
<consequence>Conditioning on post-treatment variables creates bias (bad controls)</consequence>
<prevention>Only use covariates measured before randomization in balance checks and controls</prevention>
</mistake>
</common_mistakes>
<interpretation_guide>
<reporting_standards>
Minimum reporting for RCT results:
- Sample size (by treatment arm)
- Balance table on baseline characteristics
- Attrition rates (overall and by arm)
- ITT estimates with cluster-robust/HC3 SEs
- 95% confidence intervals
- Control group mean (for context)
- Effect size in SD units
- P-values (exact, not <0.05)
- Number of clusters if cluster-randomized
</reporting_standards>
<red_flags>
Results are questionable if:
- No balance table provided
- Classical (non-robust) standard errors used
- ITT not reported for study with non-compliance
- Attrition >20% without bounding analysis
- Multiple outcomes tested without adjustment
- Post-treatment covariates included as controls
- Results only reported for "compliers" without ITT
</red_flags>
</interpretation_guide>
<references>
<paper>Angrist, J. D., & Pischke, J. S. (2009). Mostly Harmless Econometrics: An Empiricist's Companion. Princeton University Press.</paper>
<paper>Bertrand, M., Duflo, E., & Mullainathan, S. (2004). How much should we trust differences-in-differences estimates? Quarterly Journal of Economics, 119(1), 249-275.</paper>
<paper>Lee, D. S. (2009). Training, wages, and sample selection: Estimating sharp bounds on treatment effects. Review of Economic Studies, 76(3), 1071-1102.</paper>
<paper>Imbens, G. W., & Rubin, D. B. (2015). Causal Inference in Statistics, Social, and Biomedical Sciences. Cambridge University Press.</paper>
</references>
</skill_content>
示例提示词
帮我分析这项RCT的依从性问题,估计ITT和LATE处理效应。
所属插件
安装该插件会一并获得同领域的 58 个 Skill / MCP。
同插件的其他科研 Skill
- Mesa社会主体建模Builds Mesa agent-based models of opinion dynamics, segregation, social contagion, and network diffusion. Use when simulating emergent social behavior from agent rules.
- Mesa3主体建模引擎Builds agent-based models with the Mesa 3 AgentSet API, cell spaces, and batch_run sweeps for segregation, diffusion, and cooperation. Use for a Mesa 3.x ABM build or debug.
- 观察数据因果推断Estimates causal effects with DiD, IV, RDD, panel fixed effects, and propensity scores via statsmodels, linearmodels, DoWhy, and EconML. Use for observational or quasi-experimental causal estimation.
- 效应量元分析Pools effect sizes across studies with fixed/random-effects models, heterogeneity stats, forest/funnel plots, and publication-bias tests. Use for a systematic review’s quantitative synthesis.
- 缺失数据插补处理Handles missing data by naming the MCAR/MAR/MNAR mechanism, then applying multiple imputation (MICE) or FIML instead of single imputation. Use when a dataset has missing values to treat.
- 多层线性混合模型Fits mixed-effects/multilevel models (random slopes, ICC, GLMMs) for nested or longitudinal data via statsmodels, bambi, or R lme4/brms. Use for grouped data with within- vs between-cluster questions.
- 定性比较分析QCARuns crisp-, multi-value, and fuzzy-set Qualitative Comparative Analysis via R’s QCA package. Use for configurational or necessary/sufficient-condition research.
- 质性编码与主题分析Builds codebooks and computes intercoder reliability (Krippendorff’s alpha, kappa) for thematic or content coding, including human-vs-LLM double coding. Use when coding text or reporting reliability.
- 社会网络结构分析Computes network centrality, community detection, and ERGM inferential models matched to the research question via networkx/igraph. Use for social-network analysis beyond basic visualization.
- 社科研究设计把关Routes a study to its research design (experiment, quasi-experiment, observational, qualitative) and pins the identifying assumption first. Use when choosing a study design.
- 社科推论结论审核Audits inferential claims against the design before publication: unwarranted causal language, p-value/CI misreadings, and multiple-comparison issues. Use when checking a results section.
- 量表测量质量把关Checks that a scale’s reliability, validity, and measurement invariance are established before trusting or comparing it across groups. Use when adopting or reporting a survey instrument.
- 社科研究流程总控Coordinates a study through design, measurement, sampling, analysis, and inference gates with a shared Design Passport. Use for an end-to-end methodology review of a whole study.
- 质性研究可信度把关Gates qualitative trustworthiness before write-up: positionality, credibility, transferability, dependability, confirmability per Lincoln and Guba. Use before writing ethnographic findings.
- 抽样方法与样本量把关Checks that the sampling frame, method, and size match the inference paradigm before data collection. Use when planning recruitment or judging sample generalization.
- 复杂抽样调查分析Analyzes complex-sample survey data with design-based inference: weights, strata, PSUs, and calibration via Python svy or R survey/srvyr. Use for weighted surveys like GSS, ANES, or DHS.
- 文本数据定量分析Analyzes text as social-science data with topic modeling, embeddings, and validated dictionary or classification methods. Use for computational text analysis beyond a keyword count.
- 政策议题框架分析Runs anthropological framing and metaphor analysis on contested-policy documents, producing actor-attributed CSVs for NVivo import. Use for policy-framing or metaphor-analysis coding.
- 跨国比较政治分析Merges QoG, Polity5, and Freedom House data and runs cross-sectional OLS with fixed effects and regime classification. Use for comparative political-institution research.
- 计算社会学方法Covers social-media data collection, bot detection, network homophily, echo-chamber analysis, and conjoint survey experiments. Use for large-scale digital-trace sociology research.
- 冲突事件数据分析Downloads ACLED/UCDP conflict data and runs spatio-temporal clustering, conflict-onset logit panels, and fatality estimation. Use for quantitative armed-conflict research.
- 联合分析调查实验Designs conjoint survey experiments and estimates AMCEs via OLS, marginal means, and respondent-level heterogeneity. Use when designing or analyzing a choice-based conjoint experiment.
- 统计方法教学规范Defines the baseline statistical teaching and reproducibility standards shared across this analysis skill family. Use as the methodology reference other skills in the family build on.
- 人口统计方法分析Computes life tables, Leslie-matrix population projections, Lee-Carter mortality forecasts, and Kitagawa decomposition. Use for formal demographic analysis of mortality or population structure.
常见问题
随机实验数据分析能做什么?
Analyzes RCT data for ITT, LATE/TOT, compliance, attrition, balance checks, and heterogeneous treatment effects. Use when analyzing results from a completed RCT。
如何安装随机实验数据分析?
在科搭 Codar 左侧点击「插件」,在「组件」页签搜索「随机实验数据分析」并安装;在 Claude Code 或 Codex 中可以发送本页的安装提示词,或下载 Skill 包后解压到 ~/.claude/skills 或 ~/.codex/skills。
随机实验数据分析需要哪些权限?
该科研 Skill没有声明额外权限;执行具体操作时仍由 Agent 按自身规则请求确认。
随机实验数据分析经过官方审核吗?
是。SpeedSkills 收录的每个版本都会记录内容摘要与权限声明,当前版本已通过安全审核。