Simulation-based statistical power analysis for hierarchical mixed-effects
models (lme4::lmer() / lme4::glmer()), built on top of the
simr package. Works equally well
for field-monitoring designs (habitats/sites/visits) and lab-experiment
designs (tanks/individuals/trials) - see the vignette's study design
vocabulary section for a glossary mapping hierPoweR's generic terms to both.
# install.packages("pak")
pak::pak("pbzonek/hierPoweR")library(hierPoweR)
# A small synthetic nested dataset (subunit nested within unit) -
# e.g. sites within habitats, or fish within tanks
toy_data <- make_toy_hierarchical_data(n_units = 4, n_subunits = 3, n_occasions = 8)
model <- lme4::lmer(y ~ treatment + (1 | unit/subunit), data = toy_data)
# Extract variance components / ICC to see how "noisy" the design is
extract_variance_components(model)
icc_decompose(model)
# Build a hypothetical future design and simulate power for it
future_design <- build_design_scaffold(
group_levels = list(unit = paste0("unit_", 1:4), subunit_id = 1:5, occasion = 1:10),
time_var = "occasion"
)
future_design$subunit <- factor(paste(future_design$unit, future_design$subunit_id, sep = "_"))
simulate_power(model, design_data = future_design, effect_var = "treatmentpost",
effect_size = 0.5, n_sim = 100, seed = 1)
# Or sweep several design parameters at once
scenarios <- design_grid(n_subunits = c(3, 6, 9), effect_multipliers = c(0.25, 0.5, 0.75),
effect_base = lme4::fixef(model)[["treatmentpost"]])
# ...see vignette("getting-started", "hierPoweR") for the full design_fun + grid workflowextract_variance_components()/extract_fixed_effects()/icc_decompose()- pull tidy, name-matched parameters out of a fitted model to understand (and seed a simulation from) its variance structure.cross_design_factors()/build_design_scaffold()- build a hypothetical future sampling design from scratch: cross grouping levels, attach one or more higher-level lookup tables (chainable, for 3+ levels of nesting), assign a within-subject treatment split.design_grid()/simulate_power_grid()- sweep several design parameters simultaneously (e.g. replication x effect size), each scenario run viasimr, with optional parallel execution.min_design_for_power()/most_efficient_design()- search a table of power results for the smallest (or cheapest) design meeting a power target.extract_contrast()/backtransform_effect()- tidyemmeanscontrasts, rescaled back to raw response units.plot_power_curve()/plot_power_heatmap()/plot_variance_components()/plot_ranef_caterpillar()/plot_effect_forest()-ggplot2plotting helpers for the outputs above.
hierPoweR does not reimplement Monte Carlo simulation from a fitted mixed
model - simr already does that correctly for arbitrary lme4 fits. What
hierPoweR adds is everything around it: building the hypothetical design to
simulate on, orchestrating a grid of design scenarios instead of one variable
at a time, and reporting/plotting the results. See
vignette("getting-started", package = "hierPoweR") for the full
end-to-end workflow, including a glossary connecting the package's generic
unit/subunit/occasion/treatment vocabulary to both field-monitoring
and lab-experiment study designs.
Paul Bzonek