科研 Skill

RCT随机分配实现

rct-randomization

Implements RCT randomization: stratification, blocking, cluster randomization, and balance/misfit handling. Use when assigning or checking treatment-arm balance in an RCT.

发布者 Codar安全审核通过v0.1.0更新于 2026-11-18

SKILL.md

<skill_content>

Randomization must be reproducible, documented, and executed exactly once per study.

<mandatory_requirements>

</mandatory_requirements>

<thinking_process> When implementing randomization:

  1. Define eligible sample BEFORE randomization
  2. Choose randomization method (simple/permutation/stratified/cluster)
  3. Set random seed ONCE, document seed value
  4. Generate assignments (never re-run)
  5. Save assignments to persistent storage immediately
  6. Create backup of assignments
  7. Check balance (but don't re-randomize if unbalanced)
  8. Document any implementation deviations </thinking_process>

<implementation_pattern>

<code_template>

@app.cell
def randomize_trial():
    # Permutation randomization with stratification
    # CRITICAL: Set seed ONCE, save immediately, NEVER re-run

    import numpy as np
    import pandas as pd
    from datetime import datetime

    # Set seed ONCE at beginning (DOCUMENT THIS VALUE)
    RANDOM_SEED = 42
    np.random.seed(RANDOM_SEED)

    # Load eligible sample
    df = pd.read_csv("/mnt/data/eligible_units.csv")
    n = len(df)

    print(f"RANDOMIZATION PROTOCOL")
    print(f"Date: {datetime.now()}")
    print(f"Random seed: {RANDOM_SEED}")
    print(f"Sample size: {n}")

    # Stratified permutation randomization
    treatment_prop = 0.5
    strata_var = 'baseline_risk'  # Strong outcome predictor

    df['treatment'] = 0

    for stratum in df[strata_var].unique():
        strata_mask = df[strata_var] == stratum
        n_strata = strata_mask.sum()
        n_treat = int(n_strata * treatment_prop)

        # Permutation within stratum
        assignments = np.concatenate([
            np.ones(n_treat, dtype=int),
            np.zeros(n_strata - n_treat, dtype=int)
        ])
        np.random.shuffle(assignments)

        df.loc[strata_mask, 'treatment'] = assignments

    # CRITICAL: Save immediately (NEVER re-run randomization)
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    df.to_csv(f"/mnt/data/randomization_assignments_{timestamp}.csv", index=False)

    # Create backup
    df.to_csv(f"/mnt/data/randomization_assignments_BACKUP.csv", index=False)

    print(f"\nAssignments saved to:")
    print(f"  /mnt/data/randomization_assignments_{timestamp}.csv")
    print(f"Treatment: {df['treatment'].sum()} ({df['treatment'].mean():.1%})")
    print(f"Control: {(~df['treatment'].astype(bool)).sum()} ({(~df['treatment'].astype(bool)).mean():.1%})")

    return df,

</code_template>

</implementation_pattern>

import numpy as np
import pandas as pd

# CRITICAL: Set seed once
np.random.seed(123)

# Identify clusters
clusters = df['village_id'].unique()
n_clusters = len(clusters)
n_treat_clusters = int(n_clusters * 0.5)

print(f"CLUSTER RANDOMIZATION")
print(f"Total clusters: {n_clusters}")
print(f"Treatment clusters: {n_treat_clusters}")

# Permutation at cluster level
cluster_assignments = np.concatenate([
    np.ones(n_treat_clusters, dtype=int),
    np.zeros(n_clusters - n_treat_clusters, dtype=int)
])
np.random.shuffle(cluster_assignments)

# Map cluster assignments to individuals
cluster_map = dict(zip(clusters, cluster_assignments))
df['treatment'] = df['village_id'].map(cluster_map)

# Report
print(f"\nIndividuals in treatment: {df['treatment'].sum()}")
print(f"Individuals in control: {(~df['treatment'].astype(bool)).sum()}")

# Calculate design effect
avg_cluster_size = df.groupby('village_id').size().mean()
icc = 0.05  # Assumed intra-cluster correlation
design_effect = 1 + (avg_cluster_size - 1) * icc

print(f"\nAverage cluster size: {avg_cluster_size:.1f}")
print(f"Assumed ICC: {icc:.3f}")
print(f"Design effect: {design_effect:.2f}")
print(f"Effective sample size: {len(df)/design_effect:.0f}")

return df,
</code>
<lesson>
Cluster randomization trades power for validity. Design effect = 1 + (m-1) × ICC where m is cluster size and ICC is intra-cluster correlation. Higher ICC or larger clusters → larger design effect → greater power loss. But necessary when spillovers would contaminate individual randomization.
</lesson>
</example>

</examples>

<common_mistakes>

<mistake severity="critical">
  <what>Re-running randomization to get "better" balance</what>
  <consequence>Invalidates randomization inference, creates selection bias, corrupts p-values</consequence>
  <prevention>Run ONCE, save immediately, accept whatever balance you get. If deeply concerned about imbalance, use stratification or re-randomization with proper adjustment (difficult)</prevention>
</mistake>

<mistake severity="critical">
  <what>Setting random seed multiple times in randomization code</what>
  <consequence>Compromises randomness, makes sequence non-random, creates reproducibility issues</consequence>
  <prevention>Set seed ONCE at very beginning, never call np.random.seed() again in randomization code</prevention>
</mistake>

<mistake severity="high">
  <what>Using simple randomization when sample size is known</what>
  <consequence>Sampling variation creates unbalanced groups, power loss, difficult to explain discrepancies</consequence>
  <prevention>Use permutation randomization (shuffle fixed number of 1s and 0s) when N is known</prevention>
</mistake>

<mistake severity="high">
  <what>Not stratifying when strong predictors available</what>
  <consequence>Unnecessary power loss, imbalanced groups on important characteristics</consequence>
  <prevention>Stratify on 1-3 strong outcome predictors to improve precision</prevention>
</mistake>

<mistake severity="medium">
  <what>Not documenting randomization seed and procedure</what>
  <consequence>Non-reproducible, inability to verify implementation, suspicion of manipulation</consequence>
  <prevention>Document seed, date, time, code version, sample characteristics in protocol</prevention>
</mistake>

</common_mistakes>

<interpretation_guide>

<balance_check_interpretation>
After randomization (but NOT as reason to re-randomize):
- Normalized diff |d| < 0.1: Excellent balance
- 0.1 ≤ |d| < 0.25: Acceptable balance
- |d| ≥ 0.25: Imbalanced on this variable (include as control, but don't re-randomize)
- Joint F-test p > 0.10: Overall balance adequate
- Joint F-test p < 0.05: Some imbalance (expected 5% of time by chance)

Never re-randomize based on balance. If you must, use proper re-randomization protocol with inference adjustment (complex).
</balance_check_interpretation>

<when_to_use_each_method>
- **Simple randomization**: Sequential enrollment, N unknown at randomization
- **Permutation**: N known, want exact allocation ratio
- **Stratified**: Strong baseline predictors available, want precision gains
- **Cluster**: Spillovers likely within clusters (social, geographic)
- **Re-randomization**: Advanced method requiring special inference (seek expert help)
</when_to_use_each_method>

</interpretation_guide>

<references>
<paper>Bruhn, M., & McKenzie, D. (2009). In pursuit of balance: Randomization in practice in development field experiments. American Economic Journal: Applied Economics, 1(4), 200-232.</paper>
<paper>Athey, S., & Imbens, G. W. (2017). The econometrics of randomized experiments. Handbook of Economic Field Experiments, 1, 73-140.</paper>
<paper>Duflo, E., Glennerster, R., & Kremer, M. (2007). Using randomization in development economics research. Handbook of Development Economics, 4, 3895-3962.</paper>
</references>

</skill_content>

示例提示词

帮我设计分层区组随机化方案,并检查各组基线平衡性。

所属插件

社会学、政治学与城市研究面向社会科学研究:问卷与访谈设计、质性编码、文本即数据、社会网络、ABM、比较政治与选举数据、RCT 与 DID、城市与区域分析。

安装该插件会一并获得同领域的 58 个 Skill / MCP。

同插件的其他科研 Skill

常见问题

RCT随机分配实现能做什么?

Implements RCT randomization: stratification, blocking, cluster randomization, and balance/misfit handling. Use when assigning or checking treatment-arm balance in an RCT。

如何安装RCT随机分配实现?

在科搭 Codar 左侧点击「插件」,在「组件」页签搜索「RCT随机分配实现」并安装;在 Claude Code 或 Codex 中可以发送本页的安装提示词,或下载 Skill 包后解压到 ~/.claude/skills 或 ~/.codex/skills。

RCT随机分配实现需要哪些权限?

该科研 Skill没有声明额外权限;执行具体操作时仍由 Agent 按自身规则请求确认。

RCT随机分配实现经过官方审核吗?

是。SpeedSkills 收录的每个版本都会记录内容摘要与权限声明,当前版本已通过安全审核。