Movatterモバイル変換


[0]ホーム

URL:


Title:Power Analysis for Research Experiments
Version:1.0.1
Description:Provides tools for calculating statistical power for experiments analyzed using linear mixed models. It supports standard designs, including randomized block, split-plot, and Latin Square designs, while offering flexibility to accommodate a variety of other complex study designs.
License:GPL-2 |GPL-3 [expanded from: GPL (≥ 2)]
URL:https://github.com/an-ethz/pwr4exp,https://an-ethz.github.io/pwr4exp/
BugReports:https://github.com/an-ethz/pwr4exp/issues
Depends:R (≥ 2.10), stats
Suggests:agricolae, AlgDesign, crossdes, FrF2, knitr, rmarkdown
VignetteBuilder:knitr
Encoding:UTF-8
LazyData:true
RoxygenNote:7.3.2
Imports:emmeans, MASS, Matrix, methods, nlme, numDeriv
NeedsCompilation:no
Packaged:2025-11-11 11:20:28 UTC; wangkai
Author:Kai WangORCID iD [aut, cre, cph], Mutian NiuORCID iD [aut, cph]
Maintainer:Kai Wang <kai.wang@usys.ethz.ch>
Repository:CRAN
Date/Publication:2025-11-11 11:50:02 UTC

Computes power of t-test for one-dimensional contrast matrices

Description

Computes power of t-test for one-dimensional contrast matrices

Usage

contrast1D(  object,  L,  method = c("Satterthwaite"),  sig.level = 0.05,  alternative = c("two.sided", "one.sided"),  strict = TRUE)

Arguments

object

design object

L

contrast vector

method

DF approximation method, only "Satterthwaite" available currently

sig.level

significance level, default 0.05

alternative

one- or two-sided test

strict

whether or not use strict interpretation in two-sided case

Value

A data frame with columns for effect size, degrees of freedom, significance level, power, and test type.


Computes power of F-test for multi-dimensional contrast matrices

Description

Computes power of F-test for multi-dimensional contrast matrices

Usage

contrastMD(object, L, sig.level = 0.05, eps = sqrt(.Machine$double.eps))

Arguments

object

design object

L

contrast matrix

sig.level

significance level

eps

numeric tolerance

Value

A data frame


AR(1) Correlation Structure

Description

Re-exportsnlme::corAR1 from thenlme package.

Usage

corAR1(value, form, fixed)

Arguments

value

numeric value for the correlation parameter.

form

a one-sided formula of the form~ t,~ 1 | g, or~ t | g,specifying a time covariatet, a grouping factorg, or both.When no time covariate is specified, the row order of the data within each group is assumed torepresent the chronological order of measurements.

fixed

unused

Details

In the originalnlme::corAR1 function, a covariatet must be an integer class.Inpwr4exp,t can also be a factor class, which is then converted to an integer internally for chronological order.The class oft in the model formula, if present, is not converted. For example, a time covariate can be fitted as a factorin the model formula, whereas it is converted to an integer in the correlation formula.

See Also

corClassesSeenlme::corAR1 for original documentation.


ARMA(p,q) Correlation Structure

Description

Re-exportsnlme::corARMA from thenlme package.

Usage

corARMA(value, form, p, q, fixed)

Arguments

value

numeric vector of parameter values

form

a one-sided formula of the form~ t,~ 1 | g, or~ t | g,specifying a time covariatet, a grouping factorg, or both.When no time covariate is specified, the row order of the data within each group is assumed torepresent the chronological order of measurements.

p

non-negative integer specifying the autoregressive order

q

non-negative integer specifying the moving average order

fixed

unused

Details

In the originalnlme::corARMA function, a covariatet must be an integer class.Inpwr4exp,t can also be a factor class, which is then converted to an integer internally for chronological order.The class oft in the model formula, if present, is not converted. For example, a time covariate can be fitted as a factorin the model formula, whereas it is converted to an integer in the correlation formula.

See Also

corClassesSeenlme::corARMA for original documentation.


Continuous AR(1) Correlation Structure

Description

Re-exportsnlme::corCAR1 from thenlme package.

Usage

corCAR1(value, form, fixed)

Arguments

value

numeric value for the correlation parameter

form

a one-sided formula of the form~ t,~ 1 | g, or~ t | g,specifying a time covariatet, a grouping factorg, or both.

fixed

unused

See Also

corClassesSeenlme::corCAR1 for original documentation.


Correlation Structure Classes

Description

