When to use aov() vs. anova() in R (original) (raw)

Last Updated : 28 Jul, 2025

Analysis of Variance (ANOVA) is a statistical technique used to determine if there are significant differences between the means of three or more independent groups. In R, ANOVA can be performed using aov() or anova() functions.

aov() Function in R

The aov() function fits an ANOVA model using a formula to compare group means based on categorical variables.

**Syntax:

aov(formula, data, subset, na.action)

**Parameters:

**Implementation of aov() Function in R

We use the aov() function in R programming language to perform a one-way ANOVA that checks whether multiple groups have different means based on a single factor.

**1. Creating the Dataset and Applying aov() Function

We create a dataset representing weight loss from three different exercise programs and fit a one-way ANOVA model.

set.seed(0) df <- data.frame(program = rep(c("A", "B", "C"), each=30), weight_loss = c(runif(30, 0, 3), runif(30, 0, 5), runif(30, 1, 7))) fit <- aov(weight_loss ~ program, data=df) summary(fit)

`

**Output:

result

Output

anova() Function in R

The anova() function conducts an ANOVA test, which partitions the total variance observed in a dataset into different components attributed to different sources of variation. These sources can include factors, interactions between factors and residual error.

**Syntax:

anova(model)

**Parameter:

**Implementation of anova() Function in R

We use the anova() function to compare a full and reduced regression model and check if adding more predictors improves model fit significantly.

**1. Creating the Dataset and Comparing Models

We simulate a dataset where the exam score is affected by the number of study hours, then compare a linear and polynomial model.

set.seed(1) df <- data.frame(hours = runif(50, 5, 15), score=50) df$score = df$score + df$hours^3/150 + df$hours*runif(50, 1, 2) full <- lm(score ~ poly(hours,2), data=df) reduced <- lm(score ~ hours, data=df) anova(full, reduced)

`

**Output:

dataframe

Output

**Conclusion: Quadratic model is significantly better.

Key Differences between aov() and anova()

Here's a detailed difference between aov() and anova() with respect to different parameters.

Feature aov() anova()
Purpose Fits ANOVA models directly to the data. Performs ANOVA on fitted model objects.
Input Format of Model Accepts formula-based models (y ~ x1 + x2). Accepts model objects generated by functions like lm() or glm().
Output Format ANOVA table showing sources of variation and statistics. ANOVA table comparing models or testing added terms.
Usage Used for directly conducting ANOVA on data. Used for comparing models or testing significance of predictors.
Flexibility Best suited for simpler models with categorical predictors. More flexible for comparing nested models or complex designs.
Example aov(response ~ factor, data = my_data). anova(lm_model1, lm_model2).

When to Use aov() in R

When to Use anova() in R