Power Calculation for ANCOVA with Continuous Covariate and Two Groups
Source:R/analytical_power.R
analytical_power_ancova_cont_2arms.RdComputes the analytical power to detect a group effect in an Analysis of Covariance
(ANCOVA) model with one continuous covariate and two groups. The model takes the form:
y ~ covariate + group, where the covariate is assumed to follow N(0,1).
This function is vectorized and can handle multiple values for n and/or d parameters.
Arguments
- n
Integer or vector of integers. Total sample size(s) across both groups.
- d
Numeric or vector of numerics. Cohen's d for the group effect (standardized mean difference). This represents the difference in group means divided by the pooled standard deviation, after adjusting for the covariate. For two-sided tests, the sign doesn't matter. For one-sided tests, positive d means group 1 > group 0.
- beta_cov
Numeric. True regression coefficient for the continuous covariate. Assumes the covariate follows a standard normal distribution N(0,1).
- sigma
Numeric. Residual standard deviation of the outcome after fitting the full model (including both covariate and group effects).
- equal_groups
Logical. If TRUE (default), assumes equal group sizes (n1 = n2). If FALSE, requires p_group to be specified.
- p_group
Numeric. Proportion in group 1 (only used if equal_groups = FALSE).
- alpha
Numeric. Type I error rate (significance level). Default is 0.05.
- alternative
Character. Specifies the alternative hypothesis. Must be one of "two.sided" (default), "greater", or "less". For one-sided tests, "greater" tests if group 1 > group 0, "less" tests if group 1 < group 0.
- method
Character. Specifies the lambda calculation method. Must be one of:
"cohen" (default): Uses Cohen's convention where \(\lambda = f^{2}(u + v + 1)\)
"theory": Uses statistical theory where \(\lambda = n \cdot f^{2}\)
- covariate_method
Character. Specifies how to handle the covariate. Must be one of:
"fixed" (default): Assumes fixed covariate values (conditional power)
"expected": Integrates over covariate distribution (expected power, SAS-style)
- numint
Integer. Number of integration points for covariate_method = "expected" (default = 2000).
Value
Numeric vector. The statistical power(s) to detect the specified group effect(s). If both n and d are vectors, returns a vector with length equal to the maximum of their lengths.
Details
The function calculates power for testing the group effect in a linear model:
The outcome y is modeled as: \(y = \beta_0 + \beta_1 \cdot \text{covariate} + \beta_2 \cdot \text{group} + \epsilon\)
The null hypothesis is \(H_0: \beta_2 = 0\)
For two-sided tests: \(H_1: \beta_2 \neq 0\)
For one-sided tests: \(H_1: \beta_2 > 0\) (if alternative = "greater") or \(H_1: \beta_2 < 0\) (if alternative = "less")
The covariate follows \(N(0,1)\)
Group is coded as 0 or 1
Residuals \(\epsilon\) follow \(N(0, \sigma^{2})\)
Method options:
"cohen": Uses Cohen's convention matching pwr.f2.test
"theory": Uses classical statistical theory
Covariate method options:
"fixed": Conditional power given fixed covariate values
"expected": Expected power integrating over covariate distribution (SAS approach)
For two-sided tests, the function uses the F-test which is equivalent to the square of a two-sided t-test. For one-sided tests, it uses the t-distribution directly.
Note
This function assumes:
No interaction between group and covariate
The covariate has the same variance in both groups
The residual variance is homogeneous across groups
References
Cohen, J. (1988). Statistical Power Analysis for the Behavioral Sciences (2nd ed.). Lawrence Erlbaum Associates.
Shieh, G. (2020). Power analysis and sample size planning in ANCOVA designs. Psychometrika, 85(1), 101-120.
Examples
# \donttest{
# Two-sided test with Cohen's method and fixed covariate
analytical_power_ancova_cont_2arms(n = 100, d = 0.5, beta_cov = 0.4, sigma = 1)
#> [1] 0.6924571
# Using statistical theory method
analytical_power_ancova_cont_2arms(n = 100, d = 0.5, beta_cov = 0.4, sigma = 1,
method = "theory")
#> [1] 0.6968047
# Using expected power with Cohen's lambda
analytical_power_ancova_cont_2arms(n = 100, d = 0.5, beta_cov = 0.4, sigma = 1,
covariate_method = "expected")
#> [1] 0.6879996
# Using expected power with theory lambda
analytical_power_ancova_cont_2arms(n = 100, d = 0.5, beta_cov = 0.4, sigma = 1,
method = "theory", covariate_method = "expected")
#> [1] 0.6923516
# Vectorized: multiple sample sizes
analytical_power_ancova_cont_2arms(n = c(50, 100, 150), d = 0.5, beta_cov = 0.4, sigma = 1)
#> [1] 0.4030670 0.6924571 0.8580682
# One-sided test (group 1 > group 0)
analytical_power_ancova_cont_2arms(n = 100, d = 0.5, beta_cov = 0.4, sigma = 1,
alternative = "greater")
#> [1] 0.7988858
# Unequal groups (70/30 split)
analytical_power_ancova_cont_2arms(n = 150, d = 0.5, beta_cov = 0.4, sigma = 1,
equal_groups = FALSE, p_group = 0.7)
#> [1] 0.7934917
# }