科研 Skill

RCT统计检定力计算

rct-power-calculations

Calculates statistical power, minimum detectable effects, and sample sizes for RCTs, including clustering effects. Use when sizing an RCT or running a power analysis.

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

SKILL.md

<skill_content>

Power = 80% is conventional. Higher power (90%) is better when study is expensive or one-shot.

<mandatory_requirements>

</mandatory_requirements>

<thinking_process> When conducting power analysis:

  1. Specify primary outcome and effect size (MDE or expected effect)
  2. Choose significance level (α=0.05 typical) and desired power (0.80-0.90)
  3. Calculate base sample size for simple design
  4. Apply design effect if cluster randomization (multiply by 1+(m-1)×ICC)
  5. Inflate for expected non-compliance (divide by compliance²)
  6. Inflate for expected attrition (divide by (1-attrition_rate))
  7. Adjust for multiple testing if applicable
  8. Check feasibility; if infeasible, reduce scope or accept larger MDE </thinking_process>

<implementation_pattern>

<code_template>

@app.cell
def comprehensive_power_calculation():
    # Full power calculation with all real-world adjustments
    # Demonstrates compounding inflation factors for RCT sample size

    import numpy as np
    from scipy.stats import norm

    # Parameters
    alpha = 0.05
    power = 0.80
    mde = 0.25  # Effect size in SD units (realistic for social programs)
    p = 0.50  # Treatment allocation

    # Base calculation
    z_alpha = norm.ppf(1 - alpha/2)
    z_power = norm.ppf(power)
    n_base = ((z_alpha + z_power)**2) / (p * (1-p) * mde**2)

    print(f"COMPREHENSIVE POWER ANALYSIS")
    print(f"=" * 60)
    print(f"Parameters: α={alpha}, power={power}, MDE={mde} SD")
    print(f"\n1. Base sample size: {int(np.ceil(n_base))}")

    # Adjustment 1: Clustering
    icc = 0.05
    cluster_size = 30
    design_effect = 1 + (cluster_size - 1) * icc
    n_after_clustering = n_base * design_effect

    print(f"\n2. Clustering adjustment:")
    print(f"   ICC={icc}, avg cluster size={cluster_size}")
    print(f"   Design effect: {design_effect:.2f}")
    print(f"   After clustering: {int(np.ceil(n_after_clustering))}")

    # Adjustment 2: Non-compliance
    compliance_rate = 0.70
    n_after_compliance = n_after_clustering / (compliance_rate**2)

    print(f"\n3. Non-compliance adjustment:")
    print(f"   Expected compliance: {compliance_rate:.0%}")
    print(f"   Inflation factor: {1/compliance_rate**2:.2f}")
    print(f"   After compliance: {int(np.ceil(n_after_compliance))}")

    # Adjustment 3: Attrition
    attrition_rate = 0.20
    n_final = n_after_compliance / (1 - attrition_rate)

    print(f"\n4. Attrition adjustment:")
    print(f"   Expected attrition: {attrition_rate:.0%}")
    print(f"   Inflation factor: {1/(1-attrition_rate):.2f}")
    print(f"   FINAL REQUIRED N: {int(np.ceil(n_final))}")

    print(f"\n" + "=" * 60)
    print(f"Total inflation: {n_final/n_base:.2f}x base sample size")
    print(f"Baseline N needed: {int(np.ceil(n_final))}")
    print(f"Endline N expected: {int(np.ceil(n_final * (1-attrition_rate)))}")

    return int(np.ceil(n_final)),

</code_template>

</implementation_pattern>

import numpy as np
from scipy.stats import norm

alpha = 0.05
power = 0.80
mde = 0.30

# Individual randomization (baseline)
z_a = norm.ppf(1 - alpha/2)
z_p = norm.ppf(power)
n_individual = ((z_a + z_p)**2) / (0.25 * mde**2)

print(f"CLUSTER vs. INDIVIDUAL RANDOMIZATION")
print(f"=" * 60)
print(f"Individual randomization: {int(np.ceil(n_individual))} participants")

# Cluster randomization scenarios
scenarios = [
    {"icc": 0.05, "cluster_size": 20},
    {"icc": 0.05, "cluster_size": 50},
    {"icc": 0.10, "cluster_size": 30},
    {"icc": 0.20, "cluster_size": 30},
]

for scenario in scenarios:
    icc = scenario["icc"]
    m = scenario["cluster_size"]
    de = 1 + (m - 1) * icc
    n_cluster = n_individual * de
    n_clusters_needed = int(np.ceil(n_cluster / m))

    print(f"\nICC={icc}, cluster size={m}:")
    print(f"  Design effect: {de:.2f}")
    print(f"  Total N needed: {int(np.ceil(n_cluster))}")
    print(f"  Clusters needed: {n_clusters_needed}")
    print(f"  Inflation: {de:.2f}x individual design")