Standard classes of correlation structures (corStruct) available from thenlme packageand re-exported bypwr4exp for convenience when specifying correlation structures inmkdesign.

All arguments are identical to the correspondingnlme functions. For more details on the original implementations,seenlme::corClasses.

Note: In the originalnlme::corAR1,nlme::corARMA, andnlme::corSymm functions,the covariatet in the correlation formula~ t or~ t | g must be an integer class. Inpwr4exp,the covariate can also be a factor class, which is then converted to an integer internally for sorting purposes.The class of the covariate variable in the model formula, if present, will not be converted. For example, a time covariatecan be fitted as a factor in the model formula, whereas it is converted to an integer in the correlation formula temporarily for matrix sorting.

Details

Available standard classes:

corAR1

autoregressive process of order 1.

corARMA

autoregressive moving average process, with arbitrary ordersfor the autoregressive and moving average components.

corCAR1

continuous AR(1)

corCompSymm

compound symmetry structure corresponding to a constant correlation.

corExp

exponential spatial correlation.

corGaus

Gaussian spatial correlation.

corLin

linear spatial correlation.

corRatio

Rational quadratics spatial correlation.

corSpher

spherical spatial correlation.

corSymm

general correlation matrix, with no additional structure.

References

Pinheiro, J.C., and Bates, D.M. (2000) "Mixed-Effects Models in S and S-PLUS", Springer.


Compound Symmetry Correlation Structure

Description

Re-exportsnlme::corCompSymm from thenlme package.

Usage

corCompSymm(value, form, fixed)

Arguments

value

numeric vector of parameter values

form

a one-sided formula of the form~ 1 | g specifying a grouping factor.

fixed

unused

See Also

corClassesSeenlme::corCompSymm for original documentation.


Exponential Correlation Structure

Description

Re-exportsnlme::corExp from thenlme package.

Usage

corExp(value, form, nugget, metric, fixed)

Arguments

value

numeric value(s) for the parameter(s)

form

a one-sided formula of the form~ S1 + ... + Sp, or~ S1 + ... + Sp | gspatial covariates S1 through Sp and, optionally, a grouping factor g.

nugget

logical; ifTRUE a nugget effect is added

metric

character string specifying the distance metric

fixed

unused

See Also

corClassesSeenlme::corExp for original documentation.


Gaussian Correlation Structure

Description

Re-exportsnlme::corGaus from thenlme package.

Usage

corGaus(value, form, nugget, metric, fixed)

Arguments

value

numeric value(s) for the parameter(s)

form

a one-sided formula of the form~ S1 + ... + Sp, or~ S1 + ... + Sp | gspatial covariates S1 through Sp and, optionally, a grouping factor g.

nugget

logical; ifTRUE a nugget effect is added

metric

character string specifying the distance metric

fixed

unused

See Also

corClassesSeenlme::corGaus for original documentation.


Linear Correlation Structure

Description

Re-exportsnlme::corLin from thenlme package.

Usage

corLin(value, form, nugget, metric, fixed)

Arguments

value

numeric value(s) for the parameter(s)

form

a one-sided formula of the form~ S1 + ... + Sp, or~ S1 + ... + Sp | gspatial covariates S1 through Sp and, optionally, a grouping factor g.

nugget

logical; ifTRUE a nugget effect is added

metric

character string specifying the distance metric

fixed

unused

See Also

corClassesSeenlme::corLin for original documentation.


Rational Quadratics Correlation Structure

Description

Re-exportsnlme::corRatio from thenlme package.

Usage

corRatio(value, form, nugget, metric, fixed)

Arguments

value

numeric value(s) for the parameter(s)

form

a one-sided formula of the form~ S1 + ... + Sp, or~ S1 + ... + Sp | gspatial covariates S1 through Sp and, optionally, a grouping factor g.

nugget

logical; ifTRUE a nugget effect is added

metric

character string specifying the distance metric

fixed

unused

See Also

corClassesSeenlme::corRatio for original documentation.


Spherical Correlation Structure

Description

Re-exportsnlme::corSpher from thenlme package.

Usage

corSpher(value, form, nugget, metric, fixed)

Arguments

value

numeric value(s) for the parameter(s)

form

a one-sided formula of the form~ S1 + ... + Sp, or~ S1 + ... + Sp | gspatial covariates S1 through Sp and, optionally, a grouping factor g.

nugget

logical; ifTRUE a nugget effect is added

metric

character string specifying the distance metric

fixed

unused

See Also

corClassesSeenlme::corSpher for original documentation.


General Correlation Structure

Description

Re-exportsnlme::corSymm from thenlme package.

