Experiment Design
Scientific experiment planning, power analysis, and protocol development.
Design Selection Guide
| Research Question |
Recommended Design |
| Does X cause Y? |
RCT (gold standard) |
| Does X cause Y? (can't randomize) |
Quasi-experiment, natural experiment |
| How do factors interact? |
Factorial design |
| Which version performs better? |
A/B test |
| What is the prevalence/association? |
Cross-sectional survey |
| How does outcome change over time? |
Longitudinal / cohort study |
| What is the lived experience? |
Qualitative (interviews, ethnography) |
| Does intervention work in practice? |
Pragmatic trial |
Power Analysis & Sample Size
source /Users/zhangmingda/clawd/.venv/bin/activate
python3 << 'EOF'
from scipy import stats
import numpy as np
# --- Two-sample t-test ---
def sample_size_ttest(effect_size, alpha=0.05, power=0.80):
"""Cohen's d effect sizes: small=0.2, medium=0.5, large=0.8"""
from scipy.stats import norm
z_alpha = norm.ppf(1 - alpha/2)
z_beta = norm.ppf(power)
n = 2 * ((z_alpha + z_beta) / effect_size) ** 2
return int(np.ceil(n))
# --- Chi-square test ---
def sample_size_chi2(effect_size, alpha=0.05, power=0.80, df=1):
"""Cohen's w effect sizes: small=0.1, medium=0.3, large=0.5"""
from scipy.stats import norm, chi2
z_beta = norm.ppf(power)
z_alpha = norm.ppf(1 - alpha)
n = ((z_alpha + z_beta) / effect_size) ** 2
return int(np.ceil(n))
# --- Correlation ---
def sample_size_correlation(r, alpha=0.05, power=0.80):
from scipy.stats import norm
z_alpha = norm.ppf(1 - alpha/2)
z_beta = norm.ppf(power)
z_r = 0.5 * np.log((1+r)/(1-r)) # Fisher's z
n = ((z_alpha + z_beta) / z_r) ** 2 + 3
return int(np.ceil(n))
# Examples
print(f"t-test (d=0.5): n={sample_size_ttest(0.5)} per group")
print(f"t-test (d=0.3): n={sample_size_ttest(0.3)} per group")
print(f"Chi-square (w=0.3): n={sample_size_chi2(0.3)}")
print(f"Correlation (r=0.3): n={sample_size_correlation(0.3)}")
EOF
Key Design Principles
Controls
- Positive control: Known to produce effect (validates method works)
- Negative control: Known to produce no effect (validates baseline)
- Placebo control: Inert treatment (controls for expectation effects)
- Active control: Existing standard treatment (for superiority/non-inferiority)
Randomization
- Simple: Coin flip / random number
- Block: Ensures equal groups per block
- Stratified: Randomize within strata (age, sex, severity)
- Cluster: Randomize groups, not individuals
Blinding
- Single-blind: Participants don't know assignment
- Double-blind: Participants and researchers don't know
- Triple-blind: Participants, researchers, and analysts don't know
Bias Mitigation
| Bias |
Mitigation |
| Selection bias |
Random sampling, clear inclusion criteria |
| Allocation bias |
Random assignment, concealed allocation |
| Performance bias |
Blinding, standardized protocols |
| Detection bias |
Blinded outcome assessment |
| Attrition bias |
ITT analysis, minimize dropout |
| Reporting bias |
Pre-registration, analysis plan |
Study Protocol Template
# Study Protocol: [Title]
## 1. Background & Rationale
## 2. Objectives & Hypotheses
- Primary:
- Secondary:
## 3. Study Design
- Type: [RCT / quasi-experiment / observational / ...]
- Duration:
## 4. Participants
- Population:
- Inclusion criteria:
- Exclusion criteria:
- Sample size: N = [calculated], power = 0.80, α = 0.05
## 5. Intervention / Exposure
## 6. Outcome Measures
- Primary:
- Secondary:
## 7. Randomization & Blinding
## 8. Data Collection Procedures
## 9. Statistical Analysis Plan
- Primary analysis:
- Secondary analyses:
- Handling of missing data:
## 10. Ethical Considerations
- IRB/Ethics approval:
- Informed consent:
- Data privacy:
## 11. Timeline
## 12. Budget
Pre-registration
Recommend pre-registration for confirmatory studies:
- OSF: osf.io (general)
- ClinicalTrials.gov: clinical trials
- PROSPERO: systematic reviews
- AsPredicted: aspredicted.org (quick)
Tips
- Always justify sample size with power analysis
- Pre-register hypotheses and analysis plan
- Plan for 10-20% attrition in sample size calculation
- Document all deviations from protocol
- Consider pilot study for novel methods
1---2name: experiment-design3description: Design scientific experiments including sample size calculation, randomization, control groups, blinding, and study protocols. Covers RCTs, quasi-experiments, factorial designs, A/B tests, survey design, and observational studies. Use when user asks to design an experiment, calculate sample size, plan a study, set up controls, or create a research protocol. Triggers on "design experiment", "sample size", "power analysis", "study design", "control group", "randomization", "A/B test", "factorial design", "survey design".4---5
6# Experiment Design
7
8Scientific experiment planning, power analysis, and protocol development.
9
10## Design Selection Guide
11
12| Research Question | Recommended Design |
13|---|---|
14| Does X cause Y? | RCT (gold standard) |
15| Does X cause Y? (can't randomize) | Quasi-experiment, natural experiment |
16| How do factors interact? | Factorial design |
17| Which version performs better? | A/B test |
18| What is the prevalence/association? | Cross-sectional survey |
19| How does outcome change over time? | Longitudinal / cohort study |
20| What is the lived experience? | Qualitative (interviews, ethnography) |
21| Does intervention work in practice? | Pragmatic trial |
22
23## Power Analysis & Sample Size
24
25```python
26source /Users/zhangmingda/clawd/.venv/bin/activate
27python3 << 'EOF'
28from scipy import stats
29import numpy as np
30
31# --- Two-sample t-test ---
32def sample_size_ttest(effect_size, alpha=0.05, power=0.80):
33 """Cohen's d effect sizes: small=0.2, medium=0.5, large=0.8"""
34 from scipy.stats import norm
35 z_alpha = norm.ppf(1 - alpha/2)
36 z_beta = norm.ppf(power)
37 n = 2 * ((z_alpha + z_beta) / effect_size) ** 2
38 return int(np.ceil(n))
39
40# --- Chi-square test ---
41def sample_size_chi2(effect_size, alpha=0.05, power=0.80, df=1):
42 """Cohen's w effect sizes: small=0.1, medium=0.3, large=0.5"""
43 from scipy.stats import norm, chi2
44 z_beta = norm.ppf(power)
45 z_alpha = norm.ppf(1 - alpha)
46 n = ((z_alpha + z_beta) / effect_size) ** 2
47 return int(np.ceil(n))
48
49# --- Correlation ---
50def sample_size_correlation(r, alpha=0.05, power=0.80):
51 from scipy.stats import norm
52 z_alpha = norm.ppf(1 - alpha/2)
53 z_beta = norm.ppf(power)
54 z_r = 0.5 * np.log((1+r)/(1-r)) # Fisher's z
55 n = ((z_alpha + z_beta) / z_r) ** 2 + 3
56 return int(np.ceil(n))
57
58# Examples
59print(f"t-test (d=0.5): n={sample_size_ttest(0.5)} per group")
60print(f"t-test (d=0.3): n={sample_size_ttest(0.3)} per group")
61print(f"Chi-square (w=0.3): n={sample_size_chi2(0.3)}")
62print(f"Correlation (r=0.3): n={sample_size_correlation(0.3)}")
63EOF
64```
65
66## Key Design Principles
67
68### Controls
69- **Positive control**: Known to produce effect (validates method works)
70- **Negative control**: Known to produce no effect (validates baseline)
71- **Placebo control**: Inert treatment (controls for expectation effects)
72- **Active control**: Existing standard treatment (for superiority/non-inferiority)
73
74### Randomization
75- **Simple**: Coin flip / random number
76- **Block**: Ensures equal groups per block
77- **Stratified**: Randomize within strata (age, sex, severity)
78- **Cluster**: Randomize groups, not individuals
79
80### Blinding
81- **Single-blind**: Participants don't know assignment
82- **Double-blind**: Participants and researchers don't know
83- **Triple-blind**: Participants, researchers, and analysts don't know
84
85### Bias Mitigation
86| Bias | Mitigation |
87|------|-----------|
88| Selection bias | Random sampling, clear inclusion criteria |
89| Allocation bias | Random assignment, concealed allocation |
90| Performance bias | Blinding, standardized protocols |
91| Detection bias | Blinded outcome assessment |
92| Attrition bias | ITT analysis, minimize dropout |
93| Reporting bias | Pre-registration, analysis plan |
94
95## Study Protocol Template
96
97```markdown
98# Study Protocol: [Title]
99
100## 1. Background & Rationale
101## 2. Objectives & Hypotheses
102 - Primary:
103 - Secondary:
104## 3. Study Design
105 - Type: [RCT / quasi-experiment / observational / ...]
106 - Duration:
107## 4. Participants
108 - Population:
109 - Inclusion criteria:
110 - Exclusion criteria:
111 - Sample size: N = [calculated], power = 0.80, α = 0.05
112## 5. Intervention / Exposure
113## 6. Outcome Measures
114 - Primary:
115 - Secondary:
116## 7. Randomization & Blinding
117## 8. Data Collection Procedures
118## 9. Statistical Analysis Plan
119 - Primary analysis:
120 - Secondary analyses:
121 - Handling of missing data:
122## 10. Ethical Considerations
123 - IRB/Ethics approval:
124 - Informed consent:
125 - Data privacy:
126## 11. Timeline
127## 12. Budget
128```
129
130## Pre-registration
131
132Recommend pre-registration for confirmatory studies:
133- **OSF**: osf.io (general)
134- **ClinicalTrials.gov**: clinical trials
135- **PROSPERO**: systematic reviews
136- **AsPredicted**: aspredicted.org (quick)
137
138## Tips
139- Always justify sample size with power analysis
140- Pre-register hypotheses and analysis plan
141- Plan for 10-20% attrition in sample size calculation
142- Document all deviations from protocol
143- Consider pilot study for novel methods