print("\nKey lesson: Higher ICC or larger clusters → much larger N needed")

return ()
</code>
<lesson>
Design effect = 1 + (m-1) × ICC. With ICC=0.10 and m=30, DE=3.9, meaning you need 4x the individual-randomized sample size. Cluster randomization trades power for validity when spillovers are a concern.
</lesson>
</example>

</examples>

<common_mistakes>

<mistake severity="critical">
  <what>Conducting power analysis after data collection (post-hoc power)</what>
  <consequence>Meaningless exercise, doesn't inform anything, misinterpreted as evidence quality</consequence>
  <prevention>Power analysis is for design phase only. Never calculate "observed power" after study</prevention>
</mistake>

<mistake severity="critical">
  <what>Ignoring clustering design effect in sample size calculation</what>
  <consequence>Study underpowered by 2-4x, fails to detect real effects despite large nominal sample</consequence>
  <prevention>ALWAYS multiply by design effect DE = 1+(m-1)×ICC for cluster randomization</prevention>
</mistake>

<mistake severity="high">
  <what>Using overoptimistic effect size assumptions</what>
  <consequence>Study designed to detect implausibly large effects, fails for realistic effects</consequence>
  <prevention>Use conservative estimates from pilots, literature, or smallest policy-relevant effect</prevention>
</mistake>

<mistake severity="high">
  <what>Not inflating for attrition and non-compliance</what>
  <consequence>Final analysis underpowered even if enrollment meets naive target</consequence>
  <prevention>Always inflate for expected attrition (÷ (1-attrition_rate)) and compliance (÷ compliance²)</prevention>
</mistake>

<mistake severity="medium">
  <what>Treating power as binary threshold (80% good, 79% bad)</what>
  <consequence>Arbitrary decisions, missing that power is continuous and context-dependent</consequence>
  <prevention>Power is a continuum. Consider costs, effect importance, and Type I vs. II error tradeoffs</prevention>
</mistake>

</common_mistakes>

<interpretation_guide>

<sample_size_rules_of_thumb>
For 80% power, α=0.05, 50/50 allocation:
- MDE = 0.10 SD: ~3,140 total
- MDE = 0.20 SD: ~786 total
- MDE = 0.30 SD: ~350 total
- MDE = 0.40 SD: ~198 total
- MDE = 0.50 SD: ~128 total

Then multiply by:
- Design effect if clustering: 1+(m-1)×ICC
- 1/compliance² for non-compliance
- 1/(1-attrition_rate) for attrition
</sample_size_rules_of_thumb>

<when_study_is_underpowered>
If power calculation shows infeasible N:
- Increase MDE (accept detecting only larger effects)
- Reduce scope (fewer outcomes, simpler design)
- Improve efficiency (stratification, ANCOVA with baseline)
- Pool with other studies (consortium approach)
- Use alternative design (regression discontinuity, DID)
- Accept higher Type II error risk (document explicitly)

Do NOT: proceed with underpowered study and hope for best.
</when_study_is_underpowered>

</interpretation_guide>

<references>
<paper>Cohen, J. (1988). Statistical Power Analysis for the Behavioral Sciences. 2nd ed. Erlbaum.</paper>
<paper>Duflo, E., Glennerster, R., & Kremer, M. (2007). Using randomization in development economics research. Handbook of Development Economics, 4, 3895-3962.</paper>
<paper>Donner, A., & Klar, N. (2000). Design and Analysis of Cluster Randomization Trials in Health Research. Arnold Publishers.</paper>
<paper>Hoenig, J. M., & Heisey, D. M. (2001). The abuse of power: The pervasive fallacy of power calculations for data analysis. American Statistician, 55(1), 19-24.</paper>
<paper>Vivalt, E. (2020). How much can we generalize from impact evaluations? Journal of the European Economic Association, 18(6), 3045-3089.</paper>
</references>

</skill_content>

示例提示词

我想计算这个集群随机实验所需的样本量和最小可侦测效应。

所属插件

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

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

同插件的其他科研 Skill

常见问题

RCT统计检定力计算能做什么?

Calculates statistical power, minimum detectable effects, and sample sizes for RCTs, including clustering effects. Use when sizing an RCT or running a power analysis。

如何安装RCT统计检定力计算?

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

RCT统计检定力计算需要哪些权限?

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

RCT统计检定力计算经过官方审核吗?

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