Usage

corSymm(value, form, fixed)

Arguments

value

numeric vector of correlation parameter values

form

a one-sided formula of the form~ t,~ 1 | g, or~ t | g,specifying a covariatet, a grouping factorg, or both. A covariate indicatesthe order of the data rows in the residual error matrix of each group.

fixed

unused

See Also

corClassesSeenlme::corSymm for original documentation.


Creation of Standard Experimental Designs

Description

These functions facilitate the creation of standard experimental designscommonly used in agricultural studies for power analysis. Unlikemkdesignwhich requires a pre-existing data frame, these functions allow users todirectly specify key design characteristics to generate experimental layouts.Quantitative parameters describing fixed and random effects remain consistentwith those inmkdesign.

Usage

designCRD(  treatments,  label,  replicates,  formula,  beta = NULL,  means = NULL,  sigma2,  template = FALSE,  REML = TRUE)designRCBD(  treatments,  label,  blocks,  formula,  beta = NULL,  means = NULL,  vcomp,  sigma2,  template = FALSE,  REML = TRUE)designLSD(  treatments,  label,  squares = 1,  reuse = c("row", "col", "none"),  formula,  beta = NULL,  means = NULL,  vcomp,  sigma2,  template = FALSE,  REML = TRUE)designCOD(  treatments,  label,  squares = 1,  formula,  beta = NULL,  means = NULL,  vcomp,  sigma2,  template = FALSE,  REML = TRUE)designSPD(  trt.main,  trt.sub,  label,  replicates,  formula,  beta = NULL,  means = NULL,  vcomp,  sigma2,  template = FALSE,  REML = TRUE)

Arguments

treatments

An integer vector where each element represents the number of levelsof the corresponding treatment factor. A single integer (e.g.,treatments = n)specifies one treatment factor withn levels. When multiple factors are provided,they are arranged in a factorial treatment factor design. For example,treatments = c(2, 3) creates a 2x3 factorial design with the first factor having 2 levelsand the second factor having 3 levels.

label

Optional. A list of character vectors, each corresponding to a treatment factor.The name of each vector specifies the factor's name, and its elements provide the labels for that factor's levels.If no labels are provided, default labels will be used. For a single treatment factor, the default islist(trt = c("1", "2", ...)), and for two treatment factors, the default islist(facA = c("1", "2", ...), facB = c("1", "2", ...)).For split-plot designs, the defaults are similar but include the ".main" and ".sub" suffixes for main plot and subplot factors.For example:list(trt.main = c("1", "2", ...), trt.sub = c("1", "2", ...)) andlist(facA.main = c("1", "2", ...), facB.main = c("1", "2", ...), facA.sub = c("1", "2", ...), facB.sub = c("1", "2", ...)).Label sets should be arranged so that the main plot factors come first, followed by the subplot factors.

replicates

The number of experimental units per treatment in a completelyrandomized design or the number of experimental units (main plots) per treatmentof main plot factors.

formula

A right-hand-sideformula specifying the model for testing treatment effects,with terms on the right of~ , followinglme4::lmer syntax for random effects.If not specified, a default formula with main effects and all interactions is used internally.

beta

One of the optional inputs for fixed effects.A vector of model coefficients where factor variable coefficients correspondto dummy variables created using treatment contrast (stats::contr.treatment).

means

One of the optional inputs for fixed effects.A vector of marginal or conditioned means (if factors have interactions).Regression coefficients are required for numerical variables.Eitherbeta ormeans must be provided, and their values must strictly follow a specific order.A template can be created to indicate the required input values and their order.Seemkdesign for more information.

sigma2

error variance.

template

Default isFALSE.IfTRUE, a template forbeta,means, andvcomp is generated to indicate the required input order.

REML

Specifies whether to use REML or ML information matrix. Default isTRUE (REML).

blocks

The number of blocks.

vcomp

A vector of variance-covariance components for random effects, if present.The values must follow a strict order. Seemkdesign.

squares

The number of replicated squares. By default, 1, i.e., noreplicated squares.

reuse

A character string specifying how to replicate squares whenthere are multiple squares. Options are: "row" for reusing row blocks, "col"for reusing column blocks, or "none" for reusing neither row nor column blocksto replicate a single square.

trt.main

An integer-valued vector specifying the treatment structure atmain plot level for a split plot design, similar totreatments.

trt.sub

An integer-valued vector specifying the treatment structure atsub plot level for a split plot design, similar totreatments.

Details

Each function creates a standard design as described below:

designCRD

