Title: | Power Analysis for Research Experiments |
---|---|
Description: | Provides tools for calculating statistical power and determining sample size for a variety of experimental designs used in agricultural and biological research, including completely randomized, block, and split-plot designs. Supports customized designs and allows specification of main effects, interactions, and contrasts for accurate power analysis. |
Authors: | Kai Wang [aut, cre, cph] , Mutian Niu [aut, cph] |
Maintainer: | Kai Wang <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.0.9000 |
Built: | 2024-11-11 06:02:14 UTC |
Source: | https://github.com/an-ethz/pwr4exp |
Scale variance-covariance matrices as the relative Cholesky factors of each random effect term.
calc.theta(VarCov, sigma)
calc.theta(VarCov, sigma)
VarCov |
variance-covariance matrices. If there are multiple random effect groups, supply the variance-covariance matrix of each group as an element in a list. |
sigma |
standard deviation of random errors. |
theta
This class extends lmerModLmerTest
by adding a DenDF
slot.
DenDF
Numeric vector of denominator degrees of freedom.
These functions are used to create design objects for the further evaluation of statistical power.
designCRD(treatments, label, replicates, formula, beta, sigma2) designRCBD(treatments, label, blocks, formula, beta, VarCov, sigma2, ...) designLSD( treatments, label, squares = 1, reuse = c("row", "col", "both"), formula, beta, VarCov, sigma2, ... ) designCOD(treatments, label, squares = 1, formula, beta, VarCov, sigma2, ...) designSPD( trt.main, trt.sub, label, replicates, formula, beta, VarCov, sigma2, ... ) designCustom(design.df, formula, beta, VarCov, sigma2, design.name, ...)
designCRD(treatments, label, replicates, formula, beta, sigma2) designRCBD(treatments, label, blocks, formula, beta, VarCov, sigma2, ...) designLSD( treatments, label, squares = 1, reuse = c("row", "col", "both"), formula, beta, VarCov, sigma2, ... ) designCOD(treatments, label, squares = 1, formula, beta, VarCov, sigma2, ...) designSPD( trt.main, trt.sub, label, replicates, formula, beta, VarCov, sigma2, ... ) designCustom(design.df, formula, beta, VarCov, sigma2, design.name, ...)
treatments |
An integer-valued vector specifying the treatment structure,
in which the length of the vector indicates the number of treatment factors,
and each value represents the number of levels for each factor. A maximum of
two factors is allowed, and they are arranged in a factorial design.
For instance, |
label |
Optional. A list of character vectors specifying the names of
treatment factors and factor levels. Each vector in the list represents a
treatment factor, where the name of the vector specifies the name of the
factor, and the values in the vector are the labels for that factor's levels.
If not provided, factors and levels for one and two treatment factors are
labeled as |
replicates |
The number of experimental units per treatment in a completely randomized design or the number of experimental units (main plots) per treatment of main plot factors. |
formula |
A model formula for testing treatment effects in
post-experimental data analysis. Use the syntax of |
beta |
A numeric vector of expected model coefficients, representing the
effect sizes. The first element represents the intercept term, corresponding
to the mean of the reference level for categorical variables. Subsequent
elements correspond to the effect sizes of the independent variables in the
order they appear in the model matrix. For categorical variables, each coefficient
represents the difference between a non-reference level and the reference level
(intercept), as |
sigma2 |
error variance. |
blocks |
The number of blocks. |
VarCov |
Variance-covariance components of random effects. For multiple random effect groups, supply the variance (for a single random effect term) or variance-covariance matrix (for two or more random effect terms) of each group in a list, following the order in the model formula. |
... |
Additional arguments passed to the |
squares |
The number of replicated squares. By default, 1, i.e., no replicated squares. |
reuse |
A character string specifying how to replicate squares when there are multiple squares. Options are: "row" for reusing row blocks, "col" for reusing column blocks, or "both" for reusing both row and column blocks to replicate a single square. |
trt.main |
An integer-valued vector specifying the treatment structure at
main plot level for a split plot design, similar to |
trt.sub |
An integer-valued vector specifying the treatment structure at
sub plot level for a split plot design, similar to |
design.df |
Required input for creating a customized design. A data frame with all independent variables of the design as columns, representing the actual data structure (long format data frame) without response variables. |
design.name |
Optional input for creating a customized design. A character. |
Each function creates a specific design as described below:
designCRD
Completely Randomized Design.
By default, the model formula is y ~ trt
for one factor and
y ~ facA*facB
for two factors, unless explicitly specified. If the
label
argument is provided, the formula is automatically updated with the
specified treatment factor names.
designRCBD
Randomized Complete Block Design.
The default model formula is y ~ trt + (1|block)
for one factor and
y ~ facA*facB + (1|block)
for two factors. If label
is provided, the
fixed effect parts of the formula are automatically updated with the specified
names. The label of block factor ("block") in the formula is not changeable.
designLSD
Latin Square Design.
The default formula is y ~ trt + (1|row) + (1|col)
for one factor and
y ~ facA*facB + (1|row) + (1|col)
for two factors. If label
is provided,
the fixed effect parts of the formula are automatically updated with the specified
names. The labels of row ("row") and column ("col") block factors are not changeable.
designCOD
Crossover Design, which is a special case of LSD
with time periods and individuals as blocks. Period blocks are reused when
replicating squares.
The default formula is y ~ trt + (1|subject) + (1|period)
for one factor
and y ~ facA*facB + (1|subject) + (1|period)
for two factors. If label
is provided, the fixed effect parts of the formula are automatically updated
with the specified names. Note that "subject" and "period" are the labels for
the two blocking factors and cannot be changed.
designSPD
Split Plot Design.
The default formula includes the main effects of all treatment factors at
both the main and sub-plot levels, their interactions, and the random effects
of main plots: y ~ . + (1|mainplot)
. If label
is provided, the fixed
effect parts of the formula are automatically updated with the specified names.
The experimental unit at the main plot level (i.e., the block factor at the
subplot level) is always denoted as "mainplot".
designCustom
Customized Design.
a list with the design name, data structure (data frame), model formula, and a pseudo model object with the expected fixed and random effects.
# Example 1: Evaluate the power of a CRD with one treatment factor ## Create a design object crd <- designCRD( treatments = 4, # 4 levels of one treatment factor replicates = 12, # 12 units per level, 48 units totally # mean of level1, and the means of other levels minus level1, respectively beta = c(30, -2, 3, 5), sigma2 = 10 # error variance ) ## power of omnibus test pwr.anova(crd) ## power of contrast pwr.contrast(crd, specs = "trt", method = "pairwise") # pairwise comparisons pwr.contrast(crd, specs = "trt", method = "poly") # polynomial contrasts # Example 2: Evaluate the power of an RCBD with 2 x 2 factorial treatments # Treatment factors are A (A1 vs. A2) and B (B1 vs. B2). # To illustrate how to provide `beta`, treatment means are presented: # B1 B2 # A1 20 24 # A2 17 22 # # From these means, we calculate: # 1. the mean of reference level (A1B1): 20 # 2. the effect of A2 alone: Effect_A2 = A2B1 - A1B1 = 17 - 20 = -3 # 3. the effect of B2 alone: Effect_A2 = A1B2 - A1B1 = 24 - 20 = 4 # 4. the interaction effect of A2 and B2: # Interaction_A2B2 = A2B2 - A2B1 - A1B2 + A1B1 = 22 - 17 - 24 + 20 = 1, representing # the additional effect of combining A2B2 compared to what would be expected # from the sum of individual effects of A2 and B2. # The `beta` vector is constructed as: # beta = c(mean_A1B1, Effect_A2, Effect_B2, Interaction_A2B2) # beta = c(20, -3, 4, 1) ## Create a design object rcbd <- designRCBD( # 2x2 factorial design treatments = c(2, 2), # Specify treatment names label = list(A = c("A1", "A2"), B = c("B1", "B2")), # 12 blocks, totaling 48 experimental units blocks = 12, # Mean of the reference level and effect sizes as calculated above beta = c(20, -3, 4, 1), # Variance of block effects (between-block variance) VarCov = 30, # Error variance (within-block variance) sigma2 = 20 ) ## power of omnibus test pwr.anova(rcbd) ## power of B2 vs. B1 at each level of A pwr.contrast(rcbd, specs = ~B|A, method = "pairwise") # More examples are available in the package vignette("pwr4exp") # and on the package website: https://an-ethz.github.io/pwr4exp/
# Example 1: Evaluate the power of a CRD with one treatment factor ## Create a design object crd <- designCRD( treatments = 4, # 4 levels of one treatment factor replicates = 12, # 12 units per level, 48 units totally # mean of level1, and the means of other levels minus level1, respectively beta = c(30, -2, 3, 5), sigma2 = 10 # error variance ) ## power of omnibus test pwr.anova(crd) ## power of contrast pwr.contrast(crd, specs = "trt", method = "pairwise") # pairwise comparisons pwr.contrast(crd, specs = "trt", method = "poly") # polynomial contrasts # Example 2: Evaluate the power of an RCBD with 2 x 2 factorial treatments # Treatment factors are A (A1 vs. A2) and B (B1 vs. B2). # To illustrate how to provide `beta`, treatment means are presented: # B1 B2 # A1 20 24 # A2 17 22 # # From these means, we calculate: # 1. the mean of reference level (A1B1): 20 # 2. the effect of A2 alone: Effect_A2 = A2B1 - A1B1 = 17 - 20 = -3 # 3. the effect of B2 alone: Effect_A2 = A1B2 - A1B1 = 24 - 20 = 4 # 4. the interaction effect of A2 and B2: # Interaction_A2B2 = A2B2 - A2B1 - A1B2 + A1B1 = 22 - 17 - 24 + 20 = 1, representing # the additional effect of combining A2B2 compared to what would be expected # from the sum of individual effects of A2 and B2. # The `beta` vector is constructed as: # beta = c(mean_A1B1, Effect_A2, Effect_B2, Interaction_A2B2) # beta = c(20, -3, 4, 1) ## Create a design object rcbd <- designRCBD( # 2x2 factorial design treatments = c(2, 2), # Specify treatment names label = list(A = c("A1", "A2"), B = c("B1", "B2")), # 12 blocks, totaling 48 experimental units blocks = 12, # Mean of the reference level and effect sizes as calculated above beta = c(20, -3, 4, 1), # Variance of block effects (between-block variance) VarCov = 30, # Error variance (within-block variance) sigma2 = 20 ) ## power of omnibus test pwr.anova(rcbd) ## power of B2 vs. B1 at each level of A pwr.contrast(rcbd, specs = ~B|A, method = "pairwise") # More examples are available in the package vignette("pwr4exp") # and on the package website: https://an-ethz.github.io/pwr4exp/
Create a data frame for Crossover design
df.cod(treatments, label, squares)
df.cod(treatments, label, squares)
treatments |
An integer-valued vector specifying the treatment structure,
in which the length of the vector indicates the number of treatment factors,
and each value represents the number of levels for each factor. A maximum of
two factors is allowed, and they are arranged in a factorial design.
For instance, |
label |
Optional. A list of character vectors specifying the names of
treatment factors and factor levels. Each vector in the list represents a
treatment factor, where the name of the vector specifies the name of the
factor, and the values in the vector are the labels for that factor's levels.
If not provided, factors and levels for one and two treatment factors are
labeled as |
squares |
The number of replicated squares. By default, 1, i.e., no replicated squares. |
a data.frame with columns for treatment factors, individuals (row block factor), period (column block factor), and squares
Create a data frame of completely randomized design
df.crd(treatments, label, replicates)
df.crd(treatments, label, replicates)
treatments |
An integer-valued vector specifying the treatment structure,
in which the length of the vector indicates the number of treatment factors,
and each value represents the number of levels for each factor. A maximum of
two factors is allowed, and they are arranged in a factorial design.
For instance, |
label |
Optional. A list of character vectors specifying the names of
treatment factors and factor levels. Each vector in the list represents a
treatment factor, where the name of the vector specifies the name of the
factor, and the values in the vector are the labels for that factor's levels.
If not provided, factors and levels for one and two treatment factors are
labeled as |
replicates |
The number of experimental units per treatment. |
a data.frame with columns for treatment factors and replicates
Create a data frame for Latin square design
df.lsd(treatments, label, squares = 1, reuse = c("row", "col", "both"))
df.lsd(treatments, label, squares = 1, reuse = c("row", "col", "both"))
treatments |
An integer-valued vector specifying the treatment structure,
in which the length of the vector indicates the number of treatment factors,
and each value represents the number of levels for each factor. A maximum of
two factors is allowed, and they are arranged in a factorial design.
For instance, |
label |
Optional. A list of character vectors specifying the names of
treatment factors and factor levels. Each vector in the list represents a
treatment factor, where the name of the vector specifies the name of the
factor, and the values in the vector are the labels for that factor's levels.
If not provided, factors and levels for one and two treatment factors are
labeled as |
squares |
the number of replicated squares |
reuse |
A character string specifying how to replicate squares when there are multiple squares. Options are: "row" for reusing row blocks, "col" for reusing column blocks, or "both" for reusing both row and column blocks to replicate a single square. |
a data.frame with columns for treatment factors, row and column block factors, and squares
Create a data frame of randomized complete block design
df.rcbd(treatments, label, blocks)
df.rcbd(treatments, label, blocks)
treatments |
An integer-valued vector specifying the treatment structure,
in which the length of the vector indicates the number of treatment factors,
and each value represents the number of levels for each factor. A maximum of
two factors is allowed, and they are arranged in a factorial design.
For instance, |
label |
Optional. A list of character vectors specifying the names of
treatment factors and factor levels. Each vector in the list represents a
treatment factor, where the name of the vector specifies the name of the
factor, and the values in the vector are the labels for that factor's levels.
If not provided, factors and levels for one and two treatment factors are
labeled as |
blocks |
the number of blocks |
a data.frame with columns for blocks and treatment factors
Create data frame for split-plot design
df.spd(trt.main, trt.sub, label, replicates)
df.spd(trt.main, trt.sub, label, replicates)
trt.main |
an integer-valued vector specifying the treatment structure at
main plot level, similar to |
trt.sub |
an integer-valued vector specifying the treatment structure at
sub plot level, similar to |
label |
Optional. A list of character vectors specifying the names of
treatment factors and factor levels. Each vector in the list represents a
treatment factor, where the name of the vector specifies the name of the
factor, and the values in the vector are the labels for that factor's levels.
If not provided, factors and levels for one and two treatment factors are
labeled as |
replicates |
the number of experimental units (main plots) per treatment of main plot factors. |
a data.frame with columns for main plots, main treatments, and sub-treatments
This function finds the minimum sample size needed to achieve the target power for a given design. It uses an iterative approach to determine the minimum number of replications by traversing through a series of integers.
find_sample_size( design.quote, alpha = 0.05, target.power = 0.8, n_init = 2, n_max = 99, ... )
find_sample_size( design.quote, alpha = 0.05, target.power = 0.8, n_init = 2, n_max = 99, ... )
design.quote |
a quoted design object with unknown and unevaluated replications to be evaluated with varying values |
alpha |
type I error rate, default is 0.05 |
target.power |
the target power can be a single value for all factors or a vector of containing individual values for different factors, default is 0.8 |
n_init |
the initial replications for the iterative process, default is 2 |
n_max |
the maximum number of replications for the iterative process, default is 99 |
... |
additional arguments passed to |
A data frame with type I error rate (alpha), realized power (power), and minimum sample size (best_n).
# create a LSD object with unknown replications (\code{squares = n}) # simply \code{\link{quote}} the design generating function with lsd_quote <- quote( designLSD( treatments = 4, squares = n, reuse = "row", beta = c(10, 2, 3, 4), VarCov = list(5, 2), sigma2 = 10 ) ) # find the minimum number of squares required to achieve the target power of 0.8 find_sample_size(lsd_quote)
# create a LSD object with unknown replications (\code{squares = n}) # simply \code{\link{quote}} the design generating function with lsd_quote <- quote( designLSD( treatments = 4, squares = n, reuse = "row", beta = c(10, 2, 3, 4), VarCov = list(5, 2), sigma2 = 10 ) ) # find the minimum number of squares required to achieve the target power of 0.8 find_sample_size(lsd_quote)
Create a pseudo-model object with the response variable being simulated
according to the fixed and random effects. Model coefficients are replaced
by the expectations specified in the argument beta
. Variance-covariance
components of random effects are replaced by the values specified in argument
VarCov
. The standard deviation of random error is replaced by the
argument sigma
. Creating such a pseudo-model facilitates power
calculations by leveraging the anova
function in lmerTest
and
the Anova
function in car
.
fit.pseu.model(formula, data, beta, VarCov, sigma, ...)
fit.pseu.model(formula, data, beta, VarCov, sigma, ...)
formula |
an object of class |
data |
a data frame with the independent variables of the design as columns, e.g., treatment factors and block factors. |
beta |
a vector of the expectations of model coefficients. |
VarCov |
variance-covariance matrices. If there are multiple random effect groups, supply the variance-covariance matrix of each group as an element in a list. |
sigma |
standard deviation of error |
... |
other arguments passed to the |
a pseudo model object.
Milk yield records from 8 cows over 4 different periods in a 4x4 crossover design. The design includes 2 Latin squares, each consisting of 4 cows and 4 periods.
milk
milk
A data frame with 32 rows and 4 variables:
Factor: Cow index (8 levels)
Factor: Period index (4 levels)
Factor: Treatment index (4 levels)
Numeric: milk yield recordings (in kg)
Simulated data for package demonstration purposes.
Calculate power for testing overall effects of treatment factors and their interactions, i.e., statistical power of ANOVA.
pwr.anova(design, alpha = 0.05, ...)
pwr.anova(design, alpha = 0.05, ...)
design |
a design object created using design generating functions. |
alpha |
significance level (type I error rate), default 0.05 |
... |
Additional arguments passed to anova.lmerModLmerTest
for linear mixed models and to Anova for linear models. The type
of sum of squares (SS, default is Type III) and the method for computing denominator
degrees of freedom (DDF, default is Satterthwaite's method) can be modified.
For balanced designs, types of SS and DDF do not affect results. Note that
these additional arguments should be consistent in the design-generating
function and |
a data frame with numerator degrees of freedom (NumDF), denominator degrees of freedom (DenDF), non-centrality parameter, type I error rate (alpha), and power.
designCRD()
, designRCBD()
, designLSD()
, designCOD()
, designSPD()
, designCustom()
, and pwr.contrast()
# generate an RCBD rcbd = designRCBD(treatments = c(2, 2), blocks = 10, beta = c(10, 9, 8, 7), VarCov = 10, sigma2 = 9) # power of omnibus test pwr.anova(rcbd, alpha = 0.05)
# generate an RCBD rcbd = designRCBD(treatments = c(2, 2), blocks = 10, beta = c(10, 9, 8, 7), VarCov = 10, sigma2 = 9) # power of omnibus test pwr.anova(rcbd, alpha = 0.05)
Calculate power for testing various contrasts. The same syntax of emmeans package is employed to specify contrast types.
pwr.contrast(design, specs, method, alpha = 0.05, ...)
pwr.contrast(design, specs, method, alpha = 0.05, ...)
design |
a design object created using design generating functions. |
specs |
an argument inherited from emmeans specifying the names of the factors over which the contrasts are performed. |
method |
an argument inherited from contrast specifying the method of contrasts, e.g., pairwise, linear, and polynomials. |
alpha |
significance level (type I error rate), default 0.05 |
... |
other arguments passed to contrast. |
a data frame showing the power of the specific contrast
rcbd = designRCBD(treatments = c(2, 2), blocks = 10, beta = c(10, 9, 8, 7), VarCov = 10, sigma2 = 9) pwr.contrast(rcbd, specs = ~ facA|facB, method = "pairwise")
rcbd = designRCBD(treatments = c(2, 2), blocks = 10, beta = c(10, 9, 8, 7), VarCov = 10, sigma2 = 9) pwr.contrast(rcbd, specs = ~ facA|facB, method = "pairwise")
Naming theta Naming the vector in the order of model specification and in the actual order used in the model
theta.names(data, formula)
theta.names(data, formula)
data |
data frame |
formula |
model formula |