The “aov” Function in R
Package: Base R (no specific package required)
Purpose: Fits analysis of variance (ANOVA) models for comparing means across multiple groups.
General Class: Statistical Modeling
Required Argument(s):
formula: A symbolic description of the model to be fitted. In the form response ~ terms.
data: An optional data frame in which to interpret the variables.
Notable Optional Arguments:
...: Additional arguments to be passed to or from methods.
subset: An optional vector specifying a subset of observations to be used in the fitting process.
na.action: A function that indicates what should happen when data contain missing values.
contrasts: An optional list of contrasts to be used for the factor variables.
Example:
# Example data for analysis of variance
set.seed(123)
group1 <- rnorm(30, mean = 10, sd = 2)
group2 <- rnorm(30, mean = 12, sd = 2)
group3 <- rnorm(30, mean = 15, sd = 2)
factors <- rep(c("A", "B", "C"), each = 30)
# Create a data frame
data_df <- data.frame(response = c(group1, group2, group3), factor = factors)
# Fit analysis of variance model
model <- aov(response ~ factor, data = data_df)
# Display the summary of the model
summary(model)In this example, the aov function is used to fit an analysis of variance (ANOVA) model to compare means across three groups (group1, group2, and group3). The factor variable is specified in the formula, and the result of the ANOVA model is then summarized and displayed.