Completely Randomized Design.By default, the model formula is~ trt for one factor and~ facA*facB for two factors, unless explicitly specified. If thelabel argument is provided, the formula is automatically updated with thespecified treatment factor names.

designRCBD

Randomized Complete Block Design.Experimental units are grouped into blocks, with each treatment appearingexactly once per block (i.e., no replicates per treatment within a block).The default model formula is~ trt + (1|block) for one factor and~ facA*facB + (1|block) for two factors. Iflabel is provided, thefixed effect parts of the formula are automatically updated with the specifiednames. The block factor is named "block" and not changeable.

designLSD

Latin Square Design.The default formula is~ trt + (1|row) + (1|col) for one factor and~ facA*facB + (1|row) + (1|col) for two factors. Iflabel is provided,the fixed effect parts of the formula are automatically updated with the specifiednames. The names of row ("row") and column ("col") block factors are not changeable.

designCOD

Crossover Design, which is a special case of LSDwith time periods and individuals as blocks. Period blocks are reused whenreplicating squares.The default formula is~ trt + (1|subject) + (1|period) for one factorand~ facA*facB + (1|subject) + (1|period) for two factors. Iflabelis provided, the fixed effect parts of the formula are automatically updatedwith the specified names. Note that "subject" and "period" are the names forthe two blocking factors and cannot be changed.

designSPD

Split Plot Design.The default formula includes the main effects of all treatment factors atboth the main and sub-plot levels, their interactions, and the random effectsof main plots:~ . + (1|mainplot). Iflabel is provided, the fixedeffect 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 thesubplot level) is always named as "mainplot".

Value

A list object containing all essential components for power calculation.This includes:

See Also

mkdesign,pwr.anova,pwr.contrast

Examples

# Evaluate the power of a CRD with one treatment factor## Create a design objectcrd <- designCRD(  treatments = 4, # 4 levels of one treatment factor  replicates = 12, # 12 units per level, 48 units totally  means = c(30, 28, 33, 35), # means of the 4 levels  sigma2 = 10 # error variance)## power of omnibus testpwr.anova(crd)## power of contrastpwr.contrast(crd, which = "trt", contrast = "pairwise") # pairwise comparisonspwr.contrast(crd, which = "trt", contrast = "poly") # polynomial contrasts# More examples are available in `vignette("pwr4exp")`# and on https://an-ethz.github.io/pwr4exp/

Create a data frame for Crossover design

Description

Create a data frame for Crossover design

Usage

df.cod(treatments, label, squares)

Arguments

treatments

An integer vector where each element represents the number of levelsof the corresponding treatment factor. A single integer (e.g.,treatments = n)specifies one treatment factor withn levels. When multiple factors are provided,they are arranged in a factorial treatment factor design. For example,treatments = c(2, 3) creates a 2x3 factorial design with the first factor having 2 levelsand the second factor having 3 levels.

label

Optional. A list of character vectors, each corresponding to a treatment factor.The name of each vector specifies the factor's name, and its elements provide the labels for that factor's levels.If no labels are provided, default labels will be used. For a single treatment factor, the default islist(trt = c("1", "2", ...)), and for two treatment factors, the default islist(facA = c("1", "2", ...), facB = c("1", "2", ...)).For split-plot designs, the defaults are similar but include the ".main" and ".sub" suffixes for main plot and subplot factors.For example:list(trt.main = c("1", "2", ...), trt.sub = c("1", "2", ...)) andlist(facA.main = c("1", "2", ...), facB.main = c("1", "2", ...), facA.sub = c("1", "2", ...), facB.sub = c("1", "2", ...)).Label sets should be arranged so that the main plot factors come first, followed by the subplot factors.

squares

The number of replicated squares. By default, 1, i.e., noreplicated squares.

Value

a data.frame representing the data structure of the design


Create a data frame of completely randomized design

Description

Create a data frame of completely randomized design

Usage

df.crd(treatments, label, replicates)

Arguments

treatments

An integer vector where each element represents the number of levelsof the corresponding treatment factor. A single integer (e.g.,treatments = n)specifies one treatment factor withn levels. When multiple factors are provided,they are arranged in a factorial treatment factor design. For example,treatments = c(2, 3) creates a 2x3 factorial design with the first factor having 2 levelsand the second factor having 3 levels.

label

