Using R to Calculate Between-By Within-Subject ANOVA Interaction Contrasts using car or lme
In this article, we will explore how to calculate between-by-within-subject ANOVA interaction contrasts in R using the car
and lme
packages.
Background on ANOVA
Before diving into the details, let’s quickly review what ANOVA is. ANOVA stands for Analysis of Variance, a statistical technique used to compare means of three or more groups to see if at least one group mean is different from the others.
In the context of this article, we are dealing with a mixed design, where we have both between-subjects and within-subjects variables. The between-subjects variable is fixed, meaning that each participant belongs to only one group. The within-subjects variable, on the other hand, is repeated measures, meaning that each participant completes multiple trials.
Types of ANOVA
There are several types of ANOVA, including:
- Between-Subjects ANOVA: This type of ANOVA is used when the independent variable is between-subjects. It compares the means of different groups to see if there are any significant differences.
- Within-Subjects ANOVA: This type of ANOVA is used when the independent variable is within-subjects. It compares the means of repeated measures across participants to see if there are any significant differences.
- Mixed Designs ANOVA: This type of ANOVA combines between-subjects and within-subjects variables.
Using car Package
The car
package in R provides a set of functions for analyzing mixed effects models. One of the key functions is Anova()
, which can be used to calculate interaction contrasts between fixed factors.
Here’s an example code that demonstrates how to use the car
package:
library(car)
Let’s assume we have a dataset with three fixed factors: A, B, and C. We want to examine the interaction between factor A and factor B.
# Create a mixed effects model
model <- lmer(value ~ A * B + (1|ID), data = test)
# Perform ANOVA on the model
anova_model <- Anova(model)
The Anova()
function takes in the mixed effects model object as an argument and returns a table with the results of the ANOVA.
Using lme Package
The lme
package in R provides a set of functions for analyzing linear mixed models. One of the key functions is lmer()
, which can be used to estimate fixed effects in a mixed effects model.
Here’s an example code that demonstrates how to use the lme
package:
library(lme4)
Let’s assume we have a dataset with three fixed factors: A, B, and C. We want to examine the interaction between factor A and factor B.
# Create a linear mixed model
model <- lmer(value ~ A * B + (1|ID), data = test)
# Print the summary of the model
summary(model)
The lmer()
function takes in the formula for the fixed effects and the random effects structure as arguments and returns an object with the estimated coefficients.
Using ezANOVA Package
One alternative to using the car
or lme
packages is to use the ezANOVA()
function from the ez
package. This package provides a simple and easy-to-use interface for performing ANOVA on mixed designs.
Here’s an example code that demonstrates how to use the ez
package:
library(ez)
Let’s assume we have a dataset with three fixed factors: A, B, and C. We want to examine the interaction between factor A and factor B.
# Create a dataframe with the data
df <- test
# Stack the dataframe
df1 <- stack(df)
# Extract the unique values for ID, condition, and classification
subj <- as.numeric(unique(df$ID))
condition <- rep(unique(df$condition), 3)
classification <- rep(unique(df$classification), 3)
# Merge the data back into a dataframe
df1[3] <- subj
df1[4] <- condition
df1[5] <- classification
# Create a new column with the stacked value
df1$value <- df$value
# Define the mixed design model
data <- list(
dv = "value",
wid = "ID",
within = c("trial_type"),
between = c("classification", "condition")
)
# Perform ANOVA on the model
mixed_aov <- ezANOVA(data, type = 3)
The ez
package provides a simple and easy-to-use interface for performing ANOVA on mixed designs.
Conclusion
In this article, we have explored how to calculate between-by-within-subject ANOVA interaction contrasts in R using the car
, lme
, and ez
packages. We have also reviewed what ANOVA is and the different types of ANOVA that can be used.
Last modified on 2025-02-05