| Title: | Power Analysis of Flexible ANOVA Designs and Related Tests |
|---|---|
| Description: | Provides functions for conducting power analysis in ANOVA designs, including between-, within-, and mixed-factor designs, with full support for both main effects and interactions. The package allows calculation of statistical power, required total sample size, significance level, and minimal detectable effect sizes expressed as partial eta squared or Cohen's f for ANOVA terms and planned contrasts. In addition, complementary functions are included for common related tests such as t-tests and correlation tests, making the package a convenient toolkit for power analysis in experimental psychology and related fields. |
| Authors: | Hiroyuki Muto [aut, cre] (ORCID: <https://orcid.org/0000-0002-0007-6019>) |
| Maintainer: | Hiroyuki Muto <[email protected]> |
| License: | GPL-3 |
| Version: | 1.1.1 |
| Built: | 2026-06-05 06:28:33 UTC |
| Source: | https://github.com/mutopsy/pwranova |
Converts Cohen's f to partial eta squared ()
using the standard definition in Cohen (1988).
cohensf_to_peta2(f)cohensf_to_peta2(f)
f |
A numeric vector of Cohen's f values. Each value must be greater than or equal to 0. |
The conversion is defined as:
This follows from the relationship:
A numeric vector of partial eta squared values.
Cohen, J. (1988). Statistical power analysis for the behavioral sciences (2nd ed.). Hillsdale, NJ: Lawrence Erlbaum Associates.
# Convert a single Cohen's f value cohensf_to_peta2(0.25) # Convert multiple values cohensf_to_peta2(c(0.1, 0.25, 0.4))# Convert a single Cohen's f value cohensf_to_peta2(0.25) # Convert multiple values cohensf_to_peta2(c(0.1, 0.25, 0.4))
Converts partial eta squared () to
Cohen's f using the standard definition in Cohen (1988).
peta2_to_cohensf(peta2)peta2_to_cohensf(peta2)
peta2 |
A numeric vector of partial eta squared values. Each value must be within the range of 0 to 1. |
The conversion is defined as:
This follows from the inverse relationship:
A numeric vector of Cohen's f values.
Cohen, J. (1988). Statistical power analysis for the behavioral sciences (2nd ed.). Hillsdale, NJ: Lawrence Erlbaum Associates.
# Convert a single partial eta squared value peta2_to_cohensf(0.06) # Convert multiple values peta2_to_cohensf(c(0.01, 0.06, 0.14))# Convert a single partial eta squared value peta2_to_cohensf(0.06) # Convert multiple values peta2_to_cohensf(c(0.01, 0.06, 0.14))
Computes power, required total sample size, alpha, or minimal detectable effect size for fixed-effects terms in between-/within-/mixed-factor ANOVA designs.
pwranova( nlevels_b = NULL, nlevels_w = NULL, n_total = NULL, alpha = NULL, power = NULL, cohensf = NULL, peta2 = NULL, epsilon = 1, target = NULL, max_nfactor = 6, nlim = c(2, 10000) )pwranova( nlevels_b = NULL, nlevels_w = NULL, n_total = NULL, alpha = NULL, power = NULL, cohensf = NULL, peta2 = NULL, epsilon = 1, target = NULL, max_nfactor = 6, nlim = c(2, 10000) )
nlevels_b |
Integer scalar or vector. Numbers of levels for
between-subjects factors. Omit or set |
nlevels_w |
Integer scalar or vector. Numbers of levels for
within-subjects factors. Omit or set |
n_total |
Integer scalar or vector. Total sample size across all groups.
If |
alpha |
Numeric in |
power |
Numeric in |
cohensf |
Numeric (non-negative). Cohen's |
peta2 |
Numeric in |
epsilon |
Numeric in |
target |
Character vector of term labels to compute (e.g., |
max_nfactor |
Integer. Safety cap for the total number of factors. |
nlim |
Integer length-2. Search range of total |
Fixed-effects, balanced designs are assumed. All groups/cells have equal cell sizes and effects are tested with standard fixed-effects ANOVA models.
Numerator degrees of freedom for within-subjects terms with
are adjusted by the nonsphericity parameter
epsilon.
Denominator degrees of freedom follow standard mixed-ANOVA formulas and
are multiplied by the same epsilon for within-subjects terms.
Critical values are computed from the central F-distribution;
power uses the noncentral F-distribution with noncentrality
parameter .
Effect-size inputs can be given as Cohen’s or partial
eta-squared (internally converted via
). If both are NULL, the minimal
detectable effect size is solved for given n_total, alpha,
and power.
Exactly one of n_total, an effect-size specification
(cohensf/peta2), alpha, or power must be
NULL; that quantity is then solved.
Validation against GPower: For the subset of designs supported
by GPower (between-, within-, and mixed-factor ANOVA with
equal cell sizes), pwranova() was validated to produce results
identical to those of GPower.
A data frame with S3 class:
"cal_power" when power is calculated given n_total,
alpha, and effect size;
"cal_n" or "cal_ns" when n_total is solved;
"cal_alpha" or "cal_alphas" when alpha is solved;
"cal_es" when minimal detectable effect sizes are solved.
Columns include term, df_num, df_denom, n_total,
alpha, power, cohensf, peta2, F_critical,
ncp, epsilon.
Cohen, J. (1988). Statistical power analysis for the behavioral sciences (2nd ed.). Hillsdale, NJ: Lawrence Erlbaum Associates.
# One between factor (k = 3), one within factor (m = 4), compute power pwranova(nlevels_b = 3, nlevels_w = 4, n_total = 60, cohensf = 0.25, alpha = 0.05, power = NULL, epsilon = 0.8) # Solve required total N for target power pwranova(nlevels_b = 2, nlevels_w = NULL, n_total = NULL, peta2 = 0.06, alpha = 0.05, power = 0.8)# One between factor (k = 3), one within factor (m = 4), compute power pwranova(nlevels_b = 3, nlevels_w = 4, n_total = 60, cohensf = 0.25, alpha = 0.05, power = NULL, epsilon = 0.8) # Solve required total N for target power pwranova(nlevels_b = 2, nlevels_w = NULL, n_total = NULL, peta2 = 0.06, alpha = 0.05, power = 0.8)
Computes power, required total sample size, alpha, or minimal detectable effect size for a single planned contrast (1 df) in between-participants or paired/repeated-measures settings.
pwrcontrast( weight = NULL, paired = FALSE, n_total = NULL, cohensf = NULL, peta2 = NULL, alpha = NULL, power = NULL, nlim = c(2, 10000) )pwrcontrast( weight = NULL, paired = FALSE, n_total = NULL, cohensf = NULL, peta2 = NULL, alpha = NULL, power = NULL, nlim = c(2, 10000) )
weight |
Numeric vector (length |
paired |
Logical. |
n_total |
Integer scalar. Total sample size. If |
cohensf |
Numeric (non-negative). Cohen's |
peta2 |
Numeric in |
alpha |
Numeric in |
power |
Numeric in |
nlim |
Integer length-2. Search range of total |
For a contrast with weights that sum to zero,
the numerator df is 1. The denominator df is for
between-subjects (unpaired) designs and for
paired/repeated-measures designs. Power uses the noncentral F-with
.
Contrast weights (weight) are not centered internally; only the
zero-sum condition is enforced (up to numerical tolerance).
When paired = FALSE, the total sample size n_total must be
a multiple of the number of contrast groups .
Exactly one of n_total, an effect-size specification
(cohensf/peta2), alpha, or power must be
NULL; that quantity is then solved.
Critical values are computed from the central F-distribution; power
is based on the noncentral F-distribution with noncentrality parameter
.
Effect-size inputs can be given as Cohen’s or partial eta-squared
(internally converted via
). If both are NULL, the
minimal detectable effect size is solved for given n_total,
alpha, and power.
A one-row data frame with class:
"cal_power" when power is calculated given n_total, alpha, and effect size;
"cal_n" when n_total is solved;
"cal_alpha" when alpha is solved;
"cal_es" when minimal detectable effect sizes are solved.
Columns: term (always "contrast"), weight (comma-separated string),
df_num, df_denom, n_total, alpha, power,
cohensf, peta2, F_critical, ncp.
Cohen, J. (1988). Statistical power analysis for the behavioral sciences (2nd ed.). Hillsdale, NJ: Lawrence Erlbaum Associates.
# Two-group contrast (1, -1), between-subjects: compute power pwrcontrast(weight = c(1, -1), paired = FALSE, n_total = 40, cohensf = 0.25, alpha = 0.05) # Four-level contrast (e.g., Helmert-like), solve required N for target power pwrcontrast(weight = c(3, -1, -1, -1), paired = FALSE, n_total = NULL, peta2 = 0.06, alpha = 0.05, power = 0.80) # Paired contrast across K=3 conditions pwrcontrast(weight = c(1, 0, -1), paired = TRUE, n_total = NULL, cohensf = 0.2, alpha = 0.05, power = 0.9)# Two-group contrast (1, -1), between-subjects: compute power pwrcontrast(weight = c(1, -1), paired = FALSE, n_total = 40, cohensf = 0.25, alpha = 0.05) # Four-level contrast (e.g., Helmert-like), solve required N for target power pwrcontrast(weight = c(3, -1, -1, -1), paired = FALSE, n_total = NULL, peta2 = 0.06, alpha = 0.05, power = 0.80) # Paired contrast across K=3 conditions pwrcontrast(weight = c(1, 0, -1), paired = TRUE, n_total = NULL, cohensf = 0.2, alpha = 0.05, power = 0.9)
Computes statistical power, required total sample size, ,
or the minimal detectable correlation coefficient for a Pearson correlation test.
Two computational methods are supported: exact noncentral t (method = "t")
and Fisher's z-transformation with normal approximation (method = "z").
pwrcortest( alternative = c("two.sided", "one.sided"), n_total = NULL, alpha = NULL, power = NULL, rho = NULL, method = c("t", "z"), ncp_scale = c("n", "df"), bias_correction = FALSE, nlim = c(2, 10000) )pwrcortest( alternative = c("two.sided", "one.sided"), n_total = NULL, alpha = NULL, power = NULL, rho = NULL, method = c("t", "z"), ncp_scale = c("n", "df"), bias_correction = FALSE, nlim = c(2, 10000) )
alternative |
Character. Either |
n_total |
Integer scalar. Total sample size ( |
alpha |
Numeric in |
power |
Numeric in |
rho |
Numeric correlation coefficient in |
method |
Character. Either |
ncp_scale |
Character. Applies only to |
bias_correction |
Logical. Applies only to |
nlim |
Integer vector of length 2. Search range of |
Exactly one of n_total, rho, alpha, or power
must be NULL; that quantity is then solved.
The sign of rho is ignored; its absolute value is used,
because statistical power depends on the magnitude of the effect
rather than its direction.
For method = "t", computations are based on the noncentral
t-distribution with noncentrality parameter
,
where the scaling factor is determined by ncp_scale.
When ncp_scale = "n" (default), , corresponding to
the formulation used in GPower. When ncp_scale = "df",
, which follows directly from the classical test
statistic formula for Pearson's correlation test.
These two formulations arise from different derivations of the power
function: the form follows directly from the test
statistic, whereas the form is obtained from alternative
derivations used in some power-analysis implementations.
For method = "z", computations use Fisher's z transformation
of the population correlation, .
Let . Under the alternative hypothesis,
with
. If bias_correction = TRUE,
is first bias-corrected before applying Fisher's transform.
Critical values are taken from the central normal distribution under
(i.e., under the null).
The returned ncp equals .
Validation against GPower: Results have been confirmed to match those produced by GPower for equivalent correlation tests using the noncentral t-distribution.
Note: Results from method = "z" will not exactly match
pwr::pwr.r.test, because pwr uses a hybrid approach
combining the Fisher-z approximation with a t-based
critical value.
A one-row data.frame with class
"cal_power", "cal_n", "cal_alpha", or "cal_es",
depending on the solved quantity. Columns:
df (only for method = "t")
n_total, alpha, power
rho, t_critical or z_critical
ncp (noncentrality parameter or mean under the alternative:
see Details)
# (1) Compute power for rho = 0.3, N = 50, two-sided test pwrcortest(alternative = "two.sided", n_total = 50, rho = 0.3, alpha = 0.05) # (2) Solve required N for target power, using Fisher-z method pwrcortest(method = "z", rho = 0.2, alpha = 0.05, power = 0.8) # (3) Solve minimal detectable correlation pwrcortest(n_total = 60, alpha = 0.05, power = 0.9, rho = NULL)# (1) Compute power for rho = 0.3, N = 50, two-sided test pwrcortest(alternative = "two.sided", n_total = 50, rho = 0.3, alpha = 0.05) # (2) Solve required N for target power, using Fisher-z method pwrcortest(method = "z", rho = 0.2, alpha = 0.05, power = 0.8) # (3) Solve minimal detectable correlation pwrcortest(n_total = 60, alpha = 0.05, power = 0.9, rho = NULL)
Computes statistical power, required total sample size, ,
or the minimal detectable effect size for a t-test in one of three designs:
one-sample, two-sample (independent), or paired/repeated measures.
pwrttest( paired = FALSE, onesample = FALSE, n_total = NULL, alpha = NULL, power = NULL, delta = NULL, cohensf = NULL, peta2 = NULL, alternative = c("two.sided", "one.sided"), nlim = c(2, 10000) )pwrttest( paired = FALSE, onesample = FALSE, n_total = NULL, alpha = NULL, power = NULL, delta = NULL, cohensf = NULL, peta2 = NULL, alternative = c("two.sided", "one.sided"), nlim = c(2, 10000) )
paired |
Logical. |
onesample |
Logical. |
n_total |
Integer scalar. Total sample size.
If |
alpha |
Numeric in |
power |
Numeric in |
delta |
Numeric. Cohen's
|
cohensf |
Numeric (non-negative). Cohen's
|
peta2 |
Numeric in |
alternative |
Character. Either |
nlim |
Integer vector of length 2. Search range of total |
The sign of delta is ignored; its absolute value is used,
because statistical power depends on the magnitude of the effect
rather than its direction.
If multiple effect-size arguments are supplied (delta, cohensf, peta2),
precedence is delta cohensf peta2; the rest are ignored with a warning.
For the two-sample design, equal allocation is assumed; n_total must be even when provided,
and the solved n_total will be an even number.
For the paired design, the effect size is interpreted as .
Computations use the central and noncentral t-distributions (stats::qt, stats::pt);
root finding uses stats::uniroot() where needed.
Results have been validated to match those produced by G*Power for equivalent one-sample, paired, and two-sample t tests.
A one-row data.frame with class
"cal_power", "cal_n", "cal_alpha", or "cal_es",
depending on the solved quantity. Columns:
df, n_total, alpha, power,
delta, cohensf, peta2,
t_critical, ncp.
# (1) Two-sample (independent), compute power given N and d pwrttest(paired = FALSE, onesample = FALSE, alternative = "two.sided", n_total = 128, delta = 0.50, alpha = 0.05) # (2) Paired t-test, solve required N for target power pwrttest(paired = TRUE, onesample = FALSE, alternative = "one.sided", n_total = NULL, delta = 0.40, alpha = 0.05, power = 0.90) # (3) One-sample t-test, solve alpha given N and power pwrttest(onesample = TRUE, alternative = "two.sided", n_total = 40, delta = 0.40, alpha = NULL, power = 0.80) # (4) Two-sample, specify effect via f or partial eta^2 (converted internally) pwrttest(paired = FALSE, cohensf = 0.25, n_total = NULL, alpha = 0.05, power = 0.80)# (1) Two-sample (independent), compute power given N and d pwrttest(paired = FALSE, onesample = FALSE, alternative = "two.sided", n_total = 128, delta = 0.50, alpha = 0.05) # (2) Paired t-test, solve required N for target power pwrttest(paired = TRUE, onesample = FALSE, alternative = "one.sided", n_total = NULL, delta = 0.40, alpha = 0.05, power = 0.90) # (3) One-sample t-test, solve alpha given N and power pwrttest(onesample = TRUE, alternative = "two.sided", n_total = 40, delta = 0.40, alpha = NULL, power = 0.80) # (4) Two-sample, specify effect via f or partial eta^2 (converted internally) pwrttest(paired = FALSE, cohensf = 0.25, n_total = NULL, alpha = 0.05, power = 0.80)