Optional. A list of character vectors, each corresponding to a treatment factor.The name of each vector specifies the factor's name, and its elements provide the labels for that factor's levels.If no labels are provided, default labels will be used. For a single treatment factor, the default islist(trt = c("1", "2", ...)), and for two treatment factors, the default islist(facA = c("1", "2", ...), facB = c("1", "2", ...)).For split-plot designs, the defaults are similar but include the ".main" and ".sub" suffixes for main plot and subplot factors.For example:list(trt.main = c("1", "2", ...), trt.sub = c("1", "2", ...))list(facA.main = c("1", "2", ...), facB.main = c("1", "2", ...), facA.sub = c("1", "2", ...), facB.sub = c("1", "2", ...))Label sets should be arranged so that the main plot factors come first, followed by the subplot factors.

replicates

The number of experimental units per treatment.

Value

a data.frame representing the data structure of the design


Create a data frame for Latin square design

Description

Create a data frame for Latin square design

Usage

df.lsd(treatments, label, squares = 1, reuse = c("row", "col", "none"))

Arguments

treatments

An integer vector where each element represents the number of levelsof the corresponding treatment factor. A single integer (e.g.,treatments = n)specifies one treatment factor withn levels. When multiple factors are provided,they are arranged in a factorial treatment factor design. For example,treatments = c(2, 3) creates a 2x3 factorial design with the first factor having 2 levelsand the second factor having 3 levels.

label

Optional. A list of character vectors, each corresponding to a treatment factor.The name of each vector specifies the factor's name, and its elements provide the labels for that factor's levels.If no labels are provided, default labels will be used. For a single treatment factor, the default islist(trt = c("1", "2", ...)), and for two treatment factors, the default islist(facA = c("1", "2", ...), facB = c("1", "2", ...)).For split-plot designs, the defaults are similar but include the ".main" and ".sub" suffixes for main plot and subplot factors.For example:list(trt.main = c("1", "2", ...), trt.sub = c("1", "2", ...)) andlist(facA.main = c("1", "2", ...), facB.main = c("1", "2", ...), facA.sub = c("1", "2", ...), facB.sub = c("1", "2", ...)).Label sets should be arranged so that the main plot factors come first, followed by the subplot factors.

squares

the number of replicated squares

reuse

A character string specifying how to replicate squares whenthere are multiple squares. Options are: "row" for reusing row blocks, "col"for reusing column blocks, or "none" for reusing neither row nor column blocksto replicate a single square.

Value

a data.frame representing the data structure of the design


Create a data frame of randomized complete block design

Description

Create a data frame of randomized complete block design

Usage

df.rcbd(treatments, label, blocks)

Arguments

treatments

An integer vector where each element represents the number of levelsof the corresponding treatment factor. A single integer (e.g.,treatments = n)specifies one treatment factor withn levels. When multiple factors are provided,they are arranged in a factorial treatment factor design. For example,treatments = c(2, 3) creates a 2x3 factorial design with the first factor having 2 levelsand the second factor having 3 levels.

label

Optional. A list of character vectors, each corresponding to a treatment factor.The name of each vector specifies the factor's name, and its elements provide the labels for that factor's levels.If no labels are provided, default labels will be used. For a single treatment factor, the default islist(trt = c("1", "2", ...)), and for two treatment factors, the default islist(facA = c("1", "2", ...), facB = c("1", "2", ...)).For split-plot designs, the defaults are similar but include the ".main" and ".sub" suffixes for main plot and subplot factors.For example:list(trt.main = c("1", "2", ...), trt.sub = c("1", "2", ...)) andlist(facA.main = c("1", "2", ...), facB.main = c("1", "2", ...), facA.sub = c("1", "2", ...), facB.sub = c("1", "2", ...)).Label sets should be arranged so that the main plot factors come first, followed by the subplot factors.

blocks

the number of blocks

Value

a data.frame representing the data structure of the design


Create data frame for split-plot design

Description

Create data frame for split-plot design

Usage

df.spd(trt.main, trt.sub, label, replicates)

Arguments

trt.main

an integer-valued vector specifying the treatment structure atmain plot level, similar todf.crd.

trt.sub

an integer-valued vector specifying the treatment structure atsub plot level, similar totrt.main.

label

Optional. A list of character vectors, each corresponding to a treatment factor.The name of each vector specifies the factor's name, and its elements provide the labels for that factor's levels.If no labels are provided, default labels will be used. For a single treatment factor, the default islist(trt = c("1", "2", ...)), and for two treatment factors, the default islist(facA = c("1", "2", ...), facB = c("1", "2", ...)).For split-plot designs, the defaults are similar but include the ".main" and ".sub" suffixes for main plot and subplot factors.For example:list(trt.main = c("1", "2", ...), trt.sub = c("1", "2", ...)) andlist(facA.main = c("1", "2", ...), facB.main = c("1", "2", ...), facA.sub = c("1", "2", ...), facB.sub = c("1", "2", ...)).Label sets should be arranged so that the main plot factors come first, followed by the subplot factors.

