AgentStack
Browse Sign in
Browse Why AgentStack Sell Docs
Sign in
SKILL verified MIT Self-run

Rct Randomization

skill-sshtomar-claude-code-skills-social-science-rct-randomization · by sshtomar

Implement randomization methods for RCTs. Use when user mentions: random assignment, treatment allocation, stratification, blocking, permutation randomization, cluster randomization, randomization balance, misfit handling, re-randomization.

No reviews yet
0 installs
20 views
0.0% view→install

Install

$ agentstack add skill-sshtomar-claude-code-skills-social-science-rct-randomization

✓ scanned · ✓ verified, works with Claude Code, Cursor, and more.

Security review

✓ Passed

No issues found. Passed automated security review. · v0.1.0 How review works →

  • Prompt-injection patterns
  • Secret / credential exfiltration
  • Dangerous shell & filesystem operations
  • Untrusted network calls
  • Known-malicious package signatures

What it can access

  • Network access No
  • Filesystem access No
  • Shell / process execution No
  • Environment & secrets No
  • Dynamic code execution No

From automated source analysis of v0.1.0. “Used” means the capability is present in the source — more access means more to trust, not that it’s unsafe.

View the full security report →

Verified badge

Passed review? Show it. Paste this badge into your README, it links to the public security report.

AgentStack Verified badge Links to your public security report.
[![AgentStack Verified](https://agentstack.voostack.com/badges/verified.svg)](https://agentstack.voostack.com/security/report/skill-sshtomar-claude-code-skills-social-science-rct-randomization)

Reliability & compatibility

Security review passed
0 installs to date
no reviews yet
4mo ago

Declared compatibility

Claude CodeClaude Desktop

Compatibility is declared by the source manifest. End-to-end runtime verification is coming, see below.

Preview Execution monitoring

We're building live execution health for every listing: tool-call success rate, median latency, uptime, and last-checked timestamps, measured, not self-reported. It isn't live yet, so we don't show numbers we can't stand behind.

How agent discovery & health will work →
Are you the author of Rct Randomization? Claim this listing to set pricing, connect Stripe payouts, and keep 70% of every sale.
Sign up to claim

About

Randomization is the foundation of experimental causal inference. Proper implementation requires setting seeds once, documenting procedures, saving assignments immediately, and never re-running randomization to "improve" balance. Stratification improves precision when strata correlate with outcomes. Cluster randomization prevents spillovers but reduces power through design effects.

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

Set Seed Once and Document Set random seed exactly ONCE at the beginning, document seed value, never reset seed within randomization code Multiple seed settings compromise randomness. Seed documentation enables replication and verification of randomization integrity Non-reproducible randomization, suspicion of manipulation, inability to verify implementation

Save Assignment Immediately, Never Re-Run Save randomization assignments to persistent storage immediately after generation, never re-run randomization Re-running randomization to "improve balance" invalidates inference and creates selection bias Invalid p-values, corrupted randomization, inability to claim true random assignment

Use Permutation (Not Simple) When Sample Size Known Use permutation randomization (shuffle fixed number of 1s and 0s) rather than independent Bernoulli draws when N is known Permutation ensures exact allocation ratios. Simple randomization creates sampling variation in treatment/control split Unbalanced groups, power loss, inability to explain allocation discrepancies

Stratify on Strong Predictors When Available Use stratified randomization when baseline covariates strongly predict outcomes Stratification ensures balance on key variables, improves precision, reduces sampling variability (Bruhn & McKenzie 2009) Larger standard errors, power loss, imbalanced groups on important characteristics

Account for Clustering in Randomization Level Randomize at cluster level when spillovers likely within clusters Individual randomization with spillovers violates SUTVA and biases estimates. Cluster randomization prevents contamination Biased treatment effect estimates from spillover contamination

Sample for randomization represents full eligible population, no post-randomization exclusions Document exclusions before randomization, never drop units after seeing assignment Selection bias, non-random sample, invalid causal inference

Treatment assignment is actually implemented as randomized (no crossover without documentation) Monitor compliance, track any protocol deviations Document and report deviations, analyze as encouragement design if substantial crossover

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
@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,

Cluster randomization to prevent spillovers

@app.cell
def cluster_randomize(df):
    # Cluster randomization when spillovers likely within clusters
    # Reduces power due to design effect, but necessary for validity

    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,

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.

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

Setting random seed multiple times in randomization code Compromises randomness, makes sequence non-random, creates reproducibility issues Set seed ONCE at very beginning, never call np.random.seed() again in randomization code

Using simple randomization when sample size is known Sampling variation creates unbalanced groups, power loss, difficult to explain discrepancies Use permutation randomization (shuffle fixed number of 1s and 0s) when N is known

Not stratifying when strong predictors available Unnecessary power loss, imbalanced groups on important characteristics Stratify on 1-3 strong outcome predictors to improve precision

Not documenting randomization seed and procedure Non-reproducible, inability to verify implementation, suspicion of manipulation Document seed, date, time, code version, sample characteristics in protocol

After randomization (but NOT as reason to re-randomize):

  • Normalized diff |d| 0.10: Overall balance adequate
  • Joint F-test p
  • 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)

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. Athey, S., & Imbens, G. W. (2017). The econometrics of randomized experiments. Handbook of Economic Field Experiments, 1, 73-140. Duflo, E., Glennerster, R., & Kremer, M. (2007). Using randomization in development economics research. Handbook of Development Economics, 4, 3895-3962.

Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

Install and usage instructions live in the source repository linked above.

Reviews

No reviews yet, be the first.

Versions

  • v0.1.0 Imported from the upstream source.