factorial_design: Build Factorial Designs for ANOVA

View source: R/factorial_design.R

factorial_designR Documentation

Build Factorial Designs for ANOVA

Description

Provides helper functions to build factorial design for easily computing ANOVA using the Anova() function. This might be very useful for repeated measures ANOVA, which is hard to set up with the car package.

Usage

factorial_design(data, dv, wid, between, within, covariate)

Arguments

data

a data frame containing the variables

dv

(numeric) dependent variable name.

wid

(factor) column name containing individuals/subjects identifier. Should be unique per individual.

between

(optional) between-subject factor variables.

within

(optional) within-subjects factor variables

covariate

(optional) covariate names (for ANCOVA)

Value

a list with the following components:

  • the specified arguments: dv, wid, between, within

  • data: the original data (long format) or independent ANOVA. The wide format is returned for repeated measures ANOVA.

  • idata: an optional data frame giving the levels of factors defining the intra-subject model for multivariate repeated-measures data.

  • idesign: a one-sided model formula using the “data” in idata and specifying the intra-subject design.

  • repeated: logical. Value is TRUE when the data is a repeated design.

  • lm_formula: the formula used to build the lm model.

  • lm_data: the data used to build the lm model. Can be either in a long format (i.e., the original data for independent measures ANOVA) or in a wide format (case of repeated measures ANOVA).

  • model: the lm model

Author(s)

Alboukadel Kassambara, alboukadel.kassambara@gmail.com

See Also

anova_test(), anova_summary()

Examples

# Load data
#:::::::::::::::::::::::::::::::::::::::
data("ToothGrowth")
df <- ToothGrowth
head(df)

# Repeated measures designs
#:::::::::::::::::::::::::::::::::::::::::
# Prepare the data
df$id <- rep(1:10, 6) # Add individuals id
head(df)
# Build factorial designs
design <- factorial_design(df, dv = len, wid = id, within = c(supp, dose))
design
# Easily perform repeated measures ANOVA using the car package
res.anova <- Anova(design$model, idata = design$idata, idesign = design$idesign, type = 3)
summary(res.anova, multivariate = FALSE)

# Independent measures designs
#:::::::::::::::::::::::::::::::::::::::::
# Build factorial designs
df$id <- 1:nrow(df)
design <- factorial_design(df, dv = len, wid = id, between = c(supp, dose))
design
# Perform ANOVA
Anova(design$model, type = 3)


rstatix documentation built on Feb. 16, 2023, 6:10 p.m.