replicates

the number of experimental units (main plots) per treatmentof main plot factors.

Value

a data.frame representing the data structure of the design


Expand terms with'||' notation into separate'|' terms

Description

Expand terms with'||' notation into separate'|' terms

Usage

expandDoubleVerts(term)

Arguments

term

a formula

Value

the modified term


Convert variables to factors as necessary

Description

Convert variables to factors as necessary

Usage

factorize(x, frloc, char.only = FALSE)

Arguments

x

a formula

frloc

a data frame

char.only

(logical) convert only character variables to factors?

Value

a copy of the data frame with factors converted


Determine random-effects expressions from a formula

Description

Determine random-effects expressions from a formula

Usage

findbars(term)

Arguments

term

a mixed-model formula

Value

pairs of expressions that were separated by vertical bars

Note

This function is called recursively on individualterms in the model, which is why the argument is calledterm and nota name likeform, indicating a formula.


An exemplary dataset of a 4x4 crossover design with 2 squares

Description

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.

Usage

milk

Format

A data frame with 32 rows and 4 variables:

Cow

Factor: Cow index (8 levels)

Period

Factor: Period index (4 levels)

Treatment

Factor: Treatment index (4 levels)

MilkYield

Numeric: milk yield recordings (in kg)

Source

Simulated data for package demonstration purposes.


Residual Variance-Covariance Matrices

Description

Residual Variance-Covariance Matrices

Usage

mkRTrms(data, correlation = NULL)

Arguments

data

a data frame with grouping factors and covariates.

correlation

acorStruct object created bycorClasses functions.If NULL, an identity matrix is assumed.

Value

A list containing:


Design Matrices and Variance Components for Random Effects

Description

Adapted from lme4, this function constructs the design matrix (Z),variance-covariance matrix (G), etc.

Usage

mkReTrms(  bars,  fr,  drop.unused.levels = TRUE,  reorder.terms = FALSE,  reorder.vars = FALSE)

Arguments

bars

a list of parsed random-effects terms

fr

a model frame in which to evaluate these terms

drop.unused.levels

(logical) drop unused factor levels?

reorder.terms

arrange random effects terms in decreasing order of number of groups (factor levels)?

reorder.vars

arrange columns of individual random effects terms in alphabetical order?

Value

A list with the following components:


Building Design Matrices and Covariance Structures for Linear Mixed Models

Description

Constructs design matrices for fixed and random effects, along withvariance-covariance structures for random effects (G-side) and residuals (R-side).

Usage

mkStruct(formula, data, correlation)

Arguments

formula

a model formula.

data

a data frame containing the variables used in the model.

correlation

acorStruct object created bycorClasses functions.If NULL, an identity matrix is assumed.

Value

A list containing:


Create a Design Object for Power Calculation

Description

Generate a design object for power analysis by specifying a model formula and data frame.This object is not a true experimental design as created by design generation procedures,where randomization and unit allocation are performed.Instead, it serves as an object containing all necessary information for power analysis,including design matrices, assumed values of model effects, and other internallycalculated parameters.

Usage

mkdesign(  formula,  data,  beta = NULL,  means = NULL,  vcomp = NULL,  sigma2 = NULL,  correlation = NULL,  template = FALSE,  REML = TRUE)

Arguments

formula

A right-hand-sideformula specifying the model for testing treatment effects,with terms on the right of~ , followinglme4::lmer syntax for random effects.

data

A data frame with all independent variables specified in the model,matching the design's structure.

beta

One of the optional inputs for fixed effects.A vector of model coefficients where factor variable coefficients correspondto dummy variables created using treatment contrast (stats::contr.treatment).

means

One of the optional inputs for fixed effects.A vector of marginal or conditioned means (if factors have interactions).Regression coefficients are required for numerical variables.Eitherbeta ormeans must be provided, and their values must strictly follow a specific order.A template can be created to indicate the required input values and their order.See "Details" for more information.

vcomp

A vector of variance-covariance components for random effects, if present.The values must follow a strict order. See "Details".

sigma2

error variance.

correlation

Specifies residual (R-side) correlation structures usingnlme::corClasses functions.See "Details" for more information.

template

Default isFALSE.IfTRUE or when only the formula and data are provided, a template forbeta,means, andvcomp is generated to indicate the required input order.

