references/simulation_based_power.md
9.9 KB查看排版效果| 1 | # Simulation-Based (Monte Carlo) Power |
| 2 | |
| 3 | Simulation estimates a specified analysis procedure under an assumed |
| 4 | data-generating process. It is useful when analytical methods omit important |
| 5 | design features; it is only as credible as those assumptions. Several GLM, |
| 6 | cluster, repeated-measures, and survival designs do have analytical approximations. |
| 7 | Use one when it matches the estimand and planned analysis; otherwise simulate. |
| 8 | |
| 9 | ## The recipe (always the same) |
| 10 | |
| 11 | 1. **Simulate** a dataset of size *n* from your assumed truth: the effect you want |
| 12 | to detect, plus realistic structure (baseline rates, residual SD, cluster random |
| 13 | effects, dropout, covariate distributions). |
| 14 | 2. **Analyze** it with the *exact* model/test planned for the real study. |
| 15 | 3. **Repeat** R times. Power = fraction of replicates where the test is significant. |
| 16 | Use R ≥ 1,000; use 5,000–10,000 for a stable estimate near the 80% decision point. |
| 17 | |
| 18 | Always report the **Monte Carlo confidence interval** on the estimate (the |
| 19 | `scripts/simulate_power.py` harness returns a Wilson interval). With R = 1,000 the |
| 20 | ±2 SE width near p = 0.8 is roughly ±0.025, so don't over-interpret 0.81 vs 0.79. |
| 21 | |
| 22 | ## Using the harness |
| 23 | |
| 24 | `scripts/simulate_power.py` gives you `simulate_power()` and `find_sample_size()`. |
| 25 | You supply a function `gen_and_test(n, rng) -> bool` that builds one dataset, runs |
| 26 | the analysis, and returns whether it was significant. The `rng` is a seeded |
| 27 | `numpy.random.Generator` so each replicate consumes fresh pseudorandom draws. Reproduction also requires |
| 28 | recording software versions; seeded generator streams need not be identical |
| 29 | across future NumPy versions. |
| 30 | |
| 31 | ```python |
| 32 | from simulate_power import (simulate_power, find_sample_size, |
| 33 | example_two_group_difference) |
| 34 | |
| 35 | alpha = 0.05 |
| 36 | # n is per group; use this as a runnable template and replace the generator/test. |
| 37 | gen_and_test = example_two_group_difference(effect=0.5, sd=1.0, alpha=alpha) |
| 38 | est = simulate_power(gen_and_test, n=64, n_sims=2000, alpha=alpha, seed=0) |
| 39 | print(est) |
| 40 | |
| 41 | # A candidate, not a guarantee of globally minimal n under Monte Carlo noise. |
| 42 | n, est = find_sample_size(gen_and_test, target_power=0.80, n_sims=1000, |
| 43 | alpha=alpha, lo=40, hi=100, seed=0, verbose=False) |
| 44 | # Check neighboring sizes with more replicates and an independent seed. |
| 45 | checks = [simulate_power(gen_and_test, n=j, n_sims=5000, |
| 46 | alpha=alpha, seed=2026) for j in (n - 1, n, n + 1)] |
| 47 | ``` |
| 48 | |
| 49 | The file ships four adaptable examples: two-group difference (a sanity check |
| 50 | against the closed-form t-test), logistic regression, a cluster-randomized trial |
| 51 | with an ICC, and a repeated-measures linear mixed model. Copy the closest one and |
| 52 | edit the data-generating block. |
| 53 | |
| 54 | ## When simulation is useful |
| 55 | |
| 56 | | Design / analysis | Features to include | What to simulate | |
| 57 | |-------------------|----------------|------------------| |
| 58 | | Logistic / Poisson regression | Power depends on the full covariate distribution | Generate predictors, compute the linear predictor, draw the outcome, fit the GLM | |
| 59 | | Mixed-effects / repeated measures | Random effects + within-subject correlation | Draw subject/cluster random effects, then observations; fit `mixedlm` | |
| 60 | | Cluster-randomized trial | ICC inflates variance; clusters are the unit | Cluster random intercepts via ICC; fit the planned cluster-aware analysis; check small-cluster inference | |
| 61 | | Survival (Cox / log-rank) | Censoring and event-time distribution | Draw event and censoring times; fit `lifelines` CoxPH or run a log-rank test | |
| 62 | | Interaction terms | The target is the interaction contrast, not either main effect | Generate the factorial structure and the interaction effect; test that coefficient | |
| 63 | | Mediation | Product-of-coefficients null is non-normal | Simulate the path model; bootstrap or test the indirect effect | |
| 64 | | Non-standard / custom test | Analytical assumptions do not match the design | Whatever your analysis script does | |
| 65 | |
| 66 | ## Key correctness points |
| 67 | |
| 68 | - **Analyze exactly as planned.** If the real analysis adjusts for covariates, |
| 69 | include them in the simulation. If it uses a robust SE or a specific correction, |
| 70 | apply it in `gen_and_test`. The whole value of simulation is fidelity to the plan. |
| 71 | - **Handle estimation failures explicitly.** The bundled examples check convergence, |
| 72 | separation/Hessian failures, and finite p-values. They raise `SimulationFitError` |
| 73 | for anticipated failures; the harness retains those runs in the denominator as |
| 74 | non-rejections and reports `n_failures` plus `failure_reasons`. Unexpected |
| 75 | exceptions propagate. Counting failures this way defines an unconditional |
| 76 | operational rejection rate; it does not prove valid statistical inference. |
| 77 | Frequent failures require revising the planned analysis/design, not silently |
| 78 | dropping replicates. Warnings are summarized in `n_warned` and `warning_counts`; |
| 79 | inspect their categories even when a fit reports convergence. |
| 80 | - **Watch Type I error too.** As a check, simulate under the *null* (effect = 0) |
| 81 | and confirm the rejection rate ≈ α. If it's inflated (common with small-cluster |
| 82 | mixed models or naive cluster SEs), your planned analysis is anticonservative and |
| 83 | the power number is meaningless until you fix the analysis. |
| 84 | - **Separate search from confirmation.** A fixed seed makes results reproducible, |
| 85 | but does not remove noise or make power monotone in n. Bisection is only a |
| 86 | heuristic for a design with increasing power. Confirm the selected n and nearby |
| 87 | feasible allocations with larger runs and fresh seeds. The ordinary Wilson CI |
| 88 | at an adaptively selected n is not selection-adjusted. If discrete tests, |
| 89 | allocation rounding, or failures cause nonmonotonicity, inspect a grid instead. |
| 90 | - **Keep alpha in the callback.** The harness validates its `alpha` argument but |
| 91 | does not forward it or threshold p-values. The callback must return bool, and |
| 92 | must use the same planned threshold/procedure; raw p-values raise `TypeError`. |
| 93 | - **Distinguish conditional assumptions.** Logistic `beta` is per raw predictor |
| 94 | unit; `base_rate` is risk at x=0. `n` is total observations for that example, |
| 95 | clusters per arm for the cluster example, and subjects for repeated measures. |
| 96 | The mixed-model examples use REML random intercepts and asymptotic Wald tests; |
| 97 | they do not implement small-sample df corrections or random slopes. |
| 98 | |
| 99 | ## Modeling realistic complications |
| 100 | |
| 101 | - **Dropout.** Either simulate missingness directly (drop rows / occasions under |
| 102 | the assumed mechanism, then apply the planned missing-data analysis — estimates rejection rates |
| 103 | under that assumed mechanism, but does not itself estimate bias or validate MAR/MNAR assumptions), or compute the analyzed n and inflate the enrolled n by |
| 104 | `1/(1−dropout)`. |
| 105 | - **Clustering / ICC.** Split total variance into between-cluster (τ²) and residual |
| 106 | (σ²) with `ICC = τ²/(τ²+σ²)`, draw a cluster random effect ~ N(0, τ), add it to |
| 107 | every member of the cluster. See `example_cluster_randomized`. |
| 108 | - **Unequal allocation / stratification.** Generate the exact group sizes and strata |
| 109 | the design will produce; don't assume balance the design won't deliver. |
| 110 | - **Repeated measures.** Subject random intercept (and slope, if relevant) plus a |
| 111 | within-subject residual; the correlation is `τ²/(τ²+σ²)` only for the random-intercept, independent-homoskedastic-residual example. Time-dependent residual correlation/random slopes require a richer generator and fit. |
| 112 | |
| 113 | ## Reporting a simulation-based power analysis |
| 114 | |
| 115 | State enough that someone could rerun it: |
| 116 | |
| 117 | ```text |
| 118 | Power was estimated at [n and its unit] with [R] independent replicates at each |
| 119 | prespecified sample size. The generator assumed [effect in raw units], [covariate |
| 120 | distribution], [conditional baseline risk / residual variance], [ICC, cluster sizes], |
| 121 | and [dropout/censoring]. The analysis used [model, estimand, test, multiplicity rule], |
| 122 | matching the protocol. At alpha [value], [hits]/[all R] runs rejected the null |
| 123 | ([power], 95% Wilson Monte Carlo CI [low, high]); [failures] fits failed and were |
| 124 | retained as non-rejections. Warning categories were [counts]. Null simulations |
| 125 | at the same design gave [Type I error and MC CI]. Candidate n was confirmed with |
| 126 | [fresh seed and R]. Assumption sensitivity covered [ranges]. Code/versions: [link]. |
| 127 | ``` |
| 128 | |
| 129 | Replace every bracket with executed results; this template contains no invented |
| 130 | study outcome. Monte Carlo CIs quantify sampling noise at fixed assumptions, |
| 131 | not uncertainty in effects, nuisance parameters, or population transportability. |
| 132 | |
| 133 | ## Current primary sources and execution scope |
| 134 | |
| 135 | Reviewed 2026-10-01; all four bundled generators ran on small synthetic data with |
| 136 | statsmodels 0.15.0 / SciPy 1.18.1 / NumPy 2.5.3 / pandas 2.3.3. The pooled t-test |
| 137 | has a native null/power calibration check; small smoke runs of other models verify |
| 138 | software mechanics only, not study-specific Type I error or sample-size adequacy. |
| 139 | |
| 140 | - [SciPy ttest_ind](https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.ttest_ind.html): pooled variance is `equal_var=True`; Welch is a different analysis. |
| 141 | - [statsmodels Logit.fit](https://www.statsmodels.org/stable/generated/statsmodels.discrete.discrete_model.Logit.fit.html), [MixedLM.fit](https://www.statsmodels.org/stable/generated/statsmodels.regression.mixed_linear_model.MixedLM.fit.html), and [fit pitfalls](https://www.statsmodels.org/stable/pitfalls.html). |
| 142 | - [NumPy Generator](https://numpy.org/doc/stable/reference/random/generator.html): RNG methods and version compatibility. |
| 143 | - [lifelines statistics](https://lifelines.readthedocs.io/en/latest/lifelines.statistics.html): logrank tests and `sample_size_necessary_under_cph` / `power_under_cph` offer specific proportional-hazards approximations. [CoxPHFitter](https://lifelines.readthedocs.io/en/latest/fitters/regression/CoxPHFitter.html) supports right-censored time/event fitting. These survival extensions are guidance, not a bundled calibrated survival generator; informative censoring and nonproportional hazards require additional modeling. |
| 144 | - [Arnold et al. (2011)](https://doi.org/10.1186/1471-2288-11-94): simulation design and reporting. |
文件内容是平台检查时读出的文本,只供查看。平台不会运行包里的任何脚本或程序。

