| Title: | Simulation-Based Power Analysis for Mixed-Effects Models |
|---|---|
| Description: | A comprehensive, simulation-based toolkit for power and sample-size analysis for linear and generalized linear mixed-effects models (LMMs and GLMMs). Supports Gaussian, binomial, Poisson, and negative binomial families via 'lme4'; Wald and likelihood-ratio tests; multi-parameter sensitivity grids; power curves and minimum sample-size solvers; parallel evaluation with deterministic seeds; and full reproducibility (manifests, result bundling, and export to CSV/JSON). Delivers thorough diagnostics per run (failure rate, singular-fit rate, effective N) and publication-ready summary tables. References: Bates et al. (2015) "Fitting Linear Mixed-Effects Models Using lme4" <doi:10.18637/jss.v067.i01>; Green and MacLeod (2016) "SIMR: an R package for power analysis of generalized linear mixed models by simulation" <doi:10.1111/2041-210X.12504>. |
| Authors: | Alex Litovchenko [aut, cre] |
| Maintainer: | Alex Litovchenko <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.1.1 |
| Built: | 2026-06-18 10:31:22 UTC |
| Source: | https://github.com/alitovchenko/mixpower |
mixpower is a simulation-based toolkit for power and sample-size analysis for linear and generalized linear mixed-effects models (LMMs and GLMMs). It is design-first (no pilot data required), supports Gaussian, binomial, Poisson, and negative binomial families via lme4; Wald and likelihood-ratio tests; multi-parameter sensitivity grids; power curves and minimum sample-size solvers; parallel evaluation with deterministic seeds; and full reproducibility (manifests, result bundling, export to CSV/JSON). Every run reports diagnostics (failure rate, singular-fit rate, effective N).
Typical workflow: (1) define a design with mp_design() (cluster sizes,
trials per cell) and effect-size assumptions with mp_assumptions(); (2) build
a scenario with a backend constructor (e.g. mp_scenario_lme4() for Gaussian
LMM); (3) run mp_power() for a single power estimate, mp_sensitivity() to
vary parameters, or mp_power_curve() / mp_solve_sample_size() for curves
and sample-size. Use a fixed seed for reproducibility. Failure and
singular-fit rates are always reported and never suppressed.
After loading the package, define a design and assumptions, build an lme4
scenario, then call mp_power(). See Examples below for a minimal
runnable workflow.
Design and assumptions: mp_design(), mp_assumptions().
Scenarios (LMM/GLMM): mp_scenario(), mp_scenario_lme4(),
mp_scenario_lme4_binomial(), mp_scenario_lme4_poisson(), mp_scenario_lme4_nb().
Power and sensitivity: mp_power() (optional aggregate = "streaming"),
mp_sensitivity(), mp_sensitivity_parallel(), mp_power_curve(),
mp_power_curve_parallel(), mp_solve_sample_size(), mp_grid_sample_size(),
mp_quick_power().
Backends and simulators: mp_backend(), validate_mp_backend(), mp_backend_lme4(),
mp_backend_lme4_binomial(), mp_backend_lme4_poisson(), mp_backend_lme4_nb(),
mp_backend_glmmtmb(), mp_scenario_glmmtmb_lmm(), simulate_glmm_binomial_data(),
simulate_glmm_poisson_data(), simulate_glmm_nb_data().
Optional tidyverse (Suggests): tibble::as_tibble() and ggplot2::autoplot() methods.
Reproducibility and reporting: mp_manifest(), mp_bundle_results(),
mp_report_table(), mp_write_results().
Plotting: plot() for mp_power_curve and mp_sensitivity objects.
Use a fixed seed (e.g. seed = 123) so runs are reproducible.
Check failure_rate and singular_rate in results; investigate if high.
For nested model comparison use LRT with an explicit null_formula.
Use mp_power_curve() or mp_solve_sample_size() to choose sample size.
Vignettes give step-by-step guides: vignette("mixpower-intro", package = "mixpower"),
vignette("mixpower-design", package = "mixpower"),
vignette("mixpower-simulations", package = "mixpower"),
vignette("mixpower-diagnostics", package = "mixpower"),
vignette("mixpower-reproducibility", package = "mixpower"),
vignette("mixpower-extending", package = "mixpower").
Maintainer: Alex Litovchenko [email protected]
Bates D, Maechler M, Bolker B, Walker S (2015). "Fitting Linear Mixed-Effects Models Using lme4." Journal of Statistical Software, 67(1), 1–48. doi:10.18637/jss.v067.i01.
Green P, MacLeod CJ (2016). "SIMR: an R package for power analysis of generalized linear mixed models by simulation." Methods in Ecology and Evolution, 7(4), 493–498. doi:10.1111/2041-210X.12504.
Entry points: mp_design(), mp_power(), mp_quick_power().
Vignettes: vignette(package = "mixpower").
# Minimal workflow: design -> assumptions -> scenario -> power d <- mp_design(clusters = list(subject = 30), trials_per_cell = 4) a <- mp_assumptions( fixed_effects = list(`(Intercept)` = 0, condition = 0.3), residual_sd = 1 ) scn <- mp_scenario_lme4(y ~ condition + (1 | subject), design = d, assumptions = a) res <- mp_power(scn, nsim = 50, seed = 123) summary(res)# Minimal workflow: design -> assumptions -> scenario -> power d <- mp_design(clusters = list(subject = 30), trials_per_cell = 4) a <- mp_assumptions( fixed_effects = list(`(Intercept)` = 0, condition = 0.3), residual_sd = 1 ) scn <- mp_scenario_lme4(y ~ condition + (1 | subject), design = d, assumptions = a) res <- mp_power(scn, nsim = 50, seed = 123) summary(res)
Requires the tibble package (installed with the tidyverse).
## S3 method for class 'mp_power' as_tibble(x, ...)## S3 method for class 'mp_power' as_tibble(x, ...)
x |
An |
... |
Passed to |
Requires ggplot2. Intended as an optional alternative to base
plot.mp_sensitivity() / plot.mp_power_curve().
## S3 method for class 'mp_sensitivity' autoplot( object, ..., y = c("estimate", "failure_rate", "singular_rate", "n_effective") )## S3 method for class 'mp_sensitivity' autoplot( object, ..., y = c("estimate", "failure_rate", "singular_rate", "n_effective") )
object |
An |
... |
Unused; reserved for consistency with ggplot2 generics. |
y |
For sensitivity/curve objects: same as |
A ggplot2 object.
Helpers that translate standardized or published effect sizes into the raw
coefficients and variance components that mp_assumptions() expects, so you
do not have to hand-pick regression coefficients. They compose directly:
mp_d_to_beta(d, sd = 1) mp_beta_to_d(beta, sd = 1) mp_r2_to_beta(r2, sd_resid = 1, predictor_sd = 1) mp_beta_to_r2(beta, sd_resid = 1, predictor_sd = 1) mp_icc_to_sd(icc, sd_resid = 1) mp_sd_to_icc(sd, sd_resid = 1) mp_or_to_logodds(or) mp_logodds_to_or(beta) mp_t_to_beta(t, se) mp_f_to_beta(f, se)mp_d_to_beta(d, sd = 1) mp_beta_to_d(beta, sd = 1) mp_r2_to_beta(r2, sd_resid = 1, predictor_sd = 1) mp_beta_to_r2(beta, sd_resid = 1, predictor_sd = 1) mp_icc_to_sd(icc, sd_resid = 1) mp_sd_to_icc(sd, sd_resid = 1) mp_or_to_logodds(or) mp_logodds_to_or(beta) mp_t_to_beta(t, se) mp_f_to_beta(f, se)
d |
Cohen's d (standardized mean difference). |
sd |
Standard deviation defining the standardization (e.g. the total
outcome SD |
beta |
A raw coefficient. |
r2 |
Target (partial) proportion of variance explained, in |
sd_resid |
Residual standard deviation. |
predictor_sd |
Standard deviation of the predictor (default 1). |
icc |
Target intraclass correlation, in |
or |
Odds ratio (> 0). |
t |
A t statistic. |
se |
Standard error of the coefficient. |
f |
An F statistic with one numerator degree of freedom. |
mp_assumptions(
fixed_effects = list("(Intercept)" = 0, condition = mp_d_to_beta(0.5, sd = 1.12)),
random_effects = list(subject = list(intercept_sd = mp_icc_to_sd(0.1, 1))),
residual_sd = 1
)
mp_d_to_beta() / mp_beta_to_d(): Cohen's d for a 0/1 predictor is the
mean difference divided by sd, so the coefficient is d * sd.
mp_r2_to_beta() / mp_beta_to_r2(): for a predictor with SD
predictor_sd, a target (partial) variance-explained r2 against residual
SD sd_resid implies beta = sqrt(r2/(1-r2)) * sd_resid / predictor_sd.
mp_icc_to_sd() / mp_sd_to_icc(): an intraclass correlation icc with
residual SD sd_resid implies a random-intercept SD
sqrt(icc/(1-icc)) * sd_resid.
mp_or_to_logodds() / mp_logodds_to_or(): a binomial GLMM coefficient is
the log odds ratio.
mp_t_to_beta() / mp_f_to_beta(): recover a coefficient from a published
t (or one-numerator-df F) statistic and its standard error.
A numeric scalar.
mp_d_to_beta(0.5, sd = 1.12) mp_icc_to_sd(0.1, sd_resid = 1) mp_r2_to_beta(0.02, sd_resid = 1) mp_or_to_logodds(1.5)mp_d_to_beta(0.5, sd = 1.12) mp_icc_to_sd(0.1, sd_resid = 1) mp_r2_to_beta(0.02, sd_resid = 1) mp_or_to_logodds(1.5)
Fit a model for a single simulated dataset
fit_model(data, formula)fit_model(data, formula)
data |
A data.frame of simulated data. |
formula |
A model formula. |
A fitted model object.
Assumptions encode effect sizes and the variance components used to simulate
data. Random-effect sizes are given as standard deviations on the linear
predictor (the scale lme4 reports), via random_effects.
mp_assumptions( fixed_effects, random_effects = NULL, icc = NULL, residual_sd = NULL, notes = NULL )mp_assumptions( fixed_effects, random_effects = NULL, icc = NULL, residual_sd = NULL, notes = NULL )
fixed_effects |
Named list of numeric values (e.g.,
|
random_effects |
Optional named list keyed by grouping factor. Each
element is a named list with |
icc |
Deprecated. Previously documented as an intraclass correlation
but used as a random-intercept SD. If supplied it is interpreted as
|
residual_sd |
Optional non-negative numeric residual SD (Gaussian). |
notes |
Optional free text. |
An object of class mp_assumptions.
a <- mp_assumptions( fixed_effects = list("(Intercept)" = 0, condition = 0.4), random_effects = list(subject = list(intercept_sd = 0.5)), residual_sd = 1 ) aa <- mp_assumptions( fixed_effects = list("(Intercept)" = 0, condition = 0.4), random_effects = list(subject = list(intercept_sd = 0.5)), residual_sd = 1 ) a
A backend is a list with three functions used by mp_power():
simulate_funFirst argument receives the mp_scenario (often named
scenario); optional seed. Must return a data.frame.
fit_funTwo arguments: simulated data.frame, then the mp_scenario
(names may differ; mp_power() passes them positionally).
test_funTwo arguments: fitted model, then the mp_scenario; returns
list(p_value = <numeric scalar>).
mp_backend( simulate_fun, fit_fun, test_fun, name = "custom", version = NULL, notes = NULL, capabilities = NULL )mp_backend( simulate_fun, fit_fun, test_fun, name = "custom", version = NULL, notes = NULL, capabilities = NULL )
simulate_fun |
A data.frame-generating simulator (see Details). |
fit_fun |
Model fitter (see Details). |
test_fun |
Extracts a scalar p-value (see Details). |
name |
Short label for printing and manifests (default |
version |
Optional character version string for the backend implementation. |
notes |
Optional longer notes. |
capabilities |
Optional named list of flags (documentation only), e.g.
|
Use mp_backend() to build a validated object of class mp_backend. Custom
backends can also be plain lists with those three names; validate_mp_backend()
checks the contract without requiring the class.
An object of class c("mp_backend", "list") with the components above.
Fits with the glmmTMB function glmmTMB() (Gaussian). Useful for comparing simulation-based
power to mp_backend_lme4() and for workflows that later extend to families
supported by glmmTMB but not lme4.
mp_backend_glmmtmb( predictor = "condition", subject = "subject", outcome = "y", item = NULL, test_method = c("wald", "lrt"), null_formula = NULL )mp_backend_glmmtmb( predictor = "condition", subject = "subject", outcome = "y", item = NULL, test_method = c("wald", "lrt"), null_formula = NULL )
predictor |
Predictor column name. |
subject |
Subject ID column name. |
outcome |
Outcome column name. |
item |
Optional item ID column name. |
test_method |
Inference method: |
null_formula |
Null-model formula required for |
An object of class mp_backend.
Build an lme4 backend for MixPower scenarios
mp_backend_lme4( predictor = "condition", subject = "subject", outcome = "y", item = NULL, test_method = c("wald", "lrt", "satterthwaite", "kenward-roger", "pb"), null_formula = NULL, pb_nsim = 100L )mp_backend_lme4( predictor = "condition", subject = "subject", outcome = "y", item = NULL, test_method = c("wald", "lrt", "satterthwaite", "kenward-roger", "pb"), null_formula = NULL, pb_nsim = 100L )
predictor |
Predictor column name. |
subject |
Subject ID column name. |
outcome |
Outcome column name. |
item |
Optional item ID column name. |
test_method |
Inference method: |
null_formula |
Null-model formula required for |
pb_nsim |
Bootstrap replicates for |
A list containing simulate_fun, fit_fun, and test_fun.
Build an lme4 backend for binomial GLMM scenarios
mp_backend_lme4_binomial( predictor = "condition", subject = "subject", outcome = "y", item = NULL, test_method = c("wald", "lrt", "pb"), null_formula = NULL, pb_nsim = 100L )mp_backend_lme4_binomial( predictor = "condition", subject = "subject", outcome = "y", item = NULL, test_method = c("wald", "lrt", "pb"), null_formula = NULL, pb_nsim = 100L )
predictor |
Predictor column name. |
subject |
Subject ID column name. |
outcome |
Outcome column name. |
item |
Optional item ID column name. |
test_method |
Inference method: |
null_formula |
Null-model formula required for |
pb_nsim |
Bootstrap replicates for |
A list containing simulate_fun, fit_fun, and test_fun.
Build an lme4 backend for Negative Binomial GLMM scenarios
mp_backend_lme4_nb( predictor = "condition", subject = "subject", outcome = "y", item = NULL, test_method = c("wald", "lrt", "pb"), null_formula = NULL, pb_nsim = 100L )mp_backend_lme4_nb( predictor = "condition", subject = "subject", outcome = "y", item = NULL, test_method = c("wald", "lrt", "pb"), null_formula = NULL, pb_nsim = 100L )
predictor |
Predictor column name. |
subject |
Subject ID column name. |
outcome |
Outcome column name. |
item |
Optional item ID column name. |
test_method |
Inference method: |
null_formula |
Null-model formula required for |
pb_nsim |
Bootstrap replicates for |
A list containing simulate_fun, fit_fun, and test_fun.
Build an lme4 backend for Poisson GLMM scenarios
mp_backend_lme4_poisson( predictor = "condition", subject = "subject", outcome = "y", item = NULL, test_method = c("wald", "lrt", "pb"), null_formula = NULL, pb_nsim = 100L )mp_backend_lme4_poisson( predictor = "condition", subject = "subject", outcome = "y", item = NULL, test_method = c("wald", "lrt", "pb"), null_formula = NULL, pb_nsim = 100L )
predictor |
Predictor column name. |
subject |
Subject ID column name. |
outcome |
Outcome column name. |
item |
Optional item ID column name. |
test_method |
Inference method: |
null_formula |
Null-model formula required for |
pb_nsim |
Bootstrap replicates for |
A list containing simulate_fun, fit_fun, and test_fun.
Combines a single result object (mp_power, mp_sensitivity, or mp_power_curve), a reproducibility manifest, and optional user labels into one object. Diagnostics and result structure are retained unchanged.
mp_bundle_results( result, manifest, study_id = NULL, analyst = NULL, notes = NULL )mp_bundle_results( result, manifest, study_id = NULL, analyst = NULL, notes = NULL )
result |
An object of class |
manifest |
An |
study_id |
Optional character; study or run identifier. |
analyst |
Optional character; analyst name or ID. |
notes |
Optional character; free-form notes. |
An object of class mp_bundle with components result, manifest,
and labels (list with study_id, analyst, notes).
Runs the scenario under the null hypothesis — the focal fixed effect set to
zero — and estimates the empirical Type I error rate, i.e. the proportion of
replicates in which the (false) effect is declared significant. A trustworthy
test rejects at approximately alpha. The estimate is compared to alpha
using an exact (Clopper-Pearson) interval, giving a verdict:
mp_calibrate( scenario, term = NULL, nsim = 1000, alpha = 0.05, seed = NULL, conf_level = 0.95, failure_policy = c("count_as_nondetect", "exclude") )mp_calibrate( scenario, term = NULL, nsim = 1000, alpha = 0.05, seed = NULL, conf_level = 0.95, failure_policy = c("count_as_nondetect", "exclude") )
scenario |
An |
term |
Focal fixed-effect term to null out. Defaults to the scenario's test term (or the first non-intercept fixed effect). |
nsim |
Number of null simulations (default 1000; Type I estimation needs more replicates than a power point estimate for a tight interval). |
alpha |
Nominal significance level being checked (default 0.05). |
seed |
Optional seed for reproducibility. |
conf_level |
Confidence level for the Type I interval (default 0.95). |
failure_policy |
Passed to |
"well-calibrated": the interval contains alpha.
"anti-conservative": the interval lies entirely above alpha (the test
rejects too often — e.g. a Wald test with few clusters, or a model that
omits a random slope that is actually present in the data-generating
process). Power computed with such a test is not trustworthy.
"conservative": the interval lies entirely below alpha.
This is the recommended sanity check to run before trusting a power number: if a design/analysis combination does not control Type I error, its power is meaningless.
An object of class mp_calibration: a list with term, alpha,
type1 (empirical Type I rate), ci, conf_level, mcse, nsim,
verdict, and the underlying power_result.
mp_recommend_method(), mp_power().
if (requireNamespace("lme4", quietly = TRUE)) { d <- mp_design(list(subject = 40), trials_per_cell = 6) a <- mp_assumptions( fixed_effects = list("(Intercept)" = 0, condition = 0.4), random_effects = list(subject = list(intercept_sd = 0.5)), residual_sd = 1 ) scn <- mp_scenario_lme4(y ~ condition + (1 | subject), design = d, assumptions = a) mp_calibrate(scn, nsim = 200, seed = 1) }if (requireNamespace("lme4", quietly = TRUE)) { d <- mp_design(list(subject = 40), trials_per_cell = 6) a <- mp_assumptions( fixed_effects = list("(Intercept)" = 0, condition = 0.4), random_effects = list(subject = list(intercept_sd = 0.5)), residual_sd = 1 ) scn <- mp_scenario_lme4(y ~ condition + (1 | subject), design = d, assumptions = a) mp_calibrate(scn, nsim = 200, seed = 1) }
Simulates data once per replicate from the first scenario, then fits and
tests every supplied scenario on that same dataset. This is the analogue of
powerlmm's sim_formula: because the models see identical data, differences
in their rejection rates isolate the effect of the analysis choice. Use it to
study power across competing specifications, or to expose Type I inflation
from a misspecified model (e.g. dropping a random slope that is present in the
data-generating process).
mp_compare_models( scenarios, nsim, alpha = 0.05, seed = NULL, conf_level = 0.95, failure_policy = c("count_as_nondetect", "exclude") )mp_compare_models( scenarios, nsim, alpha = 0.05, seed = NULL, conf_level = 0.95, failure_policy = c("count_as_nondetect", "exclude") )
scenarios |
A named list of |
nsim |
Positive integer number of simulations. |
alpha |
Significance threshold (default 0.05). |
seed |
Optional seed for reproducibility. |
conf_level |
Confidence level for the per-model power intervals. |
failure_policy |
How to treat failed fits (see |
All scenarios must share the same data-generating process (design and
assumptions); they should differ only in their analysis model (formula /
random-effects structure / test). The first scenario's simulate_fun drives
data generation.
An object of class mp_model_comparison with a results data frame
(one row per model: power, conf_low, conf_high, failure_rate,
n_effective, nsim).
if (requireNamespace("lme4", quietly = TRUE)) { d <- mp_design(list(subject = 30), trials_per_cell = 8) a <- mp_assumptions( fixed_effects = list("(Intercept)" = 0, condition = 0), random_effects = list(subject = list(intercept_sd = 0.5, slopes = list(condition = 0.8))), residual_sd = 1 ) maximal <- mp_scenario_lme4(y ~ condition + (1 + condition | subject), d, a) reduced <- mp_scenario_lme4(y ~ condition + (1 | subject), d, a) mp_compare_models(list(maximal = maximal, reduced = reduced), nsim = 50, seed = 1) }if (requireNamespace("lme4", quietly = TRUE)) { d <- mp_design(list(subject = 30), trials_per_cell = 8) a <- mp_assumptions( fixed_effects = list("(Intercept)" = 0, condition = 0), random_effects = list(subject = list(intercept_sd = 0.5, slopes = list(condition = 0.8))), residual_sd = 1 ) maximal <- mp_scenario_lme4(y ~ condition + (1 + condition | subject), d, a) reduced <- mp_scenario_lme4(y ~ condition + (1 | subject), d, a) mp_compare_models(list(maximal = maximal, reduced = reduced), nsim = 50, seed = 1) }
mp_design() encodes how data will be collected: cluster sizes and repeated
measurements. It does not encode effect sizes or analysis decisions.
mp_design( clusters, trials_per_cell = 1, predictors = NULL, nesting = NULL, notes = NULL )mp_design( clusters, trials_per_cell = 1, predictors = NULL, nesting = NULL, notes = NULL )
clusters |
Named list of positive integers. Example:
|
trials_per_cell |
Number of repeated observations per subject. A single positive integer (balanced), or a positive-integer vector recycled across subjects for an unbalanced design. |
predictors |
Optional named list giving the design type of each
predictor (each non-intercept fixed effect). Each entry is either a string
( |
nesting |
Optional named character vector mapping a child grouping
factor to its parent, e.g. |
notes |
Optional free text. |
An object of class mp_design.
d <- mp_design(clusters = list(subject = 40), trials_per_cell = 10) d # Three-level: 8 sites, 5 subjects per site, 4 trials each. d3 <- mp_design( clusters = list(site = 8, subject = 5), trials_per_cell = 4, nesting = c(subject = "site") )d <- mp_design(clusters = list(subject = 40), trials_per_cell = 10) d # Three-level: 8 sites, 5 subjects per site, 4 trials each. d3 <- mp_design( clusters = list(site = 8, subject = 5), trials_per_cell = 4, nesting = c(subject = "site") )
Sets target level counts for one or more grouping factors of a scenario built
with mp_from_fit(), so power can be evaluated at a sample size different
from the pilot's. This is the analogue of simr::extend(): levels are cloned
from the pilot's within-level structure with fresh ids, and fresh random
effects are drawn from the fitted covariance, so the extended data represent a
larger (or smaller) sample from the same population.
mp_extend(scenario, ...)mp_extend(scenario, ...)
scenario |
An |
... |
Named target level counts, e.g. |
Pair with mp_power() for a single N, or with mp_power_curve() /
mp_solve_sample_size() using the extend.<group> key for a curve over N.
The scenario with its extend targets set.
if (requireNamespace("lme4", quietly = TRUE)) { m <- lme4::lmer(Reaction ~ Days + (Days | Subject), data = lme4::sleepstudy) scn <- mp_from_fit(m, test_term = "Days") big <- mp_extend(scn, Subject = 40) mp_power(big, nsim = 20, seed = 1) }if (requireNamespace("lme4", quietly = TRUE)) { m <- lme4::lmer(Reaction ~ Days + (Days | Subject), data = lme4::sleepstudy) scn <- mp_from_fit(m, test_term = "Days") big <- mp_extend(scn, Subject = 40) mp_power(big, nsim = 20, seed = 1) }
Turns an existing lmer/glmer fit (e.g. from pilot or published data) into
an mp_scenario, so its estimated effects and variance components inform a
simulation-based power analysis. New responses are simulated from the fitted
model with stats::simulate() (keeping the estimated random-effect structure
and residual variance), the model is refit, and the focal term is tested.
mp_from_fit( fit, test_term = NULL, test_method = NULL, null_formula = NULL, pb_nsim = 100L, extend = NULL )mp_from_fit( fit, test_term = NULL, test_method = NULL, null_formula = NULL, pb_nsim = 100L, extend = NULL )
fit |
A fitted model of class |
test_term |
Fixed-effect term to test. Defaults to the first non-intercept fixed effect. |
test_method |
Inference method. Gaussian fits allow |
null_formula |
Null-model formula required for |
pb_nsim |
Bootstrap replicates for |
extend |
Optional named list of target level counts per grouping factor
(e.g. |
The fixed effects used to simulate are read from the scenario's assumptions,
which start at the fitted coefficients. This means mp_sensitivity() /
mp_power_curve() can vary an effect size (e.g.
fixed_effects.condition) for data-based vs smallest-effect-of-interest
comparisons. Sample size can be scaled up or down from the pilot with
mp_extend() (or the extend.<group> sensitivity key), which clones the
pilot's structure with fresh levels and fresh random effects.
An object of class mp_scenario.
if (requireNamespace("lme4", quietly = TRUE)) { m <- lme4::lmer(Reaction ~ Days + (Days | Subject), data = lme4::sleepstudy) scn <- mp_from_fit(m, test_term = "Days") mp_power(scn, nsim = 20, seed = 1) }if (requireNamespace("lme4", quietly = TRUE)) { m <- lme4::lmer(Reaction ~ Days + (Days | Subject), data = lme4::sleepstudy) scn <- mp_from_fit(m, test_term = "Days") mp_power(scn, nsim = 20, seed = 1) }
Returns a numeric vector suitable for mp_solve_sample_size()'s grid
argument. Specify either the number of points (length.out) or the step
size (by); bounds are always explicit (from, to).
mp_grid_sample_size(from, to, length.out = NULL, by = NULL)mp_grid_sample_size(from, to, length.out = NULL, by = NULL)
from |
Lower bound (inclusive). |
to |
Upper bound (inclusive). |
length.out |
Number of points (optional). Uses |
by |
Step size (optional). Uses |
A numeric vector. For integer cluster sizes, use
round(mp_grid_sample_size(...)) or pass by as an integer (e.g. by = 10).
mp_grid_sample_size(20, 100, length.out = 9) mp_grid_sample_size(20, 100, by = 10)mp_grid_sample_size(20, 100, length.out = 9) mp_grid_sample_size(20, 100, by = 10)
Captures scenario fingerprint, seed strategy, session info, timestamp, and
optional git SHA so results can be reproduced or audited. Output is a plain
list (and one-row data frame via as.data.frame()) suitable for saving
alongside results.
mp_manifest(scenario, seed = NULL, session = TRUE)mp_manifest(scenario, seed = NULL, session = TRUE)
scenario |
An |
seed |
The seed value used (or |
session |
Include full |
A list with components: scenario_digest, seed, seed_strategy,
timestamp, r_version, mixpower_version, session_info (if requested),
git_sha (if in a git repo). Use as.data.frame() on the list for a
single-row table (list components become columns where possible).
Produces a ready-to-edit prose description of a simulation-based power
analysis from an mp_power() result: the design, model, effect tested,
inference method, number of simulations, and the estimated power with its
interval. Intended to seed the "Power analysis" paragraph of a methods
section.
mp_methods_text(result, software = TRUE)mp_methods_text(result, software = TRUE)
result |
An |
software |
Include a sentence naming the mixpower package and version
(default |
A length-1 character string with class mp_methods_text (its
print() method word-wraps the paragraph).
if (requireNamespace("lme4", quietly = TRUE)) { d <- mp_design(list(subject = 30), trials_per_cell = 6) a <- mp_assumptions(list("(Intercept)" = 0, condition = 0.4), random_effects = list(subject = list(intercept_sd = 0.5)), residual_sd = 1) scn <- mp_scenario_lme4(y ~ condition + (1 | subject), design = d, assumptions = a) mp_methods_text(mp_power(scn, nsim = 50, seed = 1)) }if (requireNamespace("lme4", quietly = TRUE)) { d <- mp_design(list(subject = 30), trials_per_cell = 6) a <- mp_assumptions(list("(Intercept)" = 0, condition = 0.4), random_effects = list(subject = list(intercept_sd = 0.5)), residual_sd = 1) scn <- mp_scenario_lme4(y ~ condition + (1 | subject), design = d, assumptions = a) mp_methods_text(mp_power(scn, nsim = 50, seed = 1)) }
Wraps a scenario so that, on every replicate, observations are deleted from the simulated data before the model is fit. This lets a power analysis reflect realistic incomplete data (Gallop & Liu, 2017; Magnusson, 2018). Three mechanisms are supported:
mp_missing( scenario, mechanism = c("mcar", "mar", "dropout"), prob = NULL, on = NULL, slope = 0, time = NULL, dropout = NULL, subject = "subject" )mp_missing( scenario, mechanism = c("mcar", "mar", "dropout"), prob = NULL, on = NULL, slope = 0, time = NULL, dropout = NULL, subject = "subject" )
scenario |
An |
mechanism |
One of |
prob |
Baseline deletion probability for |
on |
Name of the observed column the |
slope |
Logit-scale slope for |
time |
Name of the within-subject ordering column for |
dropout |
For |
subject |
Subject grouping column (default |
"mcar": each observation is deleted independently with probability
prob (missing completely at random).
"mar": each observation is deleted with a probability that depends on an
observed column on through a logistic model
plogis(qlogis(prob) + slope * on) (missing at random).
"dropout": monotone longitudinal dropout along time within each
subject — once a subject drops out it contributes no later observations.
The dropout pattern is given either by dropout as a vector of cumulative
dropout proportions (one per ordered timepoint) or as
list(shape =, scale =) for a Weibull dropout time on the time scale.
The scenario with its simulator wrapped to apply missingness.
if (requireNamespace("lme4", quietly = TRUE)) { d <- mp_design(list(subject = 30), trials_per_cell = 6, predictors = list(time = "continuous")) a <- mp_assumptions(list("(Intercept)" = 0, time = 0.4), random_effects = list(subject = list(intercept_sd = 0.5)), residual_sd = 1) scn <- mp_scenario_lme4(y ~ time + (1 | subject), design = d, assumptions = a, predictor = "time") scn_drop <- mp_missing(scn, "dropout", time = "time", dropout = c(0, 0.1, 0.2, 0.35, 0.5, 0.6)) mp_power(scn_drop, nsim = 20, seed = 1) }if (requireNamespace("lme4", quietly = TRUE)) { d <- mp_design(list(subject = 30), trials_per_cell = 6, predictors = list(time = "continuous")) a <- mp_assumptions(list("(Intercept)" = 0, time = 0.4), random_effects = list(subject = list(intercept_sd = 0.5)), residual_sd = 1) scn <- mp_scenario_lme4(y ~ time + (1 | subject), design = d, assumptions = a, predictor = "time") scn_drop <- mp_missing(scn, "dropout", time = "time", dropout = c(0, 0.1, 0.2, 0.35, 0.5, 0.6)) mp_power(scn_drop, nsim = 20, seed = 1) }
mp_power() runs repeated simulations under a scenario and estimates power for
the scenario's test decision rule (typically p < alpha).
mp_power( scenario, nsim, alpha = 0.05, seed = NULL, failure_policy = c("count_as_nondetect", "exclude"), keep = c("minimal", "fits", "data"), conf_level = 0.95, ci_method = c("clopper-pearson", "wald"), aggregate = c("full", "streaming") )mp_power( scenario, nsim, alpha = 0.05, seed = NULL, failure_policy = c("count_as_nondetect", "exclude"), keep = c("minimal", "fits", "data"), conf_level = 0.95, ci_method = c("clopper-pearson", "wald"), aggregate = c("full", "streaming") )
scenario |
An |
nsim |
Positive integer number of simulations. |
alpha |
Significance threshold for a detection (default 0.05). |
seed |
Optional seed for reproducibility. |
failure_policy |
How to treat failed fits / missing p-values:
|
keep |
What to store:
|
conf_level |
Confidence level for the power interval (default 0.95). |
ci_method |
Power CI type: |
aggregate |
|
In Phase 4 core, the scenario must provide engine functions:
simulate_fun, fit_fun, and test_fun. Later phases will supply defaults
based on specific backends (e.g., lme4).
Diagnostics include Type S (wrong-sign) and Type M (exaggeration-ratio) errors among significant replicates (Gelman & Carlin, 2014), computed when the tested term's true effect is known and non-zero and the backend reports an estimate.
An object of class mp_power.
# A tiny toy engine (not mixed models) just to demonstrate the workflow: d <- mp_design(list(subject = 30), trials_per_cell = 1) a <- mp_assumptions(list(condition = 0.3), residual_sd = 1) sim_fun <- function(scn, seed) { n <- scn$design$clusters$subject x <- stats::rbinom(n, 1, 0.5) y <- scn$assumptions$fixed_effects$condition * x + stats::rnorm(n, sd = scn$assumptions$residual_sd) data.frame(y = y, condition = x) } fit_fun <- function(dat, scn) stats::lm(scn$formula, data = dat) test_fun <- function(fit, scn) { sm <- summary(fit) p <- sm$coefficients["condition", "Pr(>|t|)"] list(p_value = as.numeric(p)) } s <- mp_scenario( y ~ condition, d, a, simulate_fun = sim_fun, fit_fun = fit_fun, test_fun = test_fun ) res <- mp_power(s, nsim = 50, seed = 1) summary(res)# A tiny toy engine (not mixed models) just to demonstrate the workflow: d <- mp_design(list(subject = 30), trials_per_cell = 1) a <- mp_assumptions(list(condition = 0.3), residual_sd = 1) sim_fun <- function(scn, seed) { n <- scn$design$clusters$subject x <- stats::rbinom(n, 1, 0.5) y <- scn$assumptions$fixed_effects$condition * x + stats::rnorm(n, sd = scn$assumptions$residual_sd) data.frame(y = y, condition = x) } fit_fun <- function(dat, scn) stats::lm(scn$formula, data = dat) test_fun <- function(fit, scn) { sm <- summary(fit) p <- sm$coefficients["condition", "Pr(>|t|)"] list(p_value = as.numeric(p)) } s <- mp_scenario( y ~ condition, d, a, simulate_fun = sim_fun, fit_fun = fit_fun, test_fun = test_fun ) res <- mp_power(s, nsim = 50, seed = 1) summary(res)
Runs mp_power() in batches, saving the accumulated per-replicate results to
file (an .rds) after each batch. If file already exists for the same
seed, the run resumes from where it left off and only the remaining
replicates are simulated. This makes very large or long-running power
analyses robust to interruption, and lets you grow nsim later without
recomputing finished replicates.
mp_power_checkpoint( scenario, nsim, file, batch_size = 100, alpha = 0.05, seed = NULL, failure_policy = c("count_as_nondetect", "exclude"), conf_level = 0.95, ci_method = c("clopper-pearson", "wald"), progress = TRUE )mp_power_checkpoint( scenario, nsim, file, batch_size = 100, alpha = 0.05, seed = NULL, failure_policy = c("count_as_nondetect", "exclude"), conf_level = 0.95, ci_method = c("clopper-pearson", "wald"), progress = TRUE )
scenario |
An |
nsim |
Total number of simulations desired. |
file |
Path to the checkpoint |
batch_size |
Replicates per batch (default 100). |
alpha, seed, failure_policy, conf_level, ci_method
|
As in |
progress |
Emit a progress message after each batch (default |
Because replicate seeds are deterministic (seed + i - 1), the result is
identical to a single mp_power(scenario, nsim, seed = seed) call: batching
only changes when work happens, not what is computed.
An object of class mp_power for all nsim replicates.
if (requireNamespace("lme4", quietly = TRUE)) { d <- mp_design(list(subject = 30), trials_per_cell = 6) a <- mp_assumptions(list("(Intercept)" = 0, condition = 0.4), random_effects = list(subject = list(intercept_sd = 0.5)), residual_sd = 1) scn <- mp_scenario_lme4(y ~ condition + (1 | subject), design = d, assumptions = a) f <- tempfile(fileext = ".rds") mp_power_checkpoint(scn, nsim = 60, file = f, batch_size = 20, seed = 1) }if (requireNamespace("lme4", quietly = TRUE)) { d <- mp_design(list(subject = 30), trials_per_cell = 6) a <- mp_assumptions(list("(Intercept)" = 0, condition = 0.4), random_effects = list(subject = list(intercept_sd = 0.5)), residual_sd = 1) scn <- mp_scenario_lme4(y ~ condition + (1 | subject), design = d, assumptions = a) f <- tempfile(fileext = ".rds") mp_power_checkpoint(scn, nsim = 60, file = f, batch_size = 20, seed = 1) }
Runs mp_power() across a one-dimensional grid of values for one parameter
(e.g. cluster size) via mp_sensitivity(). Results include power estimates
and per-grid-point diagnostics: failure rate, singular rate, and effective N.
mp_power_curve( scenario, vary, nsim = 100, alpha = 0.05, seed = NULL, failure_policy = c("count_as_nondetect", "exclude"), conf_level = 0.95 )mp_power_curve( scenario, vary, nsim = 100, alpha = 0.05, seed = NULL, failure_policy = c("count_as_nondetect", "exclude"), conf_level = 0.95 )
scenario |
An |
vary |
Named list with a single key (e.g. |
nsim |
Number of simulations per grid point (default 100). |
alpha |
Significance level (default 0.05). |
seed |
Optional seed for reproducibility. |
failure_policy |
How to treat failed fits: |
conf_level |
Confidence level for power intervals (default 0.95). |
An object of class mp_power_curve with components vary, grid,
results (estimate, mcse, conf_low, conf_high, failure_rate, singular_rate,
n_effective, nsim, plus the varying parameter column), alpha, failure_policy,
and conf_level.
Evaluates power over a one-parameter grid by running mp_power() for each
grid cell in parallel. Uses explicit per-cell seeds (seed + cell_index - 1L)
so results are deterministic and match serial mp_power_curve() for the same
seed. Does not modify mp_power(); parallelization is at the scenario-grid
level only.
mp_power_curve_parallel( scenario, vary, workers = 2L, nsim = 100, alpha = 0.05, seed = NULL, failure_policy = c("count_as_nondetect", "exclude"), conf_level = 0.95, progress = FALSE, ... )mp_power_curve_parallel( scenario, vary, workers = 2L, nsim = 100, alpha = 0.05, seed = NULL, failure_policy = c("count_as_nondetect", "exclude"), conf_level = 0.95, progress = FALSE, ... )
scenario |
An |
vary |
Named list with exactly one parameter (e.g. |
workers |
Number of parallel workers (default 2). |
nsim |
Number of simulations per grid point (default 100). |
alpha |
Significance level (default 0.05). |
seed |
Optional base seed; each cell gets |
failure_policy |
How to treat failed fits: |
conf_level |
Confidence level for power intervals (default 0.95). |
progress |
If |
... |
Unused; reserved for future arguments. |
An object of class mp_power_curve (same structure as mp_power_curve()).
Parallel execution requires the parallel package (base R) and that
mixpower is installed (e.g. install.packages() or
devtools::install()) so that workers can load it.
One-call wrapper that builds design, assumptions, scenario (lme4 LMM), and
runs mp_power(). Intended for the common case: one fixed effect, one
random intercept. All arguments are explicit; pass further options to
mp_power() via ... (e.g. failure_policy, conf_level, keep).
mp_quick_power( formula, clusters, trials_per_cell = 1, fixed_effects, residual_sd, nsim, alpha = 0.05, seed = NULL, random_effects = NULL, icc = NULL, test_method = c("wald", "lrt"), null_formula = NULL, predictor = "condition", subject = "subject", outcome = "y", item = NULL, ... )mp_quick_power( formula, clusters, trials_per_cell = 1, fixed_effects, residual_sd, nsim, alpha = 0.05, seed = NULL, random_effects = NULL, icc = NULL, test_method = c("wald", "lrt"), null_formula = NULL, predictor = "condition", subject = "subject", outcome = "y", item = NULL, ... )
formula |
Model formula (e.g. |
clusters |
Named list of cluster sizes (e.g. |
trials_per_cell |
Number of observations per cell (default 1). |
fixed_effects |
Named list of effect sizes (e.g. |
residual_sd |
Residual standard deviation. |
nsim |
Number of simulations. |
alpha |
Significance level (default 0.05). |
seed |
Optional seed for reproducibility. |
random_effects |
Optional named list of random-effect sizes, e.g.
|
icc |
Deprecated; interpreted as a random-intercept SD. Use
|
test_method |
|
null_formula |
Required when |
predictor |
Predictor column name (default |
subject |
Subject ID column name (default |
outcome |
Outcome column name (default |
item |
Optional item ID column name. |
... |
Arguments passed to |
The result of mp_power() (object of class mp_power).
mp_quick_power( y ~ condition + (1 | subject), clusters = list(subject = 40), trials_per_cell = 8, fixed_effects = list(`(Intercept)` = 0, condition = 0.3), residual_sd = 1, nsim = 50, seed = 123 )mp_quick_power( y ~ condition + (1 | subject), clusters = list(subject = 40), trials_per_cell = 8, fixed_effects = list(`(Intercept)` = 0, condition = 0.3), residual_sd = 1, nsim = 50, seed = 123 )
Heuristic guidance on the test_method for a scenario, based on the number of
levels of the random grouping factors. Wald (normal-approximation z/t) tests
and, to a lesser extent, likelihood-ratio tests are known to be
anti-conservative when the number of clusters is small (Luke, 2017): the
degrees of freedom are overstated, so the test rejects too often. With few
clusters, a degrees-of-freedom-corrected test (Satterthwaite or Kenward-Roger,
for linear mixed models) or a parametric bootstrap (any family) controls Type
I error far better.
mp_recommend_method(scenario, small_clusters = 30L)mp_recommend_method(scenario, small_clusters = 30L)
scenario |
An |
small_clusters |
Threshold below which the smallest grouping factor is treated as "few clusters" (default 30). |
This is a fast, design-based heuristic; to measure a specific design and
method, use mp_calibrate().
An object of class mp_recommendation: a list with method (the
scenario's current method), n_groups (smallest grouping-factor size),
is_lmm, caution (logical), recommended (character vector), and
rationale.
d <- mp_design(list(subject = 12), trials_per_cell = 8) a <- mp_assumptions( fixed_effects = list("(Intercept)" = 0, condition = 0.4), random_effects = list(subject = list(intercept_sd = 0.5)), residual_sd = 1 ) scn <- mp_scenario_lme4(y ~ condition + (1 | subject), design = d, assumptions = a) mp_recommend_method(scn)d <- mp_design(list(subject = 12), trials_per_cell = 8) a <- mp_assumptions( fixed_effects = list("(Intercept)" = 0, condition = 0.4), random_effects = list(subject = list(intercept_sd = 0.5)), residual_sd = 1 ) scn <- mp_scenario_lme4(y ~ condition + (1 | subject), design = d, assumptions = a) mp_recommend_method(scn)
Returns a flat data frame with power estimate, CI, failure/singularity rates,
and effective simulation counts. Works with mp_power, mp_sensitivity,
mp_power_curve, or the result of mp_bundle_results() (uses the bundled result).
mp_report_table(x, ...)mp_report_table(x, ...)
x |
An object of class |
... |
Unused; reserved for future arguments. |
A data frame: for mp_power one row; for mp_calibration a one-row
Type I summary; for sensitivity/curve one row
per grid cell with parameter column(s), power_estimate, ci_low, ci_high,
failure_rate, singular_rate, n_effective, nsim.
Computes a safeguard effect for power analysis: the bound of a confidence interval for a fitted effect that lies closest to zero (Perugini, Gallucci & Costantini, 2014). Planning power around this conservative, uncertainty-aware value protects against the optimism of using a noisy pilot point estimate.
mp_safeguard_effect(fit, term = NULL, conf_level = 0.9)mp_safeguard_effect(fit, term = NULL, conf_level = 0.9)
fit |
A fitted |
term |
Fixed-effect term. Defaults to the first non-intercept effect. |
conf_level |
Two-sided confidence level for the interval (default
|
The interval is the Wald (normal-approximation) interval from the fitted
coefficient and its standard error. Pair the result with mp_from_fit() and
mp_sesoi() to run a safeguard-power simulation.
An object of class mp_safeguard: a list with term, estimate,
se, conf_level, the interval (lower, upper), and the safeguard
bound (the interval limit nearest zero, in the direction of the estimate).
if (requireNamespace("lme4", quietly = TRUE)) { m <- lme4::lmer(Reaction ~ Days + (Days | Subject), data = lme4::sleepstudy) sg <- mp_safeguard_effect(m, term = "Days", conf_level = 0.90) sg }if (requireNamespace("lme4", quietly = TRUE)) { m <- lme4::lmer(Reaction ~ Days + (Days | Subject), data = lme4::sleepstudy) sg <- mp_safeguard_effect(m, term = "Days", conf_level = 0.90) sg }
A scenario combines: (1) a design, (2) assumptions, (3) a model specification, and (4) an analysis engine.
mp_scenario( formula, design, assumptions, test = c("wald", "lrt", "custom"), simulate_fun = NULL, fit_fun = NULL, test_fun = NULL, notes = NULL )mp_scenario( formula, design, assumptions, test = c("wald", "lrt", "custom"), simulate_fun = NULL, fit_fun = NULL, test_fun = NULL, notes = NULL )
formula |
A model formula (stored for later backends). |
design |
An |
assumptions |
An |
test |
Character string or list identifying the test type (metadata). |
simulate_fun |
Function or NULL. |
fit_fun |
Function or NULL. |
test_fun |
Function or NULL. |
notes |
Optional free text. |
In Phase 1, the engine is pluggable via three functions:
simulate_fun(scenario, seed) returns a data.frame
fit_fun(data, scenario) returns a fit object
test_fun(fit, scenario) returns a list with at least p_value (numeric scalar)
This allows mp_power() to run before selecting a specific backend (e.g., lme4).
An object of class mp_scenario.
d <- mp_design(list(subject = 20), trials_per_cell = 5) a <- mp_assumptions(list(condition = 0.3), residual_sd = 1) s <- mp_scenario(y ~ condition, d, a, test = "wald") sd <- mp_design(list(subject = 20), trials_per_cell = 5) a <- mp_assumptions(list(condition = 0.3), residual_sd = 1) s <- mp_scenario(y ~ condition, d, a, test = "wald") s
Same data-generating process as mp_scenario_lme4() but fits with glmmTMB
(glmmTMB()).
mp_scenario_glmmtmb_lmm( formula, design, assumptions, predictor = "condition", subject = "subject", outcome = "y", item = NULL, test_term = predictor, test_method = c("wald", "lrt"), null_formula = NULL )mp_scenario_glmmtmb_lmm( formula, design, assumptions, predictor = "condition", subject = "subject", outcome = "y", item = NULL, test_term = predictor, test_method = c("wald", "lrt"), null_formula = NULL )
formula |
Model formula. |
design |
A |
assumptions |
A |
predictor |
Predictor column name. |
subject |
Subject ID column name. |
outcome |
Outcome column name. |
item |
Optional item ID column name. |
test_term |
Term to test. A single fixed effect (default |
test_method |
Inference method: |
null_formula |
Null-model formula required for |
Create a fully specified MixPower scenario with the lme4 backend
mp_scenario_lme4( formula, design, assumptions, predictor = "condition", subject = "subject", outcome = "y", item = NULL, test_term = predictor, test_method = c("wald", "lrt", "satterthwaite", "kenward-roger", "pb"), null_formula = NULL, pb_nsim = 100L, contrast = NULL )mp_scenario_lme4( formula, design, assumptions, predictor = "condition", subject = "subject", outcome = "y", item = NULL, test_term = predictor, test_method = c("wald", "lrt", "satterthwaite", "kenward-roger", "pb"), null_formula = NULL, pb_nsim = 100L, contrast = NULL )
formula |
Model formula. |
design |
A |
assumptions |
A |
predictor |
Predictor column name. |
subject |
Subject ID column name. |
outcome |
Outcome column name. |
item |
Optional item ID column name. |
test_term |
Term to test. A single fixed effect (default |
test_method |
Inference method: |
null_formula |
Null-model formula required for |
pb_nsim |
Bootstrap replicates for |
contrast |
Optional named numeric vector of fixed-effect weights
defining a linear contrast |
An object of class mp_scenario.
Create a fully specified MixPower scenario with the binomial lme4 backend
mp_scenario_lme4_binomial( formula, design, assumptions, predictor = "condition", subject = "subject", outcome = "y", item = NULL, test_term = predictor, test_method = c("wald", "lrt", "pb"), null_formula = NULL, pb_nsim = 100L )mp_scenario_lme4_binomial( formula, design, assumptions, predictor = "condition", subject = "subject", outcome = "y", item = NULL, test_term = predictor, test_method = c("wald", "lrt", "pb"), null_formula = NULL, pb_nsim = 100L )
formula |
Model formula. |
design |
A |
assumptions |
A |
predictor |
Predictor column name. |
subject |
Subject ID column name. |
outcome |
Outcome column name. |
item |
Optional item ID column name. |
test_term |
Optional explicit term to test. Defaults to |
test_method |
Inference method: |
null_formula |
Null-model formula required for |
pb_nsim |
Bootstrap replicates for |
An object of class mp_scenario.
Create a fully specified MixPower scenario with the NB lme4 backend
mp_scenario_lme4_nb( formula, design, assumptions, predictor = "condition", subject = "subject", outcome = "y", item = NULL, test_term = predictor, test_method = c("wald", "lrt", "pb"), null_formula = NULL, pb_nsim = 100L )mp_scenario_lme4_nb( formula, design, assumptions, predictor = "condition", subject = "subject", outcome = "y", item = NULL, test_term = predictor, test_method = c("wald", "lrt", "pb"), null_formula = NULL, pb_nsim = 100L )
formula |
Model formula. |
design |
A |
assumptions |
A |
predictor |
Predictor column name. |
subject |
Subject ID column name. |
outcome |
Outcome column name. |
item |
Optional item ID column name. |
test_term |
Optional explicit term to test. Defaults to |
test_method |
Inference method: |
null_formula |
Null-model formula required for |
pb_nsim |
Bootstrap replicates for |
An object of class mp_scenario.
Create a fully specified MixPower scenario with the Poisson lme4 backend
mp_scenario_lme4_poisson( formula, design, assumptions, predictor = "condition", subject = "subject", outcome = "y", item = NULL, test_term = predictor, test_method = c("wald", "lrt", "pb"), null_formula = NULL, pb_nsim = 100L )mp_scenario_lme4_poisson( formula, design, assumptions, predictor = "condition", subject = "subject", outcome = "y", item = NULL, test_term = predictor, test_method = c("wald", "lrt", "pb"), null_formula = NULL, pb_nsim = 100L )
formula |
Model formula. |
design |
A |
assumptions |
A |
predictor |
Predictor column name. |
subject |
Subject ID column name. |
outcome |
Outcome column name. |
item |
Optional item ID column name. |
test_term |
Optional explicit term to test. Defaults to |
test_method |
Inference method: |
null_formula |
Null-model formula required for |
pb_nsim |
Bootstrap replicates for |
An object of class mp_scenario.
Run power sensitivity analysis over a parameter grid
mp_sensitivity( scenario, vary, nsim = 100, alpha = 0.05, seed = NULL, failure_policy = c("count_as_nondetect", "exclude"), conf_level = 0.95 )mp_sensitivity( scenario, vary, nsim = 100, alpha = 0.05, seed = NULL, failure_policy = c("count_as_nondetect", "exclude"), conf_level = 0.95 )
scenario |
A base |
vary |
Named list of vectors. Names are dotted paths such as
|
nsim |
Number of simulations for each grid cell. |
alpha |
Significance threshold. |
seed |
Optional seed for reproducible cell-wise execution. |
failure_policy |
Failure policy passed to |
conf_level |
Confidence level passed to |
An object of class mp_sensitivity.
Like mp_sensitivity(), but evaluates each grid cell in parallel (or with a
progress bar when progress = TRUE). Uses per-cell seeds seed + cell_index - 1L
to match a serial ordering convention. Does not modify mp_power().
mp_sensitivity_parallel( scenario, vary, nsim = 100, alpha = 0.05, seed = NULL, failure_policy = c("count_as_nondetect", "exclude"), conf_level = 0.95, workers = 2L, progress = FALSE, checkpoint_dir = NULL, resume = TRUE, ... )mp_sensitivity_parallel( scenario, vary, nsim = 100, alpha = 0.05, seed = NULL, failure_policy = c("count_as_nondetect", "exclude"), conf_level = 0.95, workers = 2L, progress = FALSE, checkpoint_dir = NULL, resume = TRUE, ... )
scenario |
A base |
vary |
Named list of vectors. Names are dotted paths such as
|
nsim |
Number of simulations for each grid cell. |
alpha |
Significance threshold. |
seed |
Optional seed for reproducible cell-wise execution. |
failure_policy |
Failure policy passed to |
conf_level |
Confidence level passed to |
workers |
Number of parallel workers when |
progress |
If |
checkpoint_dir |
Optional directory to save per-cell RDS results and a
manifest. When |
resume |
Logical; only used when |
... |
Reserved. |
An object of class mp_sensitivity (same structure as mp_sensitivity()).
Parallel execution requires the parallel package and that mixpower can be loaded on workers (installed package).
Convenience helper for planning power around a smallest effect size of
interest rather than a single point estimate. It returns a copy of
scenario with the focal fixed effect replaced, either by an explicit
value or by scaling the current assumed effect (e.g. a conservative 15%
reduction, multiplier = 0.85). This is the recommended way to avoid
overoptimistic power based on a possibly inflated pilot/published effect
(Anderson, Kelley & Maxwell, 2017; Kumle, Vo & Draschkow, 2021).
mp_sesoi(scenario, multiplier = 0.85, effect = NULL, term = NULL)mp_sesoi(scenario, multiplier = 0.85, effect = NULL, term = NULL)
scenario |
An |
multiplier |
Numeric factor applied to the current fixed effect when
|
effect |
Optional explicit SESOI on the model's coefficient scale. When
supplied it overrides |
term |
Fixed-effect term to modify. Defaults to the scenario's test term (or the first non-intercept fixed effect). |
A modified mp_scenario object.
mp_safeguard_effect() for a data-driven conservative effect.
d <- mp_design(list(subject = 30), trials_per_cell = 8) a <- mp_assumptions( fixed_effects = list("(Intercept)" = 0, condition = 0.5), random_effects = list(subject = list(intercept_sd = 0.5)), residual_sd = 1 ) scn <- mp_scenario_lme4(y ~ condition + (1 | subject), design = d, assumptions = a) # Power for an effect 15% smaller than assumed: scn_sesoi <- mp_sesoi(scn, multiplier = 0.85) scn_sesoi$assumptions$fixed_effects$conditiond <- mp_design(list(subject = 30), trials_per_cell = 8) a <- mp_assumptions( fixed_effects = list("(Intercept)" = 0, condition = 0.5), random_effects = list(subject = list(intercept_sd = 0.5)), residual_sd = 1 ) scn <- mp_scenario_lme4(y ~ condition + (1 | subject), design = d, assumptions = a) # Power for an effect 15% smaller than assumed: scn_sesoi <- mp_sesoi(scn, multiplier = 0.85) scn_sesoi$assumptions$fixed_effects$condition
Evaluates power on a user-supplied grid of values for one parameter (e.g.
cluster size) via mp_power_curve(), then returns the smallest grid value
whose power estimate meets or exceeds the target. Diagnostics (failure rate,
singular rate, n_effective) are exposed in the returned results table.
mp_solve_sample_size( scenario, parameter, grid, target_power = 0.8, nsim = 100, alpha = 0.05, seed = NULL, failure_policy = c("count_as_nondetect", "exclude"), conf_level = 0.95 )mp_solve_sample_size( scenario, parameter, grid, target_power = 0.8, nsim = 100, alpha = 0.05, seed = NULL, failure_policy = c("count_as_nondetect", "exclude"), conf_level = 0.95 )
scenario |
An |
parameter |
Dotted path of the single parameter to vary (e.g. |
grid |
Numeric vector of candidate values. Use |
target_power |
Target power threshold (default 0.8). |
nsim |
Number of simulations per grid point (default 100). |
alpha |
Significance level (default 0.05). |
seed |
Optional seed for reproducibility. |
failure_policy |
How to treat failed fits: |
conf_level |
Confidence level for power intervals (default 0.95). |
A list with target_power, parameter, solution (numeric: minimum
grid value achieving target power, or NA if none), and results (data
frame with estimate, failure_rate, singular_rate, n_effective, etc., per
grid point).
Writes the report table (and for bundles, manifest/labels) to file. CSV writes
the publication-ready table only; JSON writes report table plus manifest and
labels when x is an mp_bundle.
mp_write_results(x, file, format = c("csv", "json"), ...)mp_write_results(x, file, format = c("csv", "json"), ...)
x |
An object from |
file |
Path to output file (extension need not match format). |
format |
|
... |
For CSV, arguments passed to |
Invisibly the path file.
Plot power results
plot_power(results, ...)plot_power(results, ...)
results |
A data.frame with effect and power columns. |
... |
Additional arguments passed to plot. |
Invisibly returns the plot data.
A histogram of the per-replicate p-values from mp_power(), with the alpha
threshold marked. The shaded area to the left of alpha is the estimated
power. Requires a run with aggregate = "full" (the default), which retains
per-replicate p-values.
## S3 method for class 'mp_power' plot(x, ...)## S3 method for class 'mp_power' plot(x, ...)
x |
An |
... |
Passed to |
Invisibly, the p-values plotted.
Plot a power curve
## S3 method for class 'mp_power_curve' plot(x, y = c("estimate", "failure_rate", "singular_rate", "n_effective"), ...)## S3 method for class 'mp_power_curve' plot(x, y = c("estimate", "failure_rate", "singular_rate", "n_effective"), ...)
x |
An |
y |
What to plot on the y-axis: |
... |
Arguments passed to |
Invisibly returns the plotted data.
For one varying parameter: line plot with optional CI segments when
y = "estimate". For two varying parameters: heatmap. More than two
parameters is not supported.
## S3 method for class 'mp_sensitivity' plot(x, y = c("estimate", "failure_rate", "singular_rate", "n_effective"), ...)## S3 method for class 'mp_sensitivity' plot(x, y = c("estimate", "failure_rate", "singular_rate", "n_effective"), ...)
x |
An |
y |
What to plot: |
... |
Additional graphical arguments passed to |
Invisibly returns the plotted data (1D: ordered data frame; 2D: matrix).
Placeholder for parallel execution
run_parallel(fun, ...)run_parallel(fun, ...)
fun |
Function to run. |
... |
Additional arguments to pass to fun. |
The result of fun.
Thin wrapper over the shared simulation engine (.mp_simulate_mixed) with a
logit link and Bernoulli response. Random-effect sizes (intercept and
optional slope on predictor) come from
scenario$assumptions$random_effects.
simulate_glmm_binomial_data( scenario, predictor = "condition", subject = "subject", outcome = "y", item = NULL )simulate_glmm_binomial_data( scenario, predictor = "condition", subject = "subject", outcome = "y", item = NULL )
scenario |
An |
predictor |
Predictor column name. |
subject |
Subject ID column name. |
outcome |
Outcome column name. |
item |
Optional item ID column name. |
A data.frame with outcome and predictors.
Thin wrapper over the shared simulation engine (.mp_simulate_mixed) with a
log link and negative-binomial response. Random-effect sizes (intercept and
optional slope on predictor) come from
scenario$assumptions$random_effects.
simulate_glmm_nb_data( scenario, predictor = "condition", subject = "subject", outcome = "y", item = NULL, theta = NULL )simulate_glmm_nb_data( scenario, predictor = "condition", subject = "subject", outcome = "y", item = NULL, theta = NULL )
scenario |
An |
predictor |
Predictor column name. |
subject |
Subject ID column name. |
outcome |
Outcome column name. |
item |
Optional item ID column name. |
theta |
NB dispersion parameter (size); larger means less
over-dispersion. Defaults to |
A data.frame with outcome and predictors.
Thin wrapper over the shared simulation engine (.mp_simulate_mixed) with a
log link and Poisson response. Random-effect sizes (intercept and optional
slope on predictor) come from scenario$assumptions$random_effects.
simulate_glmm_poisson_data( scenario, predictor = "condition", subject = "subject", outcome = "y", item = NULL )simulate_glmm_poisson_data( scenario, predictor = "condition", subject = "subject", outcome = "y", item = NULL )
scenario |
An |
predictor |
Predictor column name. |
subject |
Subject ID column name. |
outcome |
Outcome column name. |
item |
Optional item ID column name. |
A data.frame with outcome and predictors.
Run a simple simulation-based power study
simulate_power(scenario, nsim = 100, seed = NULL)simulate_power(scenario, nsim = 100, seed = NULL)
scenario |
A scenario object. |
nsim |
Number of simulations. |
seed |
Optional random seed. |
A data.frame of simulated p-values.
Summarize simulation outputs
summarize_simulations(simulations)summarize_simulations(simulations)
simulations |
A data.frame of simulations. |
A summary data.frame.
Extract a test statistic for a model term
test_effect(fit, term)test_effect(fit, term)
fit |
A fitted model object. |
term |
Term name to test. |
A data.frame with coefficient information.
Checks that simulate_fun, fit_fun, and test_fun are functions and that
their formal arguments are compatible with what mp_power() and
.run_one_rep() invoke. Does not run simulations.
validate_mp_backend(engine)validate_mp_backend(engine)
engine |
A |
Invisibly TRUE if valid; otherwise throws an error.