REML

Specifies whether to use REML or ML information matrix. Default isTRUE.

Details

Value

A list object containing all essential components for power calculation.This includes:

Examples

# Using templates for specifying "means"# Create an example data frame with four categorical variables (factors)# and two numerical variablesdf1 <- expand.grid(  fA = factor(1:2),  fB = factor(1:2),  fC = factor(1:3),  fD = factor(1:3),  subject = factor(1:10))df1$x <- rnorm(nrow(df1))  # Numerical variable xdf1$z <- rnorm(nrow(df1))  # Numerical variable z## Categorical variables without interactions# Means of each level of fA and fB are required in sequence.mkdesign(~ fA + fB, df1)$fixeff$means## Interactions among categorical variables# Cell means for all combinations of levels of fA and fB are required.mkdesign(~ fA * fB, df1)$fixeff$means## Numerical variables without and with interactions, identical to beta.# Without interactions: Regression coefficients are requiredmkdesign(~ x + z, df1)$fixeff$means# With interactions: Coefficients for main effects and interaction terms are required.mkdesign(~ x * z, df1)$fixeff$means## Categorical-by-numerical interactions# Marginal means for each level of fA, and regression coefficients for x# at each level of fA are required.mkdesign(~ fA * x, df1)$fixeff$means## Three factors with interactions:# Cell means for all 12 combinations of the levels of fA, fB, and fC are required.mkdesign(~ fA * fB * fC, df1)# A design that mixes the above-mentioned scenarios:# - Interactions among three categorical variables (fA, fB, fC)# - A categorical-by-numerical interaction (fD * x)# - Main effects for another numerical variable (z)# The required inputs are:# - Cell means for all combinations of levels of fA, fB, and fC# - Means for each level of fD# - Regression coefficients for x at each level of fD# - Regression coefficients for zmkdesign(~ fA * fB * fC + fD * x + z, df1)$fixeff$means# Using templates for specifying "vcomp"# Assume df1 represents an RCBD with "subject" as a random blocking factor.## Variance of the random effect "subject" (intercept) is required.mkdesign(~ fA * fB * fC * fD + (1 | subject), df1)$varcov# Demonstration of templates for more complex random effects## Note: This example is a demo and statistically incorrect for this data## (no replicates under subject*fA). It only illustrates variance-covariance templates.## Inputs required:## - Variance of the random intercept (1st)## - Covariance between the intercept and "fA2" (2nd)## - Variance of "fA2" (3rd)mkdesign(~ fA * fB * fC * fD + (1 + fA | subject), df1)$varcov# Power analysis for repeated measures## Create a data frame for a CRD with repeated measuresn_subject <- 6n_trt <- 3n_hour <- 8trt <- c("CON", "TRT1", "TRT2")df2 <- data.frame(  subject = as.factor(rep(seq_len(n_trt * n_subject), each = n_hour)), # Subject as a factor  hour = as.factor(rep(seq_len(n_hour), n_subject * n_trt)),           # Hour as a factor  trt = rep(trt, each = n_subject * n_hour)                           # Treatment as a factor)## Templatestemp <- mkdesign(formula = ~ trt * hour, data = df2)temp$fixeff$means  # Fixed effects means template## Create a design object# Assume repeated measures within a subject follow an AR1 process with a correlation of 0.6design <- mkdesign(  formula = ~ trt * hour,  data = df2,  means = c(1, 2.50, 3.50,            1, 3.50, 4.54,            1, 3.98, 5.80,            1, 4.03, 5.40,            1, 3.68, 5.49,            1, 3.35, 4.71,            1, 3.02, 4.08,            1, 2.94, 3.78),  sigma2 = 2,  correlation = corAR1(value = 0.6, form = ~ hour | subject))pwr.anova(design)  # Perform power analysis## When time is treated as a numeric variable# Means of treatments and regression coefficients for hour at each treatment level are requireddf2$hour <- as.integer(df2$hour)mkdesign(formula = ~ trt * hour, data = df2)$fixeff$means## Polynomial terms of time in the modelmkdesign(formula = ~ trt + hour + I(hour^2) + trt:hour + trt:I(hour^2), data = df2)$fixeff$means

Omit terms separated by vertical bars in a formula

Description

Remove the random-effects terms from a mixed-effects formula,thereby producing the fixed-effects formula.

Usage

nobars(term)

Arguments

term

the right-hand side of a mixed-model formula

Value

the fixed-effects part of the formula

Note

This function is called recursively on individualterms in the model, which is why the argument is calledterm and nota name likeform, indicating a formula.


Power of omnibus tests

Description

Calculates the statistical power for testing the overall effects of treatmentfactors and their interactions, i.e., power of F-test.

Usage

pwr.anova(object, sig.level = 0.05, type = c("III", "II", "I", "3", "2", "1"))

Arguments

object

a design object created in pwr4exp

sig.level

significance level, default 0.05

type

the type of ANOVA table requested, default Type III

Value

a data frame with numerator degrees of freedom (NumDF), denominatordegrees of freedom (DenDF), type I error rate (sig.level), andpower.

See Also

mkdesign,designCRD,designRCBD,designLSD,designCOD,designSPD,pwr.summary andpwr.contrast

Examples

# generate an RCBDrcbd <- designRCBD(  treatments = c(2, 2),  label = list(facA = c("1", "2"), facB = c("1", "2")),  blocks = 12,  formula = ~ facA*facB + (1|block),  means = c(32, 35, 30, 37),  vcomp = 4,  sigma2 = 6)# power of omnibus testpwr.anova(rcbd)

Power of contrasts

Description

Computes the statistical power of t-tests for comparisons among means.

Usage

pwr.contrast(  object,  which,  by = NULL,  contrast = c("pairwise", "poly", "trt.vs.ctrl"),  sig.level = 0.05,  p.adj = FALSE,  alternative = c("two.sided", "one.sided"),  strict = TRUE)

Arguments

object

a design object created in pwr4exp

which

the factor of interest. Multiple factors can be combined using: or*, e.g.,"facA*facB", which represents a single factor that combinesthe levels of both factors.

by

the variable to condition on

contrast

A character string specifying the contrast method, one of"pairwise", "poly", or "trt.vs.ctrl". Alternatively, a numeric vector defininga single contrast or a (named) list of vectors specifying multiple custom contrasts.If a list is provided, each element must be a vector whose length matches thenumber of levels of the factor in eachby group. In multi-factor scenarios,factor levels are combined and treated as a single factor.

sig.level

significance level, default 0.05

p.adj

whether the sig.level should be adjusted using the Bonferroni method, default FALSE

alternative

one- or two-sided test. Can be abbreviated.

strict

use strict interpretation in two-sided case

Value

For eachby condition, returns a data frame containing the contrast value (effect),degrees of freedom (df), type I error rate (sig.level),power, and the test direction(byalternative).When multipleby conditions are present, the results are returned as a list.

Examples

rcbd <- designRCBD(  treatments = c(2, 2),  label = list(facA = c("1", "2"), facB = c("1", "2")),  blocks = 12,  formula = ~ facA*facB + (1|block),  means = c(32, 35, 30, 37),  vcomp = 4,  sigma2 = 6)# If contrast is not specified, pairwise comparisons are conductedpwr.contrast(rcbd, which = "facA") # Marginal effect of facApwr.contrast(rcbd, which = "facA", by = "facB") # Conditional effect of facA within levels of facB# Custom contrast vector, identical to pairwise comparisonpwr.contrast(rcbd, which = "facA", contrast = c(1, -1))pwr.contrast(rcbd, which = "facA", by = "facB", contrast = c(1, -1))# A single factor combining two factorspwr.contrast(  rcbd,  which = "facA*facB",  contrast = list(    A1B1vs.A2B1 = c(1, -1, 0, 0),    A1B1vs.A2B2 = c(1, 0, 0, -1)  ))

Power for model coefficients

Description

Computes the statistical power for testing (t-test) model coefficients.

Usage

pwr.summary(object, sig.level = 0.05)

Arguments

object

design object

sig.level

significance level, default 0.05

Value

a data frame containing model coefficients, degrees of freedom (df),type I error rate (sig.level), power, and the test direction (alternative).

Examples

rcbd <- designRCBD(  treatments = c(2, 2),  label = list(facA = c("1", "2"), facB = c("1", "2")),  blocks = 12,  formula = ~ facA*facB + (1|block),  means = c(32, 35, 30, 37),  vcomp = 4,  sigma2 = 6)pwr.summary(rcbd)

Substitute Bars

Description

Substitute the '+' function for the '|' and '||' function in a mixed-modelformula.

Usage

subbars(term)

Arguments

term

a mixed-model formula

Value

the formula with all | and || operators replaced by +

Note

This function is called recursively on individualterms in the model, which is why the argument is calledterm and nota name likeform, indicating a formula.


[8]ページ先頭

©2009-2025 Movatter.jp